From d9722227d4b17704dcdf02b30121321bac533efe Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Sat, 20 Jul 2013 11:56:13 +0200 Subject: Refactor ActDefCache into DescriptionObjectCache. Create ActivityDef and StateMachine caches. Add StateMachine loading to LocalObjectLoader --- src/main/java/com/c2kernel/utils/ActDefCache.java | 105 +++++---------------- .../java/com/c2kernel/utils/DescriptionObject.java | 8 ++ .../com/c2kernel/utils/DescriptionObjectCache.java | 84 +++++++++++++++++ .../java/com/c2kernel/utils/LocalObjectLoader.java | 7 ++ .../java/com/c2kernel/utils/StateMachineCache.java | 45 +++++++++ 5 files changed, 170 insertions(+), 79 deletions(-) create mode 100644 src/main/java/com/c2kernel/utils/DescriptionObject.java create mode 100644 src/main/java/com/c2kernel/utils/DescriptionObjectCache.java create mode 100644 src/main/java/com/c2kernel/utils/StateMachineCache.java (limited to 'src/main/java/com/c2kernel/utils') diff --git a/src/main/java/com/c2kernel/utils/ActDefCache.java b/src/main/java/com/c2kernel/utils/ActDefCache.java index 2ee5c7c..23d8988 100644 --- a/src/main/java/com/c2kernel/utils/ActDefCache.java +++ b/src/main/java/com/c2kernel/utils/ActDefCache.java @@ -5,95 +5,42 @@ package com.c2kernel.utils; import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectNotFoundException; -import com.c2kernel.entity.proxy.EntityProxyObserver; import com.c2kernel.entity.proxy.ItemProxy; -import com.c2kernel.entity.proxy.MemberSubscription; import com.c2kernel.lifecycle.ActivityDef; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.persistency.outcome.Viewpoint; import com.c2kernel.process.Gateway; -public class ActDefCache { +public class ActDefCache extends DescriptionObjectCache { - SoftCache actCache = new SoftCache(); - - public ActivityDef get(String actName, String actVersion) throws ObjectNotFoundException, InvalidDataException { + + @Override + public String getDefRoot() { + return "/desc/ActivityDesc"; + } + + @Override + public ActivityDef loadObject(String name, String version, ItemProxy proxy) throws ObjectNotFoundException, InvalidDataException { ActivityDef thisActDef; - synchronized(actCache) { - ActCacheEntry thisActDefEntry = actCache.get(actName+"_"+actVersion); - if (thisActDefEntry == null) { - Logger.msg(6, actName+" v"+actVersion+" not found in cache. Retrieving."); - ItemProxy actDefItem = LocalObjectLoader.loadLocalObjectDef("/desc/ActivityDesc/", actName); - String actType = actDefItem.getProperty("Complexity"); - Viewpoint actView = (Viewpoint)actDefItem.getObject(ClusterStorage.VIEWPOINT + "/" + actType + "ActivityDef/" + actVersion); - String marshalledAct; - try { - marshalledAct = actView.getOutcome().getData(); - } catch (ClusterStorageException ex) { - Logger.error(ex); - throw new ObjectNotFoundException("Problem loading "+actName+" v"+actVersion+": "+ex.getMessage(), ""); - } - try { - thisActDef = (ActivityDef)Gateway.getMarshaller().unmarshall(marshalledAct); - } catch (Exception ex) { - Logger.error(ex); - throw new InvalidDataException("Could not unmarshall '"+actName+"' v"+actVersion+": "+ex.getMessage(), ""); - } - thisActDef.setName(actName); - thisActDef.setVersion(actVersion); - actCache.put(actName+"_"+actVersion, new ActCacheEntry(thisActDef, actDefItem, this)); - } - else { - Logger.msg(6, actName+" v"+actVersion+" found in cache."); - thisActDef = thisActDefEntry.actDef; - } - } + String actType = proxy.getProperty("Complexity"); + Viewpoint actView = (Viewpoint)proxy.getObject(ClusterStorage.VIEWPOINT + "/" + actType + "ActivityDef/" + version); + String marshalledAct; + try { + marshalledAct = actView.getOutcome().getData(); + } catch (ClusterStorageException ex) { + Logger.error(ex); + throw new ObjectNotFoundException("Problem loading "+name+" v"+version+": "+ex.getMessage(), ""); + } + try { + thisActDef = (ActivityDef)Gateway.getMarshaller().unmarshall(marshalledAct); + } catch (Exception ex) { + Logger.error(ex); + throw new InvalidDataException("Could not unmarshall '"+name+"' v"+version+": "+ex.getMessage(), ""); + } + thisActDef.setName(name); + thisActDef.setVersion(version); return thisActDef; } - public void removeAct(String id) { - synchronized(actCache) { - if (actCache.keySet().contains(id)) { - Logger.msg(7, "ActDefCache: Removing activity def "+id+" from cache"); - actCache.remove(id); - } - } - } - - public class ActCacheEntry implements EntityProxyObserver { - public String id; - public ItemProxy actProxy; - public ActivityDef actDef; - public ActDefCache parent; - public ActCacheEntry(ActivityDef actDef, ItemProxy actProxy, ActDefCache parent) { - this.id = actDef.getName()+"_"+actDef.getVersion(); - this.actDef = actDef; - this.parent = parent; - this.actProxy = actProxy; - actProxy.subscribe(new MemberSubscription(this, ClusterStorage.VIEWPOINT, false)); - } - @Override - public void finalize() { - parent.removeAct(id); - actProxy.unsubscribe(this); - } - @Override - public void add(Viewpoint contents) { - parent.removeAct(id); - } - - @Override - public void remove(String oldId) { - parent.removeAct(oldId); - } - - @Override - public String toString() { - return "ActDef cache entry: "+id; - } - @Override - public void control(String control, String msg) { - } - } } \ No newline at end of file diff --git a/src/main/java/com/c2kernel/utils/DescriptionObject.java b/src/main/java/com/c2kernel/utils/DescriptionObject.java new file mode 100644 index 0000000..eff0a57 --- /dev/null +++ b/src/main/java/com/c2kernel/utils/DescriptionObject.java @@ -0,0 +1,8 @@ +package com.c2kernel.utils; + +public interface DescriptionObject { + + public String getName(); + public String getVersion(); + +} diff --git a/src/main/java/com/c2kernel/utils/DescriptionObjectCache.java b/src/main/java/com/c2kernel/utils/DescriptionObjectCache.java new file mode 100644 index 0000000..a8cd197 --- /dev/null +++ b/src/main/java/com/c2kernel/utils/DescriptionObjectCache.java @@ -0,0 +1,84 @@ +/** + * + */ +package com.c2kernel.utils; + +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.entity.proxy.EntityProxyObserver; +import com.c2kernel.entity.proxy.ItemProxy; +import com.c2kernel.entity.proxy.MemberSubscription; +import com.c2kernel.persistency.ClusterStorage; +import com.c2kernel.persistency.outcome.Viewpoint; + +public abstract class DescriptionObjectCache { + + SoftCache> cache = new SoftCache>(); + + public D get(String name, String version) throws ObjectNotFoundException, InvalidDataException { + D thisDef; + synchronized(cache) { + CacheEntry thisDefEntry = cache.get(name+"_"+version); + if (thisDefEntry == null) { + Logger.msg(6, name+" v"+version+" not found in cache. Retrieving."); + ItemProxy defItem = LocalObjectLoader.loadLocalObjectDef(getDefRoot(), name); + thisDef = loadObject(name, version, defItem); + cache.put(name+"_"+version, new CacheEntry(thisDef, defItem, this)); + } + else { + Logger.msg(6, name+" v"+version+" found in cache."); + thisDef = thisDefEntry.def; + } + } + return thisDef; + } + + public abstract String getDefRoot(); + + public abstract D loadObject(String name, String version, ItemProxy proxy) throws ObjectNotFoundException, InvalidDataException; + + public void removeAct(String id) { + synchronized(cache) { + if (cache.keySet().contains(id)) { + Logger.msg(7, "ActDefCache: Removing activity def "+id+" from cache"); + cache.remove(id); + } + } + } + + public class CacheEntry implements EntityProxyObserver { + public String id; + public ItemProxy proxy; + public E def; + public DescriptionObjectCache parent; + public CacheEntry(E def, ItemProxy proxy, DescriptionObjectCache parent) { + this.id = def.getName()+"_"+def.getVersion(); + this.def = def; + this.parent = parent; + this.proxy = proxy; + proxy.subscribe(new MemberSubscription(this, ClusterStorage.VIEWPOINT, false)); + } + @Override + public void finalize() { + parent.removeAct(id); + proxy.unsubscribe(this); + } + @Override + public void add(Viewpoint contents) { + parent.removeAct(id); + } + + @Override + public void remove(String oldId) { + parent.removeAct(oldId); + } + + @Override + public String toString() { + return "Cache entry: "+id; + } + @Override + public void control(String control, String msg) { + } + } +} \ No newline at end of file diff --git a/src/main/java/com/c2kernel/utils/LocalObjectLoader.java b/src/main/java/com/c2kernel/utils/LocalObjectLoader.java index 63bb3c2..f139b84 100644 --- a/src/main/java/com/c2kernel/utils/LocalObjectLoader.java +++ b/src/main/java/com/c2kernel/utils/LocalObjectLoader.java @@ -4,6 +4,7 @@ import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.proxy.ItemProxy; import com.c2kernel.lifecycle.ActivityDef; +import com.c2kernel.lifecycle.instance.stateMachine.StateMachine; import com.c2kernel.lookup.DomainPath; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.persistency.ClusterStorageException; @@ -13,6 +14,7 @@ import com.c2kernel.process.Gateway; public class LocalObjectLoader { private static ActDefCache actCache = new ActDefCache(); + private static StateMachineCache smCache = new StateMachineCache(); static public ItemProxy loadLocalObjectDef(String root, String name) throws ObjectNotFoundException @@ -69,4 +71,9 @@ public class LocalObjectLoader { Logger.msg(5, "Loading activity def "+actName+" v"+actVersion); return actCache.get(actName, actVersion); } + + static public StateMachine getStateMachine(String smName, String smVersion) throws ObjectNotFoundException, InvalidDataException { + Logger.msg(5, "Loading activity def "+smName+" v"+smVersion); + return smCache.get(smName, smVersion); + } } diff --git a/src/main/java/com/c2kernel/utils/StateMachineCache.java b/src/main/java/com/c2kernel/utils/StateMachineCache.java new file mode 100644 index 0000000..0088efd --- /dev/null +++ b/src/main/java/com/c2kernel/utils/StateMachineCache.java @@ -0,0 +1,45 @@ +/** + * + */ +package com.c2kernel.utils; + +import com.c2kernel.common.InvalidDataException; +import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.entity.proxy.ItemProxy; +import com.c2kernel.lifecycle.instance.stateMachine.StateMachine; +import com.c2kernel.persistency.ClusterStorage; +import com.c2kernel.persistency.ClusterStorageException; +import com.c2kernel.persistency.outcome.Viewpoint; +import com.c2kernel.process.Gateway; + +public class StateMachineCache extends DescriptionObjectCache { + + + @Override + public String getDefRoot() { + return "/desc/StateMachine"; + } + + @Override + public StateMachine loadObject(String name, String version, ItemProxy proxy) throws ObjectNotFoundException, InvalidDataException { + StateMachine thisStateMachine; + Viewpoint smView = (Viewpoint)proxy.getObject(ClusterStorage.VIEWPOINT + "/StateMachine/" + version); + String marshalledSM; + try { + marshalledSM = smView.getOutcome().getData(); + } catch (ClusterStorageException ex) { + Logger.error(ex); + throw new ObjectNotFoundException("Problem loading State Machine "+name+" v"+version+": "+ex.getMessage(), ""); + } + try { + thisStateMachine = (StateMachine)Gateway.getMarshaller().unmarshall(marshalledSM); + } catch (Exception ex) { + Logger.error(ex); + throw new InvalidDataException("Could not unmarshall State Machine '"+name+"' v"+version+": "+ex.getMessage(), ""); + } + thisStateMachine.setName(name); + thisStateMachine.setVersion(version); + return thisStateMachine; + } + +} \ No newline at end of file -- cgit v1.2.3