KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > deployment > lib > MappingFileManager


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 : Delplanque Xavier & Sauthier Guillaume
22  * --------------------------------------------------------------------------
23  * $Id: MappingFileManager.java,v 1.1 2004/05/25 14:26:33 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_ws.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
37 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
38 import org.objectweb.jonas_lib.deployment.digester.JDigester;
39
40 import org.objectweb.jonas_ws.deployment.api.MappingFile;
41 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDescException;
42 import org.objectweb.jonas_ws.deployment.rules.JavaWsdlMappingRuleSet;
43 import org.objectweb.jonas_ws.deployment.xml.JavaWsdlMapping;
44
45 import org.objectweb.jonas.common.Log;
46
47 import org.objectweb.util.monolog.api.BasicLevel;
48 import org.objectweb.util.monolog.api.Logger;
49
50
51 /**
52  * this Class is used to manipulate jaxrpc-mapping-file. This file contains
53  * informations for mapping between XML namespaces and java packages. We
54  * actually support just a few part of this file. According with JSR 921, this
55  * file must contain class mapping information (exceptions/faults,
56  * types/classes, portTypes/interfaces ...).
57  *
58  * @author Guillaume Sauthier
59  * @author Xavier Delplanque
60  * @author Helene Joanin
61  */

62 public class MappingFileManager {
63
64     /**
65      * Digester used to parse jaxrpc_mapping.xml
66      */

67     private static JDigester mfDigester = null;
68
69     /**
70      * Rules to parse the jaxrpc_mapping.xml
71      */

72     private static JavaWsdlMappingRuleSet mfRuleSet = new JavaWsdlMappingRuleSet();
73
74     /**
75      * Flag for parser validation
76      */

77     private static boolean parsingWithValidation = true;
78
79     /**
80      * logger
81      */

82     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
83
84     /**
85      * Private empty constructor for Utility class.
86      */

87     private MappingFileManager() { }
88
89     /**
90      * Returns an instance of jaxrpc-mapping-file
91      *
92      * @param module module File containing mapping
93      * @param filename jaxrpc-mapping-file filename
94      *
95      * @return Returns an instance of jaxrpc-mapping-file
96      *
97      * @throws WSDeploymentDescException when MappingFile cannot be loader or instanciated
98      */

99     public static MappingFile getInstance(File JavaDoc module, String JavaDoc filename)
100         throws WSDeploymentDescException {
101         InputStream JavaDoc is = openStream(module, filename);
102         return new MappingFile(loadMappingFile(new InputStreamReader JavaDoc(is), filename));
103     }
104
105     /**
106      * Returns an instance of jaxrpc-mapping-file
107      *
108      * @param r mapping file reader
109      * @param filename mapping filename
110      * @param validate validate flag
111      *
112      * @return Returns an instance of jaxrpc-mapping-file
113      *
114      * @throws WSDeploymentDescException when MappingFile cannot be loader or instanciated
115      */

116     public static MappingFile getInstance(Reader JavaDoc r, String JavaDoc filename, boolean validate)
117         throws WSDeploymentDescException {
118         parsingWithValidation = validate;
119         return new MappingFile(loadMappingFile(r, filename));
120     }
121     /**
122      * Returns an instance of jaxrpc-mapping-file
123      *
124      * @param is jaxrpc-mapping-file InputStream
125      * @param filename jaxrpc-mapping-file filename
126      *
127      * @return Returns an instance of jaxrpc-mapping-file
128      *
129      * @throws WSDeploymentDescException when MappingFile cannot be loader or instanciated
130      */

131     public static MappingFile getInstance(InputStream JavaDoc is, String JavaDoc filename)
132         throws WSDeploymentDescException {
133         return new MappingFile(loadMappingFile(new InputStreamReader JavaDoc(is), filename));
134     }
135
136
137     /**
138      * Open the InputStream of the given jaxrpc filename inside module.
139      *
140      * @param module module filename containing jaxrpc mapping file
141      * @param filename jaxrpc mappinf file filename
142      *
143      * @return Returns the InputStream of the mapping file
144      *
145      * @throws WSDeploymentDescException When InputStream cannot be open (not found, ioexceptions)
146      */

147     private static InputStream JavaDoc openStream(File JavaDoc module, String JavaDoc filename)
148         throws WSDeploymentDescException {
149
150         InputStream JavaDoc isMapping = null;
151
152         // If we have file stored in Jar
153
if (module.exists()) {
154             // If we have file stored in Jar
155
if (module.isFile()) {
156                 JarFile JavaDoc jf = null;
157
158                 // Get the Mapping file of the jar as InputStream
159
try {
160                     jf = new JarFile JavaDoc(module.getAbsolutePath());
161                     isMapping = jf.getInputStream(jf.getEntry(filename));
162                 } catch (Exception JavaDoc e) {
163                     if (jf != null) {
164                         try {
165                             jf.close();
166                         } catch (IOException JavaDoc i) {
167                             logger.log(BasicLevel.WARN, "Can't close '" + module + "'");
168                         }
169                     }
170
171                     throw new WSDeploymentDescException("Cannot read the jaxrpc-mapping-file "
172                                                         + filename + " in " + module.getAbsolutePath(),
173                                                         e);
174                 }
175             } else {
176                 // filename is a Directory
177
try {
178                     isMapping =
179                         new FileInputStream JavaDoc(new File JavaDoc(module, filename));
180                 } catch (IOException JavaDoc ioe) {
181                     throw new WSDeploymentDescException("Cannot read the jaxrpc-mapping-file "
182                                                         + filename + " in " + module.getAbsolutePath(),
183                                                         ioe);
184                 }
185             }
186
187         } else {
188             // try laoding from ClassLoader
189
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
190             isMapping = loader.getResourceAsStream(filename);
191         }
192
193         return isMapping;
194     }
195
196     /**
197      * Load the MappingFile file
198      *
199      * @param reader The mapping file InputStream
200      * @param fileName mapping filename
201      *
202      * @return Zeus Root Element of the xml file.
203      *
204      * @throws WSDeploymentDescException When parsing fails.
205      */

206     private static JavaWsdlMapping loadMappingFile(Reader JavaDoc reader, String JavaDoc fileName)
207         throws WSDeploymentDescException {
208
209         JavaWsdlMapping jwm = new JavaWsdlMapping();
210
211         // Create if mfDigester is null
212
if (mfDigester == null) {
213             try {
214                 // Create and initialize the digester
215
mfDigester = new JDigester(mfRuleSet,
216                                            parsingWithValidation,
217                                            true,
218                                            null,
219                                            new JaxrpcMappingSchemas());
220             } catch (DeploymentDescException e) {
221                 throw new WSDeploymentDescException(e);
222             }
223         }
224
225         try {
226             mfDigester.parse(reader, fileName, jwm);
227         } catch (DeploymentDescException e) {
228             throw new WSDeploymentDescException(e);
229         } finally {
230             mfDigester.push(null);
231         }
232
233         return jwm;
234     }
235
236     /**
237      * @return Returns the parsingWithValidation.
238      */

239     public static boolean isParsingWithValidation() {
240         return parsingWithValidation;
241     }
242
243     /**
244      * @param parsingWithValidation The parsingWithValidation to set.
245      */

246     public static void setParsingWithValidation(boolean parsingWithValidation) {
247         MappingFileManager.parsingWithValidation = parsingWithValidation;
248     }
249 }
250
Popular Tags