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