source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/ejb/DDCreatorHelper.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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