KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > loader > URLWorkflowFactory


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

5 package com.opensymphony.workflow.loader;
6
7 import com.opensymphony.workflow.FactoryException;
8
9 import java.io.*;
10
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * @author Hani Suleiman
20  * Date: May 10, 2002
21  * Time: 11:59:47 AM
22  */

23 public class URLWorkflowFactory extends AbstractWorkflowFactory {
24     //~ Instance fields ////////////////////////////////////////////////////////
25

26     private Map JavaDoc cache = new HashMap JavaDoc();
27
28     //~ Methods ////////////////////////////////////////////////////////////////
29

30     public WorkflowDescriptor getWorkflow(String JavaDoc 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 JavaDoc url = new URL JavaDoc(name);
43             WorkflowDescriptor descriptor = WorkflowLoader.load(url);
44
45             if (useCache) {
46                 cache.put(name, descriptor);
47             }
48
49             return descriptor;
50         } catch (Exception JavaDoc e) {
51             throw new FactoryException("Unable to find workflow " + name, e);
52         }
53     }
54
55     public String JavaDoc[] getWorkflowNames() throws FactoryException {
56         throw new FactoryException("URLWorkflowFactory does not contain a list of workflow names");
57     }
58
59     public boolean removeWorkflow(String JavaDoc name) throws FactoryException {
60         throw new FactoryException("remove workflow not supported");
61     }
62
63     public boolean saveWorkflow(String JavaDoc name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException {
64         WorkflowDescriptor c = (WorkflowDescriptor) cache.get(name);
65         URL JavaDoc url = null;
66
67         try {
68             url = new URL JavaDoc(name);
69         } catch (MalformedURLException JavaDoc 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         //write it out to a new file, to ensure we don't end up with a messed up file if we're interrupted halfway for some reason
94
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         //now lets rename
102
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