1 | /*
|
---|
2 | * Copyright 2000-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.ejb;
|
---|
18 |
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.Task;
|
---|
23 | import org.apache.tools.ant.taskdefs.Java;
|
---|
24 | import org.apache.tools.ant.types.Path;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Starts a WebLogic server.
|
---|
28 | * A number of parameters are used to control the operation of the weblogic
|
---|
29 | * instance. Note that the task, and hence ant, will not complete until the
|
---|
30 | * weblogic instance is stopped.</p>
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class WLRun extends Task {
|
---|
34 | protected static final String DEFAULT_WL51_POLICY_FILE = "weblogic.policy";
|
---|
35 | protected static final String DEFAULT_WL60_POLICY_FILE = "lib/weblogic.policy";
|
---|
36 | protected static final String DEFAULT_PROPERTIES_FILE = "weblogic.properties";
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The classpath to be used when running the Java VM. It must contain the
|
---|
40 | * weblogic classes <b>and</b> the implementation classes of the home and
|
---|
41 | * remote interfaces.
|
---|
42 | */
|
---|
43 | private Path classpath;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The weblogic classpath to the be used when running weblogic.
|
---|
47 | */
|
---|
48 | private Path weblogicClasspath;
|
---|
49 |
|
---|
50 | private String weblogicMainClass = "weblogic.Server";
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Addional arguments to pass to the JVM used to run weblogic
|
---|
54 | */
|
---|
55 | private String additionalArgs = "";
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * The security policy to use when running the weblogic server
|
---|
59 | */
|
---|
60 | private String securityPolicy;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * The weblogic system home directory
|
---|
64 | */
|
---|
65 | private File weblogicSystemHome;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * The weblogic domain
|
---|
69 | */
|
---|
70 | private String weblogicDomainName;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * The name of the weblogic server - used to select the server's directory in the
|
---|
74 | * weblogic home directory.
|
---|
75 | */
|
---|
76 | private String weblogicSystemName = "myserver";
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * The file containing the weblogic properties for this server.
|
---|
80 | */
|
---|
81 | private String weblogicPropertiesFile = null;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * additional args to pass to the spawned jvm
|
---|
85 | */
|
---|
86 | private String additionalJvmArgs = "";
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * The location of the BEA Home under which this server is run.
|
---|
90 | * WL6 only
|
---|
91 | */
|
---|
92 | private File beaHome = null;
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * The management username
|
---|
96 | */
|
---|
97 | private String managementUsername = "system";
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * The management password
|
---|
101 | */
|
---|
102 | private String managementPassword = null;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * The provate key password - used for SSL
|
---|
106 | */
|
---|
107 | private String pkPassword = null;
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Add the classpath for the user classes
|
---|
111 | */
|
---|
112 | public Path createClasspath() {
|
---|
113 | if (classpath == null) {
|
---|
114 | classpath = new Path(getProject());
|
---|
115 | }
|
---|
116 | return classpath.createPath();
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Get the classpath to the weblogic classpaths
|
---|
121 | */
|
---|
122 | public Path createWLClasspath() {
|
---|
123 | if (weblogicClasspath == null) {
|
---|
124 | weblogicClasspath = new Path(getProject());
|
---|
125 | }
|
---|
126 | return weblogicClasspath.createPath();
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Do the work.
|
---|
131 | *
|
---|
132 | * The work is actually done by creating a separate JVM to run a helper task.
|
---|
133 | * This approach allows the classpath of the helper task to be set. Since the
|
---|
134 | * weblogic tools require the class files of the project's home and remote
|
---|
135 | * interfaces to be available in the classpath, this also avoids having to
|
---|
136 | * start ant with the class path of the project it is building.
|
---|
137 | *
|
---|
138 | * @exception BuildException if someting goes wrong with the build
|
---|
139 | */
|
---|
140 | public void execute() throws BuildException {
|
---|
141 | if (weblogicSystemHome == null) {
|
---|
142 | throw new BuildException("weblogic home must be set");
|
---|
143 | }
|
---|
144 | if (!weblogicSystemHome.isDirectory()) {
|
---|
145 | throw new BuildException("weblogic home directory "
|
---|
146 | + weblogicSystemHome.getPath() + " is not valid");
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (beaHome != null) {
|
---|
150 | executeWLS6();
|
---|
151 | } else {
|
---|
152 | executeWLS();
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | private File findSecurityPolicyFile(String defaultSecurityPolicy) {
|
---|
157 | String securityPolicy = this.securityPolicy;
|
---|
158 | if (securityPolicy == null) {
|
---|
159 | securityPolicy = defaultSecurityPolicy;
|
---|
160 | }
|
---|
161 | File securityPolicyFile = new File(weblogicSystemHome, securityPolicy);
|
---|
162 | // If an explicit securityPolicy file was specified, it maybe an
|
---|
163 | // absolute path. Use the project to resolve it.
|
---|
164 | if (this.securityPolicy != null && !securityPolicyFile.exists()) {
|
---|
165 | securityPolicyFile = getProject().resolveFile(securityPolicy);
|
---|
166 | }
|
---|
167 | // If we still can't find it, complain
|
---|
168 | if (!securityPolicyFile.exists()) {
|
---|
169 | throw new BuildException("Security policy " + securityPolicy
|
---|
170 | + " was not found.");
|
---|
171 | }
|
---|
172 | return securityPolicyFile;
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void executeWLS6() {
|
---|
176 | File securityPolicyFile
|
---|
177 | = findSecurityPolicyFile(DEFAULT_WL60_POLICY_FILE);
|
---|
178 | if (!beaHome.isDirectory()) {
|
---|
179 | throw new BuildException("BEA home " + beaHome.getPath()
|
---|
180 | + " is not valid");
|
---|
181 | }
|
---|
182 |
|
---|
183 | File configFile = new File(weblogicSystemHome, "config/"
|
---|
184 | + weblogicDomainName + "/config.xml");
|
---|
185 | if (!configFile.exists()) {
|
---|
186 | throw new BuildException("Server config file " + configFile
|
---|
187 | + " not found.");
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (managementPassword == null) {
|
---|
191 | throw new BuildException("You must supply a management password "
|
---|
192 | + "to start the server");
|
---|
193 | }
|
---|
194 |
|
---|
195 | Java weblogicServer = (Java) getProject().createTask("java");
|
---|
196 | weblogicServer.setTaskName(getTaskName());
|
---|
197 | weblogicServer.setFork(true);
|
---|
198 | weblogicServer.setDir(weblogicSystemHome);
|
---|
199 | weblogicServer.setClassname(weblogicMainClass);
|
---|
200 |
|
---|
201 | String jvmArgs = additionalJvmArgs;
|
---|
202 |
|
---|
203 | jvmArgs += " -Dweblogic.Domain=" + weblogicDomainName;
|
---|
204 | jvmArgs += " -Dweblogic.Name=" + weblogicSystemName;
|
---|
205 | jvmArgs += " -Dweblogic.system.home=" + weblogicSystemHome;
|
---|
206 |
|
---|
207 | jvmArgs += " -Dbea.home=" + beaHome;
|
---|
208 | jvmArgs += " -Djava.security.policy==" + securityPolicyFile;
|
---|
209 |
|
---|
210 | jvmArgs += " -Dweblogic.management.username=" + managementUsername;
|
---|
211 | jvmArgs += " -Dweblogic.management.password=" + managementPassword;
|
---|
212 | if (pkPassword != null) {
|
---|
213 | jvmArgs += " -Dweblogic.pkpassword=" + pkPassword;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | weblogicServer.createJvmarg().setLine(jvmArgs);
|
---|
218 | weblogicServer.createArg().setLine(additionalArgs);
|
---|
219 |
|
---|
220 | if (classpath != null) {
|
---|
221 | weblogicServer.setClasspath(classpath);
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (weblogicServer.executeJava() != 0) {
|
---|
225 | throw new BuildException("Execution of weblogic server failed");
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void executeWLS() {
|
---|
230 | File securityPolicyFile
|
---|
231 | = findSecurityPolicyFile(DEFAULT_WL51_POLICY_FILE);
|
---|
232 | File propertiesFile = null;
|
---|
233 |
|
---|
234 |
|
---|
235 | if (weblogicPropertiesFile == null) {
|
---|
236 | weblogicPropertiesFile = DEFAULT_PROPERTIES_FILE;
|
---|
237 | }
|
---|
238 | propertiesFile = new File(weblogicSystemHome, weblogicPropertiesFile);
|
---|
239 | if (!propertiesFile.exists()) {
|
---|
240 | // OK, properties file may be absolute
|
---|
241 | propertiesFile = getProject().resolveFile(weblogicPropertiesFile);
|
---|
242 | if (!propertiesFile.exists()) {
|
---|
243 | throw new BuildException("Properties file "
|
---|
244 | + weblogicPropertiesFile
|
---|
245 | + " not found in weblogic home " + weblogicSystemHome
|
---|
246 | + " or as absolute file");
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | Java weblogicServer = (Java) getProject().createTask("java");
|
---|
251 | weblogicServer.setTaskName(getTaskName());
|
---|
252 | weblogicServer.setFork(true);
|
---|
253 | weblogicServer.setClassname(weblogicMainClass);
|
---|
254 |
|
---|
255 | String jvmArgs = additionalJvmArgs;
|
---|
256 |
|
---|
257 | if (weblogicClasspath != null) {
|
---|
258 | jvmArgs += " -Dweblogic.class.path=" + weblogicClasspath;
|
---|
259 | }
|
---|
260 |
|
---|
261 | jvmArgs += " -Djava.security.manager -Djava.security.policy==" + securityPolicyFile;
|
---|
262 | jvmArgs += " -Dweblogic.system.home=" + weblogicSystemHome;
|
---|
263 | jvmArgs += " -Dweblogic.system.name=" + weblogicSystemName;
|
---|
264 | jvmArgs += " -Dweblogic.system.propertiesFile=" + weblogicPropertiesFile;
|
---|
265 |
|
---|
266 | weblogicServer.createJvmarg().setLine(jvmArgs);
|
---|
267 | weblogicServer.createArg().setLine(additionalArgs);
|
---|
268 |
|
---|
269 | if (classpath != null) {
|
---|
270 | weblogicServer.setClasspath(classpath);
|
---|
271 | }
|
---|
272 | if (weblogicServer.executeJava() != 0) {
|
---|
273 | throw new BuildException("Execution of weblogic server failed");
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * The classpath to be used with the Java Virtual Machine that runs the Weblogic
|
---|
280 | * Server; required. Prior to Weblogic 6.0, this is typically set to the Weblogic
|
---|
281 | * boot classpath. Under Weblogic 6.0 this should include all the
|
---|
282 | * weblogic jars
|
---|
283 | *
|
---|
284 | * @param classpath the classpath to use when executing the weblogic server.
|
---|
285 | */
|
---|
286 | public void setClasspath(Path classpath) {
|
---|
287 | this.classpath = classpath;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Set the weblogic classpath used by the Weblogic Server;
|
---|
292 | * optional, and only applicable to WL4.5.1
|
---|
293 | *
|
---|
294 | * The weblogic classpath is used by weblogic to support dynamic class loading.
|
---|
295 | *
|
---|
296 | * @param weblogicClasspath the weblogic classpath
|
---|
297 | */
|
---|
298 | public void setWlclasspath(Path weblogicClasspath) {
|
---|
299 | this.weblogicClasspath = weblogicClasspath;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * The name of the security policy file within the weblogic home directory that
|
---|
304 | * is to be used. If not specified, the default policy file <code>weblogic.policy</code>
|
---|
305 | * is used.
|
---|
306 | *
|
---|
307 | * @param securityPolicy the security policy to use.
|
---|
308 | */
|
---|
309 | public void setPolicy(String securityPolicy) {
|
---|
310 | this.securityPolicy = securityPolicy;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * The location where weblogic lives.
|
---|
315 | * Required. This is the absolute location, not relative to
|
---|
316 | * BEA home.
|
---|
317 | * @param weblogicHome the home directory of weblogic.
|
---|
318 | *
|
---|
319 | */
|
---|
320 | public void setHome(File weblogicHome) {
|
---|
321 | weblogicSystemHome = weblogicHome;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * The location of the BEA Home; implicitly
|
---|
326 | * selects Weblogic 6.0; optional.
|
---|
327 | *
|
---|
328 | * @param beaHome the BEA Home directory.
|
---|
329 | *
|
---|
330 | */
|
---|
331 | public void setBEAHome(File beaHome) {
|
---|
332 | this.beaHome = beaHome;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * The name of the weblogic server within the weblogic home which is to be run.
|
---|
337 | * Optiona, defaults to "myserver"
|
---|
338 | *
|
---|
339 | * @param serverName the name of the server.
|
---|
340 | */
|
---|
341 | public void setName(String serverName) {
|
---|
342 | this.weblogicSystemName = serverName;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Set the Domain to run in; required for WL6.0
|
---|
347 | *
|
---|
348 | * @param domain the domain
|
---|
349 | */
|
---|
350 | public void setDomain(String domain) {
|
---|
351 | this.weblogicDomainName = domain;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * The name of the server's properties file within the weblogic home directory
|
---|
356 | * used to control the weblogic instance;
|
---|
357 | * required for WL4.5.1
|
---|
358 | *
|
---|
359 | *
|
---|
360 | * @param propertiesFilename the properties file name
|
---|
361 | */
|
---|
362 | public void setProperties(String propertiesFilename) {
|
---|
363 | this.weblogicPropertiesFile = propertiesFilename;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Set the additional arguments to pass to the weblogic JVM
|
---|
368 | * @param args the arguments to be passed to the JVM
|
---|
369 | */
|
---|
370 | public void setJvmargs(String args) {
|
---|
371 | this.additionalJvmArgs = args;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Set the management username to run the server;
|
---|
376 | * optional and only applicable to WL6.0.
|
---|
377 | *
|
---|
378 | * @param username the management username of the server.
|
---|
379 | */
|
---|
380 | public void setUsername(String username) {
|
---|
381 | this.managementUsername = username;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Set the management password of the server;
|
---|
387 | * optional and only applicable to WL6.0.
|
---|
388 | * @param password the management pasword of the server.
|
---|
389 | */
|
---|
390 | public void setPassword(String password) {
|
---|
391 | this.managementPassword = password;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Set the private key password so the server can decrypt the SSL private key file;
|
---|
396 | * optional and only applicable to WL6.0.
|
---|
397 | * @param pkpassword the private key password,
|
---|
398 | */
|
---|
399 | public void setPKPassword(String pkpassword) {
|
---|
400 | this.pkPassword = pkpassword;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Additional argument string passed to the Weblogic instance;
|
---|
405 | * optional.
|
---|
406 | */
|
---|
407 | public void setArgs(String args) {
|
---|
408 | additionalArgs = args;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * name of the main class for weblogic; optional.
|
---|
413 | */
|
---|
414 | public void setWeblogicMainClass(String c) {
|
---|
415 | weblogicMainClass = c;
|
---|
416 | }
|
---|
417 | }
|
---|