001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial Developer : Delplanque Xavier & Sauthier Guillaume
022: * --------------------------------------------------------------------------
023: * $Id: MappingFileManager.java 7467 2005-10-04 12:53:14Z sauthieg $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_ws.deployment.lib;
026:
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.InputStreamReader;
032: import java.io.Reader;
033: import java.util.jar.JarFile;
034:
035: import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
036: import org.objectweb.jonas_lib.deployment.digester.JDigester;
037:
038: import org.objectweb.jonas_ws.deployment.api.JaxrpcMappingSchemas;
039: import org.objectweb.jonas_ws.deployment.api.MappingFile;
040: import org.objectweb.jonas_ws.deployment.api.WSDeploymentDescException;
041: import org.objectweb.jonas_ws.deployment.rules.JavaWsdlMappingRuleSet;
042: import org.objectweb.jonas_ws.deployment.xml.JavaWsdlMapping;
043:
044: import org.objectweb.jonas.common.Log;
045:
046: import org.objectweb.util.monolog.api.BasicLevel;
047: import org.objectweb.util.monolog.api.Logger;
048:
049: /**
050: * this Class is used to manipulate jaxrpc-mapping-file. This file contains
051: * informations for mapping between XML namespaces and java packages. We
052: * actually support just a few part of this file. According with JSR 921, this
053: * file must contain class mapping information (exceptions/faults,
054: * types/classes, portTypes/interfaces ...).
055: *
056: * @author Guillaume Sauthier
057: * @author Xavier Delplanque
058: * @author Helene Joanin
059: */
060: public class MappingFileManager {
061:
062: /**
063: * Digester used to parse jaxrpc_mapping.xml
064: */
065: private static JDigester mfDigester = null;
066:
067: /**
068: * Rules to parse the jaxrpc_mapping.xml
069: */
070: private static JavaWsdlMappingRuleSet mfRuleSet = new JavaWsdlMappingRuleSet();
071:
072: /**
073: * Flag for parser validation
074: */
075: private static boolean parsingWithValidation = true;
076:
077: /**
078: * logger
079: */
080: private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
081:
082: /**
083: * Private empty constructor for Utility class.
084: */
085: private MappingFileManager() {
086: }
087:
088: /**
089: * Returns an instance of jaxrpc-mapping-file
090: *
091: * @param module module File containing mapping
092: * @param filename jaxrpc-mapping-file filename
093: *
094: * @return Returns an instance of jaxrpc-mapping-file
095: *
096: * @throws WSDeploymentDescException when MappingFile cannot be loader or instanciated
097: */
098: public static MappingFile getInstance(File module, String filename)
099: throws WSDeploymentDescException {
100: InputStream is = openStream(module, filename);
101: return new MappingFile(loadMappingFile(
102: new InputStreamReader(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 r, String filename,
117: boolean validate) throws WSDeploymentDescException {
118: parsingWithValidation = validate;
119: return new MappingFile(loadMappingFile(r, filename));
120: }
121:
122: /**
123: * Returns an instance of jaxrpc-mapping-file
124: *
125: * @param is jaxrpc-mapping-file InputStream
126: * @param filename jaxrpc-mapping-file filename
127: *
128: * @return Returns an instance of jaxrpc-mapping-file
129: *
130: * @throws WSDeploymentDescException when MappingFile cannot be loader or instanciated
131: */
132: public static MappingFile getInstance(InputStream is,
133: String filename) throws WSDeploymentDescException {
134: return new MappingFile(loadMappingFile(
135: new InputStreamReader(is), filename));
136: }
137:
138: /**
139: * Open the InputStream of the given jaxrpc filename inside module.
140: *
141: * @param module module filename containing jaxrpc mapping file
142: * @param filename jaxrpc mappinf file filename
143: *
144: * @return Returns the InputStream of the mapping file
145: *
146: * @throws WSDeploymentDescException When InputStream cannot be open (not found, ioexceptions)
147: */
148: private static InputStream openStream(File module, String filename)
149: throws WSDeploymentDescException {
150:
151: InputStream isMapping = null;
152:
153: // If we have file stored in Jar
154: if (module.exists()) {
155: // If we have file stored in Jar
156: if (module.isFile()) {
157: JarFile jf = null;
158:
159: // Get the Mapping file of the jar as InputStream
160: try {
161: jf = new JarFile(module.getAbsolutePath());
162: isMapping = jf
163: .getInputStream(jf.getEntry(filename));
164: } catch (Exception e) {
165: if (jf != null) {
166: try {
167: jf.close();
168: } catch (IOException i) {
169: logger.log(BasicLevel.WARN, "Can't close '"
170: + module + "'");
171: }
172: }
173:
174: throw new WSDeploymentDescException(
175: "Cannot read the jaxrpc-mapping-file "
176: + filename + " in "
177: + module.getAbsolutePath(), e);
178: }
179: } else {
180: // filename is a Directory
181: try {
182: isMapping = new FileInputStream(new File(module,
183: filename));
184: } catch (IOException ioe) {
185: throw new WSDeploymentDescException(
186: "Cannot read the jaxrpc-mapping-file "
187: + filename + " in "
188: + module.getAbsolutePath(), ioe);
189: }
190: }
191:
192: } else {
193: // try laoding from ClassLoader
194: ClassLoader loader = Thread.currentThread()
195: .getContextClassLoader();
196: isMapping = loader.getResourceAsStream(filename);
197: }
198:
199: return isMapping;
200: }
201:
202: /**
203: * Load the MappingFile file
204: *
205: * @param reader The mapping file InputStream
206: * @param fileName mapping filename
207: *
208: * @return Zeus Root Element of the xml file.
209: *
210: * @throws WSDeploymentDescException When parsing fails.
211: */
212: private static JavaWsdlMapping loadMappingFile(Reader reader,
213: String fileName) throws WSDeploymentDescException {
214:
215: JavaWsdlMapping jwm = new JavaWsdlMapping();
216:
217: // Create if mfDigester is null
218: if (mfDigester == null) {
219: try {
220: // Create and initialize the digester
221: mfDigester = new JDigester(mfRuleSet,
222: parsingWithValidation, true, null,
223: new JaxrpcMappingSchemas());
224: } catch (DeploymentDescException e) {
225: throw new WSDeploymentDescException(e);
226: }
227: }
228:
229: try {
230: mfDigester.parse(reader, fileName, jwm);
231: } catch (DeploymentDescException e) {
232: throw new WSDeploymentDescException(e);
233: } finally {
234: mfDigester.push(null);
235: }
236:
237: return jwm;
238: }
239:
240: /**
241: * @return Returns the parsingWithValidation.
242: */
243: public static boolean isParsingWithValidation() {
244: return parsingWithValidation;
245: }
246:
247: /**
248: * @param parsingWithValidation The parsingWithValidation to set.
249: */
250: public static void setParsingWithValidation(
251: boolean parsingWithValidation) {
252: MappingFileManager.parsingWithValidation = parsingWithValidation;
253: }
254: }
|