KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > idefix > IdefixProcessorFactory


1 package org.sapia.util.xml.idefix;
2
3
4 // Import of Sapia's utility classes
5
// ---------------------------------
6
import org.sapia.util.CompositeRuntimeException;
7
8 // Import of Sun's JDK classes
9
// ---------------------------
10
import java.io.BufferedReader JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17
18 import java.lang.reflect.Constructor JavaDoc;
19
20 import java.util.Properties JavaDoc;
21
22
23 /**
24  * A factory that instantiates <code>IdefixProcessorIF</code> instances. The
25  * class of the Idefix processor to use must be specified through the following
26  * mechanism:
27  * <p>
28  *
29  * <ol>
30  * <li>The value of the property <code>org.sapia.xml.IdefixProcessor</code> in the system properties.
31  * <li>The value of the property <code>org.sapia.xml.IdefixProcessor</code> in a "sapia.properties" file in $JAVA_HOME/lib.
32  * <li>In a "META-INF/service/org.sapia.xml.IdefixProcessor" file that is loaded as a resource.
33  * </ol>
34  *
35  * If none of the methods above worked, the factory return a <code>ReflectionProcessor</code>.
36  *
37  * @author Jean-Cedric Desrochers
38  * <dl>
39  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
40  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
41  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
42  * </dl>
43  */

44 public class IdefixProcessorFactory {
45   /////////////////////////////////////////////////////////////////////////////////////////
46
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
47
/////////////////////////////////////////////////////////////////////////////////////////
48

49   /** Defines the system property that defines the processor class name. */
50   public static final String JavaDoc PROCESSOR_CLASS_PROPERTY = "org.sapia.xml.IdefixProcessor";
51
52   /** Defines the properties filename that contains the processor class name. */
53   public static final String JavaDoc PROCESSOR_CLASS_FILENAME = "sapia.properties";
54
55   /** Defines the resource name that contains the processor class name. */
56   public static final String JavaDoc PROCESSOR_CLASS_RESOURCE = "META-INF/services/" +
57     PROCESSOR_CLASS_PROPERTY;
58
59   /** Defines the default Idefix processor class. */
60   public static final String JavaDoc DEFAULT_PROCESSOR_CLASS = "org.sapia.util.xml.idefix.DefaultProcessor";
61
62   /////////////////////////////////////////////////////////////////////////////////////////
63
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
64
/////////////////////////////////////////////////////////////////////////////////////////
65

66   /** The class of the idefix processor of this factory. */
67   private Class JavaDoc _theProcessorClass;
68
69   /////////////////////////////////////////////////////////////////////////////////////////
70
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
71
/////////////////////////////////////////////////////////////////////////////////////////
72

73   /**
74    * Creates a new IdefixProcessorFactory instance.
75    */

76   protected IdefixProcessorFactory(Class JavaDoc aClass) {
77     _theProcessorClass = aClass;
78   }
79
80   /////////////////////////////////////////////////////////////////////////////////////////
81
/////////////////////////////////// STATIC METHODS ////////////////////////////////////
82
/////////////////////////////////////////////////////////////////////////////////////////
83

84   /**
85    * Factory method that creates a new <CODE>IdefixProcessorIF</CODE> instance.
86    *
87    * @return The new <CODE>IdefixProcessorIF</CODE> instance.
88    */

89   public static IdefixProcessorFactory newFactory() {
90     Class JavaDoc 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   /**
108    * Returns the <CODE>Class</CODE> object of the Idefix processor
109    * to use from the system properties.
110    *
111    * @return The found class object or null if no class is found or if
112    * the class passed in does not implement the interface
113    * <CODE>IdefixProcessorIF</CODE>
114    */

115   private static Class JavaDoc retrieveClassFromSystemProperties() {
116     String JavaDoc aClassName = System.getProperty(PROCESSOR_CLASS_PROPERTY);
117
118     return loadClassFromName(aClassName);
119   }
120
121   /**
122    * Returns the <CODE>Class</CODE> object of the Idefix processor
123    * to use from the file <CODE>$JAVA_HOME/jre/lib/sapia.properties</CODE>.
124    *
125    * @return The found class object or null if no class is found or if
126    * the class passed in does not implement the interface
127    * <CODE>IdefixProcessorIF</CODE>
128    */

129   private static Class JavaDoc retrieveClassFromJavaHome() {
130     StringBuffer JavaDoc aFileName = new StringBuffer JavaDoc(System.getProperty("java.home"));
131     aFileName.append(File.separator).append("lib").append(File.separator)
132              .append(PROCESSOR_CLASS_FILENAME);
133
134     File JavaDoc aFile = new File JavaDoc(aFileName.toString());
135
136     if (aFile.exists()) {
137       FileInputStream JavaDoc anInput = null;
138
139       try {
140         Properties JavaDoc someProperties = new Properties JavaDoc();
141         anInput = new FileInputStream JavaDoc(aFile);
142         someProperties.load(anInput);
143
144         String JavaDoc aClassName = someProperties.getProperty(PROCESSOR_CLASS_PROPERTY);
145
146         return loadClassFromName(aClassName);
147       } catch (IOException JavaDoc ioe) {
148         StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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 JavaDoc ioe) {
159           }
160         }
161       }
162     } else {
163       return null;
164     }
165   }
166
167   /**
168    *
169    */

170   private static Class JavaDoc retrieveClassFromResource() {
171     InputStream JavaDoc anInput = null;
172     BufferedReader JavaDoc 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 JavaDoc(new InputStreamReader JavaDoc(anInput, "UTF-8"));
181         } catch (UnsupportedEncodingException JavaDoc uee) {
182           aReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(anInput));
183         }
184
185         String JavaDoc aClassName = aReader.readLine();
186
187         return loadClassFromName(aClassName);
188       } else {
189         return null;
190       }
191     } catch (IOException JavaDoc ioe) {
192       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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 JavaDoc ioe) {
203         }
204       }
205
206       if (anInput != null) {
207         try {
208           anInput.close();
209         } catch (IOException JavaDoc ioe) {
210         }
211       }
212     }
213   }
214
215   /**
216    * Returns the <CODE>Class</CODE> object of the Idefix processor
217    * to use from the system properties.
218    *
219    * @return The found class object or null if no class is found or if
220    * the class passed in does not implement the interface
221    * <CODE>IdefixProcessorIF</CODE>
222    */

223   private static Class JavaDoc loadClassFromName(String JavaDoc aClassName) {
224     if ((aClassName != null) && (aClassName.length() > 0)) {
225       try {
226         Class JavaDoc aClass = Thread.currentThread().getContextClassLoader().loadClass(aClassName);
227
228         if (IdefixProcessorIF.class.isAssignableFrom(aClass)) {
229           return aClass;
230         } else {
231           StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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 JavaDoc cnfe) {
239         StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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   /////////////////////////////////////////////////////////////////////////////////////////
253
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
254
/////////////////////////////////////////////////////////////////////////////////////////
255

256   /**
257    * Factory method that creates a new <CODE>IdefixProcessorIF</CODE> instance.
258    *
259    * @param aSerializerFactory The serializer factory of the procesor.
260    * @param aNamespaceFactory The namespace factory of the processor.
261    * @return The new <CODE>IdefixProcessorIF</CODE> instance.
262    */

263   public IdefixProcessorIF createProcessor(
264     SerializerFactoryIF aSerializerFactory, NamespaceFactoryIF aNamespaceFactory) {
265     try {
266       Constructor JavaDoc aConstructor = _theProcessorClass.getConstructor(new Class JavaDoc[] {
267             SerializerFactoryIF.class, NamespaceFactoryIF.class
268           });
269
270       return (IdefixProcessorIF) aConstructor.newInstance(new Object JavaDoc[] {
271           aSerializerFactory, aNamespaceFactory
272         });
273     } catch (Exception JavaDoc e) {
274       throw new CompositeRuntimeException("Unable to create an Idefix processor",
275         e);
276     }
277   }
278 }
279
Popular Tags