KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > spi > memory > SerializableWorkflowStore


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

5 package com.opensymphony.workflow.spi.memory;
6
7 import com.opensymphony.module.propertyset.PropertySet;
8 import com.opensymphony.module.propertyset.PropertySetManager;
9
10 import com.opensymphony.workflow.query.WorkflowQuery;
11 import com.opensymphony.workflow.spi.*;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import java.io.*;
17
18 import java.util.*;
19
20
21 /**
22  * Simple flat file implementation.
23  *
24  * Following properties are <b>required</b>:
25  * <ul>
26  * <li><b>storeFile</b> - the absolute path to the store file
27  (<i>ex:c:\workflow.store</i>)</li>
28  * </ul>
29  *
30  * @author <a HREF="mailto:gbort@msn.com">Guillaume Bort</a>
31  */

32 public class SerializableWorkflowStore extends MemoryWorkflowStore {
33     //~ Static fields/initializers /////////////////////////////////////////////
34

35     protected static final Log log = LogFactory.getLog(SerializableWorkflowStore.class);
36     static String JavaDoc storeFile;
37
38     //~ Methods ////////////////////////////////////////////////////////////////
39

40     public PropertySet getPropertySet(long entryId) {
41         PropertySet ps = (PropertySet) SerializableCache.getInstance().propertySetCache.get(new Long JavaDoc(entryId));
42
43         if (ps == null) {
44             ps = PropertySetManager.getInstance("serializable", null);
45             SerializableCache.getInstance().propertySetCache.put(new Long JavaDoc(entryId), ps);
46         }
47
48         return ps;
49     }
50
51     public static void setStoreFile(String JavaDoc storeFile) {
52         SerializableWorkflowStore.storeFile = storeFile;
53     }
54
55     public static String JavaDoc getStoreFile() {
56         return storeFile;
57     }
58
59     public Step createCurrentStep(long entryId, int stepId, String JavaDoc owner, Date startDate, Date dueDate, String JavaDoc status, long[] previousIds) {
60         long id = SerializableCache.getInstance().globalStepId++;
61         SimpleStep step = new SimpleStep(id, entryId, stepId, 0, owner, startDate, dueDate, null, status, previousIds, null);
62
63         List currentSteps = (List) SerializableCache.getInstance().currentStepsCache.get(new Long JavaDoc(entryId));
64
65         if (currentSteps == null) {
66             currentSteps = new ArrayList();
67             SerializableCache.getInstance().currentStepsCache.put(new Long JavaDoc(entryId), currentSteps);
68         }
69
70         currentSteps.add(step);
71         SerializableCache.store();
72
73         return step;
74     }
75
76     public WorkflowEntry createEntry(String JavaDoc workflowName) {
77         long id = SerializableCache.getInstance().globalEntryId++;
78         SimpleWorkflowEntry entry = new SimpleWorkflowEntry(id, workflowName, WorkflowEntry.CREATED);
79         SerializableCache.getInstance().entryCache.put(new Long JavaDoc(id), entry);
80         SerializableCache.store();
81
82         return entry;
83     }
84
85     public List findCurrentSteps(long entryId) {
86         List currentSteps = (List) SerializableCache.getInstance().currentStepsCache.get(new Long JavaDoc(entryId));
87
88         if (currentSteps == null) {
89             currentSteps = new ArrayList();
90             SerializableCache.getInstance().currentStepsCache.put(new Long JavaDoc(entryId), currentSteps);
91         }
92
93         return currentSteps;
94     }
95
96     public WorkflowEntry findEntry(long entryId) {
97         return (WorkflowEntry) SerializableCache.getInstance().entryCache.get(new Long JavaDoc(entryId));
98     }
99
100     public List findHistorySteps(long entryId) {
101         List historySteps = (List) SerializableCache.getInstance().historyStepsCache.get(new Long JavaDoc(entryId));
102
103         if (historySteps == null) {
104             historySteps = new ArrayList();
105             SerializableCache.getInstance().historyStepsCache.put(new Long JavaDoc(entryId), historySteps);
106         }
107
108         return historySteps;
109     }
110
111     public void init(Map props) {
112         storeFile = (String JavaDoc) props.get("storeFile");
113
114         // check whether the file denoted by the storeFile property is a normal file.
115
if (!new File(storeFile).isFile()) {
116             log.fatal("storePath property should indicate a normal file");
117         }
118
119         // check wheter the directory containing the storeFile exist
120
if (!new File(storeFile).getParentFile().exists()) {
121             log.fatal("directory " + new File(storeFile).getParent() + " not found");
122         }
123     }
124
125     public Step markFinished(Step step, int actionId, Date finishDate, String JavaDoc status, String JavaDoc caller) {
126         List currentSteps = (List) SerializableCache.getInstance().currentStepsCache.get(new Long JavaDoc(step.getEntryId()));
127
128         for (Iterator iterator = currentSteps.iterator(); iterator.hasNext();) {
129             SimpleStep theStep = (SimpleStep) iterator.next();
130
131             if (theStep.getId() == step.getId()) {
132                 theStep.setStatus(status);
133                 theStep.setActionId(actionId);
134                 theStep.setFinishDate(finishDate);
135                 theStep.setCaller(caller);
136
137                 return theStep;
138             }
139         }
140
141         SerializableCache.store();
142
143         return null;
144     }
145
146     public void moveToHistory(Step step) {
147         List currentSteps = (List) SerializableCache.getInstance().currentStepsCache.get(new Long JavaDoc(step.getEntryId()));
148
149         List historySteps = (List) SerializableCache.getInstance().historyStepsCache.get(new Long JavaDoc(step.getEntryId()));
150
151         if (historySteps == null) {
152             historySteps = new ArrayList();
153             SerializableCache.getInstance().historyStepsCache.put(new Long JavaDoc(step.getEntryId()), historySteps);
154         }
155
156         SimpleStep simpleStep = (SimpleStep) step;
157
158         for (Iterator iterator = currentSteps.iterator(); iterator.hasNext();) {
159             Step currentStep = (Step) iterator.next();
160
161             if (simpleStep.getId() == currentStep.getId()) {
162                 iterator.remove();
163                 historySteps.add(simpleStep);
164
165                 break;
166             }
167         }
168
169         SerializableCache.store();
170     }
171 }
172
173
174 class SerializableCache implements Serializable {
175     //~ Static fields/initializers /////////////////////////////////////////////
176

177     private static transient SerializableCache instance;
178
179     //~ Instance fields ////////////////////////////////////////////////////////
180

181     HashMap currentStepsCache;
182     HashMap entryCache;
183     HashMap historyStepsCache;
184     HashMap propertySetCache;
185     long globalEntryId = 1;
186     long globalStepId = 1;
187
188     //~ Constructors ///////////////////////////////////////////////////////////
189

190     private SerializableCache() {
191         entryCache = new HashMap();
192         currentStepsCache = new HashMap();
193         historyStepsCache = new HashMap();
194         propertySetCache = new HashMap();
195     }
196
197     //~ Methods ////////////////////////////////////////////////////////////////
198

199     public List query(WorkflowQuery query) {
200         // not implemented
201
return Collections.EMPTY_LIST;
202     }
203
204     static SerializableCache getInstance() {
205         if (instance == null) {
206             instance = load();
207         }
208
209         return instance;
210     }
211
212     static SerializableCache load() {
213         try {
214             FileInputStream fis = new FileInputStream(new File(SerializableWorkflowStore.storeFile));
215             ObjectInputStream ois = new ObjectInputStream(fis);
216             SerializableCache o = (SerializableCache) ois.readObject();
217             fis.close();
218
219             return o;
220         } catch (Exception JavaDoc e) {
221             SerializableWorkflowStore.log.fatal("cannot store in file " + SerializableWorkflowStore.storeFile + ". Create a new blank store.");
222         }
223
224         return new SerializableCache();
225     }
226
227     static void store() {
228         try {
229             FileOutputStream fos = new FileOutputStream(new File(SerializableWorkflowStore.storeFile));
230             ObjectOutputStream oos = new ObjectOutputStream(fos);
231             oos.writeObject(getInstance());
232             fos.close();
233         } catch (Exception JavaDoc e) {
234             SerializableWorkflowStore.log.fatal("cannot store in file " + SerializableWorkflowStore.storeFile + ".");
235         }
236     }
237 }
238
Popular Tags