001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.ejb;
019:
020: import java.io.File;
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Task;
023: import org.apache.tools.ant.taskdefs.Java;
024: import org.apache.tools.ant.types.Path;
025:
026: /**
027: * Starts a WebLogic server.
028: * A number of parameters are used to control the operation of the weblogic
029: * instance. Note that the task, and hence ant, will not complete until the
030: * weblogic instance is stopped.</p>
031: *
032: */
033: public class WLRun extends Task {
034: protected static final String DEFAULT_WL51_POLICY_FILE = "weblogic.policy";
035: protected static final String DEFAULT_WL60_POLICY_FILE = "lib/weblogic.policy";
036: protected static final String DEFAULT_PROPERTIES_FILE = "weblogic.properties";
037:
038: /**
039: * The classpath to be used when running the Java VM. It must contain the
040: * weblogic classes <b>and</b> the implementation classes of the home and
041: * remote interfaces.
042: */
043: private Path classpath;
044:
045: /**
046: * The weblogic classpath to the be used when running weblogic.
047: */
048: private Path weblogicClasspath;
049:
050: private String weblogicMainClass = "weblogic.Server";
051:
052: /**
053: * Addional arguments to pass to the JVM used to run weblogic
054: */
055: private String additionalArgs = "";
056:
057: /**
058: * The security policy to use when running the weblogic server
059: */
060: private String securityPolicy;
061:
062: /**
063: * The weblogic system home directory
064: */
065: private File weblogicSystemHome;
066:
067: /**
068: * The weblogic domain
069: */
070: private String weblogicDomainName;
071:
072: /**
073: * The name of the weblogic server - used to select the server's directory in the
074: * weblogic home directory.
075: */
076: private String weblogicSystemName = "myserver";
077:
078: /**
079: * The file containing the weblogic properties for this server.
080: */
081: private String weblogicPropertiesFile = null;
082:
083: /**
084: * additional args to pass to the spawned jvm
085: */
086: private String additionalJvmArgs = "";
087:
088: /**
089: * The location of the BEA Home under which this server is run.
090: * WL6 only
091: */
092: private File beaHome = null;
093:
094: /**
095: * The management username
096: */
097: private String managementUsername = "system";
098:
099: /**
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: * @return a path to be configured
112: */
113: public Path createClasspath() {
114: if (classpath == null) {
115: classpath = new Path(getProject());
116: }
117: return classpath.createPath();
118: }
119:
120: /**
121: * Get the classpath to the weblogic classpaths
122: * @return a path to be configured
123: */
124: public Path createWLClasspath() {
125: if (weblogicClasspath == null) {
126: weblogicClasspath = new Path(getProject());
127: }
128: return weblogicClasspath.createPath();
129: }
130:
131: /**
132: * Do the work.
133: *
134: * The work is actually done by creating a separate JVM to run a helper task.
135: * This approach allows the classpath of the helper task to be set. Since the
136: * weblogic tools require the class files of the project's home and remote
137: * interfaces to be available in the classpath, this also avoids having to
138: * start ant with the class path of the project it is building.
139: *
140: * @exception BuildException if someting goes wrong with the build
141: */
142: public void execute() throws BuildException {
143: if (weblogicSystemHome == null) {
144: throw new BuildException("weblogic home must be set");
145: }
146: if (!weblogicSystemHome.isDirectory()) {
147: throw new BuildException("weblogic home directory "
148: + weblogicSystemHome.getPath() + " is not valid");
149: }
150:
151: if (beaHome != null) {
152: executeWLS6();
153: } else {
154: executeWLS();
155: }
156: }
157:
158: private File findSecurityPolicyFile(String defaultSecurityPolicy) {
159: String securityPolicy = this .securityPolicy;
160: if (securityPolicy == null) {
161: securityPolicy = defaultSecurityPolicy;
162: }
163: File securityPolicyFile = new File(weblogicSystemHome,
164: 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(
169: securityPolicy);
170: }
171: // If we still can't find it, complain
172: if (!securityPolicyFile.exists()) {
173: throw new BuildException("Security policy "
174: + securityPolicy + " was not found.");
175: }
176: return securityPolicyFile;
177: }
178:
179: private void executeWLS6() {
180: File securityPolicyFile = 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 configFile = new File(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(
195: "You must supply a management password "
196: + "to start the server");
197: }
198:
199: Java weblogicServer = new Java(this );
200: weblogicServer.setTaskName(getTaskName());
201: weblogicServer.setFork(true);
202: weblogicServer.setDir(weblogicSystemHome);
203: weblogicServer.setClassname(weblogicMainClass);
204:
205: String jvmArgs = additionalJvmArgs;
206:
207: jvmArgs += " -Dweblogic.Domain=" + weblogicDomainName;
208: jvmArgs += " -Dweblogic.Name=" + weblogicSystemName;
209: jvmArgs += " -Dweblogic.system.home=" + weblogicSystemHome;
210:
211: jvmArgs += " -Dbea.home=" + beaHome;
212: jvmArgs += " -Djava.security.policy==" + securityPolicyFile;
213:
214: jvmArgs += " -Dweblogic.management.username="
215: + managementUsername;
216: jvmArgs += " -Dweblogic.management.password="
217: + managementPassword;
218: if (pkPassword != null) {
219: jvmArgs += " -Dweblogic.pkpassword=" + pkPassword;
220: }
221:
222: weblogicServer.createJvmarg().setLine(jvmArgs);
223: weblogicServer.createArg().setLine(additionalArgs);
224:
225: if (classpath != null) {
226: weblogicServer.setClasspath(classpath);
227: }
228:
229: if (weblogicServer.executeJava() != 0) {
230: throw new BuildException(
231: "Execution of weblogic server failed");
232: }
233: }
234:
235: private void executeWLS() {
236: File securityPolicyFile = findSecurityPolicyFile(DEFAULT_WL51_POLICY_FILE);
237: File propertiesFile = null;
238:
239: if (weblogicPropertiesFile == null) {
240: weblogicPropertiesFile = DEFAULT_PROPERTIES_FILE;
241: }
242: propertiesFile = new File(weblogicSystemHome,
243: weblogicPropertiesFile);
244: if (!propertiesFile.exists()) {
245: // OK, properties file may be absolute
246: propertiesFile = getProject().resolveFile(
247: weblogicPropertiesFile);
248: if (!propertiesFile.exists()) {
249: throw new BuildException("Properties file "
250: + weblogicPropertiesFile
251: + " not found in weblogic home "
252: + weblogicSystemHome + " or as absolute file");
253: }
254: }
255:
256: Java weblogicServer = new Java(this );
257: weblogicServer.setFork(true);
258: weblogicServer.setClassname(weblogicMainClass);
259:
260: String jvmArgs = additionalJvmArgs;
261:
262: if (weblogicClasspath != null) {
263: jvmArgs += " -Dweblogic.class.path=" + weblogicClasspath;
264: }
265:
266: jvmArgs += " -Djava.security.manager -Djava.security.policy=="
267: + securityPolicyFile;
268: jvmArgs += " -Dweblogic.system.home=" + weblogicSystemHome;
269: jvmArgs += " -Dweblogic.system.name=" + weblogicSystemName;
270: jvmArgs += " -Dweblogic.system.propertiesFile="
271: + weblogicPropertiesFile;
272:
273: weblogicServer.createJvmarg().setLine(jvmArgs);
274: weblogicServer.createArg().setLine(additionalArgs);
275:
276: if (classpath != null) {
277: weblogicServer.setClasspath(classpath);
278: }
279: if (weblogicServer.executeJava() != 0) {
280: throw new BuildException(
281: "Execution of weblogic server failed");
282: }
283: }
284:
285: /**
286: * The classpath to be used with the Java Virtual Machine that runs the Weblogic
287: * Server; required. Prior to Weblogic 6.0, this is typically set to the Weblogic
288: * boot classpath. Under Weblogic 6.0 this should include all the
289: * weblogic jars
290: *
291: * @param classpath the classpath to use when executing the weblogic server.
292: */
293: public void setClasspath(Path classpath) {
294: this .classpath = classpath;
295: }
296:
297: /**
298: * Set the weblogic classpath used by the Weblogic Server;
299: * optional, and only applicable to WL4.5.1
300: *
301: * The weblogic classpath is used by weblogic to support dynamic class loading.
302: *
303: * @param weblogicClasspath the weblogic classpath
304: */
305: public void setWlclasspath(Path weblogicClasspath) {
306: this .weblogicClasspath = weblogicClasspath;
307: }
308:
309: /**
310: * The name of the security policy file within the weblogic home directory that
311: * is to be used. If not specified, the default policy file <code>weblogic.policy</code>
312: * is used.
313: *
314: * @param securityPolicy the security policy to use.
315: */
316: public void setPolicy(String securityPolicy) {
317: this .securityPolicy = securityPolicy;
318: }
319:
320: /**
321: * The location where weblogic lives.
322: * Required. This is the absolute location, not relative to
323: * BEA home.
324: * @param weblogicHome the home directory of weblogic.
325: *
326: */
327: public void setHome(File weblogicHome) {
328: weblogicSystemHome = weblogicHome;
329: }
330:
331: /**
332: * The location of the BEA Home; implicitly
333: * selects Weblogic 6.0; optional.
334: *
335: * @param beaHome the BEA Home directory.
336: *
337: */
338: public void setBEAHome(File beaHome) {
339: this .beaHome = beaHome;
340: }
341:
342: /**
343: * The name of the weblogic server within the weblogic home which is to be run.
344: * Optiona, defaults to "myserver"
345: *
346: * @param serverName the name of the server.
347: */
348: public void setName(String serverName) {
349: this .weblogicSystemName = serverName;
350: }
351:
352: /**
353: * Set the Domain to run in; required for WL6.0
354: *
355: * @param domain the domain
356: */
357: public void setDomain(String domain) {
358: this .weblogicDomainName = domain;
359: }
360:
361: /**
362: * The name of the server's properties file within the weblogic home directory
363: * used to control the weblogic instance;
364: * required for WL4.5.1
365: *
366: *
367: * @param propertiesFilename the properties file name
368: */
369: public void setProperties(String propertiesFilename) {
370: this .weblogicPropertiesFile = propertiesFilename;
371: }
372:
373: /**
374: * Set the additional arguments to pass to the weblogic JVM
375: * @param args the arguments to be passed to the JVM
376: */
377: public void setJvmargs(String args) {
378: this .additionalJvmArgs = args;
379: }
380:
381: /**
382: * Set the management username to run the server;
383: * optional and only applicable to WL6.0.
384: *
385: * @param username the management username of the server.
386: */
387: public void setUsername(String username) {
388: this .managementUsername = username;
389: }
390:
391: /**
392: * Set the management password of the server;
393: * optional and only applicable to WL6.0.
394: * @param password the management pasword of the server.
395: */
396: public void setPassword(String password) {
397: this .managementPassword = password;
398: }
399:
400: /**
401: * Set the private key password so the server can decrypt the SSL private key file;
402: * optional and only applicable to WL6.0.
403: * @param pkpassword the private key password,
404: */
405: public void setPKPassword(String pkpassword) {
406: this .pkPassword = pkpassword;
407: }
408:
409: /**
410: * Additional argument string passed to the Weblogic instance;
411: * optional.
412: * @param args the argument string
413: */
414: public void setArgs(String args) {
415: additionalArgs = args;
416: }
417:
418: /**
419: * name of the main class for weblogic; optional.
420: * @param c the name of the class
421: */
422: public void setWeblogicMainClass(String c) {
423: weblogicMainClass = c;
424: }
425: }
|