1
2
3
4
5
6
7
8
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
74 pico.registerComponentImplementation(Dispatcher.class,CompositeDispatcher.class);
75 pico.registerComponentImplementation(CeaApplicationDispatcher.class);
76 pico.registerComponentImplementation(CeaApplicationDispatcher.Endpoints.class,EndpointsFromConfig.class);
77
78 pico.registerComponentImplementation(ConeSearchDispatcher.class);
79 pico.registerComponentImplementation(SiapDispatcher.class);
80 pico.registerComponentImplementation(SsapDispatcher.class);
81
82 pico.registerComponentImplementation(InProcessCeaComponentManager.class,InProcessCeaComponentManager.class,
83 new Parameter[]{
84 new ConstantParameter(pico)
85 , new ConstantParameter(conf)
86 }
87 );
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 {
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269