KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > identity > ant > LoadIdentitiesTask


1 package org.jbpm.identity.ant;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6
7 import org.apache.tools.ant.BuildException;
8 import org.apache.tools.ant.Task;
9 import org.hibernate.Session;
10 import org.jbpm.ant.AntTaskJbpmSessionFactory;
11 import org.jbpm.db.JbpmSession;
12 import org.jbpm.db.JbpmSessionFactory;
13 import org.jbpm.identity.Entity;
14 import org.jbpm.identity.hibernate.IdentitySession;
15 import org.jbpm.identity.xml.IdentityXmlParser;
16
17 public class LoadIdentitiesTask extends Task {
18
19   private String JavaDoc file = null;
20   private String JavaDoc cfg = null;
21   private String JavaDoc properties = null;
22
23   public void execute() throws BuildException {
24     // get the JbpmSessionFactory
25
JbpmSessionFactory jbpmSessionFactory = AntTaskJbpmSessionFactory.getJbpmSessionFactory(cfg,properties);
26     
27     // if attribute file is set, deploy that file file
28
if (file==null) throw new BuildException("no file specified in the loadidentities task");
29
30     log( "loading identities from "+file+" ..." );
31     File JavaDoc identitiesFile = new File JavaDoc(file);
32     FileInputStream JavaDoc fileInputStream = null;
33     try {
34       fileInputStream = new FileInputStream JavaDoc(identitiesFile);
35     } catch (FileNotFoundException JavaDoc e) {
36       throw new BuildException("identities file '"+file+"' not found");
37     }
38     Entity[] entities = IdentityXmlParser.parseEntitiesResource(fileInputStream);
39     
40     JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSessionAndBeginTransaction();
41     Session session = jbpmSession.getSession();
42     IdentitySession identitySession = new IdentitySession(session);
43     try {
44       for (int i=0; i<entities.length; i++) {
45         identitySession.saveEntity(entities[i]);
46       }
47     } finally {
48       jbpmSession.commitTransactionAndClose();
49     }
50   }
51
52   public void setFile(String JavaDoc file) {
53     this.file = file;
54   }
55   public void setCfg(String JavaDoc cfg) {
56     this.cfg = cfg;
57   }
58   public void setProperties(String JavaDoc properties) {
59     this.properties = properties;
60   }
61 }
62
Popular Tags