View Javadoc

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