MappingFileManager.java

00001 
00027 package org.objectweb.jonas_ws.deployment.lib;
00028 
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.IOException;
00032 import java.io.InputStream;
00033 import java.io.InputStreamReader;
00034 import java.io.Reader;
00035 import java.util.jar.JarFile;
00036 
00037 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00038 import org.objectweb.jonas_lib.deployment.digester.JDigester;
00039 
00040 import org.objectweb.jonas_ws.deployment.api.MappingFile;
00041 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDescException;
00042 import org.objectweb.jonas_ws.deployment.rules.JavaWsdlMappingRuleSet;
00043 import org.objectweb.jonas_ws.deployment.xml.JavaWsdlMapping;
00044 
00045 import org.objectweb.jonas.common.Log;
00046 
00047 import org.objectweb.util.monolog.api.BasicLevel;
00048 import org.objectweb.util.monolog.api.Logger;
00049 
00050 
00062 public class MappingFileManager {
00063 
00067     private static JDigester mfDigester = null;
00068 
00072     private static JavaWsdlMappingRuleSet mfRuleSet = new JavaWsdlMappingRuleSet();
00073 
00077     private static boolean parsingWithValidation = true;
00078 
00082     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
00083 
00087     private MappingFileManager() { }
00088 
00099     public static MappingFile getInstance(File module, String filename)
00100         throws WSDeploymentDescException {
00101         InputStream is = openStream(module, filename);
00102         return new MappingFile(loadMappingFile(new InputStreamReader(is), filename));
00103     }
00104 
00116     public static MappingFile getInstance(Reader r, String filename, boolean validate)
00117         throws WSDeploymentDescException {
00118         parsingWithValidation = validate;
00119         return new MappingFile(loadMappingFile(r, filename));
00120     }
00131     public static MappingFile getInstance(InputStream is, String filename)
00132         throws WSDeploymentDescException {
00133         return new MappingFile(loadMappingFile(new InputStreamReader(is), filename));
00134     }
00135 
00136 
00147     private static InputStream openStream(File module, String filename)
00148         throws WSDeploymentDescException {
00149 
00150         InputStream isMapping = null;
00151 
00152         // If we have file stored in Jar
00153         if (module.exists()) {
00154             // If we have file stored in Jar
00155             if (module.isFile()) {
00156                 JarFile jf = null;
00157 
00158                 // Get the Mapping file of the jar as InputStream
00159                 try {
00160                     jf = new JarFile(module.getAbsolutePath());
00161                     isMapping = jf.getInputStream(jf.getEntry(filename));
00162                 } catch (Exception e) {
00163                     if (jf != null) {
00164                         try {
00165                             jf.close();
00166                         } catch (IOException i) {
00167                             logger.log(BasicLevel.WARN, "Can't close '" + module + "'");
00168                         }
00169                     }
00170 
00171                     throw new WSDeploymentDescException("Cannot read the jaxrpc-mapping-file "
00172                                                         + filename + " in " +  module.getAbsolutePath(),
00173                                                         e);
00174                 }
00175             } else {
00176                 // filename is a Directory
00177                 try {
00178                     isMapping =
00179                         new FileInputStream(new File(module, filename));
00180                 } catch (IOException ioe) {
00181                     throw new WSDeploymentDescException("Cannot read the jaxrpc-mapping-file "
00182                                                         + filename + " in " + module.getAbsolutePath(),
00183                                                         ioe);
00184                 }
00185             }
00186 
00187         } else {
00188             // try laoding from ClassLoader
00189             ClassLoader loader = Thread.currentThread().getContextClassLoader();
00190             isMapping = loader.getResourceAsStream(filename);
00191         }
00192 
00193         return isMapping;
00194     }
00195 
00206     private static JavaWsdlMapping loadMappingFile(Reader reader, String fileName)
00207         throws WSDeploymentDescException {
00208 
00209         JavaWsdlMapping jwm = new JavaWsdlMapping();
00210 
00211         // Create if mfDigester is null
00212         if (mfDigester == null) {
00213             try {
00214                 // Create and initialize the digester
00215                 mfDigester = new JDigester(mfRuleSet,
00216                                            parsingWithValidation,
00217                                            true,
00218                                            null,
00219                                            new JaxrpcMappingSchemas());
00220             } catch (DeploymentDescException  e) {
00221                 throw new WSDeploymentDescException(e);
00222             }
00223         }
00224 
00225         try {
00226             mfDigester.parse(reader, fileName, jwm);
00227         } catch (DeploymentDescException  e) {
00228             throw new WSDeploymentDescException(e);
00229         } finally {
00230             mfDigester.push(null);
00231         }
00232 
00233         return jwm;
00234     }
00235 
00239     public static boolean isParsingWithValidation() {
00240         return parsingWithValidation;
00241     }
00242 
00246     public static void setParsingWithValidation(boolean parsingWithValidation) {
00247         MappingFileManager.parsingWithValidation = parsingWithValidation;
00248     }
00249 }

Generated on Tue Feb 15 15:05:59 2005 for JOnAS by  doxygen 1.3.9.1