KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > conf > ConfiguratorFactory


1 // $Id: ConfiguratorFactory.java,v 1.14 2004/09/23 16:29:14 belaban Exp $
2

3 package org.jgroups.conf;
4
5 import org.w3c.dom.Element JavaDoc;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import org.jgroups.ChannelException;
10 import org.jgroups.JChannel;
11
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20
21 import java.util.Properties JavaDoc;
22
23 /**
24  * The ConfigurationFactory is a factory that returns a protocol stack configurator.
25  * The protocol stack configurator is an object that read a stack configuration and
26  * parses it so that the ProtocolStack can create a stack.
27  * <BR>
28  * Currently the factory returns one of the following objects:<BR>
29  * 1. XmlConfigurator - parses XML files that are according to the jgroups-protocol.dtd<BR>
30  * 2. PlainConfigurator - uses the old style strings UDP:FRAG: etc etc<BR>
31  *
32  * @author Filip Hanik (<a HREF="mailto:filip@filip.net">filip@filip.net)
33  * @version 1.0
34  */

35 public class ConfiguratorFactory {
36     public static final String JavaDoc JAXP_MISSING_ERROR_MSG=
37             "JAXP Error: the required XML parsing classes are not available; " +
38             "make sure that JAXP compatible libraries are in the classpath.";
39
40     static final String JavaDoc FORCE_CONFIGURATION="force.properties";
41
42     static final Log log=LogFactory.getLog(ConfiguratorFactory.class);
43
44     static String JavaDoc propertiesOverride=null;
45
46     // Check for the presence of the system property "force.properties", and
47
// act appropriately if it is set. We only need to do this once since the
48
// system properties are highly unlikely to change.
49
static {
50         try {
51             Properties JavaDoc properties = System.getProperties();
52             propertiesOverride = properties.getProperty(FORCE_CONFIGURATION);
53         }
54         catch (SecurityException JavaDoc e) {
55             propertiesOverride = null;
56         }
57
58         if(propertiesOverride != null && log.isInfoEnabled()) {
59             log.info("using properties override: " + propertiesOverride);
60         }
61     }
62
63     protected ConfiguratorFactory() {
64     }
65
66     /**
67      * Returns a protocol stack configurator based on the XML configuration
68      * provided at the specified URL.
69      *
70      * @param url a URL pointing to a JGroups XML configuration.
71      *
72      * @return a <code>ProtocolStackConfigurator</code> containing the stack
73      * configuration.
74      *
75      * @throws ChannelException if problems occur during the configuration of
76      * the protocol stack.
77      */

78     public static ProtocolStackConfigurator getStackConfigurator(File JavaDoc file)
79     throws ChannelException {
80         ProtocolStackConfigurator returnValue;
81
82         if (propertiesOverride != null) {
83             returnValue = getStackConfigurator(propertiesOverride);
84         }
85         else {
86             checkForNullConfiguration(file);
87             checkJAXPAvailability();
88
89             try {
90                 returnValue=
91                     XmlConfigurator.getInstance(new FileInputStream JavaDoc(file));
92             }
93             catch (IOException JavaDoc ioe) {
94                 throw createChannelConfigurationException(ioe);
95             }
96         }
97
98         return returnValue;
99     }
100
101     /**
102      * Returns a protocol stack configurator based on the XML configuration
103      * provided at the specified URL.
104      *
105      * @param url a URL pointing to a JGroups XML configuration.
106      *
107      * @return a <code>ProtocolStackConfigurator</code> containing the stack
108      * configuration.
109      *
110      * @throws ChannelException if problems occur during the configuration of
111      * the protocol stack.
112      */

113     public static ProtocolStackConfigurator getStackConfigurator(URL JavaDoc url)
114     throws ChannelException {
115         ProtocolStackConfigurator returnValue;
116
117         if (propertiesOverride != null) {
118             returnValue = getStackConfigurator(propertiesOverride);
119         }
120         else {
121             checkForNullConfiguration(url);
122             checkJAXPAvailability();
123
124             try {
125                 returnValue=XmlConfigurator.getInstance(url);
126             }
127             catch (IOException JavaDoc ioe) {
128                 throw createChannelConfigurationException(ioe);
129             }
130         }
131
132         return returnValue;
133     }
134
135     /**
136      * Returns a protocol stack configurator based on the XML configuration
137      * provided by the specified XML element.
138      *
139      * @param element a XML element containing a JGroups XML configuration.
140      *
141      * @return a <code>ProtocolStackConfigurator</code> containing the stack
142      * configuration.
143      *
144      * @throws ChannelException if problems occur during the configuration of
145      * the protocol stack.
146      */

147     public static ProtocolStackConfigurator getStackConfigurator(Element JavaDoc element)
148     throws ChannelException {
149         ProtocolStackConfigurator returnValue;
150
151         if (propertiesOverride != null) {
152             returnValue = getStackConfigurator(propertiesOverride);
153         }
154         else {
155             checkForNullConfiguration(element);
156
157             // Since Element is a part of the JAXP specification and because an
158
// Element instance already exists, there is no need to check for
159
// JAXP availability.
160
//
161
// checkJAXPAvailability();
162

163             try {
164                 returnValue=XmlConfigurator.getInstance(element);
165             }
166             catch (IOException JavaDoc ioe) {
167                 throw createChannelConfigurationException(ioe);
168             }
169         }
170
171         return returnValue;
172     }
173
174     /**
175      * Returns a protocol stack configurator based on the provided properties
176      * string.
177      *
178      * @param properties an old style property string, a string representing a
179      * system resource containing a JGroups XML configuration,
180      * a string representing a URL pointing to a JGroups XML
181      * XML configuration, or a string representing a file name
182      * that contains a JGroups XML configuration.
183      */

184     public static ProtocolStackConfigurator getStackConfigurator(String JavaDoc properties) throws ChannelException {
185         if (propertiesOverride != null) {
186             properties = propertiesOverride;
187         }
188
189         // added by bela: for null String props we use the default properties
190
if(properties == null)
191             properties=JChannel.DEFAULT_PROTOCOL_STACK;
192
193         checkForNullConfiguration(properties);
194
195         ProtocolStackConfigurator returnValue;
196
197         // Attempt to treat the properties string as a pointer to an XML
198
// configuration.
199
XmlConfigurator configurator = null;
200
201         try {
202             configurator=getXmlConfigurator(properties);
203         }
204         catch (IOException JavaDoc ioe) {
205             throw createChannelConfigurationException(ioe);
206         }
207
208         // Did the properties string point to a JGroups XML configuration?
209
if (configurator != null) {
210             returnValue=configurator;
211         }
212         else {
213             // Attempt to process the properties string as the old style
214
// property string.
215
returnValue=new PlainConfigurator(properties);
216         }
217
218         return returnValue;
219     }
220
221     /**
222      * Returns a protocol stack configurator based on the properties passed in.<BR>
223      * If the properties parameter is a plain string UDP:FRAG:MERGE:GMS etc, a PlainConfigurator is returned.<BR>
224      * If the properties parameter is a string that represents a url for example http://www.filip.net/test.xml
225      * or the parameter is a java.net.URL object, an XmlConfigurator is returned<BR>
226      *
227      * @param properties old style property string, url string, or java.net.URL object
228      * @return a ProtocolStackConfigurator containing the stack configuration
229      * @throws IOException if it fails to parse the XML content
230      * @throws IOException if the URL is invalid or a the content can not be reached
231      * @deprecated Used by the JChannel(Object) constructor which has been deprecated.
232      */

233     public static ProtocolStackConfigurator getStackConfigurator(Object JavaDoc properties) throws IOException JavaDoc {
234         InputStream JavaDoc input=null;
235
236         if (propertiesOverride != null) {
237             properties = propertiesOverride;
238         }
239
240         // added by bela: for null String props we use the default properties
241
if(properties == null)
242             properties=JChannel.DEFAULT_PROTOCOL_STACK;
243
244         if(properties instanceof URL JavaDoc) {
245             try {
246                 input=((URL JavaDoc)properties).openStream();
247             }
248             catch(Throwable JavaDoc t) {
249             }
250         }
251
252         // if it is a string, then it could be a plain string or a url
253
if(input == null && properties instanceof String JavaDoc) {
254             try {
255                 input=new URL JavaDoc((String JavaDoc)properties).openStream();
256             }
257             catch(Exception JavaDoc ignore) {
258                 // if we get here this means we don't have a URL
259
}
260
261             // another try - maybe it is a resource, e.g. default.xml
262
if(input == null && ((String JavaDoc)properties).endsWith("xml")) {
263                 try {
264                     ClassLoader JavaDoc classLoader=Thread.currentThread().getContextClassLoader();
265                     input=classLoader.getResourceAsStream((String JavaDoc)properties);
266                 }
267                 catch(Throwable JavaDoc ignore) {
268                 }
269             }
270
271             // try a regular file name
272
//
273
// This code was moved from the parent block (below) because of the
274
// possibility of causing a ClassCastException.
275

276             if(input == null) {
277                 try {
278                     input=new FileInputStream JavaDoc((String JavaDoc)properties);
279                 }
280                 catch(Throwable JavaDoc t) {
281                 }
282             }
283         }
284
285         // try a regular file
286
if(input == null && properties instanceof File JavaDoc) {
287             try {
288                 input=new FileInputStream JavaDoc((File JavaDoc)properties);
289             }
290             catch(Throwable JavaDoc t) {
291             }
292         }
293
294         if(input != null) {
295             return XmlConfigurator.getInstance(input);
296         }
297
298         if(properties instanceof Element JavaDoc) {
299             return XmlConfigurator.getInstance((Element JavaDoc)properties);
300         }
301
302         return new PlainConfigurator((String JavaDoc)properties);
303     }
304
305
306     /**
307      * Returns a JGroups XML configuration InputStream based on the provided
308      * properties string.
309      *
310      * @param properties a string representing a system resource containing a
311      * JGroups XML configuration, a string representing a URL
312      * pointing to a JGroups ML configuration, or a string
313      * representing a file name that contains a JGroups XML
314      * configuration.
315      *
316      * @throws IOException if the provided properties string appears to be a
317      * valid URL but is unreachable.
318      */

319     static InputStream JavaDoc getConfigStream(String JavaDoc properties) throws IOException JavaDoc {
320         InputStream JavaDoc configStream = null;
321
322         // Check to see if the properties string is a URL.
323
try {
324             configStream=new URL JavaDoc(properties).openStream();
325         }
326         catch (MalformedURLException JavaDoc mre) {
327             // the properties string is not a URL
328
}
329         // Commented so the caller is notified of this condition, but left in
330
// the code for documentation purposes.
331
//
332
// catch (IOException ioe) {
333
// the specified URL string was not reachable
334
// }
335

336         // Check to see if the properties string is the name of a resource,
337
// e.g. default.xml.
338
if(configStream == null && properties.endsWith("xml")) {
339             ClassLoader JavaDoc classLoader=Thread.currentThread().getContextClassLoader();
340             configStream=classLoader.getResourceAsStream(properties);
341         }
342
343         // Check to see if the properties string is the name of a file.
344
if (configStream == null) {
345             try {
346                 configStream=new FileInputStream JavaDoc(properties);
347             }
348             catch(FileNotFoundException JavaDoc fnfe) {
349                 // the properties string is likely not a file
350
}
351         }
352
353         return configStream;
354     }
355
356     /**
357      * Returns an XmlConfigurator based on the provided properties string (if
358      * possible).
359      *
360      * @param properties a string representing a system resource containing a
361      * JGroups XML configuration, a string representing a URL
362      * pointing to a JGroups ML configuration, or a string
363      * representing a file name that contains a JGroups XML
364      * configuration.
365      *
366      * @return an XmlConfigurator instance based on the provided properties
367      * string; <code>null</code> if the provided properties string does
368      * not point to an XML configuration.
369      *
370      * @throws IOException if the provided properties string appears to be a
371      * valid URL but is unreachable, or if the JGroups XML
372      * configuration pointed to by the URL can not be
373      * parsed.
374      */

375     static XmlConfigurator getXmlConfigurator(String JavaDoc properties) throws IOException JavaDoc {
376         XmlConfigurator returnValue=null;
377         InputStream JavaDoc configStream=getConfigStream(properties);
378
379         if (configStream != null) {
380             checkJAXPAvailability();
381
382             returnValue=XmlConfigurator.getInstance(configStream);
383         }
384
385         return returnValue;
386     }
387
388     /**
389      * Creates a <code>ChannelException</code> instance based upon a
390      * configuration problem.
391      *
392      * @param cause the exceptional configuration condition to be used as the
393      * created <code>ChannelException</code>'s cause.
394      */

395     static ChannelException createChannelConfigurationException(Throwable JavaDoc cause) {
396         return new ChannelException("unable to load the protocol stack", cause);
397     }
398
399     /**
400      * Check to see if the specified configuration properties are
401      * <code>null</null> which is not allowed.
402      *
403      * @param properties the specified protocol stack configuration.
404      *
405      * @throws NullPointerException if the specified configuration properties
406      * are <code>null</code>.
407      */

408     static void checkForNullConfiguration(Object JavaDoc properties) {
409         if (properties == null) {
410             final String JavaDoc msg =
411                 "the specifed protocol stack configuration was null.";
412
413             throw new NullPointerException JavaDoc(msg);
414         }
415     }
416
417     /**
418      * Checks the availability of the JAXP classes on the classpath.
419      *
420      * @throws NoClassDefFoundError if the required JAXP classes are not
421      * availabile on the classpath.
422      */

423     static void checkJAXPAvailability() {
424         try {
425             // TODO: Do some real class checking here instead of forcing the
426
// load of a JGroups class that happens (by default) to do it
427
// for us.
428
XmlConfigurator.class.getName();
429         }
430         catch (NoClassDefFoundError JavaDoc error) {
431             throw new NoClassDefFoundError JavaDoc(JAXP_MISSING_ERROR_MSG);
432         }
433     }
434 }
435
Popular Tags