KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > ant > taskdef > ApplicationFileGenerator


1 package net.sf.invicta.ant.taskdef;
2
3 import org.apache.tools.ant.BuildException;
4 import org.apache.tools.ant.Task;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.jar.JarFile JavaDoc;
18 import java.util.zip.ZipEntry JavaDoc;
19
20 /**
21  * ANT task for generating the 'application.xml' file that defines
22  * the modules of an EAR file.
23  *
24  */

25 public class ApplicationFileGenerator extends Task {
26
27
28     private final static String JavaDoc EAR_APPLICATION_XML_PATH =
29         "META-INF/application.xml";
30     
31     private final static String JavaDoc APPLICATION_CLOSING_TAG = "</application>";
32     private final String JavaDoc header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
33         "<!DOCTYPE application PUBLIC \"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"" +
34         
35         " \"http://java.sun.com/dtd/application_1_3.dtd\">";
36     
37     private final String JavaDoc TYPE_EJB = "ejb";
38     private final String JavaDoc TYPE_JAVA = "java";
39     private final String JavaDoc TYPE_WEB = "web";
40     
41     private String JavaDoc appxml;
42     private String JavaDoc projectName;
43     private List JavaDoc modules;
44     private String JavaDoc earFile;
45     
46     /***
47      * Default Constructor
48      */

49     public ApplicationFileGenerator()
50     {
51         super();
52         this.appxml = null;
53         this.modules = new ArrayList JavaDoc();
54         this.earFile = null;
55     }
56
57     public void execute() throws BuildException {
58         // Make sure we got all parameters.
59
if (this.appxml == null)
60             throw new BuildException("Appxml file was not specified.");
61         if (this.projectName == null)
62                 throw new BuildException("Project name was not specified.");
63         
64         StringBuffer JavaDoc existingApp = null;
65         if (this.earFile != null) {
66             log("Using application.xml from existing ear file '" + this.earFile + "'");
67             existingApp = fetchExistingContent(this.earFile);
68         }
69                 
70         // Read the current content of application.xml
71
byte[] buffer = new byte[1024];
72         InputStream JavaDoc inputStream;
73         StringBuffer JavaDoc previousFileContent = new StringBuffer JavaDoc();
74         try {
75             inputStream = new FileInputStream JavaDoc(this.appxml);
76             int res = 0;
77             while ((res = inputStream.read(buffer)) != -1)
78                 previousFileContent.append(new String JavaDoc(buffer, 0, res));
79             inputStream.close();
80         } catch (FileNotFoundException JavaDoc e) {
81         } catch (IOException JavaDoc e) {
82         }
83             
84         
85         
86         // Write header
87
StringBuffer JavaDoc output = new StringBuffer JavaDoc();
88             
89         // Write the module elements for all given modules.
90
for (Iterator JavaDoc iter = this.modules.iterator(); iter.hasNext();) {
91             Module module = (Module) iter.next();
92
93             
94             // Make sure that the type and file were given
95
if (module.getType() == null)
96                 throw new BuildException("Module type was not specified.");
97             if (module.getFile() == null)
98                 throw new BuildException("Module file was not specified.");
99             
100             // Check if this module already defined
101
if ((this.earFile != null) &&
102                 (existingApp.toString().indexOf(">" + module.getFile() + "<") != -1)) {
103                 log("Warning: Module '" + module.getFile() + "' is already defined in application.xml");
104                 continue;
105             }
106                     
107             output.append("\t<module>\n");
108
109             File JavaDoc file = new File JavaDoc(module.getFile());
110             
111             // Handle a Java module
112
if(module.getType().equalsIgnoreCase(TYPE_JAVA)) {
113                 output.append("\t\t<java>" + file.getName() + "</java>\n");
114                 
115             // Handle an EJB module
116
} else if(module.getType().equalsIgnoreCase(TYPE_EJB)) {
117                 output.append("\t\t<ejb>" + file.getName() + "</ejb>\n");
118                 
119             // Handle a WEB module
120
} else if(module.getType().equalsIgnoreCase(TYPE_WEB)) {
121                 if (module.getName() == null)
122                     throw new BuildException("Module name was not specified.");
123             
124                 output.append("\t\t<web>\n");
125                 output.append("\t\t\t<web-uri>" + file.getName() + "</web-uri>\n");
126                 output.append("\t\t\t<context-root>" + module.getName() + "</context-root>\n");
127                 output.append("\t\t</web>\n");
128                 
129             } else {
130                 throw new BuildException("Unknown type: " + module.getType());
131             }
132                         
133             output.append("\t</module>\n");
134
135         }
136         
137         if (this.earFile == null) {
138                     
139             output.insert(0,
140                         header + "\n\n<application>\n" +
141                         " <display-name>" + this.projectName + "</display-name>\n" +
142                         " <description>'" + this.projectName + "' applications descriptor file</description>\n");
143         
144             
145         
146             // Print footer and close the file.
147
output.append("</application>\n");
148         } else {
149             int tagPos = existingApp.toString().indexOf(APPLICATION_CLOSING_TAG);
150             if (tagPos == 1)
151                 throw new BuildException("Invalid EAR application.xml (missing closing tag)");
152                 
153             existingApp.insert(tagPos, output.toString());
154             
155             output = existingApp;
156         }
157
158         // If the content is differnet from the existing output file,
159
// then write the output file.
160
if (!previousFileContent.toString().equals(output.toString())) {
161         
162             // Open the file for writing.
163
PrintWriter JavaDoc out = null;
164             try {
165                 out = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(this.appxml));
166             } catch (FileNotFoundException JavaDoc e) {
167                 throw new BuildException(e.toString());
168             }
169             out.print(output.toString());
170                 
171             out.close();
172             log("Application file was created successfully (" + this.appxml + ")");
173         } else {
174             log("Application file should not be modified (" + this.appxml + ")");
175         }
176                 
177          
178     }
179     /**
180      * Method fetchExistingContent.
181      * @param string
182      * @return String
183      */

184     private StringBuffer JavaDoc fetchExistingContent(String JavaDoc earFile) {
185         
186         char[] buffer = new char[1024];
187         try {
188             
189             JarFile JavaDoc jarFile = new JarFile JavaDoc(earFile);
190             ZipEntry JavaDoc appEntry = jarFile.getEntry(EAR_APPLICATION_XML_PATH);
191             if (appEntry == null)
192                 throw new BuildException("No application.xml found in '" + earFile + "'");
193             
194             InputStreamReader JavaDoc appStream = new InputStreamReader JavaDoc(jarFile.getInputStream(appEntry));
195             
196             StringBuffer JavaDoc appContent = new StringBuffer JavaDoc();
197             int res = 0;
198             while ((res = appStream.read(buffer)) != -1)
199              appContent.append(new String JavaDoc(buffer, 0, res));
200             
201             return appContent;
202         } catch (IOException JavaDoc e) {
203             throw new BuildException("Error in reading EAR file '" + earFile + "': " + e.getMessage());
204         }
205     }
206
207     public void setAppxml(String JavaDoc appxml)
208     {
209         this.appxml = appxml;
210     }
211
212     public void addConfiguredModule(Module module)
213     {
214         this.modules.add(module);
215     }
216
217     
218     /**
219      * Sets the projectName.
220      * @param projectName The projectName to set
221      */

222     public void setProjectName(String JavaDoc projectName) {
223         this.projectName = projectName;
224     }
225
226     /**
227      * Returns the earFile.
228      * @return String
229      */

230     public String JavaDoc getEarFile() {
231         return earFile;
232     }
233
234     /**
235      * Sets the earFile.
236      * @param earFile The earFile to set
237      */

238     public void setEarFile(String JavaDoc earFile) {
239         this.earFile = earFile;
240     }
241
242 }
243
244
245
Popular Tags