1 26 27 package org.objectweb.jonas.ws.handler; 28 29 import java.util.Properties ; 30 31 import java.lang.reflect.Constructor ; 32 33 import java.io.InputStream ; 34 import java.io.IOException ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 38 import org.objectweb.jonas.ws.WSServiceException; 39 40 import org.objectweb.util.monolog.api.BasicLevel; 41 import org.objectweb.util.monolog.api.Logger; 42 43 import org.objectweb.jonas.common.JProp; 44 import org.objectweb.jonas.common.Log; 45 import org.objectweb.jonas_lib.I18n; 46 47 55 public class WSDLHandlerFactory { 56 57 58 public static final String CLASSNAME = "jonas.service.wsdl.class"; 59 60 63 private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX); 64 65 68 private static I18n i18n = I18n.getInstance(WSDLHandlerFactory.class); 69 70 73 private WSDLHandlerFactory() { } 74 75 78 public static WSDLHandlerFactory newInstance() { 79 return new WSDLHandlerFactory(); 80 } 81 82 92 public WSDLHandler newHandler(String filename) 93 throws WSServiceException { 94 95 Properties props = loadProperties(filename + ".properties"); 96 97 String classname = props.getProperty(CLASSNAME); 98 99 if (classname == null) { 100 String err = i18n.getMessage("WSDLHandlerFactory.newHandler.noClassname", 102 filename + ".properties"); 103 logger.log(BasicLevel.ERROR, err); 104 throw new WSServiceException(err); 105 } 106 107 Object [] pvalues = new Object [] {props}; 108 Class [] types = new Class [] {Properties .class}; 109 110 WSDLHandler handler = null; 112 try { 113 Class handlerClazz = Class.forName(classname); 114 Constructor handlerConstr = handlerClazz.getConstructor(types); 115 handler = (WSDLHandler) handlerConstr.newInstance(pvalues); 116 } catch (Exception e) { 117 String err = i18n.getMessage("WSDLHandlerFactory.newHandler.instantiationFailure", 119 filename, 120 classname); 121 logger.log(BasicLevel.ERROR, err); 122 throw new WSServiceException(err, e); 123 } 124 125 return handler; 126 } 127 128 139 private Properties loadProperties(String filename) 140 throws WSServiceException { 141 142 Properties props = new Properties (); 143 144 InputStream is = getFromClassLoader(filename); 145 146 if (is == null) { 147 is = getFromFile(JProp.getJonasBase() + File.separator + "conf" + File.separator + filename); 149 } 150 151 if (is != null) { 152 try { 153 props.load(is); 154 } catch (IOException ioe) { 155 String err = i18n.getMessage("WSDLHandlerFactory.loadProperties.ioe", 157 filename); 158 logger.log(BasicLevel.ERROR, err); 159 throw new WSServiceException(err); 160 } 161 } else { 162 String err = i18n.getMessage("WSDLHandlerFactory.loadProperties.notFound", 164 filename); 165 logger.log(BasicLevel.ERROR, err); 166 throw new WSServiceException(err); 167 } 168 169 return props; 170 } 171 172 179 private InputStream getFromClassLoader(String filename) { 180 181 String clProps = filename; 182 183 if (!clProps.startsWith("/")) { 187 clProps = "/" + clProps; 188 } 189 190 return getClass().getResourceAsStream(clProps); 191 } 192 193 200 private InputStream getFromFile(String filename) { 201 202 InputStream is = null; 203 204 File file = new File (filename); 206 try { 207 is = new FileInputStream (file); 208 } catch (IOException ioe) { 209 logger.log(BasicLevel.WARN, "Cannot load " + filename + " from file system."); 210 is = null; 211 } 212 213 return is; 214 } 215 216 } 217 | Popular Tags |