1. /*
  2. * Copyright 2000-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 java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Enumeration;
  24. import java.util.Hashtable;
  25. import java.util.Iterator;
  26. import java.util.Vector;
  27. import java.util.jar.JarEntry;
  28. import java.util.jar.JarFile;
  29. import java.util.jar.JarOutputStream;
  30. import javax.xml.parsers.SAXParser;
  31. import javax.xml.parsers.SAXParserFactory;
  32. import org.apache.tools.ant.AntClassLoader;
  33. import org.apache.tools.ant.BuildException;
  34. import org.apache.tools.ant.Project;
  35. import org.apache.tools.ant.taskdefs.Java;
  36. import org.apache.tools.ant.types.Environment;
  37. import org.apache.tools.ant.types.Path;
  38. import org.apache.tools.ant.util.FileUtils;
  39. import org.xml.sax.InputSource;
  40. /**
  41. The weblogic element is used to control the weblogic.ejbc compiler for
  42. generating weblogic EJB jars. Prior to Ant 1.3, the method of locating CMP
  43. descriptors was to use the ejbjar naming convention. So if your ejb-jar was
  44. called, Customer-ejb-jar.xml, your weblogic descriptor was called Customer-
  45. weblogic-ejb-jar.xml and your CMP descriptor had to be Customer-weblogic-cmp-
  46. rdbms-jar.xml. In addition, the <type-storage> element in the weblogic
  47. descriptor had to be set to the standard name META-INF/weblogic-cmp-rdbms-
  48. jar.xml, as that is where the CMP descriptor was mapped to in the generated
  49. jar.
  50. */
  51. public class WeblogicDeploymentTool extends GenericDeploymentTool {
  52. public static final String PUBLICID_EJB11
  53. = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN";
  54. public static final String PUBLICID_EJB20
  55. = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN";
  56. public static final String PUBLICID_WEBLOGIC_EJB510
  57. = "-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN";
  58. public static final String PUBLICID_WEBLOGIC_EJB600
  59. = "-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN";
  60. public static final String PUBLICID_WEBLOGIC_EJB700
  61. = "-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN";
  62. protected static final String DEFAULT_WL51_EJB11_DTD_LOCATION
  63. = "/weblogic/ejb/deployment/xml/ejb-jar.dtd";
  64. protected static final String DEFAULT_WL60_EJB11_DTD_LOCATION
  65. = "/weblogic/ejb20/dd/xml/ejb11-jar.dtd";
  66. protected static final String DEFAULT_WL60_EJB20_DTD_LOCATION
  67. = "/weblogic/ejb20/dd/xml/ejb20-jar.dtd";
  68. protected static final String DEFAULT_WL51_DTD_LOCATION
  69. = "/weblogic/ejb/deployment/xml/weblogic-ejb-jar.dtd";
  70. protected static final String DEFAULT_WL60_51_DTD_LOCATION
  71. = "/weblogic/ejb20/dd/xml/weblogic510-ejb-jar.dtd";
  72. protected static final String DEFAULT_WL60_DTD_LOCATION
  73. = "/weblogic/ejb20/dd/xml/weblogic600-ejb-jar.dtd";
  74. protected static final String DEFAULT_WL70_DTD_LOCATION
  75. = "/weblogic/ejb20/dd/xml/weblogic700-ejb-jar.dtd";
  76. protected static final String DEFAULT_COMPILER = "default";
  77. protected static final String WL_DD = "weblogic-ejb-jar.xml";
  78. protected static final String WL_CMP_DD = "weblogic-cmp-rdbms-jar.xml";
  79. protected static final String COMPILER_EJB11 = "weblogic.ejbc";
  80. protected static final String COMPILER_EJB20 = "weblogic.ejbc20";
  81. /** Instance variable that stores the suffix for the weblogic jarfile. */
  82. private String jarSuffix = ".jar";
  83. /** Instance variable that stores the location of the weblogic DTD file. */
  84. private String weblogicDTD;
  85. /** Instance variable that stores the location of the ejb 1.1 DTD file. */
  86. private String ejb11DTD;
  87. /** Instance variable that determines whether generic ejb jars are kept. */
  88. private boolean keepgenerated = false;
  89. /**
  90. * Instance variable that stores the fully qualified classname of the
  91. * weblogic EJBC compiler
  92. */
  93. private String ejbcClass = null;
  94. private String additionalArgs = "";
  95. /**
  96. * additional args to pass to the spawned jvm
  97. */
  98. private String additionalJvmArgs = "";
  99. private boolean keepGeneric = false;
  100. private String compiler = null;
  101. private boolean alwaysRebuild = true;
  102. /** controls whether ejbc is run on the generated jar */
  103. private boolean noEJBC = false;
  104. /** Indicates if the old CMP location convention is to be used. */
  105. private boolean newCMP = false;
  106. /** The classpath to the weblogic classes. */
  107. private Path wlClasspath = null;
  108. /** System properties for the JVM. */
  109. private Vector sysprops = new Vector();
  110. /**
  111. * The weblogic.StdoutSeverityLevel to use when running the JVM that
  112. * executes ejbc. Set to 16 to avoid the warnings about EJB Home and
  113. * Remotes being in the classpath
  114. */
  115. private Integer jvmDebugLevel = null;
  116. /** File utilities instance for copying jars */
  117. private FileUtils fileUtils = FileUtils.newFileUtils();
  118. private File outputDir;
  119. /**
  120. * Add a nested sysproperty element.
  121. */
  122. public void addSysproperty(Environment.Variable sysp) {
  123. sysprops.add(sysp);
  124. }
  125. /** Get the classpath to the weblogic classpaths */
  126. public Path createWLClasspath() {
  127. if (wlClasspath == null) {
  128. wlClasspath = new Path(getTask().getProject());
  129. }
  130. return wlClasspath.createPath();
  131. }
  132. /**
  133. * If set ejbc will use this directory as the output
  134. * destination rather than a jar file. This allows for the
  135. * generation of "exploded" jars.
  136. */
  137. public void setOutputDir(File outputDir) {
  138. this.outputDir = outputDir;
  139. }
  140. /**
  141. * Optional classpath to WL6.0.
  142. * Weblogic 6.0 will give a warning if the home and remote interfaces
  143. * of a bean are on the system classpath used to run weblogic.ejbc.
  144. * In that case, the standard weblogic classes should be set with
  145. * this attribute (or equivalent nested element) and the
  146. * home and remote interfaces located with the standard classpath
  147. * attribute
  148. */
  149. public void setWLClasspath(Path wlClasspath) {
  150. this.wlClasspath = wlClasspath;
  151. }
  152. /**
  153. * The compiler (switch <code>-compiler</code>) to use; optional.
  154. * This allows for the selection of a different compiler
  155. * to be used for the compilation of the generated Java
  156. * files. This could be set, for example, to Jikes to
  157. * compile with the Jikes compiler. If this is not set
  158. * and the <code>build.compiler</code> property is set
  159. * to jikes, the Jikes compiler will be used. If this
  160. * is not desired, the value "<code>default</code>"
  161. * may be given to use the default compiler
  162. */
  163. public void setCompiler(String compiler) {
  164. this.compiler = compiler;
  165. }
  166. /**
  167. * Set the rebuild flag to false to only update changes in the jar rather
  168. * than rerunning ejbc; optional, default true.
  169. * This flag controls whether weblogic.ejbc is always
  170. * invoked to build the jar file. In certain circumstances,
  171. * such as when only a bean class has been changed, the jar
  172. * can be generated by merely replacing the changed classes
  173. * and not rerunning ejbc. Setting this to false will reduce
  174. * the time to run ejbjar.
  175. */
  176. public void setRebuild(boolean rebuild) {
  177. this.alwaysRebuild = rebuild;
  178. }
  179. /**
  180. * Sets the weblogic.StdoutSeverityLevel to use when running the JVM that
  181. * executes ejbc; optional. Set to 16 to avoid the warnings about EJB Home and
  182. * Remotes being in the classpath
  183. */
  184. public void setJvmDebugLevel(Integer jvmDebugLevel) {
  185. this.jvmDebugLevel = jvmDebugLevel;
  186. }
  187. public Integer getJvmDebugLevel() {
  188. return jvmDebugLevel;
  189. }
  190. /**
  191. * Setter used to store the suffix for the generated weblogic jar file.
  192. *
  193. * @param inString the string to use as the suffix.
  194. */
  195. public void setSuffix(String inString) {
  196. this.jarSuffix = inString;
  197. }
  198. /**
  199. * controls whether the generic file used as input to
  200. * ejbc is retained; defaults to false
  201. *
  202. * @param inValue true for keep generic
  203. */
  204. public void setKeepgeneric(boolean inValue) {
  205. this.keepGeneric = inValue;
  206. }
  207. /**
  208. * Controls whether weblogic will keep the generated Java
  209. * files used to build the class files added to the
  210. * jar. This can be useful when debugging; default is false.
  211. *
  212. * @param inValue either 'true' or 'false'
  213. */
  214. public void setKeepgenerated(String inValue) {
  215. this.keepgenerated = Boolean.valueOf(inValue).booleanValue();
  216. }
  217. /**
  218. * Any optional extra arguments pass to the weblogic.ejbc
  219. * tool.
  220. */
  221. public void setArgs(String args) {
  222. this.additionalArgs = args;
  223. }
  224. /**
  225. * Set any additional arguments to pass to the weblogic JVM; optional.
  226. * @param args the arguments to be passed to the JVM
  227. */
  228. public void setJvmargs(String args) {
  229. this.additionalJvmArgs = args;
  230. }
  231. /**
  232. * Set the classname of the ejbc compiler; optional
  233. * Normally ejbjar determines
  234. * the appropriate class based on the DTD used for the EJB. The EJB 2.0 compiler
  235. * featured in weblogic 6 has, however, been deprecated in version 7. When
  236. * using with version 7 this attribute should be set to
  237. * "weblogic.ejbc" to avoid the deprecation warning.
  238. */
  239. public void setEjbcClass(String ejbcClass) {
  240. this.ejbcClass = ejbcClass;
  241. }
  242. /** Get the ejbc compiler class */
  243. public String getEjbcClass() {
  244. return ejbcClass;
  245. }
  246. /**
  247. * <b>Deprecated</b>. Defines the location of the ejb-jar DTD in
  248. * the weblogic class hierarchy. Should not be needed, and the
  249. * nested <dtd> element is recommended when it is.
  250. *
  251. * @param inString the string to use as the DTD location.
  252. */
  253. public void setWeblogicdtd(String inString) {
  254. setEJBdtd(inString);
  255. }
  256. /**
  257. * <b>Deprecated</b>. Defines the location of weblogic DTD in
  258. * the weblogic class hierarchy. Should not be needed, and the
  259. * nested <dtd> element is recommended when it is.
  260. *
  261. * @param inString the string to use as the DTD location.
  262. */
  263. public void setWLdtd(String inString) {
  264. this.weblogicDTD = inString;
  265. }
  266. /**
  267. * <b>Deprecated</b>. Defines the location of Sun's EJB DTD in
  268. * the weblogic class hierarchy. Should not be needed, and the
  269. * nested <dtd> element is recommended when it is.
  270. *
  271. * @param inString the string to use as the DTD location.
  272. */
  273. public void setEJBdtd(String inString) {
  274. this.ejb11DTD = inString;
  275. }
  276. /**
  277. * Set the value of the oldCMP scheme. This is an antonym for newCMP
  278. * @ant.attribute ignore="true'
  279. */
  280. public void setOldCMP(boolean oldCMP) {
  281. this.newCMP = !oldCMP;
  282. }
  283. /**
  284. * If this is set to true, the new method for locating
  285. * CMP descriptors will be used; optional, default false.
  286. * <P>
  287. * The old CMP scheme locates the
  288. * weblogic CMP descriptor based on the naming convention where the
  289. * weblogic CMP file is expected to be named with the bean name as the
  290. * prefix. Under this scheme the name of the CMP descriptor does not match
  291. * the name actually used in the main weblogic EJB descriptor. Also,
  292. * descriptors which contain multiple CMP references could not be used.
  293. */
  294. public void setNewCMP(boolean newCMP) {
  295. this.newCMP = newCMP;
  296. }
  297. /**
  298. * Do not EJBC the jar after it has been put together;
  299. * optional, default false
  300. */
  301. public void setNoEJBC(boolean noEJBC) {
  302. this.noEJBC = noEJBC;
  303. }
  304. protected void registerKnownDTDs(DescriptorHandler handler) {
  305. // register all the known DTDs
  306. handler.registerDTD(PUBLICID_EJB11, DEFAULT_WL51_EJB11_DTD_LOCATION);
  307. handler.registerDTD(PUBLICID_EJB11, DEFAULT_WL60_EJB11_DTD_LOCATION);
  308. handler.registerDTD(PUBLICID_EJB11, ejb11DTD);
  309. handler.registerDTD(PUBLICID_EJB20, DEFAULT_WL60_EJB20_DTD_LOCATION);
  310. }
  311. protected DescriptorHandler getWeblogicDescriptorHandler(final File srcDir) {
  312. DescriptorHandler handler =
  313. new DescriptorHandler(getTask(), srcDir) {
  314. protected void processElement() {
  315. if (currentElement.equals("type-storage")) {
  316. // Get the filename of vendor specific descriptor
  317. String fileNameWithMETA = currentText;
  318. //trim the META_INF\ off of the file name
  319. String fileName
  320. = fileNameWithMETA.substring(META_DIR.length(),
  321. fileNameWithMETA.length());
  322. File descriptorFile = new File(srcDir, fileName);
  323. ejbFiles.put(fileNameWithMETA, descriptorFile);
  324. }
  325. }
  326. };
  327. handler.registerDTD(PUBLICID_WEBLOGIC_EJB510, DEFAULT_WL51_DTD_LOCATION);
  328. handler.registerDTD(PUBLICID_WEBLOGIC_EJB510, DEFAULT_WL60_51_DTD_LOCATION);
  329. handler.registerDTD(PUBLICID_WEBLOGIC_EJB600, DEFAULT_WL60_DTD_LOCATION);
  330. handler.registerDTD(PUBLICID_WEBLOGIC_EJB700, DEFAULT_WL70_DTD_LOCATION);
  331. handler.registerDTD(PUBLICID_WEBLOGIC_EJB510, weblogicDTD);
  332. handler.registerDTD(PUBLICID_WEBLOGIC_EJB600, weblogicDTD);
  333. for (Iterator i = getConfig().dtdLocations.iterator(); i.hasNext();) {
  334. EjbJar.DTDLocation dtdLocation = (EjbJar.DTDLocation) i.next();
  335. handler.registerDTD(dtdLocation.getPublicId(), dtdLocation.getLocation());
  336. }
  337. return handler;
  338. }
  339. /**
  340. * Add any vendor specific files which should be included in the EJB Jar.
  341. */
  342. protected void addVendorFiles(Hashtable ejbFiles, String ddPrefix) {
  343. File weblogicDD = new File(getConfig().descriptorDir, ddPrefix + WL_DD);
  344. if (weblogicDD.exists()) {
  345. ejbFiles.put(META_DIR + WL_DD,
  346. weblogicDD);
  347. } else {
  348. log("Unable to locate weblogic deployment descriptor. "
  349. + "It was expected to be in "
  350. + weblogicDD.getPath(), Project.MSG_WARN);
  351. return;
  352. }
  353. if (!newCMP) {
  354. log("The old method for locating CMP files has been DEPRECATED.", Project.MSG_VERBOSE);
  355. log("Please adjust your weblogic descriptor and set "
  356. + "newCMP=\"true\" to use the new CMP descriptor "
  357. + "inclusion mechanism. ", Project.MSG_VERBOSE);
  358. // The the weblogic cmp deployment descriptor
  359. File weblogicCMPDD = new File(getConfig().descriptorDir, ddPrefix + WL_CMP_DD);
  360. if (weblogicCMPDD.exists()) {
  361. ejbFiles.put(META_DIR + WL_CMP_DD,
  362. weblogicCMPDD);
  363. }
  364. } else {
  365. // now that we have the weblogic descriptor, we parse the file
  366. // to find other descriptors needed to deploy the bean.
  367. // this could be the weblogic-cmp-rdbms.xml or any other O/R
  368. // mapping tool descriptors.
  369. try {
  370. File ejbDescriptor = (File) ejbFiles.get(META_DIR + EJB_DD);
  371. SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
  372. saxParserFactory.setValidating(true);
  373. SAXParser saxParser = saxParserFactory.newSAXParser();
  374. DescriptorHandler handler
  375. = getWeblogicDescriptorHandler(ejbDescriptor.getParentFile());
  376. saxParser.parse(new InputSource
  377. (new FileInputStream(weblogicDD)),
  378. handler);
  379. Hashtable ht = handler.getFiles();
  380. Enumeration e = ht.keys();
  381. while (e.hasMoreElements()) {
  382. String key = (String) e.nextElement();
  383. ejbFiles.put(key, ht.get(key));
  384. }
  385. } catch (Exception e) {
  386. String msg = "Exception while adding Vendor specific files: " + e.toString();
  387. throw new BuildException(msg, e);
  388. }
  389. }
  390. }
  391. /**
  392. * Get the vendor specific name of the Jar that will be output. The
  393. * modification date of this jar will be checked against the dependent
  394. * bean classes.
  395. */
  396. File getVendorOutputJarFile(String baseName) {
  397. return new File(getDestDir(), baseName + jarSuffix);
  398. }
  399. /**
  400. * Helper method invoked by execute() for each WebLogic jar to be built.
  401. * Encapsulates the logic of constructing a java task for calling
  402. * weblogic.ejbc and executing it.
  403. *
  404. * @param sourceJar java.io.File representing the source (EJB1.1) jarfile.
  405. * @param destJar java.io.File representing the destination, WebLogic
  406. * jarfile.
  407. */
  408. private void buildWeblogicJar(File sourceJar, File destJar, String publicId) {
  409. Java javaTask = null;
  410. if (noEJBC) {
  411. try {
  412. fileUtils.copyFile(sourceJar, destJar);
  413. if (!keepgenerated) {
  414. sourceJar.delete();
  415. }
  416. return;
  417. } catch (IOException e) {
  418. throw new BuildException("Unable to write EJB jar", e);
  419. }
  420. }
  421. String ejbcClassName = ejbcClass;
  422. try {
  423. javaTask = (Java) getTask().getProject().createTask("java");
  424. javaTask.setTaskName("ejbc");
  425. javaTask.createJvmarg().setLine(additionalJvmArgs);
  426. if (!(sysprops.isEmpty())) {
  427. for (Enumeration en = sysprops.elements(); en.hasMoreElements();) {
  428. Environment.Variable entry
  429. = (Environment.Variable) en.nextElement();
  430. javaTask.addSysproperty(entry);
  431. }
  432. }
  433. if (getJvmDebugLevel() != null) {
  434. javaTask.createJvmarg().setLine(" -Dweblogic.StdoutSeverityLevel=" + jvmDebugLevel);
  435. }
  436. if (ejbcClassName == null) {
  437. // try to determine it from publicId
  438. if (PUBLICID_EJB11.equals(publicId)) {
  439. ejbcClassName = COMPILER_EJB11;
  440. } else if (PUBLICID_EJB20.equals(publicId)) {
  441. ejbcClassName = COMPILER_EJB20;
  442. } else {
  443. log("Unrecognized publicId " + publicId
  444. + " - using EJB 1.1 compiler", Project.MSG_WARN);
  445. ejbcClassName = COMPILER_EJB11;
  446. }
  447. }
  448. javaTask.setClassname(ejbcClassName);
  449. javaTask.createArg().setLine(additionalArgs);
  450. if (keepgenerated) {
  451. javaTask.createArg().setValue("-keepgenerated");
  452. }
  453. if (compiler == null) {
  454. // try to use the compiler specified by build.compiler.
  455. // Right now we are just going to allow Jikes
  456. String buildCompiler
  457. = getTask().getProject().getProperty("build.compiler");
  458. if (buildCompiler != null && buildCompiler.equals("jikes")) {
  459. javaTask.createArg().setValue("-compiler");
  460. javaTask.createArg().setValue("jikes");
  461. }
  462. } else {
  463. if (!compiler.equals(DEFAULT_COMPILER)) {
  464. javaTask.createArg().setValue("-compiler");
  465. javaTask.createArg().setLine(compiler);
  466. }
  467. }
  468. Path combinedClasspath = getCombinedClasspath();
  469. if (wlClasspath != null && combinedClasspath != null
  470. && combinedClasspath.toString().trim().length() > 0) {
  471. javaTask.createArg().setValue("-classpath");
  472. javaTask.createArg().setPath(combinedClasspath);
  473. }
  474. javaTask.createArg().setValue(sourceJar.getPath());
  475. if (outputDir == null) {
  476. javaTask.createArg().setValue(destJar.getPath());
  477. } else {
  478. javaTask.createArg().setValue(outputDir.getPath());
  479. }
  480. Path classpath = wlClasspath;
  481. if (classpath == null) {
  482. classpath = getCombinedClasspath();
  483. }
  484. javaTask.setFork(true);
  485. if (classpath != null) {
  486. javaTask.setClasspath(classpath);
  487. }
  488. log("Calling " + ejbcClassName + " for " + sourceJar.toString(),
  489. Project.MSG_VERBOSE);
  490. if (javaTask.executeJava() != 0) {
  491. throw new BuildException("Ejbc reported an error");
  492. }
  493. } catch (Exception e) {
  494. // Have to catch this because of the semantics of calling main()
  495. String msg = "Exception while calling " + ejbcClassName
  496. + ". Details: " + e.toString();
  497. throw new BuildException(msg, e);
  498. }
  499. }
  500. /**
  501. * Method used to encapsulate the writing of the JAR file. Iterates over
  502. * the filenames/java.io.Files in the Hashtable stored on the instance
  503. * variable ejbFiles.
  504. */
  505. protected void writeJar(String baseName, File jarFile, Hashtable files,
  506. String publicId) throws BuildException {
  507. // need to create a generic jar first.
  508. File genericJarFile = super.getVendorOutputJarFile(baseName);
  509. super.writeJar(baseName, genericJarFile, files, publicId);
  510. if (alwaysRebuild || isRebuildRequired(genericJarFile, jarFile)) {
  511. buildWeblogicJar(genericJarFile, jarFile, publicId);
  512. }
  513. if (!keepGeneric) {
  514. log("deleting generic jar " + genericJarFile.toString(),
  515. Project.MSG_VERBOSE);
  516. genericJarFile.delete();
  517. }
  518. }
  519. /**
  520. * Called to validate that the tool parameters have been configured.
  521. */
  522. public void validateConfigured() throws BuildException {
  523. super.validateConfigured();
  524. }
  525. /**
  526. * Helper method to check to see if a weblogic EBJ1.1 jar needs to be
  527. * rebuilt using ejbc. Called from writeJar it sees if the "Bean" classes
  528. * are the only thing that needs to be updated and either updates the Jar
  529. * with the Bean classfile or returns true, saying that the whole weblogic
  530. * jar needs to be regened with ejbc. This allows faster build times for
  531. * working developers. <p>
  532. *
  533. * The way weblogic ejbc works is it creates wrappers for the publicly
  534. * defined methods as they are exposed in the remote interface. If the
  535. * actual bean changes without changing the the method signatures then
  536. * only the bean classfile needs to be updated and the rest of the
  537. * weblogic jar file can remain the same. If the Interfaces, ie. the
  538. * method signatures change or if the xml deployment descriptors changed,
  539. * the whole jar needs to be rebuilt with ejbc. This is not strictly true
  540. * for the xml files. If the JNDI name changes then the jar doesnt have to
  541. * be rebuild, but if the resources references change then it does. At
  542. * this point the weblogic jar gets rebuilt if the xml files change at
  543. * all.
  544. *
  545. * @param genericJarFile java.io.File The generic jar file.
  546. * @param weblogicJarFile java.io.File The weblogic jar file to check to
  547. * see if it needs to be rebuilt.
  548. */
  549. protected boolean isRebuildRequired(File genericJarFile, File weblogicJarFile) {
  550. boolean rebuild = false;
  551. JarFile genericJar = null;
  552. JarFile wlJar = null;
  553. File newWLJarFile = null;
  554. JarOutputStream newJarStream = null;
  555. try {
  556. log("Checking if weblogic Jar needs to be rebuilt for jar " + weblogicJarFile.getName(),
  557. Project.MSG_VERBOSE);
  558. // Only go forward if the generic and the weblogic file both exist
  559. if (genericJarFile.exists() && genericJarFile.isFile()
  560. && weblogicJarFile.exists() && weblogicJarFile.isFile()) {
  561. //open jar files
  562. genericJar = new JarFile(genericJarFile);
  563. wlJar = new JarFile(weblogicJarFile);
  564. Hashtable genericEntries = new Hashtable();
  565. Hashtable wlEntries = new Hashtable();
  566. Hashtable replaceEntries = new Hashtable();
  567. //get the list of generic jar entries
  568. for (Enumeration e = genericJar.entries(); e.hasMoreElements();) {
  569. JarEntry je = (JarEntry) e.nextElement();
  570. genericEntries.put(je.getName().replace('\\', '/'), je);
  571. }
  572. //get the list of weblogic jar entries
  573. for (Enumeration e = wlJar.entries(); e.hasMoreElements();) {
  574. JarEntry je = (JarEntry) e.nextElement();
  575. wlEntries.put(je.getName(), je);
  576. }
  577. //Cycle Through generic and make sure its in weblogic
  578. ClassLoader genericLoader
  579. = getClassLoaderFromJar(genericJarFile);
  580. for (Enumeration e = genericEntries.keys(); e.hasMoreElements();) {
  581. String filepath = (String) e.nextElement();
  582. if (wlEntries.containsKey(filepath)) {
  583. // File name/path match
  584. // Check files see if same
  585. JarEntry genericEntry = (JarEntry) genericEntries.get(filepath);
  586. JarEntry wlEntry = (JarEntry) wlEntries.get(filepath);
  587. if ((genericEntry.getCrc() != wlEntry.getCrc())
  588. || (genericEntry.getSize() != wlEntry.getSize())) {
  589. if (genericEntry.getName().endsWith(".class")) {
  590. //File are different see if its an object or an interface
  591. String classname
  592. = genericEntry.getName().replace(File.separatorChar, '.');
  593. classname = classname.substring(0, classname.lastIndexOf(".class"));
  594. Class genclass = genericLoader.loadClass(classname);
  595. if (genclass.isInterface()) {
  596. //Interface changed rebuild jar.
  597. log("Interface " + genclass.getName()
  598. + " has changed", Project.MSG_VERBOSE);
  599. rebuild = true;
  600. break;
  601. } else {
  602. //Object class Changed update it.
  603. replaceEntries.put(filepath, genericEntry);
  604. }
  605. } else {
  606. // is it the manifest. If so ignore it
  607. if (!genericEntry.getName().equals("META-INF/MANIFEST.MF")) {
  608. //File other then class changed rebuild
  609. log("Non class file " + genericEntry.getName()
  610. + " has changed", Project.MSG_VERBOSE);
  611. rebuild = true;
  612. break;
  613. }
  614. }
  615. }
  616. } else {
  617. // a file doesnt exist rebuild
  618. log("File " + filepath + " not present in weblogic jar",
  619. Project.MSG_VERBOSE);
  620. rebuild = true;
  621. break;
  622. }
  623. }
  624. if (!rebuild) {
  625. log("No rebuild needed - updating jar", Project.MSG_VERBOSE);
  626. newWLJarFile = new File(weblogicJarFile.getAbsolutePath() + ".temp");
  627. if (newWLJarFile.exists()) {
  628. newWLJarFile.delete();
  629. }
  630. newJarStream = new JarOutputStream(new FileOutputStream(newWLJarFile));
  631. newJarStream.setLevel(0);
  632. //Copy files from old weblogic jar
  633. for (Enumeration e = wlEntries.elements(); e.hasMoreElements();) {
  634. byte[] buffer = new byte[1024];
  635. int bytesRead;
  636. InputStream is;
  637. JarEntry je = (JarEntry) e.nextElement();
  638. if (je.getCompressedSize() == -1
  639. || je.getCompressedSize() == je.getSize()) {
  640. newJarStream.setLevel(0);
  641. } else {
  642. newJarStream.setLevel(9);
  643. }
  644. // Update with changed Bean class
  645. if (replaceEntries.containsKey(je.getName())) {
  646. log("Updating Bean class from generic Jar "
  647. + je.getName(), Project.MSG_VERBOSE);
  648. // Use the entry from the generic jar
  649. je = (JarEntry) replaceEntries.get(je.getName());
  650. is = genericJar.getInputStream(je);
  651. } else {
  652. //use fle from original weblogic jar
  653. is = wlJar.getInputStream(je);
  654. }
  655. newJarStream.putNextEntry(new JarEntry(je.getName()));
  656. while ((bytesRead = is.read(buffer)) != -1) {
  657. newJarStream.write(buffer, 0, bytesRead);
  658. }
  659. is.close();
  660. }
  661. } else {
  662. log("Weblogic Jar rebuild needed due to changed "
  663. + "interface or XML", Project.MSG_VERBOSE);
  664. }
  665. if (genericLoader instanceof AntClassLoader) {
  666. AntClassLoader loader = (AntClassLoader) genericLoader;
  667. loader.cleanup();
  668. }
  669. } else {
  670. rebuild = true;
  671. }
  672. } catch (ClassNotFoundException cnfe) {
  673. String cnfmsg = "ClassNotFoundException while processing ejb-jar file"
  674. + ". Details: "
  675. + cnfe.getMessage();
  676. throw new BuildException(cnfmsg, cnfe);
  677. } catch (IOException ioe) {
  678. String msg = "IOException while processing ejb-jar file "
  679. + ". Details: "
  680. + ioe.getMessage();
  681. throw new BuildException(msg, ioe);
  682. } finally {
  683. // need to close files and perhaps rename output
  684. if (genericJar != null) {
  685. try {
  686. genericJar.close();
  687. } catch (IOException closeException) {
  688. }
  689. }
  690. if (wlJar != null) {
  691. try {
  692. wlJar.close();
  693. } catch (IOException closeException) {
  694. }
  695. }
  696. if (newJarStream != null) {
  697. try {
  698. newJarStream.close();
  699. } catch (IOException closeException) {
  700. }
  701. try {
  702. fileUtils.rename(newWLJarFile, weblogicJarFile);
  703. } catch (IOException renameException) {
  704. log(renameException.getMessage(), Project.MSG_WARN);
  705. rebuild = true;
  706. }
  707. }
  708. }
  709. return rebuild;
  710. }
  711. /**
  712. * Helper method invoked by isRebuildRequired to get a ClassLoader for a
  713. * Jar File passed to it.
  714. *
  715. * @param classjar java.io.File representing jar file to get classes from.
  716. */
  717. protected ClassLoader getClassLoaderFromJar(File classjar) throws IOException {
  718. Path lookupPath = new Path(getTask().getProject());
  719. lookupPath.setLocation(classjar);
  720. Path classpath = getCombinedClasspath();
  721. if (classpath != null) {
  722. lookupPath.append(classpath);
  723. }
  724. return getTask().getProject().createClassLoader(lookupPath);
  725. }
  726. }