KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ejb > WLRun


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.ejb;
19
20
21 import java.io.File JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.taskdefs.Java;
25 import org.apache.tools.ant.types.Path;
26
27 /**
28  * Starts a WebLogic server.
29  * A number of parameters are used to control the operation of the weblogic
30  * instance. Note that the task, and hence ant, will not complete until the
31  * weblogic instance is stopped.</p>
32  *
33  */

34 public class WLRun extends Task {
35     protected static final String JavaDoc DEFAULT_WL51_POLICY_FILE = "weblogic.policy";
36     protected static final String JavaDoc DEFAULT_WL60_POLICY_FILE = "lib/weblogic.policy";
37     protected static final String JavaDoc DEFAULT_PROPERTIES_FILE = "weblogic.properties";
38
39     /**
40      * The classpath to be used when running the Java VM. It must contain the
41      * weblogic classes <b>and</b> the implementation classes of the home and
42      * remote interfaces.
43      */

44     private Path classpath;
45
46     /**
47      * The weblogic classpath to the be used when running weblogic.
48      */

49     private Path weblogicClasspath;
50
51     private String JavaDoc weblogicMainClass = "weblogic.Server";
52
53     /**
54      * Addional arguments to pass to the JVM used to run weblogic
55      */

56     private String JavaDoc additionalArgs = "";
57
58     /**
59      * The security policy to use when running the weblogic server
60      */

61     private String JavaDoc securityPolicy;
62
63     /**
64      * The weblogic system home directory
65      */

66     private File JavaDoc weblogicSystemHome;
67
68     /**
69      * The weblogic domain
70      */

71     private String JavaDoc weblogicDomainName;
72
73     /**
74      * The name of the weblogic server - used to select the server's directory in the
75      * weblogic home directory.
76      */

77     private String JavaDoc weblogicSystemName = "myserver";
78
79     /**
80      * The file containing the weblogic properties for this server.
81      */

82     private String JavaDoc weblogicPropertiesFile = null;
83
84     /**
85      * additional args to pass to the spawned jvm
86      */

87     private String JavaDoc additionalJvmArgs = "";
88
89     /**
90      * The location of the BEA Home under which this server is run.
91      * WL6 only
92      */

93     private File JavaDoc beaHome = null;
94
95     /**
96      * The management username
97      */

98     private String JavaDoc managementUsername = "system";
99
100     /**
101      * The management password
102      */

103     private String JavaDoc managementPassword = null;
104
105     /**
106      * The provate key password - used for SSL
107      */

108     private String JavaDoc pkPassword = null;
109
110     /**
111      * Add the classpath for the user classes
112      * @return a path to be configured
113      */

114     public Path createClasspath() {
115         if (classpath == null) {
116             classpath = new Path(getProject());
117         }
118         return classpath.createPath();
119     }
120
121     /**
122      * Get the classpath to the weblogic classpaths
123      * @return a path to be configured
124      */

125     public Path createWLClasspath() {
126         if (weblogicClasspath == null) {
127             weblogicClasspath = new Path(getProject());
128         }
129         return weblogicClasspath.createPath();
130     }
131
132     /**
133      * Do the work.
134      *
135      * The work is actually done by creating a separate JVM to run a helper task.
136      * This approach allows the classpath of the helper task to be set. Since the
137      * weblogic tools require the class files of the project's home and remote
138      * interfaces to be available in the classpath, this also avoids having to
139      * start ant with the class path of the project it is building.
140      *
141      * @exception BuildException if someting goes wrong with the build
142      */

143     public void execute() throws BuildException {
144         if (weblogicSystemHome == null) {
145             throw new BuildException("weblogic home must be set");
146         }
147         if (!weblogicSystemHome.isDirectory()) {
148             throw new BuildException("weblogic home directory "
149                 + weblogicSystemHome.getPath() + " is not valid");
150         }
151
152         if (beaHome != null) {
153             executeWLS6();
154         } else {
155             executeWLS();
156         }
157     }
158
159     private File JavaDoc findSecurityPolicyFile(String JavaDoc defaultSecurityPolicy) {
160         String JavaDoc securityPolicy = this.securityPolicy;
161         if (securityPolicy == null) {
162             securityPolicy = defaultSecurityPolicy;
163         }
164         File JavaDoc securityPolicyFile = new File JavaDoc(weblogicSystemHome, securityPolicy);
165         // If an explicit securityPolicy file was specified, it maybe an
166
// absolute path. Use the project to resolve it.
167
if (this.securityPolicy != null && !securityPolicyFile.exists()) {
168             securityPolicyFile = getProject().resolveFile(securityPolicy);
169         }
170         // If we still can't find it, complain
171
if (!securityPolicyFile.exists()) {
172             throw new BuildException("Security policy " + securityPolicy
173                                     + " was not found.");
174         }
175         return securityPolicyFile;
176     }
177
178     private void executeWLS6() {
179         File JavaDoc securityPolicyFile
180             = findSecurityPolicyFile(DEFAULT_WL60_POLICY_FILE);
181         if (!beaHome.isDirectory()) {
182             throw new BuildException("BEA home " + beaHome.getPath()
183                                      + " is not valid");
184         }
185
186         File JavaDoc configFile = new File JavaDoc(weblogicSystemHome, "config/"
187             + weblogicDomainName + "/config.xml");
188         if (!configFile.exists()) {
189             throw new BuildException("Server config file " + configFile
190                 + " not found.");
191         }
192
193         if (managementPassword == null) {
194             throw new BuildException("You must supply a management password "
195                                     + "to start the server");
196         }
197
198         Java weblogicServer = new Java(this);
199         weblogicServer.setTaskName(getTaskName());
200         weblogicServer.setFork(true);
201         weblogicServer.setDir(weblogicSystemHome);
202         weblogicServer.setClassname(weblogicMainClass);
203
204         String JavaDoc jvmArgs = additionalJvmArgs;
205
206         jvmArgs += " -Dweblogic.Domain=" + weblogicDomainName;
207         jvmArgs += " -Dweblogic.Name=" + weblogicSystemName;
208         jvmArgs += " -Dweblogic.system.home=" + weblogicSystemHome;
209
210         jvmArgs += " -Dbea.home=" + beaHome;
211         jvmArgs += " -Djava.security.policy==" + securityPolicyFile;
212
213         jvmArgs += " -Dweblogic.management.username=" + managementUsername;
214         jvmArgs += " -Dweblogic.management.password=" + managementPassword;
215         if (pkPassword != null) {
216             jvmArgs += " -Dweblogic.pkpassword=" + pkPassword;
217         }
218
219
220         weblogicServer.createJvmarg().setLine(jvmArgs);
221         weblogicServer.createArg().setLine(additionalArgs);
222
223         if (classpath != null) {
224             weblogicServer.setClasspath(classpath);
225         }
226
227         if (weblogicServer.executeJava() != 0) {
228             throw new BuildException("Execution of weblogic server failed");
229         }
230      }
231
232     private void executeWLS() {
233         File JavaDoc securityPolicyFile
234             = findSecurityPolicyFile(DEFAULT_WL51_POLICY_FILE);
235         File JavaDoc propertiesFile = null;
236
237
238         if (weblogicPropertiesFile == null) {
239             weblogicPropertiesFile = DEFAULT_PROPERTIES_FILE;
240         }
241         propertiesFile = new File JavaDoc(weblogicSystemHome, weblogicPropertiesFile);
242         if (!propertiesFile.exists()) {
243             // OK, properties file may be absolute
244
propertiesFile = getProject().resolveFile(weblogicPropertiesFile);
245             if (!propertiesFile.exists()) {
246                 throw new BuildException("Properties file "
247                     + weblogicPropertiesFile
248                     + " not found in weblogic home " + weblogicSystemHome
249                     + " or as absolute file");
250             }
251         }
252
253         Java weblogicServer = new Java(this);
254         weblogicServer.setFork(true);
255         weblogicServer.setClassname(weblogicMainClass);
256
257         String JavaDoc jvmArgs = additionalJvmArgs;
258
259         if (weblogicClasspath != null) {
260             jvmArgs += " -Dweblogic.class.path=" + weblogicClasspath;
261         }
262
263         jvmArgs += " -Djava.security.manager -Djava.security.policy==" + securityPolicyFile;
264         jvmArgs += " -Dweblogic.system.home=" + weblogicSystemHome;
265         jvmArgs += " -Dweblogic.system.name=" + weblogicSystemName;
266         jvmArgs += " -Dweblogic.system.propertiesFile=" + weblogicPropertiesFile;
267
268         weblogicServer.createJvmarg().setLine(jvmArgs);
269         weblogicServer.createArg().setLine(additionalArgs);
270
271         if (classpath != null) {
272             weblogicServer.setClasspath(classpath);
273         }
274         if (weblogicServer.executeJava() != 0) {
275             throw new BuildException("Execution of weblogic server failed");
276         }
277     }
278
279
280     /**
281      * The classpath to be used with the Java Virtual Machine that runs the Weblogic
282      * Server; required. Prior to Weblogic 6.0, this is typically set to the Weblogic
283      * boot classpath. Under Weblogic 6.0 this should include all the
284      * weblogic jars
285      *
286      * @param classpath the classpath to use when executing the weblogic server.
287      */

288     public void setClasspath(Path classpath) {
289         this.classpath = classpath;
290     }
291
292     /**
293      * Set the weblogic classpath used by the Weblogic Server;
294      * optional, and only applicable to WL4.5.1
295      *
296      * The weblogic classpath is used by weblogic to support dynamic class loading.
297      *
298      * @param weblogicClasspath the weblogic classpath
299      */

300     public void setWlclasspath(Path weblogicClasspath) {
301         this.weblogicClasspath = weblogicClasspath;
302     }
303
304     /**
305      * The name of the security policy file within the weblogic home directory that
306      * is to be used. If not specified, the default policy file <code>weblogic.policy</code>
307      * is used.
308      *
309      * @param securityPolicy the security policy to use.
310      */

311     public void setPolicy(String JavaDoc securityPolicy) {
312         this.securityPolicy = securityPolicy;
313     }
314
315     /**
316      * The location where weblogic lives.
317      * Required. This is the absolute location, not relative to
318      * BEA home.
319      * @param weblogicHome the home directory of weblogic.
320      *
321      */

322     public void setHome(File JavaDoc weblogicHome) {
323         weblogicSystemHome = weblogicHome;
324     }
325
326     /**
327      * The location of the BEA Home; implicitly
328      * selects Weblogic 6.0; optional.
329      *
330      * @param beaHome the BEA Home directory.
331      *
332      */

333     public void setBEAHome(File JavaDoc beaHome) {
334         this.beaHome = beaHome;
335     }
336
337     /**
338      * The name of the weblogic server within the weblogic home which is to be run.
339      * Optiona, defaults to &quot;myserver&quot;
340      *
341      * @param serverName the name of the server.
342      */

343     public void setName(String JavaDoc serverName) {
344         this.weblogicSystemName = serverName;
345     }
346
347     /**
348      * Set the Domain to run in; required for WL6.0
349      *
350      * @param domain the domain
351      */

352     public void setDomain(String JavaDoc domain) {
353         this.weblogicDomainName = domain;
354     }
355
356     /**
357      * The name of the server's properties file within the weblogic home directory
358      * used to control the weblogic instance;
359      * required for WL4.5.1
360      *
361      *
362      * @param propertiesFilename the properties file name
363      */

364     public void setProperties(String JavaDoc propertiesFilename) {
365         this.weblogicPropertiesFile = propertiesFilename;
366     }
367
368     /**
369      * Set the additional arguments to pass to the weblogic JVM
370      * @param args the arguments to be passed to the JVM
371      */

372     public void setJvmargs(String JavaDoc args) {
373         this.additionalJvmArgs = args;
374     }
375
376     /**
377      * Set the management username to run the server;
378      * optional and only applicable to WL6.0.
379      *
380      * @param username the management username of the server.
381      */

382     public void setUsername(String JavaDoc username) {
383         this.managementUsername = username;
384     }
385
386
387     /**
388      * Set the management password of the server;
389      * optional and only applicable to WL6.0.
390      * @param password the management pasword of the server.
391      */

392     public void setPassword(String JavaDoc password) {
393         this.managementPassword = password;
394     }
395
396     /**
397      * Set the private key password so the server can decrypt the SSL private key file;
398      * optional and only applicable to WL6.0.
399      * @param pkpassword the private key password,
400      */

401     public void setPKPassword(String JavaDoc pkpassword) {
402         this.pkPassword = pkpassword;
403     }
404
405     /**
406      * Additional argument string passed to the Weblogic instance;
407      * optional.
408      * @param args the argument string
409      */

410     public void setArgs(String JavaDoc args) {
411         additionalArgs = args;
412     }
413
414     /**
415      * name of the main class for weblogic; optional.
416      * @param c the name of the class
417      */

418     public void setWeblogicMainClass(String JavaDoc c) {
419         weblogicMainClass = c;
420     }
421 }
422
Popular Tags