KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_rar > deployment > lib > RarDeploymentDescManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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): Eric HARDESTY
22  * --------------------------------------------------------------------------
23  * $Id: RarDeploymentDescManager.java,v 1.4 2004/07/21 16:09:48 camillej Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_rar.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 javax.naming.Context JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40
41 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
42 import org.objectweb.jonas_lib.deployment.digester.JDigester;
43 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager;
44
45 import org.objectweb.jonas_rar.deployment.api.RarDeploymentDesc;
46 import org.objectweb.jonas_rar.deployment.api.RarDeploymentDescException;
47 import org.objectweb.jonas_rar.deployment.rules.ConnectorRuleSet;
48 import org.objectweb.jonas_rar.deployment.rules.JonasConnectorRuleSet;
49 import org.objectweb.jonas_rar.deployment.xml.Connector;
50 import org.objectweb.jonas_rar.deployment.xml.JonasConnector;
51
52 import org.objectweb.jonas.common.Log;
53 import org.objectweb.jonas.naming.CompNamingContext;
54
55 import org.objectweb.util.monolog.api.BasicLevel;
56 import org.objectweb.util.monolog.api.Logger;
57
58 /**
59  * Manage Rar deployment descriptors
60  * @author Guillaume Sauthier
61  */

62 public class RarDeploymentDescManager extends AbsDeploymentDescManager {
63
64     /**
65      * Path of the ra.xml deploymnet descriptor file
66      */

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

72     public static final String JavaDoc JONAS_RA_FILE_NAME = "META-INF/jonas-ra.xml";
73
74     /**
75      * Flag for parser validation
76      */

77     private static boolean parsingWithValidation = true;
78
79     /**
80      * Digester use to parse ra.xml
81      */

82     private static JDigester connectorDigester = null;
83
84     /**
85      * Digester use to parse jonas-ra.xml
86      */

87     private static JDigester jonasConnectorDigester = null;
88
89     /**
90      * Rules to parse the ra.xml
91      */

92     private static ConnectorRuleSet connectorRuleSet = new ConnectorRuleSet();
93
94     /**
95      * Rules to parse the jonas-ra.xml
96      */

97     private static JonasConnectorRuleSet jonasConnectorRuleSet = new JonasConnectorRuleSet();
98
99     /**
100      * logger TODO Get a Resource Logger
101      */

102     private static Logger logger = Log.getLogger(Log.JONAS_DBM_PREFIX);
103
104     /**
105      * Private empty constructor for Utility Class
106      */

107     private RarDeploymentDescManager() {
108     }
109
110     /**
111      * Get an instance of an RAR deployment descriptor by parsing the ra.xml and
112      * jonas-ra.xml deployment descriptors.
113      * @param rarFileName the fileName of the rar file for the deployment
114      * descriptors.
115      * @param classLoader the classloader for the classes.
116      * @return an RAR deployment descriptor by parsing the ra.xml & jonas-ra.xml
117      * deployment descriptors.
118      * @throws DeploymentDescException if the deployment descriptors are
119      * corrupted.
120      */

121     public static RarDeploymentDesc getInstance(String JavaDoc rarFileName, ClassLoader JavaDoc classLoader)
122             throws DeploymentDescException {
123
124         // The context to give for the creation of an instance
125
// of RarDeploymentDesc
126
Context JavaDoc contctx = null;
127         try {
128             contctx = new CompNamingContext(rarFileName);
129             contctx.rebind("rarFileName", rarFileName);
130             contctx.rebind("classloader", classLoader);
131         } catch (Exception JavaDoc e) {
132             // An exception occurred trying to bind context
133
// logger.log(BasicLevel.ERROR, "Error when binding context for
134
// RarDeploymentDesc for '" + rarFileName + "'");
135
// logger.log(BasicLevel.ERROR, e.getMessage());
136

137             throw new RarDeploymentDescException("Error during the deployment", e);
138         }
139         // want both ra.xml & jonas-ra.xml
140
return getInstance(contctx);
141     }
142
143     /**
144      * Get an instance of an RAR deployment descriptor by parsing the ra.xml and
145      * jonas-ra.xml deployment descriptors.
146      * @param ctx the context which contains the configuration in order to load
147      * the deployment descriptors. There are 6 possible parameters : -
148      * rarFileName is the path of the RAR file (required param). -
149      * classLoader is the classloader (required param) - altDD is the
150      * optional deployment descriptor (optional param).
151      * @return an RAR deployment descriptor by parsing the ra.xml(or altdd) &
152      * jonas-ra.xml deployment descriptors.
153      * @throws DeploymentDescException if the deployment descriptors are
154      * corrupted.
155      */

156     public static RarDeploymentDesc getInstance(Context JavaDoc ctx) throws DeploymentDescException {
157
158         // init xml contents values;
159
String JavaDoc xmlContent = "";
160         String JavaDoc jonasXmlContent = "";
161         boolean altDD = false;
162
163        String JavaDoc rarFileName = null;
164         ClassLoader JavaDoc classLoader = null;
165         try {
166             rarFileName = (String JavaDoc) ctx.lookup("rarFileName");
167             classLoader = (ClassLoader JavaDoc) ctx.lookup("classloader");
168         } catch (NamingException JavaDoc e) {
169             String JavaDoc err = "Error while getting parameter from context param ";
170             throw new RarDeploymentDescException(err, e);
171         }
172
173         // optional parameter
174
String JavaDoc raDeployDesc;
175         try {
176             raDeployDesc = (String JavaDoc) ctx.lookup("altDD");
177             altDD = true;
178         } catch (NamingException JavaDoc e) {
179             // no alt DD
180
raDeployDesc = "";
181         }
182
183         //rar file
184
JarFile JavaDoc rarFile = null;
185
186         //Input Stream
187
InputStream JavaDoc raInputStream = null;
188         InputStream JavaDoc jonasRaInputStream = null;
189
190         //ZipEntry
191
ZipEntry JavaDoc raZipEntry = null;
192         ZipEntry JavaDoc jonasRaZipEntry = null;
193
194         Connector connector = null;
195         JonasConnector jonasConnector = null;
196
197         //Build the file
198
File JavaDoc fRar = new File JavaDoc(rarFileName);
199
200         //Check if the file exists.
201
if (!(fRar.exists())) {
202             throw new RarDeploymentDescException("The file '" + rarFileName + "' was not found.");
203         }
204
205         // load deployment descriptor data (META-INF/ra.xml)
206
boolean setupRa = true;
207         boolean setupJonasRa = true;
208         try {
209             if (!altDD) {
210                 
211                 // If rar is a directory, there is no jar, just read the file from
212
// the directory
213
if (fRar.isDirectory()) {
214                     File JavaDoc rarXmlF = new File JavaDoc(rarFileName, RA_FILE_NAME);
215                     if (!rarXmlF.exists()) {
216                         connector = null;
217                         setupRa = false;
218                     } else {
219                         raInputStream = new FileInputStream JavaDoc(rarXmlF);
220                         xmlContent = xmlContent(raInputStream);
221                         raInputStream = new FileInputStream JavaDoc(rarXmlF);
222                     }
223                 } else {
224                     rarFile = new JarFile JavaDoc(rarFileName);
225
226                     //Check the ra entry
227
raZipEntry = rarFile.getEntry(RA_FILE_NAME);
228                     if (raZipEntry == null) {
229                         connector = null;
230                         setupRa = false;
231                     } else {
232                         //Get the stream
233
raInputStream = rarFile.getInputStream(raZipEntry);
234                         xmlContent = xmlContent(raInputStream);
235                         raInputStream = rarFile.getInputStream(raZipEntry);
236                     }
237                 }
238             } else {
239                 raInputStream = new FileInputStream JavaDoc(raDeployDesc);
240                 xmlContent = xmlContent(raInputStream);
241                 raInputStream = new FileInputStream JavaDoc(raDeployDesc);
242             }
243
244             if (fRar.isDirectory()) {
245                 //lookup a WEB-INF/jonas-web.xml file
246
File JavaDoc rarJXmlF = new File JavaDoc(rarFileName, JONAS_RA_FILE_NAME);
247                 if (rarJXmlF.exists()) {
248                     jonasRaInputStream = new FileInputStream JavaDoc(rarJXmlF);
249                     jonasXmlContent = xmlContent(jonasRaInputStream);
250                     jonasRaInputStream = new FileInputStream JavaDoc(rarJXmlF);
251                 }
252             } else {
253                 //Check the jonas-ra entry
254
rarFile = new JarFile JavaDoc(rarFileName);
255                 jonasRaZipEntry = rarFile.getEntry(JONAS_RA_FILE_NAME);
256                 if (jonasRaZipEntry == null) {
257                     jonasConnector = null;
258                     setupJonasRa = false;
259                 } else {
260                     //Get the stream
261
jonasRaInputStream = rarFile.getInputStream(jonasRaZipEntry);
262                     jonasXmlContent = xmlContent(jonasRaInputStream);
263                     jonasRaInputStream = rarFile.getInputStream(jonasRaZipEntry);
264                 }
265             }
266         } catch (Exception JavaDoc e) {
267             if (rarFile != null) {
268                 try {
269                     rarFile.close();
270                 } catch (IOException JavaDoc ioe) {
271                     // Can't close the file
272
logger.log(BasicLevel.WARN, "Can't close '" + rarFileName + "'");
273                 }
274             }
275             throw new RarDeploymentDescException("Cannot read the XML deployment descriptors of the rar file '"
276                     + rarFileName + "'.", e);
277         }
278
279         if (setupRa) {
280             connector = loadConnector(new InputStreamReader JavaDoc(raInputStream), raDeployDesc);
281             try {
282                 raInputStream.close();
283             } catch (IOException JavaDoc e) {
284                 // can't close
285
logger.log(BasicLevel.WARN, "Can't close META-INF/ra.xml of '" + rarFileName + "'");
286             }
287         }
288
289         if (setupJonasRa) {
290             jonasConnector = loadJonasConnector(new InputStreamReader JavaDoc(jonasRaInputStream), JONAS_RA_FILE_NAME);
291             try {
292                 jonasRaInputStream.close();
293             } catch (IOException JavaDoc e) {
294                 // can't close
295
logger.log(BasicLevel.WARN, "Can't close META-INF/jonas-ra.xml of '" + rarFileName + "'");
296             }
297         }
298
299         // instantiate deployment descriptor
300
RarDeploymentDesc rdd = new RarDeploymentDesc(classLoader, connector, jonasConnector);
301         rdd.setXmlContent(xmlContent);
302         rdd.setJOnASXmlContent(jonasXmlContent);
303         return rdd;
304     }
305
306     /**
307      * Load the ra.xml file.
308      * @param reader the Reader of the XML file.
309      * @param fileName the name of the file (ra.xml).
310      * @throws DeploymentDescException if the deployment descriptor is
311      * corrupted.
312      * @return a connector object.
313      */

314     public static Connector loadConnector(Reader JavaDoc reader, String JavaDoc fileName) throws DeploymentDescException {
315
316         Connector connector = new Connector();
317
318         // Create if null
319
if (connectorDigester == null) {
320             connectorDigester = new JDigester(connectorRuleSet, parsingWithValidation, true, new ConnectorDTDs(),
321                     new ConnectorSchemas());
322         }
323
324         try {
325             connectorDigester.parse(reader, fileName, connector);
326         } catch (DeploymentDescException e) {
327             throw e;
328         } finally {
329             connectorDigester.push(null);
330         }
331         return connector;
332     }
333
334     /**
335      * Load the jonas-ra.xml file.
336      * @param reader the Reader of the XML file.
337      * @param fileName the name of the file (jonas-ra.xml).
338      * @throws DeploymentDescException if the deployment descriptor is
339      * corrupted.
340      * @return a JonasConnector object.
341      */

342     public static JonasConnector loadJonasConnector(Reader JavaDoc reader, String JavaDoc fileName) throws DeploymentDescException {
343
344         JonasConnector jonasConnector = new JonasConnector();
345
346         // Create if null
347
if (jonasConnectorDigester == null) {
348             jonasConnectorDigester = new JDigester(jonasConnectorRuleSet, parsingWithValidation, true,
349                     new JonasConnectorDTDs(), new JonasConnectorSchemas());
350         }
351
352         try {
353             jonasConnectorDigester.parse(reader, fileName, jonasConnector);
354         } catch (DeploymentDescException e) {
355             throw e;
356         } finally {
357             jonasConnectorDigester.push(null);
358         }
359         return jonasConnector;
360     }
361
362     /**
363      * Controls whether the parser is reporting all validity errors.
364      * @return if true, all external entities will be read.
365      */

366     public static boolean getParsingWithValidation() {
367         return RarDeploymentDescManager.parsingWithValidation;
368     }
369
370      /**
371      * Controls whether the parser is reporting all validity errors.
372      * @param validation if true, all external entities will be read.
373      */

374     public static void setParsingWithValidation(boolean validation) {
375         RarDeploymentDescManager.parsingWithValidation = validation;
376     }
377
378 }
Popular Tags