KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmpp > component > ComponentManagerFactory


1 /**
2  * $RCSfile: ComponentManagerFactory.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/02/08 21:48:59 $
5  *
6  * Copyright 2005 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.xmpp.component;
22
23 /**
24  * Factory to get a ComponentManager implementation. The ComponentManager implementation
25  * used will determined in the following way:<ul>
26  *
27  * <li>An external process can set the ComponentManager using
28  * {@link #setComponentManager(ComponentManager)}.
29  * <li>If the component manager is <tt>null</tt>, the factory will check for
30  * the Java system property "whack.componentManagerClass". The value of the
31  * property should be the fully qualified class name of a ComponentManager
32  * implementation (e.g. com.foo.MyComponentManager). The class must have a default
33  * constructor.
34  * </ul>
35  *
36  * @author Matt Tucker
37  */

38 public class ComponentManagerFactory {
39
40     private static ComponentManager componentManager;
41
42     /**
43      * Returns a ComponentManager instance.
44      *
45      * @return a ComponentManager instance.
46      */

47     public static synchronized ComponentManager getComponentManager() {
48         if (componentManager != null) {
49             return componentManager;
50         }
51         // ComponentManager is null so we have to try to figure out how to load
52
// an instance. Look for a Java property.
53
String JavaDoc className = System.getProperty("whack.componentManagerClass");
54         if (className != null) {
55             try {
56                 Class JavaDoc c = Class.forName(className);
57                 componentManager = (ComponentManager)c.newInstance();
58                 return componentManager;
59             }
60             catch (Exception JavaDoc e) {
61                 e.printStackTrace();
62             }
63         }
64         // Got here, so throw exception.
65
throw new NullPointerException JavaDoc("No ComponentManager implementation available.");
66     }
67
68     /**
69      * Sets the ComponentManager instance that will be used.
70      *
71      * @param manager the ComponentManager instance.
72      */

73     public static void setComponentManager(ComponentManager manager) {
74         componentManager = manager;
75     }
76 }
77
Popular Tags