View Javadoc

1   /*$Id: GroovyComponentManager.java,v 1.4 2004/11/05 16:52:42 jdt Exp $
2    * Created on 27-Jul-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.production;
12  
13  import org.astrogrid.component.ComponentManagerException;
14  import org.astrogrid.component.descriptor.ComponentDescriptor;
15  import org.astrogrid.component.descriptor.SimpleComponentDescriptor;
16  import org.astrogrid.config.Config;
17  import org.astrogrid.config.PropertyNotFoundException;
18  import org.astrogrid.config.SimpleConfig;
19  import org.astrogrid.jes.component.EmptyJesComponentManager;
20  import org.astrogrid.jes.delegate.v1.jobcontroller.JobController;
21  import org.astrogrid.jes.delegate.v1.jobmonitor.JobMonitor;
22  import org.astrogrid.jes.impl.workflow.AbstractJobFactoryImpl;
23  import org.astrogrid.jes.impl.workflow.DBJobFactoryImpl;
24  import org.astrogrid.jes.impl.workflow.FileJobFactoryImpl;
25  import org.astrogrid.jes.impl.workflow.SqlCommands;
26  import org.astrogrid.jes.jobscheduler.Dispatcher;
27  import org.astrogrid.jes.jobscheduler.JobScheduler;
28  import org.astrogrid.jes.jobscheduler.Locator;
29  import org.astrogrid.jes.jobscheduler.dispatcher.ApplicationControllerDispatcher;
30  import org.astrogrid.jes.jobscheduler.impl.SchedulerTaskQueueDecorator;
31  import org.astrogrid.jes.jobscheduler.impl.groovy.GroovyInterpreterFactory;
32  import org.astrogrid.jes.jobscheduler.impl.groovy.GroovySchedulerImpl;
33  import org.astrogrid.jes.jobscheduler.impl.groovy.GroovyTransformers;
34  import org.astrogrid.jes.jobscheduler.impl.groovy.XStreamPickler;
35  import org.astrogrid.jes.jobscheduler.locator.RegistryToolLocator;
36  import org.astrogrid.jes.jobscheduler.locator.XMLFileLocator;
37  import org.astrogrid.jes.resultlistener.JesResultsListener;
38  import org.astrogrid.jes.service.v1.cearesults.ResultsListener;
39  import org.astrogrid.jes.util.TemporaryBuffer;
40  
41  import org.picocontainer.MutablePicoContainer;
42  import org.picocontainer.Parameter;
43  import org.picocontainer.defaults.ComponentParameter;
44  import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
45  
46  import javax.sql.DataSource;
47  
48  /***
49   * @author Noel Winstanley nw@jb.man.ac.uk 27-Jul-2004
50   *
51   */
52  public class GroovyComponentManager extends EmptyJesComponentManager {
53      public GroovyComponentManager() throws ComponentManagerException {
54          this(SimpleConfig.getSingleton());
55      }
56      
57      public GroovyComponentManager(Config conf) throws ComponentManagerException {
58          super();
59          try {
60              pico.registerComponentInstance("jes-meta",JES_META);
61              pico.registerComponentInstance(Config.class,conf); 
62  
63              registerGroovyEngine(pico);   
64             
65             pico.registerComponentImplementation(Dispatcher.class,ApplicationControllerDispatcher.class);
66             pico.registerComponentImplementation(ApplicationControllerDispatcher.Endpoints.class,EndpointsFromConfig.class);
67             // register factory for temp buffers- so each component that requires one is passed it.
68             pico.registerComponent(new ConstructorInjectionComponentAdapter(TemporaryBuffer.class,TemporaryBuffer.class));
69             registerStandardComponents(pico);
70             registerLocator(pico,conf);                    
71             registerJobFactory(pico,conf);
72  
73             } catch (Exception e) {
74                 log.fatal("Could not create component manager",e);
75                 throw new ComponentManagerException(e);
76            }
77        }
78        /***Description-only component - metadata for entire system. 
79         * 
80         * @todo add config tests for a production system in here */
81        private static final ComponentDescriptor JES_META = new SimpleComponentDescriptor(
82            "Groovy-Driven Job Execution System",
83            "job execution system"
84        );    
85  
86        public static void registerGroovyEngine(MutablePicoContainer pico) {
87                 pico.registerComponentImplementation(SCHEDULER_ENGINE,GroovySchedulerImpl.class);          
88                   pico.registerComponentImplementation(GroovySchedulerImpl.Transformers.class,GroovyTransformers.class);         
89                   pico.registerComponentImplementation(GroovyInterpreterFactory.class,GroovyInterpreterFactory.class);
90                   pico.registerComponentImplementation(GroovyInterpreterFactory.Pickler.class,XStreamPickler.class);
91                   
92          }
93        /*** registers standard web-interface implementations (monitor / controller / listener),standard facade., standard scheduler task queue. */
94        public static void registerStandardComponents(MutablePicoContainer pico) {   
95            
96            pico.registerComponentImplementation(JobMonitor.class,org.astrogrid.jes.jobmonitor.JobMonitor.class);
97            pico.registerComponentImplementation(JobController.class,org.astrogrid.jes.jobcontroller.JobController.class);       
98            pico.registerComponentImplementation(ResultsListener.class,JesResultsListener.class);
99            pico.registerComponentImplementation(JobScheduler.class,SchedulerTaskQueueDecorator.class,
100              new Parameter[]{
101                  new ComponentParameter(SCHEDULER_ENGINE)
102              });        
103       }
104 
105       /*** key to look in config for implementation of job factory to use
106        * <p>
107        * possible values = <tt>db</tt> | <tt>file</tt> | <tt><i>java.class.name</i></tt> */
108       public static final String JOB_FACTORY_KEY = "jes.jobfactory";
109       /*** key to look in config for a datasource object */
110       public static final String DB_JOB_FACTORY_DATASOURCE_KEY = "jdbc/jes-datasource";
111       public static final ComponentDescriptor JOBFACTORY_META_DATA 
112           = new SimpleComponentDescriptor("Job Factory Configuration",
113             "to select job factory implementation set key " + JOB_FACTORY_KEY + " to \n" +
114               "'db' - for database-backed job factory, and set jndi key " + DB_JOB_FACTORY_DATASOURCE_KEY + " to the datasource \n" +
115                   "'file' - for file-backed job factory\n" +
116                   "or name of java class to instantiate"
117                   );
118       /***
119        * 
120        */
121       public static  final void registerJobFactory(MutablePicoContainer pico,Config conf) {
122           String jobFactory = conf.getString(JOB_FACTORY_KEY,"file").trim();
123           pico.registerComponentInstance("jobfactory-meta",JOBFACTORY_META_DATA);        
124           if ("db".equalsIgnoreCase(jobFactory)) {
125               try {
126                   Object o = conf.getProperty(DB_JOB_FACTORY_DATASOURCE_KEY);
127                   if (! (o instanceof DataSource)) {
128                       throw new PropertyNotFoundException("datasource does not implement javax.sql.DataSource");
129                   }
130                   DataSource ds = (DataSource)o;
131                   pico.registerComponentImplementation(AbstractJobFactoryImpl.class,DBJobFactoryImpl.class);
132                   pico.registerComponentImplementation(SqlCommands.class,SqlCommandsFromConfig.class);
133                   pico.registerComponentInstance(DataSource.class,ds);
134               } catch (PropertyNotFoundException e) {
135                   log.error("Could not find datasource, falling back to file-backed job factory",e);
136                   registerFallbackFactory(pico);
137               }
138           } else if ("file".equalsIgnoreCase(jobFactory)){ 
139               registerFallbackFactory(pico);
140           } else { // try treating it as a classname
141               log.info("Trying to instantiate " + jobFactory);
142               try {
143                   Class c = Class.forName(jobFactory);
144                   pico.registerComponentImplementation(AbstractJobFactoryImpl.class,c);
145               } catch (ClassNotFoundException e) {
146                   log.error("Could not find java class " + jobFactory + " falling back to file-backed job factory",e);
147                   registerFallbackFactory(pico);
148               }
149           }
150       }
151       
152       private static final void registerFallbackFactory(MutablePicoContainer pico) {
153               pico.registerComponentImplementation(AbstractJobFactoryImpl.class,FileJobFactoryImpl.class);
154               pico.registerComponentImplementation(FileJobFactoryImpl.BaseDirectory.class,BaseDirectoryFromConfig.class);
155                      
156       }
157       /*** key to look in config for implementation of locator to use
158        * <p>
159        * possible values: <tt>xml</tt> | <tt>registry</tt> | <tt><i>java.class.name</i></tt>
160        */
161       public static final String LOCATOR_KEY = "jes.locator";
162       private static final ComponentDescriptor LOCATOR_META = new SimpleComponentDescriptor("Tool Locator Configuration",
163           "to select tool locator implementation, set key " + LOCATOR_KEY + " to \n" + 
164           "'registry' - for locator that queries the registry\n" +
165           "'xml' - for locator that is backed by xml config file\n" +
166           "or name of java class to instantiate"
167           );
168       /***
169        * @todo registry should be the default.
170        */
171       public static final  void registerLocator(MutablePicoContainer pico, Config conf) {
172           pico.registerComponentInstance("locator-meta",LOCATOR_META);
173           String locator = conf.getString(LOCATOR_KEY,"xml").trim();
174           if ("registry".equalsIgnoreCase(locator)) {
175               pico.registerComponentImplementation(Locator.class,RegistryToolLocator.class);
176               pico.registerComponentImplementation(RegistryToolLocator.RegistryEndpoint.class,RegistryEndpointFromConfig.class);
177           } else if ("xml".equalsIgnoreCase(locator)) {
178               registerFallbackLocator(pico);
179           } else {
180               log.info("Trying to instantiate " + locator);
181               try {
182                   Class c = Class.forName(locator);
183                   pico.registerComponentImplementation(Locator.class,c);
184               } catch (ClassNotFoundException e) {
185                   log.error("Could not find java class " + locator + " falling back to xml-backed tool locator",e);
186                   registerFallbackLocator(pico);
187               }
188           }
189       }
190           
191       private static final void registerFallbackLocator(MutablePicoContainer pico) {
192           pico.registerComponentImplementation(Locator.class,XMLFileLocator.class);
193           pico.registerComponentImplementation(XMLFileLocator.ToolList.class,ToolListFromConfig.class);
194           
195       }          
196   }
197             
198     
199 
200 
201 /* 
202 $Log: GroovyComponentManager.java,v $
203 Revision 1.4  2004/11/05 16:52:42  jdt
204 Merges from branch nww-itn07-scratchspace
205 
206 Revision 1.3.60.1  2004/11/05 15:25:44  nw
207 added temporary buffers into the component manager
208 
209 Revision 1.3  2004/08/03 16:31:25  nw
210 simplified interface to dispatcher and locator components.
211 removed redundant implementations.
212 
213 Revision 1.2  2004/07/30 15:42:34  nw
214 merged in branch nww-itn06-bz#441 (groovy scripting)
215 
216 Revision 1.1.2.3  2004/07/30 15:10:04  nw
217 removed policy-based implementation,
218 adjusted tests, etc to use groovy implementation
219 
220 Revision 1.1.2.2  2004/07/27 23:50:09  nw
221 removed betwixt (duff). replaces with xstream.
222 
223 Revision 1.1.2.1  2004/07/27 23:37:59  nw
224 refactoed framework.
225 experimented with betwixt - can't get it to work.
226 got XStream working in 5 mins.
227 about to remove betwixt code.
228  
229 */