View Javadoc

1   /*$Id: WorkflowManagerFactory.java,v 1.12 2005/04/27 13:43:17 clq2 Exp $
2    * Created on 24-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.portal.workflow.intf;
12  
13  import java.net.URL;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.astrogrid.config.Config;
18  import org.astrogrid.config.PropertyNotFoundException;
19  import org.astrogrid.config.SimpleConfig;
20  import org.astrogrid.portal.workflow.impl.BasicWorkflowBuilder;
21  import org.astrogrid.portal.workflow.impl.FileApplicationRegistry;
22  import org.astrogrid.portal.workflow.impl.JesJobExecutionService;
23  import org.astrogrid.portal.workflow.impl.RegistryApplicationRegistry;
24  import org.astrogrid.portal.workflow.impl.RegistryV10ApplicationRegistry;
25  import org.astrogrid.portal.workflow.impl.VoSpaceClientWorkflowStore;
26  
27  /*** Factory that creates a new workflow manager
28   * <p>
29   * After creation, a factory instance maintains a single instance of the manager
30   * <p>
31   * The composition of the created workflow manager depends on the keys set in the {@link org.astrogrid.config.Config} system. Refer to the documentation
32   * for this package for how to set keys.
33   * <p>
34   * The keys that control the configuration of the workflow manager are all defined in this class:
35   * <h2>Workflow Store</h2>
36   * Based on the VoSpaceClient delegate - configuration details given there. no futher configuration here.
37   * <h2>Application Registry</h2>
38   * <li>
39   * <li>{@link #WORKFLOW_APPLIST_KEY}
40   * <li>{@link #WORKFLOW_APPLIST_REGISTRY_ENDPOINT_KEY}
41   * <li>{@link #WORKFLOW_APPLIST_XML_URL_KEY}
42   * </ul>
43   * <h2>Job Execution Service</h2>
44   * <ul>
45   * <li>{@link #WORKFLOW_JES_ENDPOINT_KEY}
46   * 
47   * </ul>
48   * @author Noel Winstanley nw@jb.man.ac.uk 24-Feb-2004
49   *
50   */
51  public class WorkflowManagerFactory {
52      private static Log log = LogFactory.getLog(WorkflowManagerFactory.class);
53      /*** Construct a new WorkflowManagerFactory
54       * will use default configuration object from {@link SimpleConfig}
55       */
56      public WorkflowManagerFactory() {
57          this(SimpleConfig.getSingleton());
58      }
59      
60      /*** construct a workflow manager factory, which will use the passed in configuration object */
61      public WorkflowManagerFactory(Config conf) {
62          this.conf = conf;
63      }
64      private final Config conf;
65      
66      /*** access the manager instance */
67      public synchronized WorkflowManager getManager() throws WorkflowInterfaceException{
68          if (theInstance == null) {
69             theInstance = createManager();           
70          }
71          assert theInstance != null;
72          return theInstance;
73      }
74      
75      private WorkflowManager theInstance;
76      
77  
78      /*** key to look under to determin implmenetation of workflow applicationRegistry to use
79       * <p>possible values: <tt>xml</tt> | <tt>registry</tt> | <tt><i>java.class.name</i></tt>
80       * <br> default value: <tt>registry</tt>
81       */
82      public static final String WORKFLOW_APPLIST_KEY = "workflow.applist";
83      /*** key to look under to determine url of xml file to use for xml-based application registry 
84       * <p> default value: <tt>/application-list.xml</tt> on classpath*/
85      public static final String WORKFLOW_APPLIST_XML_URL_KEY = "workflow.applist.xml.url";
86      /*** default location to look for xml file for xml-based application registry */
87      public static final String WORKFLOW_APPLIST_XML_DEFAULT = "/application-list.xml";
88      private static final URL WORKFLOW_APPLIST_XML_DEFAULT_URL = WorkflowManagerFactory.class.getResource(WORKFLOW_APPLIST_XML_DEFAULT);
89      /*** key to look under to determine endpoint of registry-based application registry */
90      public static final String WORKFLOW_APPLIST_REGISTRY_ENDPOINT_KEY = "workflow.applist.registry.endpoint";
91      /*** key to look under to determine endpoint of jes controller service */
92      public static final String WORKFLOW_JES_ENDPOINT_KEY = "workflow.jes.endpoint";
93      
94      /*** create workflow manager, based on properties in config */
95      private WorkflowManager createManager() throws WorkflowInterfaceException{
96          try {
97          WorkflowBuilder builder = new BasicWorkflowBuilder();
98          WorkflowStore store = buildStore();
99          ApplicationRegistry reg = buildAppReg();
100         JobExecutionService jes = buildJes();
101         return new WorkflowManager(builder,store,reg,jes);
102         } catch (Exception e) {
103             log.fatal("Failed to create Workflow Manager",e);
104             throw new WorkflowInterfaceException(e);       
105         }        
106     }
107 
108     /***
109      * @return
110      */
111     private JobExecutionService buildJes() {
112         try {
113             String url = conf.getString(WORKFLOW_JES_ENDPOINT_KEY);
114             log.info(WORKFLOW_JES_ENDPOINT_KEY + " := " + url);
115              return new JesJobExecutionService(url);
116         } catch (PropertyNotFoundException e) {
117             log.info("no url found under " + WORKFLOW_JES_ENDPOINT_KEY + ", trying letting delegate configure itself");
118             return new JesJobExecutionService();
119         }
120     }
121 
122     /***
123      * @return
124      */
125     private ApplicationRegistry buildAppReg() throws Exception {
126         try {
127             String option = conf.getString(WORKFLOW_APPLIST_KEY).trim();            
128             log.info(WORKFLOW_APPLIST_KEY + " := " + option);
129             if ("xml".equalsIgnoreCase(option)) {
130                 log.info("Creating xml-file backed application list");
131                 URL applicationListDocument= conf.getUrl(WORKFLOW_APPLIST_XML_URL_KEY,WORKFLOW_APPLIST_XML_DEFAULT_URL);
132                 log.info(WORKFLOW_APPLIST_XML_URL_KEY + " := " + applicationListDocument.toString());
133                 return new FileApplicationRegistry(applicationListDocument);
134             } else if ("registry".equalsIgnoreCase(option)) {
135                 return buildDefaultAppReg();
136             } else { // assume its a class name;
137                 log.info("option unrecognized - assuming its a java classname. attempting to instantiate..");
138                  Class clazz = Class.forName(option);
139                  return (ApplicationRegistry)clazz.newInstance();
140             }
141         } catch (PropertyNotFoundException e) {
142             log.warn("no value for " + WORKFLOW_APPLIST_KEY + " falling back to default");
143             return buildDefaultAppReg();
144         }
145     }
146     /*** build default, astorgrid-registry based implementation */
147     private ApplicationRegistry buildDefaultAppReg() {
148         log.info("Creating registry backed application list");
149         try {
150             URL endpoint = conf.getUrl(WORKFLOW_APPLIST_REGISTRY_ENDPOINT_KEY);
151             log.info(WORKFLOW_APPLIST_REGISTRY_ENDPOINT_KEY + " := " + endpoint.toString());
152             if (conf.getString("org.astrogrid.registry.result.version","0.10").equals("0.9")) {
153                 log.info("Creating v0.9 application registry");
154                 return new RegistryApplicationRegistry(endpoint);
155             } else {
156                 log.info("Creating v10 Application registry");
157                 return new RegistryV10ApplicationRegistry(endpoint);
158             }
159         }catch (PropertyNotFoundException e) {
160             log.info("No registry endpoint specified, attempting to let registry delegate configure itself");
161             if (conf.getString("org.astrogrid.registry.result.version","0.10").equals("0.9")) {
162                 log.info("Creating v0.9 application registry");
163                 return new RegistryApplicationRegistry();
164             } else {
165                 log.info("Creating v10 Application registry");
166                 return new RegistryV10ApplicationRegistry();
167             }
168         }
169     }
170 
171     /***
172      */
173     private WorkflowStore buildStore() throws Exception {
174         return new VoSpaceClientWorkflowStore();
175     }
176 
177 }
178 
179 
180 /* 
181 $Log: WorkflowManagerFactory.java,v $
182 Revision 1.12  2005/04/27 13:43:17  clq2
183 1082
184 
185 Revision 1.11.140.2  2005/04/22 11:01:27  nw
186 will do the right thing - create a v10 or v9 registry - in all the right places.
187 
188 Revision 1.11.140.1  2005/04/15 17:30:07  nw
189 added registry implementation for v10.
190 
191 Revision 1.11  2004/06/30 13:34:42  jl99
192 Reorg of import statements to cover removal of unused MySpace delegate classes
193 
194 Revision 1.10  2004/04/14 13:48:26  nw
195 updated docs
196 
197 Revision 1.9  2004/04/14 13:45:48  nw
198 implemented cut down workflow store interface over Ivo Delegate
199 
200 Revision 1.8  2004/04/14 13:05:20  nw
201 cut down workflow store interface. now to implement it.
202 
203 Revision 1.7  2004/04/14 13:02:57  nw
204 cut down workflow store interface. now to implement it.
205 
206 Revision 1.6  2004/03/15 17:01:01  nw
207 loosened type of endpoint for JesJobExecutionService from URL to String -
208 allows the dummy urn:test to be passed in.
209 
210 Revision 1.5  2004/03/11 13:53:36  nw
211 merged in branch bz#236 - implementation of interfaces
212 
213 Revision 1.4.4.1  2004/03/11 13:36:46  nw
214 tidied up interfaces, documented
215 
216 Revision 1.4  2004/03/03 11:15:23  nw
217 tarted up javadocs, reviewed types
218 
219 Revision 1.3  2004/03/01 15:03:38  nw
220 simplified by removing facade - will expose object model directly
221 
222 Revision 1.2  2004/02/25 10:57:43  nw
223 merged in branch nww-itn05-bz#140 (refactor in preparation for changing object model)
224 
225 Revision 1.1.2.1  2004/02/24 15:35:46  nw
226 extracted public interface from each implementation class.
227 altered types to reference interface rather than implementation whever possible.
228 added factory and manager facade at the front
229  
230 */