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