01: /**
02: * Copyright (C) 2004-2007 Jive Software. All rights reserved.
03: *
04: * This software is published under the terms of the GNU Public License (GPL),
05: * a copy of which is included in this distribution.
06: */package org.xmpp.component;
07:
08: /**
09: * Factory to get a ComponentManager implementation. The ComponentManager implementation
10: * used will determined in the following way:<ul>
11: *
12: * <li>An external process can set the ComponentManager using
13: * {@link #setComponentManager(ComponentManager)}.
14: * <li>If the component manager is <tt>null</tt>, the factory will check for
15: * the Java system property "whack.componentManagerClass". The value of the
16: * property should be the fully qualified class name of a ComponentManager
17: * implementation (e.g. com.foo.MyComponentManager). The class must have a default
18: * constructor.
19: * </ul>
20: *
21: * @author Matt Tucker
22: */
23: public class ComponentManagerFactory {
24:
25: private static ComponentManager componentManager;
26:
27: /**
28: * Returns a ComponentManager instance.
29: *
30: * @return a ComponentManager instance.
31: */
32: public static synchronized ComponentManager getComponentManager() {
33: if (componentManager != null) {
34: return componentManager;
35: }
36: // ComponentManager is null so we have to try to figure out how to load
37: // an instance. Look for a Java property.
38: String className = System
39: .getProperty("whack.componentManagerClass");
40: if (className != null) {
41: try {
42: Class c = Class.forName(className);
43: componentManager = (ComponentManager) c.newInstance();
44: return componentManager;
45: } catch (Exception e) {
46: e.printStackTrace();
47: }
48: }
49: // Got here, so throw exception.
50: throw new NullPointerException(
51: "No ComponentManager implementation available.");
52: }
53:
54: /**
55: * Sets the ComponentManager instance that will be used.
56: *
57: * @param manager the ComponentManager instance.
58: */
59: public static void setComponentManager(ComponentManager manager) {
60: componentManager = manager;
61: }
62: }
|