KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ear > deployment > lib > EarDeploymentDescManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: EarDeploymentDescManager.java,v 1.5 2004/10/14 16:20:45 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_ear.deployment.lib;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.Reader JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
36 import java.util.zip.ZipEntry JavaDoc;
37
38 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
39 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
40 import org.objectweb.jonas_ear.deployment.rules.ApplicationRuleSet;
41 import org.objectweb.jonas_ear.deployment.rules.JonasApplicationRuleSet;
42 import org.objectweb.jonas_ear.deployment.xml.Application;
43 import org.objectweb.jonas_ear.deployment.xml.JonasApplication;
44
45 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
46 import org.objectweb.jonas_lib.deployment.digester.JDigester;
47 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager;
48
49 import org.objectweb.jonas.common.Log;
50
51 import org.objectweb.util.monolog.api.BasicLevel;
52 import org.objectweb.util.monolog.api.Logger;
53
54 /**
55  * This class extends the AbsDeploymentDescriptor class of JOnAS It provides a
56  * description of the specific EAR desployment descriptor
57  * @author Florent Benoit
58  * @author Ludovic Bert
59  * @author Helene Joanin
60  */

61 public class EarDeploymentDescManager extends AbsDeploymentDescManager {
62
63     /**
64      * Path of the application.xml deploymnet descriptor file
65      */

66     public static final String JavaDoc APPLICATION_FILE_NAME = "META-INF/application.xml";
67
68     /**
69      * Path of the jonas-application.xml deploymnet descriptor file
70      */

71     public static final String JavaDoc JONAS_APPLICATION_FILE_NAME = "META-INF/jonas-application.xml";
72
73
74     /**
75      * Digester used to parse application.xml
76      */

77     private static JDigester earDigester = null;
78
79     /**
80      * Digester used to parse jonas-application.xml
81      */

82     private static JDigester jonasEarDigester = null;
83
84     /**
85      * Rules to parse the application.xml
86      */

87     private static ApplicationRuleSet appRuleSet = new ApplicationRuleSet();
88
89     /**
90      * Rules to parse the jonas-application.xml
91      */

92     private static JonasApplicationRuleSet jonasApplicationRuleSet = new JonasApplicationRuleSet();
93
94     /**
95      * logger
96      */

97     private static Logger logger = Log.getLogger(Log.JONAS_EAR_PREFIX);
98
99     /**
100      * Flag for parser validation
101      */

102     private static boolean parsingWithValidation = true;
103
104     /**
105      * Private Empty constructor for utility class
106      */

107     private EarDeploymentDescManager() {
108     }
109
110     /**
111      * Get an instance of an EAR deployment descriptor by parsing the
112      * application.xml deployment descriptor.
113      * @param earFileName the fileName of the war file for the deployment
114      * descriptors.
115      * @param classLoaderForCls the classloader for the classes.
116      * @return an EAR deployment descriptor by parsing the application.xml
117      * deployment descriptor.
118      * @throws EarDeploymentDescException if the deployment descriptor is
119      * corrupted.
120      */

121     public static EarDeploymentDesc getDeploymentDesc(String JavaDoc earFileName, ClassLoader JavaDoc classLoaderForCls)
122             throws EarDeploymentDescException {
123
124         //ear file
125
JarFile JavaDoc earFile = null;
126
127         //Input Stream
128
InputStream JavaDoc appInputStream = null;
129         InputStream JavaDoc jonasApplicationInputStream = null;
130
131         //ZipEntry
132
ZipEntry JavaDoc appZipEntry = null;
133         ZipEntry JavaDoc jonasApplicationZipEntry = null;
134
135         Application application = null;
136         JonasApplication jonasApplication = null;
137
138         String JavaDoc xmlContent = "";
139         String JavaDoc jonasXmlContent = "";
140
141         //Check if the file exists.
142
File JavaDoc earF = new File JavaDoc(earFileName);
143         if (!earF.exists()) {
144             throw new EarDeploymentDescException("The file '" + earFileName + "' was not found.");
145         }
146
147         // load deployment descriptor data (META-INF/application.xml)
148
try {
149             // Is it a directory ?
150
if (earF.isDirectory()) {
151                 //lookup a META-INF/application.xml file
152
File JavaDoc earXmlF = new File JavaDoc(earFileName, APPLICATION_FILE_NAME);
153                 if (!earXmlF.exists()) {
154                     String JavaDoc err = "You have choose to deploy an ear directory but there is no " + APPLICATION_FILE_NAME
155                             + " file in the directory " + earFileName;
156                     throw new EarDeploymentDescException(err);
157                 }
158                 appInputStream = new FileInputStream JavaDoc(earXmlF);
159                 xmlContent = xmlContent(appInputStream);
160                 // necessary to have a not empty InputStream !!!
161
appInputStream = new FileInputStream JavaDoc(earXmlF);
162
163                 //lookup a META-INF/jonas-application.xml file
164
File JavaDoc jonasEarXmlF = new File JavaDoc(earFileName, JONAS_APPLICATION_FILE_NAME);
165                 // file is optional
166
if (jonasEarXmlF.exists()) {
167                     jonasApplicationInputStream = new FileInputStream JavaDoc(jonasEarXmlF);
168                     jonasXmlContent = xmlContent(jonasApplicationInputStream);
169                     // necessary to have a not empty InputStream !!!
170
jonasApplicationInputStream = new FileInputStream JavaDoc(jonasEarXmlF);
171                 }
172             } else {
173                 // it is an .ear file
174
earFile = new JarFile JavaDoc(earFileName);
175
176                 //Check the application entry
177
appZipEntry = earFile.getEntry(APPLICATION_FILE_NAME);
178                 if (appZipEntry == null) {
179                     throw new EarDeploymentDescException("The entry '" + APPLICATION_FILE_NAME
180                             + "' was not found in the file '" + earFileName + "'.");
181                 }
182
183                 //Get the stream
184
appInputStream = earFile.getInputStream(appZipEntry);
185                 xmlContent = xmlContent(appInputStream);
186                 // necessary to have a not empty InputStream !!!
187
appInputStream = earFile.getInputStream(appZipEntry);
188
189                 // Check the jonas-application entry
190
jonasApplicationZipEntry = earFile.getEntry(JONAS_APPLICATION_FILE_NAME);
191                 if (jonasApplicationZipEntry != null) {
192                     //Get the stream
193
jonasApplicationInputStream = earFile.getInputStream(jonasApplicationZipEntry);
194                     jonasXmlContent = xmlContent(jonasApplicationInputStream);
195                     // necessary to have a not empty InputStream !!!
196
jonasApplicationInputStream = earFile.getInputStream(jonasApplicationZipEntry);
197                 }
198             }
199         } catch (Exception JavaDoc e) {
200             if (earFile != null) {
201                 try {
202                     earFile.close();
203                 } catch (IOException JavaDoc ioe) {
204                     //We can't close the file
205
logger.log(BasicLevel.WARN, "Cannot close InputStream for '" + earFileName + "'");
206                 }
207             }
208             throw new EarDeploymentDescException("Cannot read the XML deployment descriptors of the ear file '"
209                     + earFileName + "'.", e);
210         }
211
212         application = loadApplication(new InputStreamReader JavaDoc(appInputStream), APPLICATION_FILE_NAME);
213         try {
214             appInputStream.close();
215         } catch (IOException JavaDoc e) {
216             // Can't close the file
217
logger.log(BasicLevel.WARN, "Cannot close InputStream for META-INF/application.xml in '" + earFileName
218                     + "'");
219         }
220
221         // load jonas-client deployment descriptor data
222
// (META-INF/jonas-client.xml)
223
if (jonasApplicationInputStream != null) {
224             jonasApplication = loadJonasApplication(new InputStreamReader JavaDoc(jonasApplicationInputStream), JONAS_APPLICATION_FILE_NAME);
225             try {
226                 jonasApplicationInputStream.close();
227             } catch (IOException JavaDoc e) {
228                 //We can't close the file
229
logger.log(BasicLevel.WARN, "Cannot close InputStream for '" + earFileName + "'");
230             }
231         } else {
232             jonasApplication = new JonasApplication();
233         }
234
235
236         // instantiate deployment descriptor
237
EarDeploymentDesc earDD = new EarDeploymentDesc(classLoaderForCls, application, jonasApplication);
238         earDD.setXmlContent(xmlContent);
239         earDD.setJonasXmlContent(jonasXmlContent);
240         return earDD;
241     }
242
243     /**
244      * Load the application.xml file.
245      * @param reader the Reader of the XML file.
246      * @param fileName the name of the file (application.xml).
247      * @throws EarDeploymentDescException if the deployment descriptor is
248      * corrupted.
249      * @return an application object.
250      */

251     public static Application loadApplication(Reader JavaDoc reader, String JavaDoc fileName) throws EarDeploymentDescException {
252
253         Application app = new Application();
254         // Create if earDigester is null
255
if (earDigester == null) {
256             try {
257                 // Create and initialize the digester
258
earDigester = new JDigester(appRuleSet, getParsingWithValidation(), true, new EarDTDs(),
259                         new EarSchemas());
260             } catch (DeploymentDescException e) {
261                 throw new EarDeploymentDescException(e);
262             }
263         }
264
265         try {
266             earDigester.parse(reader, fileName, app);
267         } catch (DeploymentDescException e) {
268             throw new EarDeploymentDescException(e);
269         } finally {
270             earDigester.push(null);
271         }
272
273         return app;
274     }
275
276     /**
277      * Load the jonas-application.xml file.
278      * @param reader the stream of the XML file.
279      * @param fileName the name of the file (jonas-application.xml).
280      * @return a structure containing the result of the jonas-application.xml
281      * parsing.
282      * @throws EarDeploymentDescException if the deployment descriptor is
283      * corrupted.
284      */

285     public static JonasApplication loadJonasApplication(Reader JavaDoc reader, String JavaDoc fileName) throws EarDeploymentDescException {
286
287         JonasApplication ja = new JonasApplication();
288
289         // Create if null
290
if (jonasEarDigester == null) {
291             try {
292             jonasEarDigester = new JDigester(jonasApplicationRuleSet, getParsingWithValidation(), true,
293                     null, new JonasEarSchemas());
294             } catch (DeploymentDescException e) {
295                 throw new EarDeploymentDescException(e);
296             }
297         }
298
299         try {
300             jonasEarDigester.parse(reader, fileName, ja);
301
302         } catch (DeploymentDescException e) {
303             throw new EarDeploymentDescException(e);
304         } finally {
305             jonasEarDigester.push(null);
306         }
307         return ja;
308     }
309
310     /**
311      * Controls whether the parser is reporting all validity errors.
312      * @return if true, all external entities will be read.
313      */

314     public static boolean getParsingWithValidation() {
315         return parsingWithValidation;
316     }
317
318     /**
319      * Controls whether the parser is reporting all validity errors.
320      * @param validation if true, all external entities will be read.
321      */

322     public static void setParsingWithValidation(boolean validation) {
323         EarDeploymentDescManager.parsingWithValidation = validation;
324     }
325 }
Popular Tags