KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > par > ProcessArchiveDeployer


1 package org.jbpm.jpdl.par;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.util.zip.ZipInputStream JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.jbpm.db.JbpmSession;
10 import org.jbpm.db.JbpmSessionFactory;
11 import org.jbpm.graph.def.ProcessDefinition;
12
13 /**
14  * deploys process archives.
15  */

16 public class ProcessArchiveDeployer {
17   
18   JbpmSessionFactory jbpmSessionFactory = null;
19   
20   // constructors /////////////////////////////////////////////////////////////
21

22   public ProcessArchiveDeployer() {
23     jbpmSessionFactory = JbpmSessionFactory.buildJbpmSessionFactory();
24   }
25   
26   public ProcessArchiveDeployer(JbpmSessionFactory jbpmSessionFactory) {
27     this.jbpmSessionFactory = jbpmSessionFactory;
28   }
29
30   // deployer methods /////////////////////////////////////////////////////////
31

32   public static void deployResource(String JavaDoc parResource) {
33     deployResource(parResource, JbpmSessionFactory.getInstance());
34   }
35
36   public static void deployResource(String JavaDoc parResource, JbpmSessionFactory jbpmSessionFactory) {
37     ProcessDefinition processDefinition = ProcessDefinition.parseParResource(parResource);
38     deployProcessDefinition(processDefinition, jbpmSessionFactory);
39   }
40
41   public static void deployZipInputStream(ZipInputStream JavaDoc zipInputStream) {
42     deployZipInputStream(zipInputStream, JbpmSessionFactory.getInstance());
43   }
44
45   public static void deployZipInputStream(ZipInputStream JavaDoc zipInputStream, JbpmSessionFactory jbpmSessionFactory) {
46     ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
47     deployProcessDefinition(processDefinition, jbpmSessionFactory);
48   }
49
50   public static void deployProcessDefinition(ProcessDefinition processDefinition) {
51     deployProcessDefinition(processDefinition, JbpmSessionFactory.getInstance());
52   }
53
54   public static void deployProcessDefinition(ProcessDefinition processDefinition, JbpmSessionFactory jbpmSessionFactory) {
55     JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
56     try {
57       log.debug("starting transaction to deploy process "+processDefinition);
58       jbpmSession.beginTransaction();
59       
60       // assign the version number
61
String JavaDoc processDefinitionName = processDefinition.getName();
62       // if the process definition has a name (process versioning only applies to named process definitions)
63
if (processDefinitionName!=null) {
64         // find the current latest process definition
65
ProcessDefinition previousLatestVersion = jbpmSession.getGraphSession().findLatestProcessDefinition(processDefinitionName);
66         // if there is a current latest process definition
67
if (previousLatestVersion!=null) {
68           // take the next version number
69
processDefinition.setVersion( previousLatestVersion.getVersion()+1 );
70         } else {
71           // start from 1
72
processDefinition.setVersion(1);
73         }
74       }
75       
76       // save the process definition in the database
77
jbpmSession.getGraphSession().saveProcessDefinition(processDefinition);
78       
79       log.debug("flushing...");
80       jbpmSession.getSession().flush();
81       
82       log.debug("committing transaction to deploy process "+processDefinition);
83       jbpmSession.commitTransaction();
84     } finally {
85       jbpmSession.close();
86     }
87   }
88   
89   public static void main(String JavaDoc[] args) {
90     if ( (args!=null)
91          && (args.length>0) ) {
92       for (int i=0; i<args.length; i++) {
93         try {
94           deployZipInputStream(new ZipInputStream JavaDoc(new FileInputStream JavaDoc(new File JavaDoc(args[i]))));
95         } catch (Exception JavaDoc e) {
96           e.printStackTrace();
97         }
98       }
99     }
100   }
101
102   private static final Log log = LogFactory.getLog(ProcessArchiveDeployer.class);
103 }
104
Popular Tags