1 package org.sapia.util.xml.idefix; 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 44 public class IdefixProcessorFactory { 45 49 50 public static final String PROCESSOR_CLASS_PROPERTY = "org.sapia.xml.IdefixProcessor"; 51 52 53 public static final String PROCESSOR_CLASS_FILENAME = "sapia.properties"; 54 55 56 public static final String PROCESSOR_CLASS_RESOURCE = "META-INF/services/" + 57 PROCESSOR_CLASS_PROPERTY; 58 59 60 public static final String DEFAULT_PROCESSOR_CLASS = "org.sapia.util.xml.idefix.DefaultProcessor"; 61 62 66 67 private Class _theProcessorClass; 68 69 73 76 protected IdefixProcessorFactory(Class aClass) { 77 _theProcessorClass = aClass; 78 } 79 80 84 89 public static IdefixProcessorFactory newFactory() { 90 Class aProcessorClass = retrieveClassFromSystemProperties(); 91 92 if (aProcessorClass == null) { 93 aProcessorClass = retrieveClassFromJavaHome(); 94 95 if (aProcessorClass == null) { 96 aProcessorClass = retrieveClassFromResource(); 97 98 if (aProcessorClass == null) { 99 aProcessorClass = loadClassFromName(DEFAULT_PROCESSOR_CLASS); 100 } 101 } 102 } 103 104 return new IdefixProcessorFactory(aProcessorClass); 105 } 106 107 115 private static Class retrieveClassFromSystemProperties() { 116 String aClassName = System.getProperty(PROCESSOR_CLASS_PROPERTY); 117 118 return loadClassFromName(aClassName); 119 } 120 121 129 private static Class retrieveClassFromJavaHome() { 130 StringBuffer aFileName = new StringBuffer (System.getProperty("java.home")); 131 aFileName.append(File.separator).append("lib").append(File.separator) 132 .append(PROCESSOR_CLASS_FILENAME); 133 134 File aFile = new File (aFileName.toString()); 135 136 if (aFile.exists()) { 137 FileInputStream anInput = null; 138 139 try { 140 Properties someProperties = new Properties (); 141 anInput = new FileInputStream (aFile); 142 someProperties.load(anInput); 143 144 String aClassName = someProperties.getProperty(PROCESSOR_CLASS_PROPERTY); 145 146 return loadClassFromName(aClassName); 147 } catch (IOException ioe) { 148 StringBuffer aBuffer = new StringBuffer (); 149 aBuffer.append( 150 "IdefixProcessorFactory: WARNING unable to read the file ").append(aFileName.toString()); 151 System.out.println(aBuffer.toString()); 152 153 return null; 154 } finally { 155 if (anInput != null) { 156 try { 157 anInput.close(); 158 } catch (IOException ioe) { 159 } 160 } 161 } 162 } else { 163 return null; 164 } 165 } 166 167 170 private static Class retrieveClassFromResource() { 171 InputStream anInput = null; 172 BufferedReader aReader = null; 173 174 try { 175 anInput = Thread.currentThread().getContextClassLoader() 176 .getResourceAsStream(PROCESSOR_CLASS_RESOURCE); 177 178 if (anInput != null) { 179 try { 180 aReader = new BufferedReader (new InputStreamReader (anInput, "UTF-8")); 181 } catch (UnsupportedEncodingException uee) { 182 aReader = new BufferedReader (new InputStreamReader (anInput)); 183 } 184 185 String aClassName = aReader.readLine(); 186 187 return loadClassFromName(aClassName); 188 } else { 189 return null; 190 } 191 } catch (IOException ioe) { 192 StringBuffer aBuffer = new StringBuffer (); 193 aBuffer.append( 194 "IdefixProcessorFactory: WARNING unable to read the resource ").append(PROCESSOR_CLASS_RESOURCE); 195 System.out.println(aBuffer.toString()); 196 197 return null; 198 } finally { 199 if (aReader != null) { 200 try { 201 aReader.close(); 202 } catch (IOException ioe) { 203 } 204 } 205 206 if (anInput != null) { 207 try { 208 anInput.close(); 209 } catch (IOException ioe) { 210 } 211 } 212 } 213 } 214 215 223 private static Class loadClassFromName(String aClassName) { 224 if ((aClassName != null) && (aClassName.length() > 0)) { 225 try { 226 Class aClass = Thread.currentThread().getContextClassLoader().loadClass(aClassName); 227 228 if (IdefixProcessorIF.class.isAssignableFrom(aClass)) { 229 return aClass; 230 } else { 231 StringBuffer aBuffer = new StringBuffer (); 232 aBuffer.append("IdefixProcessorFactory: WARNING found the class ") 233 .append(aClassName).append(" but it is not a IdefixProcessorIF"); 234 System.out.println(aBuffer.toString()); 235 236 return null; 237 } 238 } catch (ClassNotFoundException cnfe) { 239 StringBuffer aBuffer = new StringBuffer (); 240 aBuffer.append( 241 "IdefixProcessorFactory: WARNING unable to load the class name ") 242 .append(aClassName); 243 System.out.println(aBuffer.toString()); 244 245 return null; 246 } 247 } else { 248 return null; 249 } 250 } 251 252 256 263 public IdefixProcessorIF createProcessor( 264 SerializerFactoryIF aSerializerFactory, NamespaceFactoryIF aNamespaceFactory) { 265 try { 266 Constructor aConstructor = _theProcessorClass.getConstructor(new Class [] { 267 SerializerFactoryIF.class, NamespaceFactoryIF.class 268 }); 269 270 return (IdefixProcessorIF) aConstructor.newInstance(new Object [] { 271 aSerializerFactory, aNamespaceFactory 272 }); 273 } catch (Exception e) { 274 throw new CompositeRuntimeException("Unable to create an Idefix processor", 275 e); 276 } 277 } 278 } 279 | Popular Tags |