1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.applications.component;
12
13 import org.astrogrid.component.ComponentManagerException;
14 import org.astrogrid.config.SimpleConfig;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 /*** static container that instantiates and provides access to the component manager.
26 * <p />
27 * don't use this class willy-nilly - only at the top-leve of the CEA server, where clients need to get an instance. Otherwise the whole model breaks.
28 * Within the server itself, the components shoudl gain access to each other through constructor-based dependency injection (which sounds more complex than it is ;)
29 *
30 * <p />
31 * uses a config key {@link #COMPONENT_MANAGER_IMPL} to determine which class of component manager to instantiate.
32 * This key is looked up in the {@link org.astrogrid.config.Config} system (its likely this key is set in jndi (i.e. in the <tt>context.xml</tt> or <tt>web.xml</tt>for that webapp).
33 * @author Noel Winstanley nw@jb.man.ac.uk 04-May-2004
34 *
35 */
36 public class CEAComponentManagerFactory {
37 /***
38 * Commons Logger for this class
39 */
40 private static final Log logger = LogFactory.getLog(CEAComponentManagerFactory.class);
41
42 /*** key to look in config for cea component manager implementation class */
43 public static final String COMPONENT_MANAGER_IMPL = "cea.component.manager.class";
44
45
46 /*** get the instnace.
47 * lazily initialized - parses configuration and creates components on first call
48 * @return instance of cea component manager, which has been started.
49 */
50 public static synchronized CEAComponentManager getInstance() throws ComponentManagerException {
51 if (theInstance == null) {
52 logger.info("Creating component manager");
53 String componentManagerClass = null;
54 try {
55 componentManagerClass = SimpleConfig.getSingleton().getString(COMPONENT_MANAGER_IMPL,JavaClassCEAComponentManager.class.getName());
56 logger.info("Will instantiate component manager class '" + componentManagerClass + "'");
57 theInstance = (CEAComponentManager)Class.forName(componentManagerClass).newInstance();
58 logger.info("Instantiated. Now Starting");
59 theInstance.start();
60 logger.info("Successfully created component manager");
61 } catch (Throwable e) {
62 logger.fatal("Could not create component manager (class '" + componentManagerClass + "')",e);
63 StringWriter sw = new StringWriter();
64 e.printStackTrace(new PrintWriter(sw));
65 logger.info(sw.toString());
66 throw new ComponentManagerException("Could not create componentManager : " + componentManagerClass,e);
67 }
68 }
69 return theInstance;
70 }
71
72 protected static CEAComponentManager theInstance;
73
74 /package-summary/html">package-protected 'clear' method - for testing *//package-summary.html">/*** package-protected 'clear' method - for testing */
75 static void clearInstance() {
76 theInstance = null;
77 }
78 public static final void stop() {
79 if (theInstance != null) {
80 theInstance.getContainer().stop();
81 theInstance.getContainer().dispose();
82 }
83 }
84 private CEAComponentManagerFactory() {
85 }
86
87
88 /*** static method - makes this look like a normal JUnit test, which can then be called in a junit runner.*/
89 public static Test suite(){
90 try {
91 return CEAComponentManagerFactory.getInstance().getSuite();
92 } catch (ComponentManagerException e) {
93 return new TestSuite("No tests available - component manager failed with " + e.getMessage());
94 }
95 }
96
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125