View Javadoc

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