WSDLHandlerFactory.java

00001 
00027 package org.objectweb.jonas.ws.handler;
00028 
00029 import java.util.Properties;
00030 
00031 import java.lang.reflect.Constructor;
00032 
00033 import java.io.InputStream;
00034 import java.io.IOException;
00035 import java.io.File;
00036 import java.io.FileInputStream;
00037 
00038 import org.objectweb.jonas.ws.WSServiceException;
00039 
00040 import org.objectweb.util.monolog.api.BasicLevel;
00041 import org.objectweb.util.monolog.api.Logger;
00042 
00043 import org.objectweb.jonas.common.JProp;
00044 import org.objectweb.jonas.common.Log;
00045 import org.objectweb.jonas_lib.I18n;
00046 
00055 public class WSDLHandlerFactory {
00056 
00058     public static final String CLASSNAME = "jonas.service.wsdl.class";
00059 
00063     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
00064 
00068     private static I18n i18n = I18n.getInstance(WSDLHandlerFactory.class);
00069 
00073     private WSDLHandlerFactory() { }
00074 
00078     public static WSDLHandlerFactory newInstance() {
00079         return new WSDLHandlerFactory();
00080     }
00081 
00092     public WSDLHandler newHandler(String filename)
00093         throws WSServiceException {
00094 
00095         Properties props = loadProperties(filename + ".properties");
00096 
00097         String classname = props.getProperty(CLASSNAME);
00098 
00099         if (classname == null) {
00100             // i18n WSDLHandlerFactory.newHandler.noClassname
00101             String err = i18n.getMessage("WSDLHandlerFactory.newHandler.noClassname",
00102                                          filename + ".properties");
00103             logger.log(BasicLevel.ERROR, err);
00104             throw new WSServiceException(err);
00105         }
00106 
00107         Object[] pvalues = new Object[] {props};
00108         Class[] types = new Class[] {Properties.class};
00109 
00110         // Instanciation
00111         WSDLHandler handler = null;
00112         try {
00113             Class handlerClazz = Class.forName(classname);
00114             Constructor handlerConstr = handlerClazz.getConstructor(types);
00115             handler = (WSDLHandler) handlerConstr.newInstance(pvalues);
00116         } catch (Exception e) {
00117             // i18n WSDLHandlerFactory.newHandler.instantiationFailure
00118             String err = i18n.getMessage("WSDLHandlerFactory.newHandler.instantiationFailure",
00119                                          filename,
00120                                          classname);
00121             logger.log(BasicLevel.ERROR, err);
00122             throw new WSServiceException(err, e);
00123         }
00124 
00125         return handler;
00126     }
00127 
00139     private Properties loadProperties(String filename)
00140         throws WSServiceException {
00141 
00142         Properties props = new Properties();
00143 
00144         InputStream is = getFromClassLoader(filename);
00145 
00146         if (is == null) {
00147             // load from ${jonas.base}/conf
00148             is = getFromFile(JProp.getJonasBase() + File.separator + "conf" + File.separator  + filename);
00149         }
00150 
00151         if (is != null) {
00152             try {
00153                 props.load(is);
00154             } catch (IOException ioe) {
00155             // !!! i18n WSDLHandlerFactory.loadProperties.ioe
00156             String err = i18n.getMessage("WSDLHandlerFactory.loadProperties.ioe",
00157                                          filename);
00158             logger.log(BasicLevel.ERROR, err);
00159             throw new WSServiceException(err);
00160             }
00161         } else {
00162             // !!! i18n WSDLHandlerFactory.loadProperties.notFound
00163             String err = i18n.getMessage("WSDLHandlerFactory.loadProperties.notFound",
00164                                          filename);
00165             logger.log(BasicLevel.ERROR, err);
00166             throw new WSServiceException(err);
00167         }
00168 
00169         return props;
00170     }
00171 
00179     private InputStream getFromClassLoader(String filename) {
00180 
00181         String clProps = filename;
00182 
00183         // if filename do not start with a "/" prepend it.
00184         // otherwise, getResourceAsStream will attemps to load it
00185         // from directory of the current package.
00186         if (!clProps.startsWith("/")) {
00187             clProps = "/" + clProps;
00188         }
00189 
00190         return getClass().getResourceAsStream(clProps);
00191     }
00192 
00200     private InputStream getFromFile(String filename) {
00201 
00202         InputStream is = null;
00203 
00204         // Create a file
00205         File file = new File(filename);
00206         try {
00207             is = new FileInputStream(file);
00208         } catch (IOException ioe) {
00209             logger.log(BasicLevel.WARN, "Cannot load " + filename + " from file system.");
00210             is = null;
00211         }
00212 
00213         return is;
00214     }
00215 
00216 }

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