KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.Vector JavaDoc;
27 import javax.ejb.deployment.DeploymentDescriptor;
28 import javax.ejb.deployment.EntityDescriptor;
29
30
31 /**
32  * A helper class which performs the actual work of the ejbc task.
33  *
34  * This class is run with a classpath which includes the weblogic tools and the home and remote
35  * interface class files referenced in the deployment descriptors being processed.
36  *
37  */

38 public final class EjbcHelper {
39     /**
40      * The root directory of the tree containing the serialised deployment desciptors.
41      */

42     private File JavaDoc descriptorDirectory;
43
44     /**
45      * The directory where generated files are placed.
46      */

47     private File JavaDoc generatedFilesDirectory;
48
49     /**
50      * The name of the manifest file generated for the EJB jar.
51      */

52     private File JavaDoc manifestFile;
53
54     /**
55      * The source directory for the home and remote interfaces. This is used to determine if
56      * the generated deployment classes are out of date.
57      */

58     private File JavaDoc sourceDirectory;
59
60     // CheckStyle:VisibilityModifier OFF - bc
61
/**
62      * The names of the serialised deployment descriptors
63      */

64     String JavaDoc[] descriptors;
65     // CheckStyle:VisibilityModifier ON
66

67     private boolean keepGenerated;
68
69     /**
70      * Command line interface for the ejbc helper task.
71      * @param args command line arguments.
72      * @throws Exception if there is a problem.
73      */

74     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
75         EjbcHelper helper = new EjbcHelper(args);
76         helper.process();
77     }
78
79     /**
80      * Initialise the EjbcHelper by reading the command arguments.
81      */

82     private EjbcHelper(String JavaDoc[] args) {
83         int index = 0;
84         descriptorDirectory = new File JavaDoc(args[index++]);
85         generatedFilesDirectory = new File JavaDoc(args[index++]);
86         sourceDirectory = new File JavaDoc(args[index++]);
87         manifestFile = new File JavaDoc(args[index++]);
88         keepGenerated = Boolean.valueOf(args[index++]).booleanValue();
89
90         descriptors = new String JavaDoc[args.length - index];
91         for (int i = 0; index < args.length; ++i) {
92             descriptors[i] = args[index++];
93         }
94     }
95
96     private String JavaDoc[] getCommandLine(boolean debug, File JavaDoc descriptorFile) {
97         Vector JavaDoc v = new Vector JavaDoc();
98         if (!debug) {
99             v.addElement("-noexit");
100         }
101         if (keepGenerated) {
102             v.addElement("-keepgenerated");
103         }
104         v.addElement("-d");
105         v.addElement(generatedFilesDirectory.getPath());
106         v.addElement(descriptorFile.getPath());
107
108         String JavaDoc[] args = new String JavaDoc[v.size()];
109         v.copyInto(args);
110         return args;
111     }
112
113     /**
114      * Determine if the weblogic EJB support classes need to be regenerated
115      * for a given deployment descriptor.
116      *
117      * This process attempts to determine if the support classes need to be
118      * rebuilt. It does this by examining only some of the support classes
119      * which are typically generated. If the ejbc task is interrupted generating
120      * the support classes for a bean, all of the support classes should be removed
121      * to force regeneration of the support classes.
122      *
123      * @param descriptorFile the serialised deployment descriptor
124      *
125      * @return true if the support classes need to be regenerated.
126      *
127      * @throws IOException if the descriptor file cannot be closed.
128      */

129     private boolean isRegenRequired(File JavaDoc descriptorFile) throws IOException JavaDoc {
130         // read in the descriptor. Under weblogic, the descriptor is a weblogic
131
// specific subclass which has references to the implementation classes.
132
// These classes must, therefore, be in the classpath when the deployment
133
// descriptor is loaded from the .ser file
134
FileInputStream JavaDoc fis = null;
135         try {
136             fis = new FileInputStream JavaDoc(descriptorFile);
137             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
138             DeploymentDescriptor dd = (DeploymentDescriptor) ois.readObject();
139             fis.close();
140
141             String JavaDoc homeInterfacePath
142                 = dd.getHomeInterfaceClassName().replace('.', '/') + ".java";
143             String JavaDoc remoteInterfacePath
144                 = dd.getRemoteInterfaceClassName().replace('.', '/') + ".java";
145             String JavaDoc primaryKeyClassPath = null;
146             if (dd instanceof EntityDescriptor) {
147                 primaryKeyClassPath
148                     = ((EntityDescriptor) dd).getPrimaryKeyClassName();
149                 primaryKeyClassPath
150                     = primaryKeyClassPath.replace('.', '/') + ".java";
151             }
152
153             File JavaDoc homeInterfaceSource = new File JavaDoc(sourceDirectory, homeInterfacePath);
154             File JavaDoc remoteInterfaceSource = new File JavaDoc(sourceDirectory, remoteInterfacePath);
155             File JavaDoc primaryKeyClassSource = null;
156             if (primaryKeyClassPath != null) {
157                 primaryKeyClassSource = new File JavaDoc(sourceDirectory, remoteInterfacePath);
158             }
159
160             // are any of the above out of date.
161
// we find the implementation classes and see if they are older than any
162
// of the above or the .ser file itself.
163
String JavaDoc beanClassBase = dd.getEnterpriseBeanClassName().replace('.', '/');
164             File JavaDoc ejbImplentationClass
165                 = new File JavaDoc(generatedFilesDirectory, beanClassBase + "EOImpl.class");
166             File JavaDoc homeImplementationClass
167                 = new File JavaDoc(generatedFilesDirectory, beanClassBase + "HomeImpl.class");
168             File JavaDoc beanStubClass
169                 = new File JavaDoc(generatedFilesDirectory, beanClassBase + "EOImpl_WLStub.class");
170
171             // if the implementation classes don;t exist regenerate
172
if (!ejbImplentationClass.exists()
173                 || !homeImplementationClass.exists()
174                 || !beanStubClass.exists()) {
175                 return true;
176             }
177
178             // Is the ser file or any of the source files newer then the class files.
179
// firstly find the oldest of the two class files.
180
long classModificationTime = ejbImplentationClass.lastModified();
181             if (homeImplementationClass.lastModified() < classModificationTime) {
182                 classModificationTime = homeImplementationClass.lastModified();
183             }
184             if (beanStubClass.lastModified() < classModificationTime) {
185                 classModificationTime = beanStubClass.lastModified();
186             }
187
188             if (descriptorFile.lastModified() > classModificationTime
189                 || homeInterfaceSource.lastModified() > classModificationTime
190                 || remoteInterfaceSource.lastModified() > classModificationTime) {
191                 return true;
192             }
193
194             if (primaryKeyClassSource != null
195                 && primaryKeyClassSource.lastModified() > classModificationTime) {
196                 return true;
197             }
198         } catch (Throwable JavaDoc descriptorLoadException) {
199             System.out.println("Exception occurred reading "
200                 + descriptorFile.getName() + " - continuing");
201             // any problems - just regenerate
202
return true;
203         } finally {
204             if (fis != null) {
205                 fis.close();
206             }
207         }
208
209         return false;
210     }
211
212     /**
213      * Process the descriptors in turn generating support classes for each and a manifest
214      * file for all of the beans.
215      */

216     private void process() throws Exception JavaDoc {
217         String JavaDoc manifest = "Manifest-Version: 1.0\n\n";
218         for (int i = 0; i < descriptors.length; ++i) {
219             String JavaDoc descriptorName = descriptors[i];
220             File JavaDoc descriptorFile = new File JavaDoc(descriptorDirectory, descriptorName);
221
222             if (isRegenRequired(descriptorFile)) {
223                 System.out.println("Running ejbc for " + descriptorFile.getName());
224                 regenerateSupportClasses(descriptorFile);
225             } else {
226                 System.out.println(descriptorFile.getName() + " is up to date");
227             }
228             manifest += "Name: " + descriptorName.replace('\\', '/')
229                         + "\nEnterprise-Bean: True\n\n";
230         }
231
232         FileWriter JavaDoc fw = new FileWriter JavaDoc(manifestFile);
233         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(fw);
234         pw.print(manifest);
235         fw.flush();
236         fw.close();
237     }
238
239     /**
240      * Perform the weblogic.ejbc call to regenerate the support classes.
241      *
242      * Note that this method relies on an undocumented -noexit option to the
243      * ejbc tool to stop the ejbc tool exiting the VM altogether.
244      */

245     private void regenerateSupportClasses(File JavaDoc descriptorFile) throws Exception JavaDoc {
246         // create a Java task to do the rebuild
247

248
249         String JavaDoc[] args = getCommandLine(false, descriptorFile);
250
251         try {
252             weblogic.ejbc.main(args);
253         } catch (Exception JavaDoc e) {
254             // run with no exit for better reporting
255
String JavaDoc[] newArgs = getCommandLine(true, descriptorFile);
256             weblogic.ejbc.main(newArgs);
257         }
258     }
259 }
260
Popular Tags