1 5 package com.opensymphony.workflow.loader; 6 7 import com.opensymphony.workflow.FactoryException; 8 9 import java.io.*; 10 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 18 23 public class URLWorkflowFactory extends AbstractWorkflowFactory { 24 26 private Map cache = new HashMap (); 27 28 30 public WorkflowDescriptor getWorkflow(String name) throws FactoryException { 31 boolean useCache = getProperties().getProperty("cache", "false").equals("true"); 32 33 if (useCache) { 34 WorkflowDescriptor descriptor = (WorkflowDescriptor) cache.get(name); 35 36 if (descriptor != null) { 37 return descriptor; 38 } 39 } 40 41 try { 42 URL url = new URL (name); 43 WorkflowDescriptor descriptor = WorkflowLoader.load(url); 44 45 if (useCache) { 46 cache.put(name, descriptor); 47 } 48 49 return descriptor; 50 } catch (Exception e) { 51 throw new FactoryException("Unable to find workflow " + name, e); 52 } 53 } 54 55 public String [] getWorkflowNames() throws FactoryException { 56 throw new FactoryException("URLWorkflowFactory does not contain a list of workflow names"); 57 } 58 59 public boolean removeWorkflow(String name) throws FactoryException { 60 throw new FactoryException("remove workflow not supported"); 61 } 62 63 public boolean saveWorkflow(String name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException { 64 WorkflowDescriptor c = (WorkflowDescriptor) cache.get(name); 65 URL url = null; 66 67 try { 68 url = new URL (name); 69 } catch (MalformedURLException ex) { 70 throw new FactoryException("workflow '" + name + "' is an invalid url:" + ex); 71 } 72 73 boolean useCache = getProperties().getProperty("cache", "false").equals("true"); 74 75 if (useCache && (c != null) && !replace) { 76 return false; 77 } 78 79 if (new File(url.getFile()).exists() && !replace) { 80 return false; 81 } 82 83 Writer out = null; 84 85 try { 86 out = new OutputStreamWriter(new FileOutputStream(url.getFile() + ".new"), "utf-8"); 87 } catch (FileNotFoundException ex) { 88 throw new FactoryException("Could not create new file to save workflow " + url.getFile()); 89 } catch (UnsupportedEncodingException ex) { 90 throw new FactoryException("utf-8 encoding not supported, contact your JVM vendor!"); 91 } 92 93 PrintWriter writer = new PrintWriter(new BufferedWriter(out)); 95 writer.println(WorkflowDescriptor.XML_HEADER); 96 writer.println(WorkflowDescriptor.DOCTYPE_DECL); 97 descriptor.writeXML(writer, 0); 98 writer.flush(); 99 writer.close(); 100 101 File original = new File(url.getFile()); 103 File backup = new File(url.getFile() + ".bak"); 104 File updated = new File(url.getFile() + ".new"); 105 boolean isOK = original.renameTo(backup); 106 107 if (!isOK) { 108 throw new FactoryException("Unable to backup original workflow file " + original + " to " + backup + ", aborting save"); 109 } 110 111 isOK = updated.renameTo(original); 112 113 if (!isOK) { 114 throw new FactoryException("Unable to rename new workflow file " + updated + " to " + original + ", aborting save"); 115 } 116 117 backup.delete(); 118 119 if (useCache) { 120 cache.put(name, descriptor); 121 } 122 123 return true; 124 } 125 } 126 | Popular Tags |