KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectInputStream JavaDoc;
23 import javax.ejb.deployment.DeploymentDescriptor;
24
25 /**
26  * A helper class which performs the actual work of the ddcreator task.
27  *
28  * This class is run with a classpath which includes the weblogic tools and the home and remote
29  * interface class files referenced in the deployment descriptors being built.
30  *
31  */

32 public final class DDCreatorHelper {
33     /**
34      * The root directory of the tree containing the textual deployment descriptors.
35      */

36     private File JavaDoc descriptorDirectory;
37
38     /**
39      * The directory where generated serialised deployment descriptors are written.
40      */

41     private File JavaDoc generatedFilesDirectory;
42
43     // CheckStyle:VisibilityModifier OFF - bc
44
/**
45      * The descriptor text files for which a serialised descriptor is to be created.
46      */

47     String JavaDoc[] descriptors;
48     // CheckStyle:VisibilityModifier ON
49

50     /**
51      * The main method.
52      *
53      * The main method creates an instance of the DDCreatorHelper, passing it the
54      * args which it then processes.
55      * @param args the arguments
56      * @throws Exception on error
57      */

58     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
59         DDCreatorHelper helper = new DDCreatorHelper(args);
60         helper.process();
61     }
62
63     /**
64      * Initialise the helper with the command arguments.
65      *
66      */

67     private DDCreatorHelper(String JavaDoc[] args) {
68         int index = 0;
69         descriptorDirectory = new File JavaDoc(args[index++]);
70         generatedFilesDirectory = new File JavaDoc(args[index++]);
71
72         descriptors = new String JavaDoc[args.length - index];
73         for (int i = 0; index < args.length; ++i) {
74             descriptors[i] = args[index++];
75         }
76     }
77
78     /**
79      * Do the actual work.
80      *
81      * The work proceeds by examining each descriptor given. If the serialised
82      * file does not exist or is older than the text description, the weblogic
83      * DDCreator tool is invoked directly to build the serialised descriptor.
84      */

85     private void process() throws Exception JavaDoc {
86         for (int i = 0; i < descriptors.length; ++i) {
87             String JavaDoc descriptorName = descriptors[i];
88             File JavaDoc descriptorFile = new File JavaDoc(descriptorDirectory, descriptorName);
89
90             int extIndex = descriptorName.lastIndexOf(".");
91             String JavaDoc serName = null;
92             if (extIndex != -1) {
93                 serName = descriptorName.substring(0, extIndex) + ".ser";
94             } else {
95                 serName = descriptorName + ".ser";
96             }
97             File JavaDoc serFile = new File JavaDoc(generatedFilesDirectory, serName);
98
99             // do we need to regenerate the file
100
if (!serFile.exists() || serFile.lastModified() < descriptorFile.lastModified()
101                 || regenerateSerializedFile(serFile)) {
102
103                 String JavaDoc[] args = {"-noexit",
104                                  "-d", serFile.getParent(),
105                                  "-outputfile", serFile.getName(),
106                                  descriptorFile.getPath()};
107                 try {
108                     weblogic.ejb.utils.DDCreator.main(args);
109                 } catch (Exception JavaDoc e) {
110                     // there was an exception - run with no exit to get proper error
111
String JavaDoc[] newArgs = {"-d", generatedFilesDirectory.getPath(),
112                                  "-outputfile", serFile.getName(),
113                                  descriptorFile.getPath()};
114                     weblogic.ejb.utils.DDCreator.main(newArgs);
115                 }
116             }
117         }
118     }
119
120     /**
121      * EJBC will fail if the serialized descriptor file does not match the bean classes.
122      * You can test for this by trying to load the deployment descriptor. If it fails,
123      * the serialized file needs to be regenerated because the associated class files
124      * don't match.
125      */

126     private boolean regenerateSerializedFile(File JavaDoc serFile) {
127         try {
128
129             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(serFile);
130             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
131             DeploymentDescriptor dd = (DeploymentDescriptor) ois.readObject();
132             fis.close();
133
134             // Since the descriptor read properly, everything should be o.k.
135
return false;
136
137         } catch (Exception JavaDoc e) {
138
139             // Weblogic will throw an error if the deployment descriptor does
140
// not match the class files.
141
return true;
142
143         }
144     }
145 }
146
Popular Tags