From b086f57f56bf0eb9dab9cf321a0f69aaaae84347 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 30 May 2012 08:37:45 +0200 Subject: Initial Maven Conversion --- src/main/java/com/c2kernel/utils/ActDefCache.java | 98 +++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/main/java/com/c2kernel/utils/ActDefCache.java (limited to 'src/main/java/com/c2kernel/utils/ActDefCache.java') diff --git a/src/main/java/com/c2kernel/utils/ActDefCache.java b/src/main/java/com/c2kernel/utils/ActDefCache.java new file mode 100644 index 0000000..0c9fea1 --- /dev/null +++ b/src/main/java/com/c2kernel/utils/ActDefCache.java @@ -0,0 +1,98 @@ +/** + * + */ +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; + +public class ActDefCache { + + SoftCache actCache = new SoftCache(); + + public ActivityDef get(String actName, String actVersion) 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)CastorXMLUtility.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; + } + } + 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 id) { + parent.removeAct(id); + } + + @Override + public String toString() { + return "ActDef cache entry: "+id; + } + @Override + public void control(String control, String msg) { + } + } +} \ No newline at end of file -- cgit v1.2.3