KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > identity > assignment > ExpressionAssignmentHandler


1 package org.jbpm.identity.assignment;
2
3 import java.util.Set JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.jbpm.db.JbpmSession;
8 import org.jbpm.graph.exe.ExecutionContext;
9 import org.jbpm.graph.exe.Token;
10 import org.jbpm.identity.Entity;
11 import org.jbpm.identity.Group;
12 import org.jbpm.identity.User;
13 import org.jbpm.identity.hibernate.IdentitySession;
14 import org.jbpm.security.Authentication;
15 import org.jbpm.taskmgmt.def.AssignmentHandler;
16 import org.jbpm.taskmgmt.exe.Assignable;
17 import org.jbpm.taskmgmt.exe.SwimlaneInstance;
18
19 /**
20  * implements an expression language for assigning actors to tasks based
21  * on this identity component.
22  *
23  * <pre>syntax : first-term --> next-term --> next-term --> ... --> next-term
24  *
25  * first-term ::= previous |
26  * swimlane(swimlane-name) |
27  * variable(variable-name) |
28  * user(user-name) |
29  * group(group-name)
30  *
31  * next-term ::= group(group-type) |
32  * member(role-name)
33  * </pre>
34  */

35 public class ExpressionAssignmentHandler implements AssignmentHandler {
36
37   private static final long serialVersionUID = 1L;
38   
39   protected String JavaDoc expression;
40   protected ExecutionContext executionContext = null;
41   protected ExpressionSession expressionSession = null;
42   protected TermTokenizer tokenizer;
43   protected Entity entity = null;
44
45   public void assign(Assignable assignable, ExecutionContext executionContext) {
46
47     try {
48       expressionSession = getExpressionSession();
49       if (expressionSession==null) {
50         throw new NullPointerException JavaDoc("getIdentitySession returned null");
51       }
52       this.tokenizer = new TermTokenizer(expression);
53       this.executionContext = executionContext;
54       entity = resolveFirstTerm(tokenizer.nextTerm());
55       while( tokenizer.hasMoreTerms()
56              && (entity!=null) ) {
57         entity = resolveNextTerm(tokenizer.nextTerm());
58       }
59       // if the expression did not resolve to an actor
60
if (entity==null) {
61         // throw an exception
62
throw new RuntimeException JavaDoc("couldn't resolve assignment expression '"+expression+"'");
63         
64       // else if the expression evaluated to a user
65
} else if (entity instanceof User) {
66         // do direct assignment
67
assignable.setActorId(entity.getName());
68         
69       // else if the expression evaluated to a group
70
} else if (entity instanceof Group) {
71         // put the group in the pool
72
assignable.setPooledActors(new String JavaDoc []{entity.getName()});
73       }
74       
75     } catch (RuntimeException JavaDoc e) {
76       throw new ExpressionAssignmentException("couldn't resolve assignment expression '"+expression+"'", e);
77     }
78   }
79
80   /**
81    * serves as a hook for customizing the way the identity session is retrieved.
82    * overload this method to reuse this expression assignment handler for your
83    * user data store.
84    */

85   protected ExpressionSession getExpressionSession() {
86     JbpmSession currentJbpmSession = JbpmSession.getCurrentJbpmSession();
87     if ( (currentJbpmSession==null)
88          || (currentJbpmSession.getSession()==null)
89          || (! currentJbpmSession.getSession().isOpen())
90        ) {
91       throw new RuntimeException JavaDoc("no active JbpmSession for resolving assignment expression'"+expression+"'");
92     }
93     return new IdentitySession(currentJbpmSession.getSession());
94   }
95
96   protected Entity resolveFirstTerm(String JavaDoc term) {
97     Entity entity = null;
98     
99     log.debug("resolving first term '"+term+"'");
100     
101     if (term.equalsIgnoreCase("previous")) {
102     String JavaDoc userName = Authentication.getAuthenticatedActorId();
103     entity = getUserByName(userName);
104     
105     } else if ( (term.startsWith("swimlane("))
106          && (term.endsWith(")")) ) {
107       String JavaDoc swimlaneName = term.substring(9,term.length()-1).trim();
108       String JavaDoc userName = getSwimlaneActorId(swimlaneName);
109       entity = getUserByName(userName);
110
111     } else if ( (term.startsWith("variable("))
112                 && (term.endsWith(")")) ) {
113       String JavaDoc variableName = term.substring(9,term.length()-1).trim();
114       Object JavaDoc value = getVariable(variableName);
115       
116       if (value==null) {
117         throw new ExpressionAssignmentException("variable '"+variableName+"' is null");
118         
119       } else if (value instanceof String JavaDoc) {
120         entity = getUserByName((String JavaDoc) value);
121         
122       } else if (value instanceof Entity) {
123         entity = (Entity) value;
124       }
125
126     } else if ( (term.startsWith("user("))
127                 && (term.endsWith(")")) ) {
128       String JavaDoc userName = term.substring(5,term.length()-1).trim();
129       entity = getUserByName(userName);
130
131     } else if ( (term.startsWith("group("))
132                 && (term.endsWith(")")) ) {
133       String JavaDoc groupName = term.substring(6,term.length()-1).trim();
134       entity = getGroupByName(groupName);
135
136     } else {
137       throw new ExpressionAssignmentException("couldn't interpret first term in expression '"+expression+"'");
138     }
139
140     return entity;
141   }
142
143   protected Entity resolveNextTerm(String JavaDoc term) {
144
145     log.debug("resolving next term '"+term+"'");
146
147     if ( (term.startsWith("group("))
148             && (term.endsWith(")")) ) {
149       String JavaDoc groupType = term.substring(6,term.length()-1).trim();
150       User user = (User) entity;
151       Set JavaDoc groups = user.getGroupsForGroupType(groupType);
152       if (groups.size()==0) {
153         throw new ExpressionAssignmentException("no groups for group-type '"+groupType+"'");
154       }
155       entity = (Entity) groups.iterator().next();
156       
157     } else if ( (term.startsWith("member("))
158             && (term.endsWith(")")) ) {
159       String JavaDoc roleName = term.substring(7,term.length()-1).trim();
160       Group group = (Group) entity;
161       Set JavaDoc users = group.getUsersForMembershipName(roleName);
162       if (users.size()==0) {
163         throw new ExpressionAssignmentException("no users in role '"+roleName+"'");
164       }
165       entity = (Entity) users.iterator().next();
166
167     } else {
168       throw new ExpressionAssignmentException("couldn't interpret term '"+term+"' in expression '"+expression+"'");
169     }
170     
171     return entity;
172   }
173
174   protected Object JavaDoc getVariable(String JavaDoc variableName) {
175     Token token = executionContext.getToken();
176     return executionContext.getContextInstance().getVariable(variableName, token);
177   }
178
179   protected Entity getGroupByName(String JavaDoc groupName) {
180     Group group = null;
181     group = expressionSession.getGroupByName(groupName);
182     if (group==null) {
183       throw new ExpressionAssignmentException("group '"+groupName+"' couldn't be fetched from the user db");
184     }
185     return group;
186   }
187
188   protected Entity getUserByName(String JavaDoc userName) {
189     User user = null;
190     user = expressionSession.getUserByName(userName);
191     if (user==null) {
192       throw new ExpressionAssignmentException("user '"+userName+"' couldn't be fetched from the user db");
193     }
194     return user;
195   }
196
197   protected String JavaDoc getSwimlaneActorId(String JavaDoc swimlaneName) {
198     SwimlaneInstance swimlaneInstance = executionContext
199           .getTaskMgmtInstance()
200           .getSwimlaneInstance(swimlaneName);
201     if (swimlaneInstance==null) {
202       throw new ExpressionAssignmentException("no swimlane instance '"+swimlaneName+"'");
203     }
204     return swimlaneInstance.getActorId();
205   }
206   
207   private static final Log log = LogFactory.getLog(ExpressionAssignmentHandler.class);
208 }
209
Popular Tags