KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > InfoGlueWorkflowFactory


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.cms.util.workflow;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.infoglue.cms.controllers.kernel.impl.simple.WorkflowDefinitionController;
33 import org.infoglue.cms.entities.workflow.WorkflowDefinitionVO;
34 import org.infoglue.cms.util.CmsPropertyHandler;
35 import org.infoglue.deliver.util.CacheController;
36
37 import com.opensymphony.workflow.FactoryException;
38 import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
39 import com.opensymphony.workflow.loader.AbstractWorkflowFactory;
40 import com.opensymphony.workflow.loader.WorkflowDescriptor;
41 import com.opensymphony.workflow.loader.WorkflowLoader;
42
43
44 /**
45  * @author Mattias Bogeblad
46  */

47
48 public class InfoGlueWorkflowFactory extends AbstractWorkflowFactory
49 {
50     //protected Map workflows;
51
protected boolean reload;
52
53     public void setLayout(String JavaDoc workflowName, Object JavaDoc layout)
54     {
55     }
56
57     public Object JavaDoc getLayout(String JavaDoc workflowName)
58     {
59         return null;
60     }
61
62     public boolean removeWorkflow(String JavaDoc name) throws FactoryException
63     {
64         throw new FactoryException("remove workflow not supported");
65     }
66
67     public void renameWorkflow(String JavaDoc oldName, String JavaDoc newName)
68     {
69     }
70
71     public void save()
72     {
73     }
74
75     public boolean isModifiable(String JavaDoc name)
76     {
77         return true;
78     }
79
80     public String JavaDoc getName()
81     {
82         return "";
83     }
84
85     public WorkflowDescriptor getWorkflow(String JavaDoc name, boolean validate) throws FactoryException
86     {
87         Map JavaDoc workflows = (Map JavaDoc)CacheController.getCachedObject("workflowCache", "workflowMap");
88         
89         if(workflows == null)
90             initDone();
91
92         workflows = (Map JavaDoc)CacheController.getCachedObject("workflowCache", "workflowMap");
93
94         WorkflowConfig c = (WorkflowConfig) workflows.get(name);
95
96         if (c == null) {
97             throw new FactoryException("Unknown workflow name \"" + name + "\"");
98         }
99
100         if (c.descriptor != null)
101         {
102             loadWorkflow(c, validate);
103         }
104         else
105         {
106             loadWorkflow(c, validate);
107         }
108
109         c.descriptor.setName(name);
110
111         return c.descriptor;
112     }
113
114     public void reload() throws FactoryException
115     {
116         initDone();
117     }
118
119     public String JavaDoc[] getWorkflowNames()
120     {
121         Map JavaDoc workflows = (Map JavaDoc)CacheController.getCachedObject("workflowCache", "workflowMap");
122         
123         if(workflows == null)
124         {
125             try
126             {
127                 initDone();
128             }
129             catch (FactoryException e)
130             {
131                 e.printStackTrace();
132             }
133         }
134
135         workflows = (Map JavaDoc)CacheController.getCachedObject("workflowCache", "workflowMap");
136
137         int i = 0;
138         String JavaDoc[] res = new String JavaDoc[workflows.keySet().size()];
139         Iterator JavaDoc it = workflows.keySet().iterator();
140
141         while (it.hasNext())
142         {
143             res[i++] = (String JavaDoc) it.next();
144         }
145
146         return res;
147     }
148
149     public void createWorkflow(String JavaDoc name)
150     {
151         try
152         {
153             initDone();
154         }
155         catch(Exception JavaDoc e)
156         {
157             e.printStackTrace();
158         }
159     }
160
161     public void initDone() throws FactoryException
162     {
163         try
164         {
165             Map JavaDoc workflows = new HashMap JavaDoc();
166             
167             List JavaDoc list = WorkflowDefinitionController.getController().getWorkflowDefinitionVOList();
168
169             Iterator JavaDoc listIterator = list.iterator();
170             while(listIterator.hasNext())
171             {
172                 WorkflowDefinitionVO workflowDefinitionVO = (WorkflowDefinitionVO)listIterator.next();
173                 WorkflowConfig config = new WorkflowConfig(workflowDefinitionVO);
174                 workflows.put(workflowDefinitionVO.getName(), config);
175             }
176
177             CacheController.cacheObject("workflowCache", "workflowMap", workflows);
178         }
179         catch (Exception JavaDoc e)
180         {
181             throw new InvalidWorkflowDescriptorException("Error in workflow config", e);
182         }
183         
184         /*
185         reload = getProperties().getProperty("reload", "false").equals("true");
186
187         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
188         InputStream is = null;
189         String name = getProperties().getProperty("resource", "workflows.xml");
190
191         if ((name != null) && (name.indexOf(":/") > -1)) {
192             try {
193                 is = new URL(name).openStream();
194             } catch (Exception e) {
195             }
196         }
197
198         if (is == null) {
199             try {
200                 is = classLoader.getResourceAsStream(name);
201             } catch (Exception e) {
202             }
203         }
204
205         if (is == null) {
206             try {
207                 is = classLoader.getResourceAsStream("/" + name);
208             } catch (Exception e) {
209             }
210         }
211
212         if (is == null) {
213             try {
214                 is = classLoader.getResourceAsStream("META-INF/" + name);
215             } catch (Exception e) {
216             }
217         }
218
219         if (is == null) {
220             try {
221                 is = classLoader.getResourceAsStream("/META-INF/" + name);
222             } catch (Exception e) {
223             }
224         }
225
226         if (is == null) {
227             throw new FactoryException("Unable to find workflows file '" + name + "' in classpath");
228         }
229
230         try {
231             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
232             dbf.setNamespaceAware(true);
233
234             DocumentBuilder db;
235
236             try {
237                 db = dbf.newDocumentBuilder();
238             } catch (ParserConfigurationException e) {
239                 throw new FactoryException("Error creating document builder", e);
240             }
241
242             Document doc = db.parse(is);
243
244             Element root = (Element) doc.getElementsByTagName("workflows").item(0);
245             workflows = new HashMap();
246
247             List list = XMLUtil.getChildElements(root, "workflow");
248
249             for (int i = 0; i < list.size(); i++) {
250                 Element e = (Element) list.get(i);
251                 WorkflowConfig config = new WorkflowConfig(e.getAttribute("type"), e.getAttribute("location"));
252                 workflows.put(e.getAttribute("name"), config);
253             }
254         } catch (Exception e) {
255             throw new InvalidWorkflowDescriptorException("Error in workflow config", e);
256         }
257         */

258     }
259
260
261     public boolean saveWorkflow(String JavaDoc name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException
262     {
263         throw new FactoryException("Not supported...");
264     }
265
266
267     private void loadWorkflow(WorkflowConfig c, boolean validate) throws FactoryException
268     {
269         try
270         {
271             String JavaDoc encoding = CmsPropertyHandler.getWorkflowEncoding();
272             if(encoding == null || encoding.length() == 0 || encoding.equalsIgnoreCase("@workflowEncoding@"))
273                 encoding = "UTF-8";
274             
275             //System.out.println("c.workflowDefinitionVO.getValue():\n" + c.workflowDefinitionVO.getValue());
276
//c.descriptor = WorkflowLoader.load(new ByteArrayInputStream(c.workflowDefinitionVO.getValue().getBytes("ISO-8859-1")) , validate);
277
c.descriptor = WorkflowLoader.load(new ByteArrayInputStream JavaDoc(c.workflowDefinitionVO.getValue().getBytes(encoding)) , validate);
278         }
279         catch (Exception JavaDoc e)
280         {
281             throw new FactoryException("Error in workflow descriptor: " + c.workflowDefinitionVO.getName(), e);
282         }
283     }
284
285     //~ Inner Classes //////////////////////////////////////////////////////////
286

287     static class WorkflowConfig
288     {
289         WorkflowDescriptor descriptor;
290         WorkflowDefinitionVO workflowDefinitionVO;
291
292         public WorkflowConfig(WorkflowDefinitionVO workflowDefinitionVO)
293         {
294             this.workflowDefinitionVO = workflowDefinitionVO;
295         }
296     }
297
298 /*
299     public WorkflowDescriptor getWorkflow(String name) throws FactoryException
300     {
301         WorkflowConfig c = (WorkflowConfig) workflows.get(name);
302
303         if (c == null)
304         {
305             throw new FactoryException("Unknown workflow name \"" + name + "\"");
306         }
307
308         if (c.descriptor != null)
309         {
310             if (reload)
311             {
312                 File file = new File(c.url.getFile());
313
314                 if (file.exists() && (file.lastModified() > c.lastModified))
315                 {
316                     c.lastModified = file.lastModified();
317                     loadWorkflow(c);
318                 }
319             }
320         }
321         else
322         {
323             loadWorkflow(c);
324         }
325
326         return c.descriptor;
327     }
328
329     public String[] getWorkflowNames()
330     {
331         int i = 0;
332         String[] res = new String[workflows.keySet().size()];
333         Iterator it = workflows.keySet().iterator();
334
335         while (it.hasNext())
336         {
337             res[i++] = (String) it.next();
338         }
339
340         return res;
341     }
342
343     public void initDone() throws FactoryException
344     {
345         reload = getProperties().getProperty("reload", "false").equals("true");
346
347         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
348         InputStream is = null;
349         String name = getProperties().getProperty("resource", "workflows.xml");
350
351         if ((name != null) && (name.indexOf(":/") > -1))
352         {
353             try
354             {
355                 is = new URL(name).openStream();
356             }
357             catch (Exception e)
358             {
359             }
360         }
361
362         if (is == null)
363         {
364             try
365             {
366                 is = classLoader.getResourceAsStream(name);
367             }
368             catch (Exception e)
369             {
370             }
371         }
372
373         if (is == null)
374         {
375             try
376             {
377                 is = classLoader.getResourceAsStream("/" + name);
378             }
379             catch (Exception e)
380             {
381             }
382         }
383
384         if (is == null)
385         {
386             try
387             {
388                 is = classLoader.getResourceAsStream("META-INF/" + name);
389             }
390             catch (Exception e)
391             {
392             }
393         }
394
395         if (is == null)
396         {
397             try {
398                 is = classLoader.getResourceAsStream("/META-INF/" + name);
399             } catch (Exception e) {
400             }
401         }
402
403         try {
404             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
405             dbf.setNamespaceAware(true);
406
407             DocumentBuilder db = null;
408
409             try {
410                 db = dbf.newDocumentBuilder();
411             } catch (ParserConfigurationException e) {
412                 throw new FactoryException("Error creating document builder", e);
413             }
414
415             Document doc = db.parse(is);
416
417             Element root = (Element) doc.getElementsByTagName("workflows").item(0);
418             workflows = new HashMap();
419
420             List list = XMLUtil.getChildElements(root, "workflow");
421
422             for (int i = 0; i < list.size(); i++) {
423                 Element e = (Element) list.get(i);
424                 WorkflowConfig config = new WorkflowConfig(e.getAttribute("type"), e.getAttribute("location"));
425                 workflows.put(e.getAttribute("name"), config);
426             }
427         } catch (Exception e) {
428             throw new InvalidWorkflowDescriptorException("Error in workflow config", e);
429         }
430     }
431
432     public boolean removeWorkflow(String name) throws FactoryException
433     {
434         throw new FactoryException("remove workflow not supported");
435     }
436
437     public boolean saveWorkflow(String name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException
438     {
439         WorkflowConfig c = (WorkflowConfig) workflows.get(name);
440
441         if ((c != null) && !replace) {
442             return false;
443         }
444
445         if (c == null) {
446             throw new UnsupportedOperationException("Saving of new workflow is not currently supported");
447         }
448
449         Writer out;
450         descriptor.validate();
451
452         try {
453             out = new OutputStreamWriter(new FileOutputStream(c.url.getFile() + ".new"), "utf-8");
454         } catch (FileNotFoundException ex) {
455             throw new FactoryException("Could not create new file to save workflow " + c.url.getFile());
456         } catch (UnsupportedEncodingException ex) {
457             throw new FactoryException("utf-8 encoding not supported, contact your JVM vendor!");
458         }
459
460         writeXML(descriptor, out);
461
462         //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
463         //now lets rename
464         File original = new File(c.url.getFile());
465         File backup = new File(c.url.getFile() + ".bak");
466         File updated = new File(c.url.getFile() + ".new");
467         boolean isOK = !original.exists() || original.renameTo(backup);
468
469         if (!isOK) {
470             throw new FactoryException("Unable to backup original workflow file " + original + " to " + backup + ", aborting save");
471         }
472
473         isOK = updated.renameTo(original);
474
475         if (!isOK) {
476             throw new FactoryException("Unable to rename new workflow file " + updated + " to " + original + ", aborting save");
477         }
478
479         backup.delete();
480
481         return true;
482     }
483
484     protected void save()
485     {
486     }
487
488
489     private void loadWorkflow(WorkflowConfig c) throws FactoryException
490     {
491         try
492         {
493             c.descriptor = WorkflowLoader.load(c.url);
494         }
495         catch (Exception e)
496         {
497             throw new FactoryException("Error in workflow descriptor: " + c.url, e);
498         }
499     }
500
501
502     class WorkflowConfig
503     {
504         String location;
505         String type;
506         URL url;
507         WorkflowDescriptor descriptor;
508         long lastModified;
509
510         public WorkflowConfig(String type, String location)
511         {
512             if ("URL".equals(type)) {
513                 try {
514                     url = new URL(location);
515
516                     File file = new File(url.getFile());
517
518                     if (file.exists()) {
519                         lastModified = file.lastModified();
520                     }
521                 } catch (Exception ex) {
522                 }
523             } else if ("file".equals(type)) {
524                 try {
525                     File file = new File(location);
526                     url = file.toURL();
527                     lastModified = file.lastModified();
528                 } catch (Exception ex) {
529                 }
530             } else {
531                 url = Thread.currentThread().getContextClassLoader().getResource(location);
532             }
533
534             this.type = type;
535             this.location = location;
536         }
537     }
538     */

539
540 }
Popular Tags