1 package org.sapia.util.xml.confix; 2 3 4 import org.sapia.util.CompositeRuntimeException; 7 8 import java.io.BufferedReader ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.InputStreamReader ; 16 import java.io.UnsupportedEncodingException ; 17 18 import java.lang.reflect.Constructor ; 19 20 import java.util.Properties ; 21 22 23 47 public class ConfixProcessorFactory { 48 52 53 public static final String PROCESSOR_CLASS_PROPERTY = "org.sapia.xml.ConfixProcessor"; 54 55 56 public static final String PROCESSOR_CLASS_FILENAME = "sapia.properties"; 57 58 59 public static final String PROCESSOR_CLASS_RESOURCE = "META-INF/services/" + 60 PROCESSOR_CLASS_PROPERTY; 61 62 63 public static final String DEFAULT_PROCESSOR_CLASS = "org.sapia.util.xml.confix.SAXProcessor"; 64 65 69 70 private Class _theProcessorClass; 71 72 76 79 protected ConfixProcessorFactory(Class aClass) { 80 _theProcessorClass = aClass; 81 } 82 83 87 92 public static ConfixProcessorFactory newFactory() { 93 Class aProcessorClass = retrieveClassFromSystemProperties(); 94 95 if (aProcessorClass == null) { 96 aProcessorClass = retrieveClassFromJavaHome(); 97 98 if (aProcessorClass == null) { 99 aProcessorClass = retrieveClassFromResource(); 100 101 if (aProcessorClass == null) { 102 aProcessorClass = loadClassFromName(DEFAULT_PROCESSOR_CLASS); 103 } 104 } 105 } 106 107 return new ConfixProcessorFactory(aProcessorClass); 108 } 109 110 118 private static Class retrieveClassFromSystemProperties() { 119 String aClassName = System.getProperty(PROCESSOR_CLASS_PROPERTY); 120 121 return loadClassFromName(aClassName); 122 } 123 124 132 private static Class retrieveClassFromJavaHome() { 133 StringBuffer aFileName = new StringBuffer (System.getProperty("java.home")); 134 aFileName.append(File.separator).append("lib").append(File.separator) 135 .append(PROCESSOR_CLASS_FILENAME); 136 137 File aFile = new File (aFileName.toString()); 138 139 if (aFile.exists()) { 140 FileInputStream anInput = null; 141 142 try { 143 Properties someProperties = new Properties (); 144 anInput = new FileInputStream (aFile); 145 someProperties.load(anInput); 146 147 String aClassName = someProperties.getProperty(PROCESSOR_CLASS_PROPERTY); 148 149 return loadClassFromName(aClassName); 150 } catch (IOException ioe) { 151 StringBuffer aBuffer = new StringBuffer (); 152 aBuffer.append( 153 "ConfixProcessorFactory: WARNING unable to read the file ").append(aFileName.toString()); 154 System.out.println(aBuffer.toString()); 155 156 return null; 157 } finally { 158 if (anInput != null) { 159 try { 160 anInput.close(); 161 } catch (IOException ioe) { 162 } 163 } 164 } 165 } else { 166 return null; 167 } 168 } 169 170 173 private static Class retrieveClassFromResource() { 174 InputStream anInput = null; 175 BufferedReader aReader = null; 176 177 try { 178 anInput = Thread.currentThread().getContextClassLoader() 179 .getResourceAsStream(PROCESSOR_CLASS_RESOURCE); 180 181 if (anInput != null) { 182 try { 183 aReader = new BufferedReader (new InputStreamReader (anInput, "UTF-8")); 184 } catch (UnsupportedEncodingException uee) { 185 aReader = new BufferedReader (new InputStreamReader (anInput)); 186 } 187 188 String aClassName = aReader.readLine(); 189 190 return loadClassFromName(aClassName); 191 } else { 192 return null; 193 } 194 } catch (IOException ioe) { 195 StringBuffer aBuffer = new StringBuffer (); 196 aBuffer.append( 197 "ConfixProcessorFactory: WARNING unable to read the resource ").append(PROCESSOR_CLASS_RESOURCE); 198 System.out.println(aBuffer.toString()); 199 200 return null; 201 } finally { 202 if (aReader != null) { 203 try { 204 aReader.close(); 205 } catch (IOException ioe) { 206 } 207 } 208 209 if (anInput != null) { 210 try { 211 anInput.close(); 212 } catch (IOException ioe) { 213 } 214 } 215 } 216 } 217 218 226 private static Class loadClassFromName(String aClassName) { 227 if ((aClassName != null) && (aClassName.length() > 0)) { 228 try { 229 Class aClass = Thread.currentThread().getContextClassLoader().loadClass(aClassName); 230 231 if (ConfixProcessorIF.class.isAssignableFrom(aClass)) { 232 return aClass; 233 } else { 234 StringBuffer aBuffer = new StringBuffer (); 235 aBuffer.append("ConfixProcessorFactory: WARNING found the class ") 236 .append(aClassName).append(" but it is not a ConfixProcessorIF"); 237 System.out.println(aBuffer.toString()); 238 239 return null; 240 } 241 } catch (ClassNotFoundException cnfe) { 242 StringBuffer aBuffer = new StringBuffer (); 243 aBuffer.append( 244 "ConfixProcessorFactory: WARNING unable to load the class name ") 245 .append(aClassName); 246 System.out.println(aBuffer.toString()); 247 248 return null; 249 } 250 } else { 251 return null; 252 } 253 } 254 255 259 264 public ConfixProcessorIF createProcessor(ObjectFactoryIF aFactory) { 265 try { 266 Constructor aConstructor = _theProcessorClass.getConstructor(new Class [] { 267 ObjectFactoryIF.class 268 }); 269 270 return (ConfixProcessorIF) aConstructor.newInstance(new Object [] { aFactory }); 271 } catch (Exception e) { 272 throw new CompositeRuntimeException("Unable to create a Confix processor", 273 e); 274 } 275 } 276 } 277 | Popular Tags |