View Javadoc

1   /*$Id: JesComponentManagerFactory.java,v 1.3 2004/07/30 15:42:34 nw Exp $
2    * Created on 16-Feb-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.jes.component;
12  
13  import org.astrogrid.component.ComponentManagerException;
14  import org.astrogrid.config.SimpleConfig;
15  import org.astrogrid.jes.component.production.GroovyComponentManager;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  
27  /***
28   Static singleton that contains a single instance of a ComponentManager. 
29   Used so that the same component manager (and so same components) can be accessed from different parts of the server - in particular the job monitor and jobscheduler
30   web services.
31   * @author Noel Winstanley nw@jb.man.ac.uk 16-Feb-2004
32   *
33   */
34  public class JesComponentManagerFactory {
35      /***
36       * Commons Logger for this class
37       */
38      private static final Log logger = LogFactory.getLog(JesComponentManagerFactory.class);
39      /*** key to look in config for cea component manager implementation class */
40      public static final String COMPONENT_MANAGER_IMPL = "jes.component.manager.class";
41  
42  
43  
44      /*** get the instnace.
45       * lazily initialized - parses configuration and creates components on first call
46       * @return
47       */
48      public static synchronized JesComponentManager getInstance() throws ComponentManagerException {
49          if (theInstance == null) {
50               logger.info("Creating component manager");
51               String componentManagerClass = null;
52               try {
53                   componentManagerClass = SimpleConfig.getSingleton().getString(COMPONENT_MANAGER_IMPL,GroovyComponentManager.class.getName());
54                   logger.info("Will instantiate component manager class '" + componentManagerClass + "'");
55                   theInstance = (JesComponentManager)Class.forName(componentManagerClass).newInstance();
56                   logger.info("Instantiated. Now Starting");
57                   theInstance.start();
58                   logger.info("Successfully created component manager");
59               } catch (Throwable e) {
60                   logger.fatal("Could not create component manager (class '" + componentManagerClass + "')",e);
61                   StringWriter sw = new StringWriter();
62                   e.printStackTrace(new PrintWriter(sw));
63                   logger.info(sw.toString());
64                   throw new ComponentManagerException("Could not create componentManager : " + componentManagerClass,e);
65               }
66           }           
67          return theInstance;
68      }
69      
70      protected static JesComponentManager theInstance;
71          
72      /*** unsafe, useful for testing */
73      public static void _setInstance(JesComponentManager mgr) {
74          theInstance = mgr;
75          theInstance.start();
76      }
77      
78      private JesComponentManagerFactory() {    
79      }
80      
81    
82      /*** static method - makes this look like a normal JUnit test, which can then be called in a junit runner.*/
83      public static Test suite(){
84          try {
85              return JesComponentManagerFactory.getInstance().getSuite();
86          } catch (ComponentManagerException e) {
87              return new TestSuite("No tests available - component manager failed with " + e.getMessage());
88          }
89      }
90  
91  
92  }
93