[jboss-svn-commits] JBL Code SVN: r25906 - in labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src: main/resources/META-INF and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Apr 1 15:32:20 EDT 2009
Author: salaboy21
Date: 2009-04-01 15:32:20 -0400 (Wed, 01 Apr 2009)
New Revision: 25906
Added:
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariableInstanceInfo.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersister.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyEntity.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyVariableSerializable.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf
Modified:
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/resources/META-INF/orm.xml
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java
labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/drools.session.conf
Log:
merging the old branch with this new one, for persistence changes
Modified: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java 2009-04-01 17:59:39 UTC (rev 25905)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -3,15 +3,22 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
import javax.persistence.EntityManager;
import org.drools.WorkingMemory;
import org.drools.common.InternalRuleBase;
import org.drools.common.InternalWorkingMemory;
import org.drools.process.core.Process;
+import org.drools.process.core.context.variable.VariableScope;
import org.drools.process.instance.ProcessInstance;
import org.drools.process.instance.ProcessInstanceManager;
+import org.drools.process.instance.context.variable.VariableScopeInstance;
import org.drools.process.instance.impl.ProcessInstanceImpl;
+import org.drools.ruleflow.instance.RuleFlowProcessInstance;
import org.drools.runtime.EnvironmentName;
public class JPAProcessInstanceManager
@@ -27,11 +34,66 @@
public void addProcessInstance(ProcessInstance processInstance) {
ProcessInstanceInfo processInstanceInfo = new ProcessInstanceInfo( processInstance );
EntityManager em = (EntityManager) this.workingMemory.getEnvironment().get( EnvironmentName.ENTITY_MANAGER );
+ persistVariables(processInstanceInfo);
em.persist( processInstanceInfo );
((ProcessInstance) processInstance).setId( processInstanceInfo.getId() );
processInstanceInfo.updateLastReadDate();
}
+ private ProcessInstance restoreVariables(ProcessInstance processInstance) {
+ VariableScopeInstance variableScopeInstance = new VariableScopeInstance();
+ EntityManager em = (EntityManager) this.workingMemory.getEnvironment().get( EnvironmentName.ENTITY_MANAGER );
+ List<VariableInstanceInfo> variablesInfo = (List<VariableInstanceInfo>)em.createNamedQuery("VariableInstancesInfoByProcessId")
+ .setParameter("processId", processInstance.getId())
+ .getResultList();
+ for(VariableInstanceInfo variableInfo : variablesInfo){
+ VariablePersistenceStrategy persistenceStrategy =
+ VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory();
+ variableScopeInstance.setVariable(variableInfo.getName(), persistenceStrategy.getVariable(variableInfo));
+ }
+ //@TODO: hmm i don't know why processIntance don't have a setContextInstance method
+ variableScopeInstance.setProcessInstance(processInstance);
+ ((RuleFlowProcessInstance)processInstance).setContextInstance(VariableScope.VARIABLE_SCOPE, variableScopeInstance);
+
+ return processInstance;
+ }
+
+ private void persistVariables(ProcessInstanceInfo processInstanceInfo) {
+ VariableScopeInstance variableScopeInstance =
+ (VariableScopeInstance)
+ ((RuleFlowProcessInstance)getProcessInstance(processInstanceInfo.getId()))
+ .getContextInstance( VariableScope.VARIABLE_SCOPE );
+ Map<String, Object> processVariables = variableScopeInstance.getVariables();
+ List<String> keys = new ArrayList<String>(processVariables.keySet());
+ Collections.sort(keys, new Comparator<String>() {
+
+ public int compare(String o1, String o2) {
+ return o1.compareTo(o2);
+ }
+ });
+ VariablePersistenceStrategy persistenceStrategy = VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory();
+ processInstanceInfo.clearVariables();
+ for (String key : keys) {
+ VariableInstanceInfo variable = persistenceStrategy.persistVariable(key, processVariables.get(key));
+ //@TODO: i'm persisting the variable with JPA and
+ //I should persist it with something that the strategy said
+ //i think the best way to achive that is sending the manager to this method overloaded
+ //like private void resolveOutputVariables(Manager manager, ProcessInstanceInfo processInstanceInfo)
+// if(variable instanceof JPAPersistedVariable){
+// manager.persist(((JPAPersistedVariable)variable).getRealVariable());
+// updateVariableInstanceInfo(variable);
+// }
+ if(variable != null){
+ variable.setProcessInstanceInfo(processInstanceInfo);
+ processInstanceInfo.addVariables(variable);
+ }
+ }
+ //If we wont find any VariableInstanceInfo that mactches with the persistence strategy,
+ //we just serialize the variables inside the blob
+ if(processInstanceInfo.getVariables().size() == 0){
+ processInstanceInfo.setSerializeVariables(true);
+ }
+ }
public void internalAddProcessInstance(ProcessInstance processInstance) {
}
Modified: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-04-01 17:59:39 UTC (rev 25905)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -5,18 +5,24 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
+import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
+import javax.persistence.OneToMany;
import javax.persistence.PreUpdate;
+import javax.persistence.Temporal;
import javax.persistence.Transient;
import javax.persistence.Version;
@@ -32,7 +38,7 @@
import org.hibernate.annotations.CollectionOfElements;
@Entity
-public class ProcessInstanceInfo {
+public class ProcessInstanceInfo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@@ -43,12 +49,15 @@
private int version;
private String processId;
+ @Temporal(javax.persistence.TemporalType.DATE)
private Date startDate;
+ @Temporal(javax.persistence.TemporalType.DATE)
private Date lastReadDate;
+ @Temporal(javax.persistence.TemporalType.DATE)
private Date lastModificationDate;
private int state;
- // TODO How do I mark a process instance info as dirty when the process
- // instance
+ private boolean serializeVariables=false;
+ // TODO How do I mark a process instance info as dirty when the process instance
// has changed (so that byte array is regenerated and saved) ?
private @Lob
byte[] processInstanceByteArray;
@@ -57,9 +66,16 @@
private @Transient
ProcessInstance processInstance;
- ProcessInstanceInfo() {
+ @OneToMany(cascade=CascadeType.ALL, mappedBy="processInstanceInfo")
+
+ private List<VariableInstanceInfo> variables = new ArrayList<VariableInstanceInfo>();
+
+ public ProcessInstanceInfo() {
}
+
+
+
public ProcessInstanceInfo(ProcessInstance processInstance) {
this.processInstance = processInstance;
this.processId = processInstance.getProcessId();
@@ -103,7 +119,7 @@
bais, (InternalRuleBase) workingMemory.getRuleBase(), null, null);
context.wm = (InternalWorkingMemory) workingMemory;
ProcessInstanceMarshaller marshaller = getMarshallerFromContext(context);
- processInstance = marshaller.readProcessInstance(context);
+ processInstance = marshaller.readProcessInstance(context,serializeVariables);
context.close();
} catch (IOException e) {
e.printStackTrace();
@@ -142,7 +158,7 @@
saveProcessInstanceType(context, processInstance, processType);
ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType);
marshaller.writeProcessInstance(
- context, processInstance);
+ context, processInstance,serializeVariables);
context.close();
} catch (IOException e) {
throw new IllegalArgumentException(
@@ -160,4 +176,33 @@
}
}
}
+
+ /**
+ * @return the variables
+ */
+ public List<VariableInstanceInfo> getVariables() {
+ return variables;
}
+
+ /**
+ * @param variables the variables to set
+ */
+ public void setVariables(List<VariableInstanceInfo> variables) {
+ this.variables = variables;
+ }
+
+ public void addVariables(VariableInstanceInfo variable){
+ this.getVariables().add(variable);
+ }
+ public void clearVariables(){
+ this.getVariables().clear();
+ }
+ public boolean getSerializeVariables(){
+ return this.serializeVariables;
+ }
+ public void setSerializeVariables(boolean serializeVariables) {
+ this.serializeVariables = serializeVariables;
+ }
+
+
+}
Added: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariableInstanceInfo.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariableInstanceInfo.java (rev 0)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariableInstanceInfo.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2009 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.persistence.processinstance;
+
+import java.io.Serializable;
+
+
+
+import javax.persistence.DiscriminatorColumn;
+import javax.persistence.DiscriminatorType;
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+
+
+
+/**
+ *
+ * @author salaboy
+ */
+ at Entity
+ at Inheritance(strategy=InheritanceType.SINGLE_TABLE)
+ at DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=50)
+ at DiscriminatorValue("GEN")
+public class VariableInstanceInfo implements Serializable {
+ @Id @GeneratedValue(strategy=GenerationType.AUTO)
+ private Long id;
+ private String name;
+ @ManyToOne()
+ @JoinColumn(name="processInstanceInfoId")
+ private ProcessInstanceInfo processInstanceInfo;
+ private String persister;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @param name the name to set
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * @return the processInstanceInfo
+ */
+ public ProcessInstanceInfo getProcessInstanceInfo() {
+ return processInstanceInfo;
+ }
+
+ /**
+ * @param processInstanceInfo the processInstanceInfo to set
+ */
+ public void setProcessInstanceInfo(ProcessInstanceInfo processInstanceInfo) {
+ this.processInstanceInfo = processInstanceInfo;
+ }
+
+ /**
+ * @return the persister
+ */
+ public String getPersister() {
+ return persister;
+ }
+
+ /**
+ * @param persister the persister to set
+ */
+ public void setPersister(String persister) {
+ this.persister = persister;
+ }
+}
Added: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java (rev 0)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2009 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.persistence.processinstance;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.drools.util.ChainedProperties;
+
+/**
+ *
+ * @author salaboy
+ */
+public class VariablePersistenceStrategy {
+
+ private Properties types;
+
+
+
+
+ @SuppressWarnings("unchecked")
+ public VariableInstanceInfo persistVariable(String name, Object o){
+ VariableInstanceInfo variable = null;
+ VariablePersister persister = null;
+ //Here i should foreach an Array of types for multiple variable persistence
+ String persisterFQN = getVariablePersistenceType(o);
+ if(persisterFQN != null && !persisterFQN.equals("")){
+ Class persisterClass = null;
+ try {
+ persisterClass = Class.forName(persisterFQN);
+ Constructor constructor;
+ constructor = persisterClass.getConstructor(null);
+ persister = (VariablePersister) constructor.newInstance(null);
+
+ } catch (ClassNotFoundException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (NoSuchMethodException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (SecurityException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (InstantiationException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IllegalAccessException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IllegalArgumentException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (InvocationTargetException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ }
+
+
+ variable = persister.persistExternalVariable(name, o);
+
+ }
+ return variable;
+ }
+
+ public Object getVariable(VariableInstanceInfo variableInfo){
+ try {
+ String variablePersister = variableInfo.getPersister();
+ VariablePersister persister = null;
+ Class clazz = Class.forName(variablePersister);
+ Constructor constructor = clazz.getDeclaredConstructor(null);
+ persister = (VariablePersister) constructor.newInstance(null);
+ return persister.getExternalPersistedVariable(variableInfo);
+ } catch (InstantiationException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IllegalAccessException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IllegalArgumentException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (InvocationTargetException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (NoSuchMethodException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (SecurityException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (ClassNotFoundException ex) {
+ Logger.getLogger(VariablePersistenceStrategy.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ return null;
+ }
+
+
+ //this method should return an Array of types if multiple variable persistece is enabled
+ private String getVariablePersistenceType(Object o) {
+
+ Annotation[] annotations = o.getClass().getDeclaredAnnotations();
+ //First annotations, because annotations have more precedence (??)
+ for(Annotation annotation : annotations){
+ String persisterFQN = getTypes().getProperty(annotation.annotationType().getName());
+ if(persisterFQN != null && !persisterFQN.equals("")){
+ return persisterFQN;
+ }
+ }
+ //Then interfaces
+ Class[] interfaces = o.getClass().getInterfaces();
+ for(Class clazz : interfaces){
+ String persisterFQN = getTypes().getProperty(clazz.getName());
+ if(persisterFQN != null && !persisterFQN.equals("")){
+ return persisterFQN;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @return the types
+ */
+ public Properties getTypes() {
+ return types;
+ }
+
+ /**
+ * @param types the types to set
+ */
+ public void setTypes(Properties types) {
+ this.types = types;
+ }
+
+
+}
Added: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java (rev 0)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2009 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.persistence.processinstance;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.drools.util.ChainedProperties;
+
+
+/**
+ *
+ * @author salaboy
+ */
+
+public class VariablePersistenceStrategyFactory{
+ private static VariablePersistenceStrategy vPRS;
+
+ public VariablePersistenceStrategyFactory(){
+
+ }
+ public static VariablePersistenceStrategy getVariablePersistenceStrategyFactory(String propsFile){
+ Properties props = new Properties();
+ try {
+ InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/"+propsFile);
+ if (is != null){
+ props.load(is);
+ }
+ } catch (IOException ex) {
+ Logger.getLogger(VariablePersistenceStrategyFactory.class.getName()).log(Level.SEVERE, null, ex);
+ }
+
+ if(vPRS == null) {
+ vPRS = new VariablePersistenceStrategy();
+ }
+ vPRS.setTypes(props);
+ return vPRS;
+
+ //types.put("javax.persistence.Entity", "org.drools.persistence.processinstance.JPAVariablePersister");
+ //types.put("java.io.Serializable", "org.drools.persistence.processinstance.SerializableVariablePersister");
+
+
+ }
+ public static VariablePersistenceStrategy getVariablePersistenceStrategyFactory(){
+
+ if(vPRS == null) {
+ vPRS = new VariablePersistenceStrategy();
+ }
+ return vPRS;
+
+ //types.put("javax.persistence.Entity", "org.drools.persistence.processinstance.JPAVariablePersister");
+ //types.put("java.io.Serializable", "org.drools.persistence.processinstance.SerializableVariablePersister");
+
+
+ }
+}
Added: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersister.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersister.java (rev 0)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersister.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2009 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.persistence.processinstance;
+
+/**
+ *
+ * @author salaboy
+ */
+
+public interface VariablePersister {
+ public VariableInstanceInfo persistExternalVariable(String name, Object o);
+
+ public Object getExternalPersistedVariable(VariableInstanceInfo variableInstanceInfo);
+
+
+
+
+
+}
Modified: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/resources/META-INF/orm.xml
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/resources/META-INF/orm.xml 2009-04-01 17:59:39 UTC (rev 25905)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/main/resources/META-INF/orm.xml 2009-04-01 19:32:20 UTC (rev 25906)
@@ -13,4 +13,14 @@
:type in elements(processInstanceInfo.eventTypes)
</query>
</named-query>
+ <named-query name="VariableInstancesInfoByProcessId">
+ <query>
+select
+ v
+from
+ VariableInstanceInfo v
+where
+ v.processInstanceInfo.id = :processId
+ </query>
+ </named-query>
</entity-mappings>
Added: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyEntity.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyEntity.java (rev 0)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyEntity.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2009 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.persistence.session;
+
+import java.io.Serializable;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+/**
+ *
+ * @author salaboy
+ */
+ at Entity
+public class MyEntity implements Serializable {
+ @Id @GeneratedValue(strategy=GenerationType.AUTO)
+ private Long id;
+ private String test;
+
+ MyEntity(){}
+
+ MyEntity(String string) {
+ this.test= string;
+ }
+
+ /**
+ * @return the id
+ */
+ public Long getId() {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ /**
+ * @return the test
+ */
+ public String getTest() {
+ return test;
+ }
+
+ /**
+ * @param test the test to set
+ */
+ public void setTest(String test) {
+ this.test = test;
+ }
+}
Added: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyVariableSerializable.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyVariableSerializable.java (rev 0)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/MyVariableSerializable.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2009 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.persistence.session;
+
+import java.io.Serializable;
+
+/**
+ *
+ * @author salaboy
+ */
+class MyVariableSerializable implements Serializable{
+
+ private String text = "";
+
+ public MyVariableSerializable(String string) {
+ this.text = string;
+ }
+
+ /**
+ * @return the text
+ */
+ public String getText() {
+ return text;
+ }
+
+ /**
+ * @param text the text to set
+ */
+ public void setText(String text) {
+ this.text = text;
+ }
+
+}
Modified: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java 2009-04-01 17:59:39 UTC (rev 25905)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java 2009-04-01 19:32:20 UTC (rev 25906)
@@ -48,6 +48,10 @@
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;
+import java.util.HashMap;
+import java.util.Map;
+import org.drools.process.core.context.variable.VariableScope;
+import org.drools.process.instance.context.variable.VariableScopeInstance;
public class SingleSessionCommandServiceTest extends TestCase {
@@ -756,5 +760,56 @@
list.add( new KnowledgePackageImp( packageBuilder.getPackage() ) );
return list;
}
+ public void testPersistenceVariables() {
+ Environment env = KnowledgeBaseFactory.newEnvironment();
+ env.set( EnvironmentName.ENTITY_MANAGER_FACTORY,
+ emf );
+ env.set( "drools.TransactionManager",
+ TransactionManagerServices.getTransactionManager() );
+ KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+ Collection<KnowledgePackage> kpkgs = getProcessWorkItems();
+ kbase.addKnowledgePackages( kpkgs );
+ Properties properties = new Properties();
+ properties.setProperty("drools.commandService", "org.drools.persistence.session.SingleSessionCommandService");
+ properties.setProperty("drools.processInstanceManagerFactory", "org.drools.persistence.processinstance.JPAProcessInstanceManagerFactory");
+ properties.setProperty("drools.workItemManagerFactory", "org.drools.persistence.processinstance.JPAWorkItemManagerFactory");
+ properties.setProperty("drools.processSignalManagerFactory", "org.drools.persistence.processinstance.JPASignalManagerFactory");
+ SessionConfiguration config = new SessionConfiguration(properties);
+
+
+
+ SingleSessionCommandService service = new SingleSessionCommandService(kbase, config,env);
+ StartProcessCommand startProcessCommand = new StartProcessCommand();
+ startProcessCommand.setProcessId("org.drools.test.TestProcess");
+ Map<String,Object> parameters = new HashMap<String,Object>();
+
+ parameters.put("var1", new MyEntity("This is a test Entity"));
+ parameters.put("var2", new MyVariableSerializable("This is a test SerializableObject"));
+
+ startProcessCommand.setParameters(parameters);
+ ProcessInstance processInstance = (ProcessInstance) service.execute(startProcessCommand);
+ System.out.println("Started process instance " + processInstance.getId());
+
+ service = new SingleSessionCommandService(kbase, config,env);
+ GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
+ getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
+ processInstance = (ProcessInstance) service.execute(getProcessInstanceCommand);
+ assertNotNull(processInstance);
+
+ VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
+ ((RuleFlowProcessInstance)processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
+ Map<String, Object> variables = variableScopeInstance.getVariables();
+
+ MyEntity var1 = (MyEntity)variables.get("var1");
+ MyVariableSerializable var2 = (MyVariableSerializable)variables.get("var2");
+
+ assertNotNull(var1);
+ assertEquals("This is a test Entity", var1.getTest());
+ assertNotNull(var2);
+ assertEquals("This is a test SerializableObject", var2.getText());
+
+
+
+ }
}
Added: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf (rev 0)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf 2009-04-01 19:32:20 UTC (rev 25906)
@@ -0,0 +1,2 @@
+javax.persistence.Entity = org.drools.persistence.processinstance.JPAVariablePersister
+java.io.Serializable = org.drools.persistence.processinstance.SerializableVariablePersister
\ No newline at end of file
Modified: labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/drools.session.conf
===================================================================
--- labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/drools.session.conf 2009-04-01 17:59:39 UTC (rev 25905)
+++ labs/jbossrules/branches/salaboy_VariablesPersistenceStrategy2/drools-persistence-jpa/src/test/resources/META-INF/drools.session.conf 2009-04-01 19:32:20 UTC (rev 25906)
@@ -2,4 +2,5 @@
#drools.commandService = org.drools.persistence.session.SingleSessionCommandService
#drools.processInstanceManagerFactory = org.drools.persistence.processinstance.JPAProcessInstanceManagerFactory
#drools.workItemManagerFactory = org.drools.persistence.processinstance.JPAWorkItemManagerFactory
-#drools.processSignalManagerFactory = org.drools.persistence.processinstance.JPASignalManagerFactory
\ No newline at end of file
+#drools.processSignalManagerFactory = org.drools.persistence.processinstance.JPASignalManagerFactory
+drools.persistence.strategies = PersistenceStrategies.conf
\ No newline at end of file
More information about the jboss-svn-commits
mailing list