1 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 ; 17 18 import java.net.URL ; 19 20 import java.util.*; 21 22 import javax.xml.parsers.*; 23 24 25 31 public class DefaultConfiguration implements Configuration { 32 34 public static DefaultConfiguration INSTANCE = new DefaultConfiguration(); 35 36 38 private AbstractWorkflowFactory factory = new URLWorkflowFactory(); 39 private Map persistenceArgs = new HashMap(); 40 private String persistenceClass; 41 private transient WorkflowStore store = null; 42 private boolean initialized; 43 44 46 public boolean isInitialized() { 47 return initialized; 48 } 49 50 public void setPersistence(String persistence) { 51 persistenceClass = persistence; 52 } 53 54 public String getPersistence() { 55 return persistenceClass; 56 } 57 58 public Map getPersistenceArgs() { 59 return persistenceArgs; 60 } 61 62 public WorkflowDescriptor getWorkflow(String 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 [] getWorkflowNames() throws FactoryException { 73 return factory.getWorkflowNames(); 74 } 75 76 public WorkflowStore getWorkflowStore() throws StoreException { 77 if (store == null) { 78 String clazz = getPersistence(); 79 80 try { 81 store = (WorkflowStore) Class.forName(clazz).newInstance(); 82 } catch (Exception 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 url) throws FactoryException { 93 InputStream 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 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 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 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 e) { 156 throw new FactoryException("Error in workflow config", e); 157 } 158 } 159 160 public boolean removeWorkflow(String workflow) throws FactoryException { 161 return factory.removeWorkflow(workflow); 162 } 163 164 public boolean saveWorkflow(String name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException { 165 return factory.saveWorkflow(name, descriptor, replace); 166 } 167 168 177 protected InputStream getInputStream(URL url) { 178 InputStream is = null; 179 180 if (url != null) { 181 try { 182 is = url.openStream(); 183 } catch (Exception ex) { 184 } 185 } 186 187 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 188 189 if (is == null) { 190 try { 191 is = classLoader.getResourceAsStream("osworkflow.xml"); 192 } catch (Exception e) { 193 } 194 } 195 196 if (is == null) { 197 try { 198 is = classLoader.getResourceAsStream("/osworkflow.xml"); 199 } catch (Exception e) { 200 } 201 } 202 203 if (is == null) { 204 try { 205 is = classLoader.getResourceAsStream("META-INF/osworkflow.xml"); 206 } catch (Exception e) { 207 } 208 } 209 210 if (is == null) { 211 try { 212 is = classLoader.getResourceAsStream("/META-INF/osworkflow.xml"); 213 } catch (Exception e) { 214 } 215 } 216 217 return is; 218 } 219 220 224 AbstractWorkflowFactory getFactory() { 225 return factory; 226 } 227 } 228 | Popular Tags |