KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > config > DefaultConfiguration


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.config;
6
7 import com.opensymphony.util.ClassLoaderUtil;
8
9 import com.opensymphony.workflow.FactoryException;
10 import com.opensymphony.workflow.StoreException;
11 import com.opensymphony.workflow.loader.*;
12 import com.opensymphony.workflow.spi.WorkflowStore;
13
14 import org.w3c.dom.*;
15
16 import java.io.InputStream JavaDoc;
17
18 import java.net.URL JavaDoc;
19
20 import java.util.*;
21
22 import javax.xml.parsers.*;
23
24
25 /**
26  * DOCUMENT ME!
27  *
28  * @author $author$
29  * @version $Revision: 1.6 $
30  */

31 public class DefaultConfiguration implements Configuration {
32     //~ Static fields/initializers /////////////////////////////////////////////
33

34     public static DefaultConfiguration INSTANCE = new DefaultConfiguration();
35
36     //~ Instance fields ////////////////////////////////////////////////////////
37

38     private AbstractWorkflowFactory factory = new URLWorkflowFactory();
39     private Map persistenceArgs = new HashMap();
40     private String JavaDoc persistenceClass;
41     private transient WorkflowStore store = null;
42     private boolean initialized;
43
44     //~ Methods ////////////////////////////////////////////////////////////////
45

46     public boolean isInitialized() {
47         return initialized;
48     }
49
50     public void setPersistence(String JavaDoc persistence) {
51         persistenceClass = persistence;
52     }
53
54     public String JavaDoc getPersistence() {
55         return persistenceClass;
56     }
57
58     public Map getPersistenceArgs() {
59         return persistenceArgs;
60     }
61
62     public WorkflowDescriptor getWorkflow(String JavaDoc name) throws FactoryException {
63         WorkflowDescriptor workflow = factory.getWorkflow(name);
64
65         if (workflow == null) {
66             throw new FactoryException("Unknown workflow name");
67         }
68
69         return workflow;
70     }
71
72     public String JavaDoc[] getWorkflowNames() throws FactoryException {
73         return factory.getWorkflowNames();
74     }
75
76     public WorkflowStore getWorkflowStore() throws StoreException {
77         if (store == null) {
78             String JavaDoc clazz = getPersistence();
79
80             try {
81                 store = (WorkflowStore) Class.forName(clazz).newInstance();
82             } catch (Exception JavaDoc ex) {
83                 throw new StoreException("Error creating store", ex);
84             }
85
86             store.init(getPersistenceArgs());
87         }
88
89         return store;
90     }
91
92     public void load(URL JavaDoc url) throws FactoryException {
93         InputStream JavaDoc is = getInputStream(url);
94
95         try {
96             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
97             dbf.setNamespaceAware(true);
98
99             DocumentBuilder db = null;
100
101             try {
102                 db = dbf.newDocumentBuilder();
103             } catch (ParserConfigurationException e) {
104                 throw new FactoryException("Error creating document builder", e);
105             }
106
107             Document doc = db.parse(is);
108
109             Element root = (Element) doc.getElementsByTagName("osworkflow").item(0);
110             Element p = XMLUtil.getChildElement(root, "persistence");
111             Element factoryElement = XMLUtil.getChildElement(root, "factory");
112
113             persistenceClass = p.getAttribute("class");
114
115             List args = XMLUtil.getChildElements(p, "property");
116
117             //persistenceArgs = new HashMap();
118
for (int i = 0; i < args.size(); i++) {
119                 Element e = (Element) args.get(i);
120                 persistenceArgs.put(e.getAttribute("key"), e.getAttribute("value"));
121             }
122
123             if (factoryElement != null) {
124                 String JavaDoc clazz = null;
125
126                 try {
127                     clazz = factoryElement.getAttribute("class");
128
129                     if (clazz == null) {
130                         throw new FactoryException("factory does not specify a class attribute");
131                     }
132
133                     factory = (AbstractWorkflowFactory) ClassLoaderUtil.loadClass(clazz, getClass()).newInstance();
134
135                     Properties properties = new Properties();
136                     List props = XMLUtil.getChildElements(factoryElement, "property");
137
138                     for (int i = 0; i < props.size(); i++) {
139                         Element e = (Element) props.get(i);
140                         properties.setProperty(e.getAttribute("key"), e.getAttribute("value"));
141                     }
142
143                     factory.init(properties);
144                     factory.initDone();
145                 } catch (FactoryException ex) {
146                     throw ex;
147                 } catch (Exception JavaDoc ex) {
148                     throw new FactoryException("Error creating workflow factory " + clazz, ex);
149                 }
150             }
151
152             initialized = true;
153         } catch (FactoryException e) {
154             throw e;
155         } catch (Exception JavaDoc e) {
156             throw new FactoryException("Error in workflow config", e);
157         }
158     }
159
160     public boolean removeWorkflow(String JavaDoc workflow) throws FactoryException {
161         return factory.removeWorkflow(workflow);
162     }
163
164     public boolean saveWorkflow(String JavaDoc name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException {
165         return factory.saveWorkflow(name, descriptor, replace);
166     }
167
168     /**
169      * Load the default configuration from the current context classloader.
170      * The search order is:
171      * <li>Specified URL</li>
172      * <li>osworkflow.xml</li>
173      * <li>/osworkflow.xml</li>
174      * <li>META-INF/osworkflow.xml</li>
175      * <li>/META-INF/osworkflow.xml</li>
176      */

177     protected InputStream JavaDoc getInputStream(URL JavaDoc url) {
178         InputStream JavaDoc is = null;
179
180         if (url != null) {
181             try {
182                 is = url.openStream();
183             } catch (Exception JavaDoc ex) {
184             }
185         }
186
187         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
188
189         if (is == null) {
190             try {
191                 is = classLoader.getResourceAsStream("osworkflow.xml");
192             } catch (Exception JavaDoc e) {
193             }
194         }
195
196         if (is == null) {
197             try {
198                 is = classLoader.getResourceAsStream("/osworkflow.xml");
199             } catch (Exception JavaDoc e) {
200             }
201         }
202
203         if (is == null) {
204             try {
205                 is = classLoader.getResourceAsStream("META-INF/osworkflow.xml");
206             } catch (Exception JavaDoc e) {
207             }
208         }
209
210         if (is == null) {
211             try {
212                 is = classLoader.getResourceAsStream("/META-INF/osworkflow.xml");
213             } catch (Exception JavaDoc e) {
214             }
215         }
216
217         return is;
218     }
219
220     /**
221      * Get the workflow factory for this configuration.
222      * This method should never ever be called from client code!
223      */

224     AbstractWorkflowFactory getFactory() {
225         return factory;
226     }
227 }
228
Popular Tags