[jbpm-commits] JBoss JBPM SVN: r3161 - in jbpm4/trunk/modules: task/src/main/java/org/jbpm/task/impl and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Dec 2 05:16:43 EST 2008


Author: tom.baeyens at jboss.com
Date: 2008-12-02 05:16:43 -0500 (Tue, 02 Dec 2008)
New Revision: 3161

Added:
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeElementImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeInstanceImpl.java
Modified:
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/CompositeElementImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ExecutionImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/VariableDefinitionImpl.java
   jbpm4/trunk/modules/task/src/main/java/org/jbpm/task/impl/TaskImpl.java
Log:
extracted scope behavior (variables and timers) in ScopeInstanceImpl so that Task can leverage it

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/CompositeElementImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/CompositeElementImpl.java	2008-12-02 08:46:03 UTC (rev 3160)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/CompositeElementImpl.java	2008-12-02 10:16:43 UTC (rev 3161)
@@ -22,11 +22,8 @@
 package org.jbpm.pvm.internal.model;
 
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import org.jbpm.model.CompositeElement;
 import org.jbpm.model.Node;
@@ -34,16 +31,11 @@
 /**
  * @author Tom Baeyens
  */
-public abstract class CompositeElementImpl extends ObservableElementImpl implements CompositeElement {
+public abstract class CompositeElementImpl extends ScopeElementImpl implements CompositeElement {
 
   private static final long serialVersionUID = 1L;
   
   protected List<NodeImpl> nodes;
-  protected boolean hasVariableDefinitions; 
-  protected List<VariableDefinitionImpl> variableDefinitions;
-  protected boolean hasTimerDefinitions; 
-  protected Set<TimerDefinitionImpl> timerDefinitions;
-
   transient protected Map<String, NodeImpl> nodesMap;
 
   // nested nodes /////////////////////////////////////////////////////////////
@@ -158,41 +150,4 @@
     return ((nodes!=null) && (!nodes.isEmpty()));
   }
 
-  // variable definitions /////////////////////////////////////////////////////
-  
-  public List<VariableDefinitionImpl> getVariableDefinitions() {
-    if (!hasVariableDefinitions) {
-      return Collections.EMPTY_LIST;
-    }
-    return (List) variableDefinitions;
-  }
-
-  public VariableDefinitionImpl createVariableDefinition() {
-    VariableDefinitionImpl variableDefinition = new VariableDefinitionImpl();
-    if (variableDefinitions==null) {
-      variableDefinitions = new ArrayList<VariableDefinitionImpl>();
-    }
-    variableDefinitions.add(variableDefinition);
-    hasVariableDefinitions = true;
-    return variableDefinition;
-  }
-
-  // timer definitions ////////////////////////////////////////////////////////
-  
-  public Set<TimerDefinitionImpl> getTimerDefinitions() {
-    if (!hasTimerDefinitions) {
-      return Collections.EMPTY_SET;
-    }
-    return timerDefinitions;
-  }
-  
-  public TimerDefinitionImpl createTimerDefinition() {
-    TimerDefinitionImpl timerDefinition = new TimerDefinitionImpl();
-    if (timerDefinitions==null) {
-      timerDefinitions = new HashSet<TimerDefinitionImpl>();
-    }
-    timerDefinitions.add(timerDefinition);
-    hasTimerDefinitions = true;
-    return timerDefinition;
-  }
 }

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ExecutionImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ExecutionImpl.java	2008-12-02 08:46:03 UTC (rev 3160)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ExecutionImpl.java	2008-12-02 10:16:43 UTC (rev 3161)
@@ -80,7 +80,8 @@
 /**
  * @author Tom Baeyens
  */
-public class ExecutionImpl implements ClientProcessInstance,
+public class ExecutionImpl extends ScopeInstanceImpl 
+                           implements ClientProcessInstance,
                                       ActivityExecution, 
                                       EventListenerExecution, 
                                       Serializable {
@@ -95,9 +96,6 @@
   public static final AtomicOperation TAKE_TRANSITION = new TakeTransition();
   public static final AtomicOperation PROPAGATE_TO_PARENT = new MoveToParentNode();
   
-  protected long dbid;
-  protected int dbversion;
-
   /** an optional name for this execution.  can be used to 
    * differentiate concurrent paths of execution like e.g. 
    * the 'shipping' and 'billing' paths. */
@@ -110,9 +108,6 @@
   /** a unique id for this execution. */
   protected String id;
 
-  /** @see Execution */  
-  protected String state;
-
   protected ProcessDefinitionImpl processDefinition;
   
   // current position /////////////////////////////////////////////////////////
@@ -139,12 +134,8 @@
   protected ExecutionImpl parent = null;
   protected ExecutionImpl processInstance;
   
-  boolean hasVariables;
-  protected Map<String, Variable> variables;
+  protected ScopeInstanceImpl scopeInstanceImpl;
   
-  boolean hasTimers;
-  protected Set<TimerImpl> timers;
-  
   /** the super process link in case this is a sub process execution */  
   protected ExecutionImpl superProcessExecution;
   
@@ -210,8 +201,8 @@
     if (state!=STATE_CREATED) {
       throw new JbpmException(toString()+" is already begun: "+state);
     }
-    ExecutionImpl scopedExecution = initializeScopes();
     this.state = STATE_ACTIVE;
+    ExecutionImpl scopedExecution = initializeScopes();
     fire(Event.PROCESS_BEGIN, processDefinition);
     if (node!=null) {
       scopedExecution.performAtomicOperation(EXECUTE_NODE);
@@ -249,7 +240,7 @@
     return scopedExecution;
   }
 
-  public ExecutionImpl createScope(CompositeElementImpl scope) {
+  public ExecutionImpl createScope(ScopeElementImpl scope) {
     ExecutionImpl child = createExecution(scope.getName());
     
     // copy the current state from the child execution to the parent execution
@@ -572,421 +563,6 @@
     }
   }
   
-  // variables ////////////////////////////////////////////////////////////////
-
-  protected void initializeVariables(CompositeElementImpl scope, ExecutionImpl outerExecution) {
-    // loop over all variable definitions
-    List<VariableDefinitionImpl> variableDefinitions = scope.getVariableDefinitions();
-    if (!variableDefinitions.isEmpty()){
-      if (log.isTraceEnabled()) {
-        log.trace("initializing variables in scope "+scope);
-      }
-      variables = new HashMap<String, Variable>();
-      for (VariableDefinitionImpl variableDefinition: variableDefinitions) {
-        String key = variableDefinition.getKey();
-        Object value = variableDefinition.getSourceValue(outerExecution);
-        Type type = variableDefinition.getType();
-        createVariable(key, value, type);
-      }
-    }
-  }
-
-  protected void destroyVariables(CompositeElementImpl scope, ExecutionImpl outerExecution) {
-    // loop over all variable definitions
-    List<VariableDefinitionImpl> variableDefinitions = scope.getVariableDefinitions();
-    if (variableDefinitions!=null) {
-      if (log.isTraceEnabled()) {
-        log.trace("destroying var scope "+scope);
-      }
-      
-      for (VariableDefinitionImpl variableDefinition: variableDefinitions) {
-        String destination = variableDefinition.getDestination();
-        if (destination!=null) {
-          String key = variableDefinition.getKey();
-          Object value = variableDefinition.getDestinationValue(this);
-          outerExecution.setVariable(key, value);
-        }
-      }
-    }
-  }
-  
-
-  public void createVariable(String key, Object value) {
-    createVariable(key, value, null, null);
-  }
-
-  public void createVariable(String key, Object value, String typeName) {
-    createVariable(key, value, typeName, null);
-  }
-
-  public void createVariable(String key, Object value, Type type) {
-    createVariable(key, value, null, type);
-  }
-
-  public void createVariable(String key, Object value, String typeName, Type type) {
-    if (isFinished()) {
-      throw new JbpmException("can't create variable '"+key+"' on "+this+": "+state);
-    }
-
-    log.debug("create variable '"+key+"' in '"+this+"' with value '"+value+"'");
-    
-    if (type==null) {
-      Environment environment = Environment.getCurrent();
-      if (environment!=null) {
-        VariableTypeResolver variableTypeResolver = environment.get(VariableTypeResolver.class);
-        if (variableTypeResolver!=null) {
-          if (typeName!=null) {
-            type = variableTypeResolver.findTypeByName(typeName);
-          }
-          if (type==null) {
-            type = variableTypeResolver.findTypeByMatch(key, value);
-          }
-        }
-      }
-    }
-    
-    Variable variable = null;
-
-    if (type!=null) {
-      Class<?> variableClass = type.getVariableClass();
-      try {
-        log.trace("creating new "+type+" variable "+key);
-        variable = (Variable) variableClass.newInstance();
-      } catch (Exception e) {
-        throw new JbpmException("couldn't instantiate variable instance class '"+variableClass.getName()+"'");
-      }
-      Converter converter = type.getConverter();
-      variable.setConverter(converter);
-
-    } else {
-      if (value==null) {
-        log.trace("creating null variable for "+key);
-        variable = new NullVariable();
-      } else {
-        log.trace("creating new unpersistable variable for "+key);
-        variable = new UnpersistableVariable();
-      }
-    }
-
-    variable.setKey(key);
-    variable.setValue(value);
-    variable.setProcessInstance(processInstance);
-    
-    if (variables==null) {
-      variables = new HashMap<String, Variable>();
-    }
-    variables.put(variable.getKey(), variable);
-    hasVariables = true;
-
-    // TODO add create-variable-log
-  }
-
-  public void setVariable(String key, Object value) {
-    if (isFinished()) {
-      throw new JbpmException("can't update variable '"+key+"' on "+this+": "+state);
-    }
-    Variable variable = getVariableObject(key);
-    // if there is already a variable instance and it doesn't support the current type...
-    if ( (variable!=null) 
-         && (!variable.supports(value))
-       ) {
-      // delete the old variable instance
-      log.debug("variable type change. deleting '"+key+"' from '"+this+"'");
-      removeVariable(key);
-      variable = null;
-    }
-
-    if (variable!=null) {
-      log.debug("updating variable '"+key+"' in '"+this+"' to value '"+value+"'");
-      variable.setValue(value);
-
-    } else if (parent==null) {
-      createVariable(key, value, null, null);
-
-    } else {
-      parent.setVariable(key,value);
-    }
-  }
-  
-  public void setVariables(Map<String, Object> variables) {
-    if (variables!=null) {
-      for (String key: variables.keySet()) {
-        Object value = variables.get(key);
-        setVariable(key, value);
-      }
-    }
-  }
-  
-  public Object getVariable(String key) {
-    Object value = null;
-
-    Variable variable = getVariableObject(key);
-    if (variable!=null) {
-      return variable.getValue();
-    }
-    
-    if (parent!=null) {
-      return parent.getVariable(key);
-    }
-
-    return null;
-  }
-
-  public Variable getVariableObject(String key) {
-    return (hasVariables ? (Variable) variables.get(key) : null);
-  }
-
-  public boolean hasVariable(String key) {
-    return ( (hasVariables && variables.containsKey(key))
-             || (parent!=null && parent.hasVariable(key))
-           );
-  }
-
-  public Set<String> getVariableKeys() {
-    Set<String> variableKeys = null;
-    if (parent!=null) {
-      variableKeys = parent.getVariableKeys();
-    } else {
-      variableKeys = new HashSet<String>();
-    }
-    if (hasVariables) {
-      variableKeys.addAll(variables.keySet());
-    }
-    return variableKeys;
-  }
-
-  public Map<String, Object> getVariables() {
-    Map<String, Object> values = null;
-    if (parent!=null) {
-      values = parent.getVariables();
-    } else {
-      values = new HashMap<String, Object>();
-    }
-    if (hasVariables) {
-      for (Map.Entry<String, Variable> entry: variables.entrySet()) {
-        String name = (String) entry.getKey();
-        Variable variable = entry.getValue();
-        Object value = variable.getValue();
-        values.put(name, value);
-      }
-    }
-    return values;
-  }
-  
-  public boolean hasVariables() {
-    return ( hasVariables
-             || (parent!=null && parent.hasVariables())
-           );
-  }
-
-  public boolean removeVariable(String key) {
-    if (isFinished()) {
-      throw new JbpmException("can't remove variable '"+key+"' on "+this+": "+state);
-    }
-
-    Variable variable = null;
-    if (hasVariables) {
-      variable = variables.remove(key);
-      if (variables.isEmpty()) {
-        hasVariables = false;
-      }
-      if (variable!=null) {
-        return true;
-      }
-    }
-    if (parent!=null) {
-      return parent.removeVariable(key);
-    }
-    // the actual value is not returned to prevent that an object 
-    // has to be fetched from the db for it to be deleted
-    return false;
-  }
-
-  public void removeVariables() {
-    if (hasVariables) {
-      variables.clear();
-    }
-    hasVariables = false;
-  }
-
-  // timers ///////////////////////////////////////////////////////////////////
-
-  protected void initializeTimers(CompositeElementImpl scope) {
-    // initialize the timers
-    Set<TimerDefinitionImpl> timerDefinitions = scope.getTimerDefinitions();
-    if (!timerDefinitions.isEmpty()) {
-      timers = new HashSet<TimerImpl>();
-      for (TimerDefinitionImpl timerDefinition: timerDefinitions) {
-        createTimer(
-            timerDefinition.getEventName(),
-            timerDefinition.getSignalName(),
-            timerDefinition.getDueDateDescription(),
-            timerDefinition.getDueDate(),
-            timerDefinition.getRepeat(),
-            timerDefinition.isExclusive(),
-            timerDefinition.getRetries()
-        );
-      }
-    }
-  }
-
-  protected void destroyTimers(CompositeElementImpl scope) {
-    log.debug("destroying timers of "+toString());
-    if (hasTimers && timers!=null && !timers.isEmpty()) {
-      // get the TimerSession from the environment
-      Environment environment = Environment.getCurrent();
-      if (environment==null) {
-        throw new JbpmException("non environment for initializing timers");
-      }
-      TimerSession timerSession = environment.get(TimerSession.class);
-      if (timerSession==null) {
-        throw new JbpmException("no TimerSession in environment for initializing timers");
-      }
-      for (Timer timer : timers) {
-        timerSession.cancel(timer);
-      }
-      timers.clear();
-      hasTimers = false;
-    }
-  }
-
-  public void createTimer(String eventName, String signalName, String dueDateDescription) {
-    createTimer(eventName, signalName, dueDateDescription, null, null, null, null);
-  }
-
-  public void createTimer(String eventName, String signalName, String dueDateDescription, String repeat) {
-    createTimer(eventName, signalName, dueDateDescription, null, repeat, null, null);
-  }
-
-  public void createTimer(String eventName, String signalName, String dueDateDescription, Date dueDate, String repeat, Boolean isExclusive, Integer retries) {
-    if ( (eventName==null)
-         && (signalName==null)
-       ) {
-      throw new JbpmException("no event or signal specified");
-    }
-    if (log.isDebugEnabled()) {
-      log.debug("creating timer on "+this.toString());
-    }
-    
-    // instantiate the timer
-    TimerImpl timerImpl = instantiateTimer();
-    // create the bidirectional reference
-    timerImpl.setExecution(this);
-    timers.add(timerImpl);
-    hasTimers = true;
-    // setInverseReference(timerImpl);
-    
-    // initialise all the timer properties
-    timerImpl.setEventName(eventName);
-    timerImpl.setSignalName(signalName);
-    if (dueDate!=null) {
-      timerImpl.setDueDate(dueDate);
-    } else {
-      timerImpl.setDueDateDescription(dueDateDescription);
-    }
-    
-    // the if is added to keep the original default
-    if (isExclusive!=null) {
-      timerImpl.setExclusive(isExclusive);
-    }
-    
-    // the if is added to keep the original default
-    if (retries!=null) {
-      timerImpl.setRetries(retries);
-    }
-
-    // the if is added to keep the original default
-    if (repeat!=null) {
-      timerImpl.setRepeat(repeat);
-    }
-
-    // get the TimerSession from the environment
-    Environment environment = Environment.getCurrent();
-    if (environment==null) {
-      throw new JbpmException("non environment for initializing timers");
-    }
-    TimerSession timerSession = environment.get(TimerSession.class);
-    if (timerSession==null) {
-      throw new JbpmException("no TimerSession in environment for initializing timers");
-    }
-    
-    // schedule the timer with the TimerSession
-    timerSession.schedule(timerImpl);
-  }
-
-  public boolean hasTimers() {
-    return hasTimers;
-  }
-
-  public Set<Timer> getTimers() {
-    return (Set) timers;
-  }
-  
-  protected TimerImpl instantiateTimer() {
-    return new TimerImpl();
-  }
-
-  
-  // state ////////////////////////////////////////////////////////////////////
-
-  /** @see Execution#getState() */
-  public String getState() {
-    return state;
-  }
-
-  /** @see Execution#lock(String) */
-  public void lock(String state) {
-    if (state==null) {
-      throw new JbpmException("given state is null");
-    }
-    checkLock();
-    log.trace("locking "+this);
-    this.state = state;
-  }
-  
-  /** @see Execution#unlock() */
-  public void unlock() {
-    if (STATE_ACTIVE.equals(state)) {
-      throw new JbpmException("state is already active");
-    }
-    log.trace("unlocking "+this);
-    this.state = STATE_ACTIVE;
-  }
-
-  /** @see Execution#isActive() */
-  public boolean isActive() {
-    return STATE_ACTIVE.equals(state);
-  }
-
-  /** @see Execution#isLocked() */
-  public boolean isLocked() {
-    return ! isActive();
-  }
-  
-  /** @see Execution#isSuspended() */
-  public boolean isSuspended() {
-    return STATE_SUSPENDED.equals(state);
-  }
-
-  /** @see Execution#isEnded() */
-  public boolean isEnded() {
-    return STATE_ENDED.equals(state);
-  }
-
-  /** @see Execution#isFinished() */
-  public boolean isFinished() {
-    return STATE_ENDED.equals(state)
-           || STATE_CANCELLED.equals(state);
-  }
-
-  // state : internal methods /////////////////////////////////////////////////
-
-  protected void checkLock() {
-    if (!STATE_ACTIVE.equals(state)) {
-      throw new JbpmException(toString()+" is not active: "+state);
-    }
-  }
-
   // asynchronous continuations ////////////////////////////////////////////////  
 
   public void sendContinuationMessage(AtomicOperation operation) {
@@ -1308,7 +884,34 @@
     subProcessInstance.start();
     return subProcessInstance;
   }
+
+  // state : internal methods /////////////////////////////////////////////////
+
+  /** @see Execution#lock(String) */
+  public void lock(String state) {
+    if (state==null) {
+      throw new JbpmException("given state is null");
+    }
+    checkLock();
+    log.trace("locking "+this);
+    this.state = state;
+  }
   
+  /** @see Execution#unlock() */
+  public void unlock() {
+    if (Execution.STATE_ACTIVE.equals(state)) {
+      throw new JbpmException("state is already active");
+    }
+    log.trace("unlocking "+this);
+    this.state = Execution.STATE_ACTIVE;
+  }
+
+  protected void checkLock() {
+    if (!Execution.STATE_ACTIVE.equals(state)) {
+      throw new JbpmException(toString()+" is not active: "+state);
+    }
+  }
+
   // node name ////////////////////////////////////////////////////////////////
   
   public String getNodeName() {
@@ -1335,7 +938,17 @@
   public int nextLogIndex() {
     return nextLogIndex++;
   }
+  
+  // overriding the ScopeInstanceImpl methods /////////////////////////////////
+  
+  public ScopeInstanceImpl getParentVariableScope() {
+    return parent;
+  }
 
+  public ExecutionImpl getTimerExecution() {
+    return this;
+  }
+
   // overridable by process languages /////////////////////////////////////////
   
   /** by default this will use {@link NodeImpl#findOutgoingTransition(String)} to 
@@ -1493,9 +1106,6 @@
   public void setNode(NodeImpl node) {
     this.node = node;
   }
-  public long getDbid() {
-    return dbid;
-  }
   public void setKey(String key) {
     this.key = key;
   }

Added: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeElementImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeElementImpl.java	                        (rev 0)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeElementImpl.java	2008-12-02 10:16:43 UTC (rev 3161)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.pvm.internal.model;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author Tom Baeyens
+ */
+public class ScopeElementImpl extends ObservableElementImpl {
+
+  private static final long serialVersionUID = 1L;
+
+  protected boolean hasVariableDefinitions; 
+  protected List<VariableDefinitionImpl> variableDefinitions;
+  protected boolean hasTimerDefinitions; 
+  protected Set<TimerDefinitionImpl> timerDefinitions;
+
+  // variable definitions /////////////////////////////////////////////////////
+  
+  public List<VariableDefinitionImpl> getVariableDefinitions() {
+    if (!hasVariableDefinitions) {
+      return Collections.EMPTY_LIST;
+    }
+    return (List) variableDefinitions;
+  }
+
+  public VariableDefinitionImpl createVariableDefinition() {
+    VariableDefinitionImpl variableDefinition = new VariableDefinitionImpl();
+    if (variableDefinitions==null) {
+      variableDefinitions = new ArrayList<VariableDefinitionImpl>();
+    }
+    variableDefinitions.add(variableDefinition);
+    hasVariableDefinitions = true;
+    return variableDefinition;
+  }
+
+  // timer definitions ////////////////////////////////////////////////////////
+  
+  public Set<TimerDefinitionImpl> getTimerDefinitions() {
+    if (!hasTimerDefinitions) {
+      return Collections.EMPTY_SET;
+    }
+    return timerDefinitions;
+  }
+  
+  public TimerDefinitionImpl createTimerDefinition() {
+    TimerDefinitionImpl timerDefinition = new TimerDefinitionImpl();
+    if (timerDefinitions==null) {
+      timerDefinitions = new HashSet<TimerDefinitionImpl>();
+    }
+    timerDefinitions.add(timerDefinition);
+    hasTimerDefinitions = true;
+    return timerDefinition;
+  }
+}


Property changes on: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeElementImpl.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Added: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeInstanceImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeInstanceImpl.java	                        (rev 0)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeInstanceImpl.java	2008-12-02 10:16:43 UTC (rev 3161)
@@ -0,0 +1,477 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.pvm.internal.model;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.jbpm.Execution;
+import org.jbpm.JbpmException;
+import org.jbpm.env.Environment;
+import org.jbpm.job.Timer;
+import org.jbpm.log.Log;
+import org.jbpm.pvm.internal.job.TimerImpl;
+import org.jbpm.pvm.internal.type.Converter;
+import org.jbpm.pvm.internal.type.Type;
+import org.jbpm.pvm.internal.type.Variable;
+import org.jbpm.pvm.internal.type.VariableTypeResolver;
+import org.jbpm.pvm.internal.type.variable.NullVariable;
+import org.jbpm.pvm.internal.type.variable.UnpersistableVariable;
+import org.jbpm.session.TimerSession;
+
+
+/**
+ * @author Tom Baeyens
+ */
+public class ScopeInstanceImpl implements Serializable {
+
+  private static final long serialVersionUID = 1L;
+  private static Log log = Log.getLog(ScopeInstanceImpl.class.getName());
+  
+  protected long dbid;
+  protected int dbversion;
+
+  protected boolean hasVariables;
+  protected Map<String, Variable> variables;
+  protected boolean hasTimers;
+  protected Set<TimerImpl> timers;
+  
+  protected String state;
+  
+  // variables ////////////////////////////////////////////////////////////////
+
+  protected void initializeVariables(ScopeElementImpl scope, ExecutionImpl outerExecution) {
+    // loop over all variable definitions
+    List<VariableDefinitionImpl> variableDefinitions = scope.getVariableDefinitions();
+    if (!variableDefinitions.isEmpty()){
+      if (log.isTraceEnabled()) {
+        log.trace("initializing variables in scope "+scope);
+      }
+      variables = new HashMap<String, Variable>();
+      for (VariableDefinitionImpl variableDefinition: variableDefinitions) {
+        String key = variableDefinition.getKey();
+        Object value = variableDefinition.getSourceValue(outerExecution);
+        Type type = variableDefinition.getType();
+        createVariable(key, value, type);
+      }
+    }
+  }
+
+  protected void destroyVariables(ScopeElementImpl scope, ExecutionImpl outerExecution) {
+    // loop over all variable definitions
+    List<VariableDefinitionImpl> variableDefinitions = scope.getVariableDefinitions();
+    if (variableDefinitions!=null) {
+      if (log.isTraceEnabled()) {
+        log.trace("destroying var scope "+scope);
+      }
+      
+      for (VariableDefinitionImpl variableDefinition: variableDefinitions) {
+        String destination = variableDefinition.getDestination();
+        if (destination!=null) {
+          String key = variableDefinition.getKey();
+          Object value = variableDefinition.getDestinationValue(this);
+          outerExecution.setVariable(key, value);
+        }
+      }
+    }
+  }
+  
+
+  public void createVariable(String key, Object value) {
+    createVariable(key, value, null, null);
+  }
+
+  public void createVariable(String key, Object value, String typeName) {
+    createVariable(key, value, typeName, null);
+  }
+
+  public void createVariable(String key, Object value, Type type) {
+    createVariable(key, value, null, type);
+  }
+
+  public void createVariable(String key, Object value, String typeName, Type type) {
+    if (isFinished()) {
+      throw new JbpmException("can't create variable '"+key+"' on "+this+": "+state);
+    }
+
+    log.debug("create variable '"+key+"' in '"+this+"' with value '"+value+"'");
+    
+    if (type==null) {
+      Environment environment = Environment.getCurrent();
+      if (environment!=null) {
+        VariableTypeResolver variableTypeResolver = environment.get(VariableTypeResolver.class);
+        if (variableTypeResolver!=null) {
+          if (typeName!=null) {
+            type = variableTypeResolver.findTypeByName(typeName);
+          }
+          if (type==null) {
+            type = variableTypeResolver.findTypeByMatch(key, value);
+          }
+        }
+      }
+    }
+    
+    Variable variable = null;
+
+    if (type!=null) {
+      Class<?> variableClass = type.getVariableClass();
+      try {
+        log.trace("creating new "+type+" variable "+key);
+        variable = (Variable) variableClass.newInstance();
+      } catch (Exception e) {
+        throw new JbpmException("couldn't instantiate variable instance class '"+variableClass.getName()+"'");
+      }
+      Converter converter = type.getConverter();
+      variable.setConverter(converter);
+
+    } else {
+      if (value==null) {
+        log.trace("creating null variable for "+key);
+        variable = new NullVariable();
+      } else {
+        log.trace("creating new unpersistable variable for "+key);
+        variable = new UnpersistableVariable();
+      }
+    }
+
+    variable.setKey(key);
+    variable.setValue(value);
+    variable.setProcessInstance(getProcessInstance());
+    
+    if (variables==null) {
+      variables = new HashMap<String, Variable>();
+    }
+    variables.put(variable.getKey(), variable);
+    hasVariables = true;
+
+    // TODO add create-variable-log
+  }
+
+  public void setVariable(String key, Object value) {
+    if (isFinished()) {
+      throw new JbpmException("can't update variable '"+key+"' on "+this+": "+state);
+    }
+    Variable variable = getVariableObject(key);
+    // if there is already a variable instance and it doesn't support the current type...
+    if ( (variable!=null) 
+         && (!variable.supports(value))
+       ) {
+      // delete the old variable instance
+      log.debug("variable type change. deleting '"+key+"' from '"+this+"'");
+      removeVariable(key);
+      variable = null;
+    }
+
+    if (variable!=null) {
+      log.debug("updating variable '"+key+"' in '"+this+"' to value '"+value+"'");
+      variable.setValue(value);
+
+    } else if (getParentVariableScope()==null) {
+      createVariable(key, value, null, null);
+
+    } else {
+      getParentVariableScope().setVariable(key,value);
+    }
+  }
+  
+  public void setVariables(Map<String, Object> variables) {
+    if (variables!=null) {
+      for (String key: variables.keySet()) {
+        Object value = variables.get(key);
+        setVariable(key, value);
+      }
+    }
+  }
+  
+  public Object getVariable(String key) {
+    Object value = null;
+
+    Variable variable = getVariableObject(key);
+    if (variable!=null) {
+      return variable.getValue();
+    }
+    
+    ScopeInstanceImpl parentScope = getParentVariableScope();
+    if (parentScope!=null) {
+      return parentScope.getVariable(key);
+    }
+
+    return null;
+  }
+
+  public Variable getVariableObject(String key) {
+    return (hasVariables ? (Variable) variables.get(key) : null);
+  }
+
+  public boolean hasVariable(String key) {
+    ScopeInstanceImpl parentScope = getParentVariableScope();
+    return ( (hasVariables && variables.containsKey(key))
+             || (parentScope!=null && parentScope.hasVariable(key))
+           );
+  }
+
+  public Set<String> getVariableKeys() {
+    Set<String> variableKeys = null;
+    ScopeInstanceImpl parentScope = getParentVariableScope();
+    if (parentScope!=null) {
+      variableKeys = parentScope.getVariableKeys();
+    } else {
+      variableKeys = new HashSet<String>();
+    }
+    if (hasVariables) {
+      variableKeys.addAll(variables.keySet());
+    }
+    return variableKeys;
+  }
+
+  public Map<String, Object> getVariables() {
+    Map<String, Object> values = null;
+    ScopeInstanceImpl parentScope = getParentVariableScope();
+    if (parentScope!=null) {
+      values = parentScope.getVariables();
+    } else {
+      values = new HashMap<String, Object>();
+    }
+    if (hasVariables) {
+      for (Map.Entry<String, Variable> entry: variables.entrySet()) {
+        String name = (String) entry.getKey();
+        Variable variable = entry.getValue();
+        Object value = variable.getValue();
+        values.put(name, value);
+      }
+    }
+    return values;
+  }
+  
+  public boolean hasVariables() {
+    ScopeInstanceImpl parentScope = getParentVariableScope();
+    return ( hasVariables
+             || (parentScope!=null && parentScope.hasVariables())
+           );
+  }
+
+  public boolean removeVariable(String key) {
+    if (isFinished()) {
+      throw new JbpmException("can't remove variable '"+key+"' on "+this+": "+state);
+    }
+
+    Variable variable = null;
+    if (hasVariables) {
+      variable = variables.remove(key);
+      if (variables.isEmpty()) {
+        hasVariables = false;
+      }
+      if (variable!=null) {
+        return true;
+      }
+    }
+    ScopeInstanceImpl parentScope = getParentVariableScope();
+    if (parentScope!=null) {
+      return parentScope.removeVariable(key);
+    }
+    // the actual value is not returned to prevent that an object 
+    // has to be fetched from the db for it to be deleted
+    return false;
+  }
+
+  public void removeVariables() {
+    if (hasVariables) {
+      variables.clear();
+    }
+    hasVariables = false;
+  }
+
+  // timers ///////////////////////////////////////////////////////////////////
+
+  protected void initializeTimers(ScopeElementImpl scope) {
+    // initialize the timers
+    Set<TimerDefinitionImpl> timerDefinitions = scope.getTimerDefinitions();
+    if (!timerDefinitions.isEmpty()) {
+      timers = new HashSet<TimerImpl>();
+      for (TimerDefinitionImpl timerDefinition: timerDefinitions) {
+        createTimer(
+            timerDefinition.getEventName(),
+            timerDefinition.getSignalName(),
+            timerDefinition.getDueDateDescription(),
+            timerDefinition.getDueDate(),
+            timerDefinition.getRepeat(),
+            timerDefinition.isExclusive(),
+            timerDefinition.getRetries()
+        );
+      }
+    }
+  }
+
+  protected void destroyTimers(CompositeElementImpl scope) {
+    log.debug("destroying timers of "+toString());
+    if (hasTimers && timers!=null && !timers.isEmpty()) {
+      // get the TimerSession from the environment
+      Environment environment = Environment.getCurrent();
+      if (environment==null) {
+        throw new JbpmException("non environment for initializing timers");
+      }
+      TimerSession timerSession = environment.get(TimerSession.class);
+      if (timerSession==null) {
+        throw new JbpmException("no TimerSession in environment for initializing timers");
+      }
+      for (Timer timer : timers) {
+        timerSession.cancel(timer);
+      }
+      timers.clear();
+      hasTimers = false;
+    }
+  }
+
+  public void createTimer(String eventName, String signalName, String dueDateDescription) {
+    createTimer(eventName, signalName, dueDateDescription, null, null, null, null);
+  }
+
+  public void createTimer(String eventName, String signalName, String dueDateDescription, String repeat) {
+    createTimer(eventName, signalName, dueDateDescription, null, repeat, null, null);
+  }
+
+  public void createTimer(String eventName, String signalName, String dueDateDescription, Date dueDate, String repeat, Boolean isExclusive, Integer retries) {
+    if ( (eventName==null)
+         && (signalName==null)
+       ) {
+      throw new JbpmException("no event or signal specified");
+    }
+    if (log.isDebugEnabled()) {
+      log.debug("creating timer on "+this.toString());
+    }
+    
+    // instantiate the timer
+    TimerImpl timerImpl = instantiateTimer();
+    // create the bidirectional reference
+    timerImpl.setExecution(getTimerExecution());
+    timers.add(timerImpl);
+    hasTimers = true;
+    // setInverseReference(timerImpl);
+    
+    // initialise all the timer properties
+    timerImpl.setEventName(eventName);
+    timerImpl.setSignalName(signalName);
+    if (dueDate!=null) {
+      timerImpl.setDueDate(dueDate);
+    } else {
+      timerImpl.setDueDateDescription(dueDateDescription);
+    }
+    
+    // the if is added to keep the original default
+    if (isExclusive!=null) {
+      timerImpl.setExclusive(isExclusive);
+    }
+    
+    // the if is added to keep the original default
+    if (retries!=null) {
+      timerImpl.setRetries(retries);
+    }
+
+    // the if is added to keep the original default
+    if (repeat!=null) {
+      timerImpl.setRepeat(repeat);
+    }
+
+    // get the TimerSession from the environment
+    Environment environment = Environment.getCurrent();
+    if (environment==null) {
+      throw new JbpmException("non environment for initializing timers");
+    }
+    TimerSession timerSession = environment.get(TimerSession.class);
+    if (timerSession==null) {
+      throw new JbpmException("no TimerSession in environment for initializing timers");
+    }
+    
+    // schedule the timer with the TimerSession
+    timerSession.schedule(timerImpl);
+  }
+
+  public boolean hasTimers() {
+    return hasTimers;
+  }
+
+  public Set<Timer> getTimers() {
+    return (Set) timers;
+  }
+  
+  protected TimerImpl instantiateTimer() {
+    return new TimerImpl();
+  }
+  
+  // state ////////////////////////////////////////////////////////////////////
+
+  /** @see Execution#getState() */
+  public String getState() {
+    return state;
+  }
+
+  /** @see Execution#isActive() */
+  public boolean isActive() {
+    return Execution.STATE_ACTIVE.equals(state);
+  }
+
+  /** @see Execution#isLocked() */
+  public boolean isLocked() {
+    return ! isActive();
+  }
+  
+  /** @see Execution#isSuspended() */
+  public boolean isSuspended() {
+    return Execution.STATE_SUSPENDED.equals(state);
+  }
+
+  /** @see Execution#isEnded() */
+  public boolean isEnded() {
+    return Execution.STATE_ENDED.equals(state);
+  }
+
+  /** @see Execution#isFinished() */
+  public boolean isFinished() {
+    return Execution.STATE_ENDED.equals(state)
+           || Execution.STATE_CANCELLED.equals(state);
+  }
+
+  // customizable methods /////////////////////////////////////////////////////
+  
+  public ExecutionImpl getProcessInstance() {
+    return null;
+  }
+
+  public ExecutionImpl getTimerExecution() {
+    return null;
+  }
+
+  public ScopeInstanceImpl getParentVariableScope() {
+    return null;
+  }
+
+  // getters and setters //////////////////////////////////////////////////////
+
+  public long getDbid() {
+    return dbid;
+  }
+}


Property changes on: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/ScopeInstanceImpl.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/VariableDefinitionImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/VariableDefinitionImpl.java	2008-12-02 08:46:03 UTC (rev 3160)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/model/VariableDefinitionImpl.java	2008-12-02 10:16:43 UTC (rev 3161)
@@ -72,13 +72,13 @@
     return null;
   }
 
-  public Object getDestinationValue(ExecutionImpl execution) {
+  public Object getDestinationValue(ScopeInstanceImpl scopeInstanceImpl) {
     if (destinationExpression!=null) {
       // TODO add variable destination expression resolution
       return null;
     }
     if (key!=null) {
-      return execution.getVariable(key);
+      return scopeInstanceImpl.getVariable(key);
     }
     return null;
   }

Modified: jbpm4/trunk/modules/task/src/main/java/org/jbpm/task/impl/TaskImpl.java
===================================================================
--- jbpm4/trunk/modules/task/src/main/java/org/jbpm/task/impl/TaskImpl.java	2008-12-02 08:46:03 UTC (rev 3160)
+++ jbpm4/trunk/modules/task/src/main/java/org/jbpm/task/impl/TaskImpl.java	2008-12-02 10:16:43 UTC (rev 3161)
@@ -36,6 +36,7 @@
 import org.jbpm.pvm.internal.cal.Duration;
 import org.jbpm.pvm.internal.model.CommentImpl;
 import org.jbpm.pvm.internal.model.ExecutionImpl;
+import org.jbpm.pvm.internal.model.ScopeInstanceImpl;
 import org.jbpm.pvm.internal.util.Clock;
 import org.jbpm.pvm.internal.util.EqualsUtil;
 import org.jbpm.pvm.internal.util.Priority;
@@ -51,7 +52,7 @@
  * someones task list) and that can trigger the continuation of execution 
  * of the token upon completion.
  */
-public class TaskImpl implements Serializable, Task {
+public class TaskImpl extends ScopeInstanceImpl implements Serializable, Task {
 
   private static final long serialVersionUID = 1L;
   // private static final Logger log = Logger.getLogger(TaskImpl.class.getName());
@@ -62,8 +63,6 @@
     // PvmEnvironment.CONTEXTNAME_APPLICATION
   };
 
-  protected long dbid;
-  protected int dbversion;
   protected String id;
   protected String name;
   protected String description;
@@ -82,7 +81,7 @@
 
   protected String state;
 
-  protected Execution execution;
+  protected ExecutionImpl execution;
   protected boolean isSignalling = true;
   protected boolean isBlocking;
 
@@ -371,11 +370,11 @@
   public void setDueDate(Date dueDate) {
     this.dueDate = dueDate;
   }
-  public Execution getExecution() {
+  public ExecutionImpl getExecution() {
     return execution;
   }
   public void setExecution(Execution execution) {
-    this.execution = execution;
+    this.execution = (ExecutionImpl) execution;
   }
   public String getState() {
     return state;




More information about the jbpm-commits mailing list