View Javadoc

1   /*$Id: CEAComponentManagerFactory.java,v 1.4 2004/08/17 15:07:55 nw Exp $
2    * Created on 04-May-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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 $Log: CEAComponentManagerFactory.java,v $
102 Revision 1.4  2004/08/17 15:07:55  nw
103 tried to improve behaviour on webapp stop
104 
105 Revision 1.3  2004/07/23 13:21:21  nw
106 Javadocs
107 
108 Revision 1.2  2004/07/01 11:16:22  nw
109 merged in branch
110 nww-itn06-componentization
111 
112 Revision 1.1.2.2  2004/07/01 01:42:46  nw
113 final version, before merge
114 
115 Revision 1.1.2.1  2004/06/14 08:56:58  nw
116 factored applications into sub-projects,
117 got packaging of wars to work again
118 
119 Revision 1.1.2.2  2004/05/28 10:23:11  nw
120 checked in early, broken version - but it builds and tests (fail)
121 
122 Revision 1.1.2.1  2004/05/21 12:00:22  nw
123 merged in latest changes from HEAD. start of refactoring effort
124  
125 */