[jboss-svn-commits] JBL Code SVN: r29275 - in labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src: test/java/org/drools/persistence/session and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Sep 9 10:43:55 EDT 2009
Author: KrisVerlaenen
Date: 2009-09-09 10:43:54 -0400 (Wed, 09 Sep 2009)
New Revision: 29275
Removed:
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf
Modified:
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/VariablePersistenceStrategyTest.java
labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf
Log:
- minor tweaks
- made sure variable persistence only works if configured
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-09-09 12:51:35 UTC (rev 29274)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-09-09 14:43:54 UTC (rev 29275)
@@ -78,17 +78,17 @@
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "processId")
@MapKey(name = "name")
- private Map<String, VariableInstanceInfo> variables;
+ private Map<String, VariableInstanceInfo> variables =
+ new HashMap<String, VariableInstanceInfo>();
+ private boolean externalVariables = false;
ProcessInstanceInfo() {
- this.variables = new HashMap<String, VariableInstanceInfo>();
}
public ProcessInstanceInfo(ProcessInstance processInstance) {
this.processInstance = processInstance;
this.processId = processInstance.getProcessId();
startDate = new Date();
- this.variables = new HashMap<String, VariableInstanceInfo>();
}
public ProcessInstanceInfo(ProcessInstance processInstance, Environment env) {
@@ -96,7 +96,6 @@
this.processId = processInstance.getProcessId();
startDate = new Date();
this.env = env;
- this.variables = new HashMap<String, VariableInstanceInfo>();
}
public long getId() {
@@ -138,7 +137,7 @@
null, null);
context.wm = (InternalWorkingMemory) workingMemory;
ProcessInstanceMarshaller marshaller = getMarshallerFromContext(context);
- processInstance = marshaller.readProcessInstance(context, false);
+ processInstance = marshaller.readProcessInstance(context, !externalVariables);
restoreVariables();
context.close();
} catch (IOException e) {
@@ -178,9 +177,9 @@
VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
((ContextableInstance) nodeInstanceContainer)
.getContextInstance(VariableScope.VARIABLE_SCOPE);
- Object value = VariablePersistenceStrategyFactory
- .getVariablePersistenceStrategyFactory().getVariable(
- variableInfo, this.env);
+ VariablePersistenceStrategy persistenceStrategy =
+ VariablePersistenceStrategyFactory.getVariablePersistenceStrategy();
+ Object value = persistenceStrategy.getVariable(variableInfo, this.env);
System.out.println(">>>>> Restoring variable " + variableName + " = " + value);
variableScopeInstance.setVariable(variableName, value);
} else {
@@ -225,7 +224,8 @@
stream.writeUTF(processType);
ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE
.getMarshaller(processType);
- marshaller.writeProcessInstance(context, processInstance, false);
+ externalVariables = VariablePersistenceStrategyFactory.getVariablePersistenceStrategy().isEnabled();
+ marshaller.writeProcessInstance(context, processInstance, !externalVariables);
variablesChanged = persistVariables();
context.close();
} catch (IOException e) {
@@ -260,15 +260,20 @@
if (nodeInstances.size() > 0) {
persistNodeVariables(nodeInstances, "", newVariables);
}
- // clear variables so unnecessary values are removed
- this.variables.clear();
- this.variables.putAll(newVariables);
- return true;
+ if (newVariables.size() > 0 || this.variables.size() > 0) {
+ // clear variables so unnecessary values are removed
+ this.variables.clear();
+ this.variables.putAll(newVariables);
+ // TODO: how can I know that no variables were changed?
+ return true;
+ } else {
+ return false;
+ }
}
private void persist(Map<String, Object> variables, String prefix, Map<String, VariableInstanceInfo> newVariables) {
- VariablePersistenceStrategy persistenceStrategy = VariablePersistenceStrategyFactory
- .getVariablePersistenceStrategyFactory("PersistenceStrategies.conf");
+ VariablePersistenceStrategy persistenceStrategy =
+ VariablePersistenceStrategyFactory.getVariablePersistenceStrategy();
for (Map.Entry<String, Object> entries : variables.entrySet()) {
String variableName = prefix + entries.getKey();
Object value = entries.getValue();
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java 2009-09-09 12:51:35 UTC (rev 29274)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategy.java 2009-09-09 14:43:54 UTC (rev 29275)
@@ -3,7 +3,8 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
-import java.util.Properties;
+import java.util.HashMap;
+import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -13,12 +14,25 @@
/**
*
+ * @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
* @author salaboy
*/
public class VariablePersistenceStrategy {
- private Properties types;
-
+ // map of variable persisters per type
+ private Map<String, String> types = new HashMap<String, String>();
+ // cache of already instantiated variable persisters
+ private Map<String, VariablePersister> variablePersisters =
+ new HashMap<String, VariablePersister>();
+
+ public void setPersister(String type, String persisterClassname) {
+ types.put(type, persisterClassname);
+ }
+
+ public boolean isEnabled() {
+ return !types.isEmpty();
+ }
+
public VariableInstanceInfo persistVariable(String name, Object o,
VariableInstanceInfo oldValue, Environment env) {
VariablePersister persister = getVariablePersister(o);
@@ -35,10 +49,15 @@
String persisterFQN = getVariablePersistenceType(o);
if (persisterFQN != null && !persisterFQN.equals("")) {
Class<VariablePersister> persisterClass = null;
+ persister = variablePersisters.get(persisterFQN);
+ if (persister != null) {
+ return persister;
+ }
try {
persisterClass = (Class<VariablePersister>) Class.forName(persisterFQN);
Constructor<VariablePersister> constructor = persisterClass.getConstructor();
persister = (VariablePersister) constructor.newInstance();
+ variablePersisters.put(persisterFQN, persister);
return persister;
} catch (ClassNotFoundException ex) {
Logger.getLogger(VariablePersistenceStrategy.class.getName())
@@ -69,11 +88,14 @@
@SuppressWarnings("unchecked")
public Object getVariable(VariableInstanceInfo variableInfo, Environment env) {
try {
- String variablePersister = variableInfo.getPersister();
- VariablePersister persister = null;
- Class<VariablePersister> clazz = (Class<VariablePersister>) Class.forName(variablePersister);
- Constructor<VariablePersister> constructor = clazz.getDeclaredConstructor();
- persister = (VariablePersister) constructor.newInstance();
+ String persisterFQN = variableInfo.getPersister();
+ VariablePersister persister = variablePersisters.get(persisterFQN);
+ if (persister == null) {
+ Class<VariablePersister> clazz = (Class<VariablePersister>) Class.forName(persisterFQN);
+ Constructor<VariablePersister> constructor = clazz.getDeclaredConstructor();
+ persister = (VariablePersister) constructor.newInstance();
+ variablePersisters.put(persisterFQN, persister);
+ }
return persister.getExternalPersistedVariable(variableInfo, env);
} catch (InstantiationException ex) {
ex.printStackTrace();
@@ -113,10 +135,9 @@
}
Annotation[] annotations = o.getClass().getDeclaredAnnotations();
if (annotations != null) {
- // First annotations, because annotations have more precedence (??)
+ // First annotations, because annotations have more precedence
for (Annotation annotation : annotations) {
- String persisterFQN = types.getProperty(
- annotation.annotationType().getName());
+ String persisterFQN = types.get(annotation.annotationType().getName());
if (persisterFQN != null && !persisterFQN.equals("")) {
return persisterFQN;
}
@@ -126,7 +147,7 @@
Class<?>[] interfaces = o.getClass().getInterfaces();
if (interfaces != null) {
for (Class<?> clazz : interfaces) {
- String persisterFQN = types.getProperty(clazz.getName());
+ String persisterFQN = types.get(clazz.getName());
if (persisterFQN != null && !persisterFQN.equals("")) {
return persisterFQN;
}
@@ -135,11 +156,4 @@
return null;
}
- public Properties getTypes() {
- return types;
- }
-
- public void setTypes(Properties types) {
- this.types = types;
- }
}
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java 2009-09-09 12:51:35 UTC (rev 29274)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/VariablePersistenceStrategyFactory.java 2009-09-09 14:43:54 UTC (rev 29275)
@@ -1,73 +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.processinstance;
-
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
+import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
-
/**
- *
+ *
+ * @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
* @author salaboy
*/
+public class VariablePersistenceStrategyFactory {
+
+ private static VariablePersistenceStrategy INSTANCE;
-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");
-
-
- }
+ public static VariablePersistenceStrategy getVariablePersistenceStrategy() {
+ if (INSTANCE == null) {
+ INSTANCE = new VariablePersistenceStrategy();
+ loadPersisters();
+ }
+ return INSTANCE;
+ }
+
+ private static VariablePersistenceStrategy loadPersisters() {
+ Properties props = new Properties();
+ try {
+ InputStream is = Thread.currentThread().getContextClassLoader()
+ .getResourceAsStream("META-INF/PersistenceStrategies.conf");
+ if (is != null) {
+ props.load(is);
+ }
+ } catch (IOException ex) {
+ Logger.getLogger(VariablePersistenceStrategyFactory.class.getName())
+ .log(Level.SEVERE, null, ex);
+ }
+ if (INSTANCE == null) {
+ INSTANCE = new VariablePersistenceStrategy();
+ }
+ for (Entry<Object, Object> entry : props.entrySet()) {
+ INSTANCE.setPersister((String) entry.getKey(), (String) entry.getValue());
+ }
+ return INSTANCE;
+ }
+
}
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java 2009-09-09 12:51:35 UTC (rev 29274)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/SingleSessionCommandServiceTest.java 2009-09-09 14:43:54 UTC (rev 29275)
@@ -72,31 +72,6 @@
"sasa" );
ds1.getDriverProperties().put( "URL",
"jdbc:h2:mem:mydb" );
-
-// ds1.setClassName( "org.postgresql.xa.PGXADataSource" );
-// ds1.setMaxPoolSize( 3 );
-// ds1.setAllowLocalTransactions( true );
-// ds1.getDriverProperties().put( "user",
-// "salaboy" );
-// ds1.getDriverProperties().put( "password",
-// "salaboy" );
-// ds1.getDriverProperties().put( "serverName",
-// "localhost:5432/droolsPersistence" );
-
-
- ds1.setClassName( "org.postgresql.xa.PGXADataSource" );
- ds1.setMaxPoolSize( 3 );
- ds1.setAllowLocalTransactions( true );
- ds1.getDriverProperties().put( "user",
- "salaboy" );
- ds1.getDriverProperties().put( "password",
- "salaboy" );
- ds1.getDriverProperties().put("databaseName", "droolsPersistence");
- ds1.getDriverProperties().put("serverName", "localhost");
-
-
-
-
ds1.init();
emf = Persistence.createEntityManagerFactory( "org.drools.persistence.jpa" );
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/VariablePersistenceStrategyTest.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/VariablePersistenceStrategyTest.java 2009-09-09 12:51:35 UTC (rev 29274)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/VariablePersistenceStrategyTest.java 2009-09-09 14:43:54 UTC (rev 29275)
@@ -18,6 +18,7 @@
import org.drools.builder.ResourceType;
import org.drools.io.impl.ClassPathResource;
import org.drools.persistence.jpa.JPAKnowledgeService;
+import org.drools.persistence.processinstance.VariablePersistenceStrategyFactory;
import org.drools.persistence.processinstance.variabletypes.VariableInstanceInfo;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;
@@ -46,6 +47,12 @@
ds1.getDriverProperties().put( "URL",
"jdbc:h2:mem:mydb" );
ds1.init();
+ VariablePersistenceStrategyFactory.getVariablePersistenceStrategy()
+ .setPersister("javax.persistence.Entity",
+ "org.drools.persistence.processinstance.persisters.JPAVariablePersister");
+ VariablePersistenceStrategyFactory.getVariablePersistenceStrategy()
+ .setPersister("java.io.Serializable",
+ "org.drools.persistence.processinstance.persisters.SerializableVariablePersister");
}
@Override
Deleted: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf 2009-09-09 12:51:35 UTC (rev 29274)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/META-INF/PersistenceStrategies.conf 2009-09-09 14:43:54 UTC (rev 29275)
@@ -1,2 +0,0 @@
-javax.persistence.Entity = org.drools.persistence.processinstance.persisters.JPAVariablePersister
-java.io.Serializable = org.drools.persistence.processinstance.persisters.SerializableVariablePersister
\ No newline at end of file
Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf 2009-09-09 12:51:35 UTC (rev 29274)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf 2009-09-09 14:43:54 UTC (rev 29275)
@@ -36,7 +36,18 @@
<nodes>
<start id="1" name="Start" x="16" y="16" width="48" height="48" />
<end id="3" name="End" x="669" y="16" width="48" height="48" />
- <humanTask id="4" name="Human Task" x="208" y="16" width="93" height="48" >
+ <actionNode id="6" name="Action" x="333" y="20" width="80" height="40" >
+ <action type="expression" dialect="java" >System.out.println("x = " + x);
+System.out.println("y = " + y);
+System.out.println("z = " + z);
+System.out.println("a = " + a);
+System.out.println("b = " + b);
+System.out.println("c = " + c);
+kcontext.setVariable("a", "Some new String");
+kcontext.setVariable("b", new MyEntity("This is a new test Entity"));
+kcontext.setVariable("c", new MyVariableSerializable("This is a new test SerializableObject"));</action>
+ </actionNode>
+ <humanTask id="7" name="Human Task" x="445" y="20" width="80" height="40" >
<work name="Human Task" >
<parameter name="ActorId" >
<type name="org.drools.process.core.datatype.impl.type.StringDataType" />
@@ -58,20 +69,18 @@
</parameter>
</work>
</humanTask>
- <actionNode id="5" name="Action" x="96" y="20" width="80" height="40" >
+ <actionNode id="8" name="Action" x="557" y="20" width="80" height="40" >
<action type="expression" dialect="java" >System.out.println("x = " + x);
System.out.println("y = " + y);
-System.out.println("z = " + z);</action>
- </actionNode>
- <actionNode id="6" name="Action" x="333" y="20" width="80" height="40" >
- <action type="expression" dialect="java" >System.out.println("x = " + x);
-System.out.println("y = " + y);
System.out.println("z = " + z);
-kcontext.setVariable("a", "Some new String");
-kcontext.setVariable("c", new MyEntity("This is a new test Entity"));
-kcontext.setVariable("b", new MyVariableSerializable("This is a new test SerializableObject"));</action>
+System.out.println("a = " + a);
+System.out.println("b = " + b);
+System.out.println("c = " + c);
+kcontext.setVariable("a", "Some changed String");
+kcontext.setVariable("c", new MyEntity("This is a changed test Entity"));
+kcontext.setVariable("b", new MyVariableSerializable("This is a changed test SerializableObject"));</action>
</actionNode>
- <humanTask id="7" name="Human Task" x="445" y="20" width="80" height="40" >
+ <humanTask id="9" name="Human Task" x="445" y="20" width="80" height="40" >
<work name="Human Task" >
<parameter name="ActorId" >
<type name="org.drools.process.core.datatype.impl.type.StringDataType" />
@@ -93,24 +102,22 @@
</parameter>
</work>
</humanTask>
- <actionNode id="8" name="Action" x="557" y="20" width="80" height="40" >
+ <actionNode id="10" name="Action" x="557" y="20" width="80" height="40" >
<action type="expression" dialect="java" >System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
-kcontext.setVariable("c", "Some changed String");
-kcontext.setVariable("a", new MyEntity("This is a changed test Entity"));
-kcontext.setVariable("b", new MyVariableSerializable("This is a changed test SerializableObject"));</action>
+ </action>
</actionNode>
</nodes>
<connections>
- <connection from="8" to="3" />
- <connection from="5" to="4" />
- <connection from="1" to="5" />
- <connection from="4" to="6" />
+ <connection from="8" to="9" />
+ <connection from="9" to="10" />
+ <connection from="10" to="3" />
+ <connection from="1" to="6" />
<connection from="6" to="7" />
<connection from="7" to="8" />
</connections>
More information about the jboss-svn-commits
mailing list