1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration;
12
13 import org.mule.transport.file.filters.FilenameWildcardFilter;
14 import org.mule.util.FileUtils;
15
16 import java.io.File;
17
18 import javax.jms.JMSException;
19
20 import org.apache.activemq.ActiveMQConnection;
21 import org.apache.activemq.ActiveMQConnectionFactory;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.DefaultLogger;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.taskdefs.ExecuteWatchdog;
26 import org.apache.tools.ant.taskdefs.Java;
27 import org.apache.tools.ant.types.Environment;
28 import org.apache.tools.ant.types.Path;
29 import org.apache.tools.ant.util.Watchdog;
30
31
32
33
34
35 public class ServerTools
36 {
37 public static final String ACTIVEMQ_HOME = "org.activemq.home";
38
39 private static KillableWatchdog activemq;
40 private static ActiveMQConnectionFactory embeddedFactory = null;
41
42 public static void launchActiveMq()
43 {
44 launchActiveMq(ActiveMQConnection.DEFAULT_BROKER_URL);
45 }
46
47 public static ActiveMQConnectionFactory launchEmbeddedActiveMq() throws JMSException
48 {
49 return new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.useJmx=false");
50 }
51
52 public static void killEmbeddedActiveMq()
53 {
54 if (embeddedFactory != null)
55 {
56
57
58
59
60
61
62
63
64 embeddedFactory = null;
65 }
66 }
67
68 public static void launchActiveMq(String brokerUrl)
69 {
70 String activeMqHome = System.getProperty(ACTIVEMQ_HOME);
71 if (activeMqHome == null)
72 {
73 throw new IllegalArgumentException(
74 "You must set the "
75 + ACTIVEMQ_HOME
76 + " system property to the root path of an ActiveMq distribution (v3.0 and greater) before running these tests");
77 }
78 Project project = new Project();
79 DefaultLogger consoleLogger = new DefaultLogger();
80 consoleLogger.setErrorPrintStream(System.err);
81 consoleLogger.setOutputPrintStream(System.out);
82 consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
83 project.addBuildListener(consoleLogger);
84 Path path = new Path(project);
85 File[] jars = FileUtils.newFile(activeMqHome + "\\lib").listFiles(new FilenameWildcardFilter("*.jar"));
86 path.add(new Path(project, FileUtils.newFile(activeMqHome, "\\conf").getAbsolutePath()));
87 for (int i = 0; i < jars.length; i++)
88 {
89 path.add(new Path(project, jars[i].getAbsolutePath()));
90 }
91 jars = FileUtils.newFile(activeMqHome + "\\lib\\optional").listFiles(new FilenameWildcardFilter("*.jar"));
92 for (int i = 0; i < jars.length; i++)
93 {
94 path.add(new Path(project, jars[i].getAbsolutePath()));
95 }
96 final JavaTask java = new JavaTask();
97 java.setProject(project);
98 java.setClasspath(path);
99 if (activeMqHome.indexOf("4.") > -1)
100 {
101 java.setClassname("org.apache.activemq.broker.Main");
102 }
103 else
104 {
105 java.setClassname("org.activemq.broker.impl.Main");
106 }
107 java.setArgs(brokerUrl);
108 java.setFork(true);
109 java.setDir(FileUtils.newFile(activeMqHome));
110 java.addSysproperty(createVar("activemq.home", FileUtils.newFile(activeMqHome).getAbsolutePath()));
111 java.addSysproperty(createVar("derby.system.home", FileUtils.newFile(activeMqHome, "\\var").getAbsolutePath()));
112 java.createWatchdog();
113 new Thread()
114 {
115 public void run()
116 {
117 java.execute();
118 }
119 }.start();
120 activemq = java.watchDog;
121 }
122
123 public static void killActiveMq()
124 {
125 try
126 {
127 if (activemq != null)
128 {
129 activemq.kill();
130 }
131 }
132 catch (Throwable e)
133 {
134 e.printStackTrace();
135 }
136 }
137
138 static class JavaTask extends Java
139 {
140 public KillableWatchdog watchDog;
141 private Long timeout = new Long(Long.MAX_VALUE);
142
143 public void setTimeout(Long value)
144 {
145 this.timeout = value;
146 super.setTimeout(value);
147 }
148
149 protected ExecuteWatchdog createWatchdog() throws BuildException
150 {
151 if (watchDog == null)
152 {
153 watchDog = new KillableWatchdog(timeout != null ? timeout.longValue() : 0);
154 }
155 return watchDog;
156 }
157
158 }
159
160 static class KillableWatchdog extends ExecuteWatchdog
161 {
162 public KillableWatchdog(long timeout)
163 {
164 super(timeout);
165 }
166
167 public void timeoutOccured(Watchdog w)
168 {
169
170 }
171
172 public synchronized void start(Process process)
173 {
174 super.start(process);
175 }
176
177 public void kill()
178 {
179 super.timeoutOccured(null);
180 }
181 }
182
183 static Environment.Variable createVar(String name, String value)
184 {
185 Environment.Variable var = new Environment.Variable();
186 var.setKey(name);
187 var.setValue(value);
188 return var;
189 }
190
191 }