1 16 package org.apache.commons.jxpath; 17 18 import java.io.BufferedReader ; 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.util.Properties ; 24 25 41 public abstract class JXPathContextFactory { 42 43 44 public static final String FACTORY_NAME_PROPERTY = 45 "org.apache.commons.jxpath.JXPathContextFactory"; 46 47 48 private static final String DEFAULT_FACTORY_CLASS = 49 "org.apache.commons.jxpath.ri.JXPathContextFactoryReferenceImpl"; 50 51 55 private static String factoryImplName = null; 56 57 protected JXPathContextFactory () { 58 59 } 60 61 96 public static JXPathContextFactory newInstance() { 97 if (factoryImplName == null) { 98 factoryImplName = 99 findFactory(FACTORY_NAME_PROPERTY, DEFAULT_FACTORY_CLASS); 100 } 101 102 JXPathContextFactory factoryImpl; 103 try { 104 Class clazz = Class.forName(factoryImplName); 105 factoryImpl = (JXPathContextFactory) clazz.newInstance(); 106 } 107 catch (ClassNotFoundException cnfe) { 108 throw new JXPathContextFactoryConfigurationError(cnfe); 109 } 110 catch (IllegalAccessException iae) { 111 throw new JXPathContextFactoryConfigurationError(iae); 112 } 113 catch (InstantiationException ie) { 114 throw new JXPathContextFactoryConfigurationError(ie); 115 } 116 return factoryImpl; 117 } 118 119 126 127 public abstract JXPathContext newContext( 128 JXPathContext parentContext, 129 Object contextBean) 130 throws JXPathContextFactoryConfigurationError; 131 132 137 139 private static boolean debug = false; 140 static { 141 try { 142 debug = System.getProperty("jxpath.debug") != null; 143 } 144 catch (SecurityException se) { 145 } 147 } 148 149 156 private static String findFactory(String property, String defaultFactory) { 157 try { 159 String systemProp = System.getProperty(property); 160 if (systemProp != null) { 161 if (debug) { 162 System.err.println( 163 "JXPath: found system property" + systemProp); 164 } 165 return systemProp; 166 } 167 168 } 169 catch (SecurityException se) { 170 } 172 173 try { 175 String javah = System.getProperty("java.home"); 176 String configFile = 177 javah 178 + File.separator 179 + "lib" 180 + File.separator 181 + "jxpath.properties"; 182 File f = new File (configFile); 183 if (f.exists()) { 184 Properties props = new Properties (); 185 props.load(new FileInputStream (f)); 186 String factory = props.getProperty(property); 187 if (factory != null) { 188 if (debug) { 189 System.err.println( 190 "JXPath: found java.home property " + factory); 191 } 192 return factory; 193 } 194 } 195 } 196 catch (Exception ex) { 197 if (debug) { 198 ex.printStackTrace(); 199 } 200 } 201 202 String serviceId = "META-INF/services/" + property; 203 try { 205 ClassLoader cl = JXPathContextFactory.class.getClassLoader(); 206 InputStream is = null; 207 if (cl == null) { 208 is = ClassLoader.getSystemResourceAsStream(serviceId); 209 } 210 else { 211 is = cl.getResourceAsStream(serviceId); 212 } 213 214 if (is != null) { 215 if (debug) { 216 System.err.println("JXPath: found " + serviceId); 217 } 218 BufferedReader rd = 219 new BufferedReader (new InputStreamReader (is)); 220 221 String factory = rd.readLine(); 222 rd.close(); 223 224 if (factory != null && !"".equals(factory)) { 225 if (debug) { 226 System.err.println( 227 "JXPath: loaded from services: " + factory); 228 } 229 return factory; 230 } 231 } 232 } 233 catch (Exception ex) { 234 if (debug) { 235 ex.printStackTrace(); 236 } 237 } 238 239 return defaultFactory; 240 } 241 } | Popular Tags |