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.ObjectInputStream;
  21. import javax.ejb.deployment.DeploymentDescriptor;
  22. /**
  23. * A helper class which performs the actual work of the ddcreator task.
  24. *
  25. * This class is run with a classpath which includes the weblogic tools and the home and remote
  26. * interface class files referenced in the deployment descriptors being built.
  27. *
  28. */
  29. public class DDCreatorHelper {
  30. /**
  31. * The root directory of the tree containing the textual deployment descriptors.
  32. */
  33. private File descriptorDirectory;
  34. /**
  35. * The directory where generated serialised deployment descriptors are written.
  36. */
  37. private File generatedFilesDirectory;
  38. /**
  39. * The descriptor text files for which a serialised descriptor is to be created.
  40. */
  41. String[] descriptors;
  42. /**
  43. * The main method.
  44. *
  45. * The main method creates an instance of the DDCreatorHelper, passing it the
  46. * args which it then processes.
  47. */
  48. public static void main(String[] args) throws Exception {
  49. DDCreatorHelper helper = new DDCreatorHelper(args);
  50. helper.process();
  51. }
  52. /**
  53. * Initialise the helper with the command arguments.
  54. *
  55. */
  56. private DDCreatorHelper(String[] args) {
  57. int index = 0;
  58. descriptorDirectory = new File(args[index++]);
  59. generatedFilesDirectory = new File(args[index++]);
  60. descriptors = new String[args.length - index];
  61. for (int i = 0; index < args.length; ++i) {
  62. descriptors[i] = args[index++];
  63. }
  64. }
  65. /**
  66. * Do the actual work.
  67. *
  68. * The work proceeds by examining each descriptor given. If the serialised
  69. * file does not exist or is older than the text description, the weblogic
  70. * DDCreator tool is invoked directly to build the serialised descriptor.
  71. */
  72. private void process() throws Exception {
  73. for (int i = 0; i < descriptors.length; ++i) {
  74. String descriptorName = descriptors[i];
  75. File descriptorFile = new File(descriptorDirectory, descriptorName);
  76. int extIndex = descriptorName.lastIndexOf(".");
  77. String serName = null;
  78. if (extIndex != -1) {
  79. serName = descriptorName.substring(0, extIndex) + ".ser";
  80. } else {
  81. serName = descriptorName + ".ser";
  82. }
  83. File serFile = new File(generatedFilesDirectory, serName);
  84. // do we need to regenerate the file
  85. if (!serFile.exists() || serFile.lastModified() < descriptorFile.lastModified()
  86. || regenerateSerializedFile(serFile)) {
  87. String[] args = {"-noexit",
  88. "-d", serFile.getParent(),
  89. "-outputfile", serFile.getName(),
  90. descriptorFile.getPath()};
  91. try {
  92. weblogic.ejb.utils.DDCreator.main(args);
  93. } catch (Exception e) {
  94. // there was an exception - run with no exit to get proper error
  95. String[] newArgs = {"-d", generatedFilesDirectory.getPath(),
  96. "-outputfile", serFile.getName(),
  97. descriptorFile.getPath()};
  98. weblogic.ejb.utils.DDCreator.main(newArgs);
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * EJBC will fail if the serialized descriptor file does not match the bean classes.
  105. * You can test for this by trying to load the deployment descriptor. If it fails,
  106. * the serialized file needs to be regenerated because the associated class files
  107. * don't match.
  108. */
  109. private boolean regenerateSerializedFile(File serFile) {
  110. try {
  111. FileInputStream fis = new FileInputStream(serFile);
  112. ObjectInputStream ois = new ObjectInputStream(fis);
  113. DeploymentDescriptor dd = (DeploymentDescriptor) ois.readObject();
  114. fis.close();
  115. // Since the descriptor read properly, everything should be o.k.
  116. return false;
  117. } catch (Exception e) {
  118. // Weblogic will throw an error if the deployment descriptor does
  119. // not match the class files.
  120. return true;
  121. }
  122. }
  123. }