1 19 package org.java.plugin; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.java.plugin.registry.PluginRegistry; 32 import org.java.plugin.standard.StandardObjectFactory; 33 import org.java.plugin.util.ExtendedProperties; 34 import org.java.plugin.util.IoUtil; 35 36 42 public abstract class ObjectFactory { 43 49 public static ObjectFactory newInstance() { 50 return newInstance(null); 51 } 52 53 76 public static ObjectFactory newInstance(final ExtendedProperties config) { 77 Log log = LogFactory.getLog(ObjectFactory.class); 78 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 79 if (cl == null) { 80 cl = ObjectFactory.class.getClassLoader(); 81 } 82 ExtendedProperties props; 83 if (config != null) { 84 props = config; 85 } else { 86 props = loadProperties(cl); 87 } 88 String className = findProperty(cl, props); 89 ObjectFactory result; 90 try { 91 if (className == null) { 92 className = "org.java.plugin.standard.StandardObjectFactory"; } 94 result = (ObjectFactory) loadClass(cl, className).newInstance(); 95 } catch (ClassNotFoundException cnfe) { 96 log.fatal("failed instantiating object factory " + className, cnfe); 98 throw new Error ("failed instantiating object factory " + className, cnfe); 100 } catch (IllegalAccessException iae) { 101 log.fatal("failed instantiating object factory " + className, iae); 103 throw new Error ("failed instantiating object factory " + className, iae); 105 } catch (SecurityException se) { 106 log.fatal("failed instantiating object factory " + className, se); 108 throw new Error ("failed instantiating object factory " + className, se); 110 } catch (InstantiationException ie) { 111 log.fatal("failed instantiating object factory " + className, ie); 113 throw new Error ("failed instantiating object factory " + className, ie); 115 } 116 result.configure(props); 117 log.debug("object factory instance created - " + result); return result; 119 } 120 121 private static Class loadClass(final ClassLoader cl, final String className) 122 throws ClassNotFoundException { 123 if (cl != null) { 124 try { 125 return cl.loadClass(className); 126 } catch (ClassNotFoundException cnfe) { 127 } 129 } 130 ClassLoader cl2 = ObjectFactory.class.getClassLoader(); 131 if (cl2 != null) { 132 try { 133 return cl2.loadClass(className); 134 } catch (ClassNotFoundException cnfe) { 135 } 137 } 138 return ClassLoader.getSystemClassLoader().loadClass(className); 139 } 140 141 private static ExtendedProperties loadProperties(final ClassLoader cl) { 142 Log log = LogFactory.getLog(ObjectFactory.class); 143 File file = new File (System.getProperty("java.home") + File.separator + "lib" + File.separator + "jpf.properties"); URL url = null; 147 if (file.canRead()) { 148 try { 149 url = IoUtil.file2url(file); 150 } catch (MalformedURLException mue) { 151 log.error("failed converting file " + file + " to URL", mue); } 154 } 155 if (url == null) { 156 if (cl != null) { 157 url = cl.getResource("jpf.properties"); if (url == null) { 159 url = ClassLoader.getSystemResource("jpf.properties"); } 161 } else { 162 url = ClassLoader.getSystemResource("jpf.properties"); } 164 if (url == null) { 165 log.debug("no jpf.properties file found in ${java.home}/lib (" + file 167 + ") nor in CLASSPATH, using standard properties"); url = StandardObjectFactory.class.getResource("jpf.properties"); } 170 } 171 try { 172 InputStream strm = IoUtil.getResourceInputStream(url); 173 try { 174 ExtendedProperties props = new ExtendedProperties(); 175 props.load(strm); 176 log.debug("loaded jpf.properties from " + url); return props; 178 } finally { 179 try { 180 strm.close(); 181 } catch (IOException ioe) { 182 } 184 } 185 } catch (Exception e) { 186 log.error("failed loading jpf.properties from CLASSPATH", e); 188 } 189 return null; 190 } 191 192 private static String findProperty(final ClassLoader cl, 193 final ExtendedProperties props) { 194 Log log = LogFactory.getLog(ObjectFactory.class); 195 String name = ObjectFactory.class.getName(); 196 String result = System.getProperty(name); 197 if (result != null) { 198 log.debug("property " + name + " found as system property"); return result; 201 } 202 if (props != null) { 203 result = props.getProperty(name); 204 if (result != null) { 205 log.debug("property " + name + " found in properties file"); return result; 208 } 209 } 210 String serviceId = "META-INF/services/" + ObjectFactory.class.getName(); 212 InputStream strm; 213 if (cl == null) { 214 strm = ClassLoader.getSystemResourceAsStream(serviceId); 215 } else { 216 strm = cl.getResourceAsStream(serviceId); 217 } 218 if (strm != null) { 219 try { 220 BufferedReader reader = new BufferedReader ( 221 new InputStreamReader (strm, "UTF-8")); try { 223 result = reader.readLine(); 224 } finally { 225 try { 226 reader.close(); 227 } catch (IOException ioe) { 228 } 230 } 231 } catch (IOException ioe) { 232 try { 233 strm.close(); 234 } catch (IOException ioe2) { 235 } 237 } 238 } 239 if (result != null) { 240 log.debug("property " + name + " found as service"); return result; 243 } 244 log.debug("no property " + name + " found"); return result; 247 } 248 249 254 protected abstract void configure(ExtendedProperties config); 255 256 264 public final PluginManager createManager() { 265 return createManager(createRegistry(), createPathResolver()); 266 } 267 268 274 public abstract PluginManager createManager(PluginRegistry registry, 275 PathResolver pathResolver); 276 277 283 public abstract PluginRegistry createRegistry(); 284 285 291 public abstract PathResolver createPathResolver(); 292 } 293 | Popular Tags |