KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > confix > ConfixProcessorFactory


1 package org.sapia.util.xml.confix;
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>ConfixProcessorIF</code> instances. The
25  * class of the Confix processor to use must be specified through the following
26  * property: <code>org.sapia.xml.ConfixProcessor</code>.
27  * <p>
28  * The property is searched:
29  *
30  * <ol>
31  * <li>In the system properties.
32  * <li>In a "sapia.properties" file in $JAVA_HOME/lib.
33  * <li>In a "sapia.properties" file that is loaded as a resource.
34  * </ol>
35  *
36  * If none of the methods above worked, the factory return a <code>SAXProcessor</code>.
37  *
38  * @see org.sapia.util.xml.confix.SAXProcessor
39  * @author JC Desrochers
40  *
41  * <dl>
42  * <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>
43  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
44  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
45  * </dl>
46  */

47 public class ConfixProcessorFactory {
48   /////////////////////////////////////////////////////////////////////////////////////////
49
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
50
/////////////////////////////////////////////////////////////////////////////////////////
51

52   /** Defines the system property that defines the processor class name. */
53   public static final String JavaDoc PROCESSOR_CLASS_PROPERTY = "org.sapia.xml.ConfixProcessor";
54
55   /** Defines the properties filename that contains the processor class name. */
56   public static final String JavaDoc PROCESSOR_CLASS_FILENAME = "sapia.properties";
57
58   /** Defines the resource name that contains the processor class name. */
59   public static final String JavaDoc PROCESSOR_CLASS_RESOURCE = "META-INF/services/" +
60     PROCESSOR_CLASS_PROPERTY;
61
62   /** Defines the default Confix processor class. */
63   public static final String JavaDoc DEFAULT_PROCESSOR_CLASS = "org.sapia.util.xml.confix.SAXProcessor";
64
65   /////////////////////////////////////////////////////////////////////////////////////////
66
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
67
/////////////////////////////////////////////////////////////////////////////////////////
68

69   /** The class of the confix processor of this factory. */
70   private Class JavaDoc _theProcessorClass;
71
72   /////////////////////////////////////////////////////////////////////////////////////////
73
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
74
/////////////////////////////////////////////////////////////////////////////////////////
75

76   /**
77    * Creates a new ConfixProcessorFactory instance.
78    */

79   protected ConfixProcessorFactory(Class JavaDoc aClass) {
80     _theProcessorClass = aClass;
81   }
82
83   /////////////////////////////////////////////////////////////////////////////////////////
84
/////////////////////////////////// STATIC METHODS ////////////////////////////////////
85
/////////////////////////////////////////////////////////////////////////////////////////
86

87   /**
88    * Factory method that creates a new <CODE>ConfixProcessorIF</CODE> instance.
89    *
90    * @return The new <CODE>ConfixProcessorIF</CODE> instance.
91    */

92   public static ConfixProcessorFactory newFactory() {
93     Class JavaDoc 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   /**
111    * Returns the <CODE>Class</CODE> object of the Confix processor
112    * to use from the system properties.
113    *
114    * @return The found class object or null if no class is found or if
115    * the class passed in does not implement the interface
116    * <CODE>ConfixProcessorIF</CODE>
117    */

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

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

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

226   private static Class JavaDoc loadClassFromName(String JavaDoc aClassName) {
227     if ((aClassName != null) && (aClassName.length() > 0)) {
228       try {
229         Class JavaDoc aClass = Thread.currentThread().getContextClassLoader().loadClass(aClassName);
230
231         if (ConfixProcessorIF.class.isAssignableFrom(aClass)) {
232           return aClass;
233         } else {
234           StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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 JavaDoc cnfe) {
242         StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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   /////////////////////////////////////////////////////////////////////////////////////////
256
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
257
/////////////////////////////////////////////////////////////////////////////////////////
258

259   /**
260    * Factory method that creates a new <CODE>ConfixProcessorIF</CODE> instance.
261    *
262    * @return The new <CODE>ConfixProcessorIF</CODE> instance.
263    */

264   public ConfixProcessorIF createProcessor(ObjectFactoryIF aFactory) {
265     try {
266       Constructor JavaDoc aConstructor = _theProcessorClass.getConstructor(new Class JavaDoc[] {
267             ObjectFactoryIF.class
268           });
269
270       return (ConfixProcessorIF) aConstructor.newInstance(new Object JavaDoc[] { aFactory });
271     } catch (Exception JavaDoc e) {
272       throw new CompositeRuntimeException("Unable to create a Confix processor",
273         e);
274     }
275   }
276 }
277
Popular Tags