1 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 32 public class SerializableWorkflowStore extends MemoryWorkflowStore { 33 35 protected static final Log log = LogFactory.getLog(SerializableWorkflowStore.class); 36 static String storeFile; 37 38 40 public PropertySet getPropertySet(long entryId) { 41 PropertySet ps = (PropertySet) SerializableCache.getInstance().propertySetCache.get(new Long (entryId)); 42 43 if (ps == null) { 44 ps = PropertySetManager.getInstance("serializable", null); 45 SerializableCache.getInstance().propertySetCache.put(new Long (entryId), ps); 46 } 47 48 return ps; 49 } 50 51 public static void setStoreFile(String storeFile) { 52 SerializableWorkflowStore.storeFile = storeFile; 53 } 54 55 public static String getStoreFile() { 56 return storeFile; 57 } 58 59 public Step createCurrentStep(long entryId, int stepId, String owner, Date startDate, Date dueDate, String 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 (entryId)); 64 65 if (currentSteps == null) { 66 currentSteps = new ArrayList(); 67 SerializableCache.getInstance().currentStepsCache.put(new Long (entryId), currentSteps); 68 } 69 70 currentSteps.add(step); 71 SerializableCache.store(); 72 73 return step; 74 } 75 76 public WorkflowEntry createEntry(String workflowName) { 77 long id = SerializableCache.getInstance().globalEntryId++; 78 SimpleWorkflowEntry entry = new SimpleWorkflowEntry(id, workflowName, WorkflowEntry.CREATED); 79 SerializableCache.getInstance().entryCache.put(new Long (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 (entryId)); 87 88 if (currentSteps == null) { 89 currentSteps = new ArrayList(); 90 SerializableCache.getInstance().currentStepsCache.put(new Long (entryId), currentSteps); 91 } 92 93 return currentSteps; 94 } 95 96 public WorkflowEntry findEntry(long entryId) { 97 return (WorkflowEntry) SerializableCache.getInstance().entryCache.get(new Long (entryId)); 98 } 99 100 public List findHistorySteps(long entryId) { 101 List historySteps = (List) SerializableCache.getInstance().historyStepsCache.get(new Long (entryId)); 102 103 if (historySteps == null) { 104 historySteps = new ArrayList(); 105 SerializableCache.getInstance().historyStepsCache.put(new Long (entryId), historySteps); 106 } 107 108 return historySteps; 109 } 110 111 public void init(Map props) { 112 storeFile = (String ) props.get("storeFile"); 113 114 if (!new File(storeFile).isFile()) { 116 log.fatal("storePath property should indicate a normal file"); 117 } 118 119 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 status, String caller) { 126 List currentSteps = (List) SerializableCache.getInstance().currentStepsCache.get(new Long (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 (step.getEntryId())); 148 149 List historySteps = (List) SerializableCache.getInstance().historyStepsCache.get(new Long (step.getEntryId())); 150 151 if (historySteps == null) { 152 historySteps = new ArrayList(); 153 SerializableCache.getInstance().historyStepsCache.put(new Long (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 177 private static transient SerializableCache instance; 178 179 181 HashMap currentStepsCache; 182 HashMap entryCache; 183 HashMap historyStepsCache; 184 HashMap propertySetCache; 185 long globalEntryId = 1; 186 long globalStepId = 1; 187 188 190 private SerializableCache() { 191 entryCache = new HashMap(); 192 currentStepsCache = new HashMap(); 193 historyStepsCache = new HashMap(); 194 propertySetCache = new HashMap(); 195 } 196 197 199 public List query(WorkflowQuery query) { 200 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 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 e) { 234 SerializableWorkflowStore.log.fatal("cannot store in file " + SerializableWorkflowStore.storeFile + "."); 235 } 236 } 237 } 238 | Popular Tags |