summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/utils/ActDefCache.java
blob: bfd5cdb9f480d38159ec40c66206ab5b172412b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * 
 */
package com.c2kernel.utils;

import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.proxy.EntityProxyObserver;
import com.c2kernel.entity.proxy.ItemProxy;
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 = ((ActCacheEntry)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(this, ClusterStorage.VIEWPOINT, false);
		}
		public void finalize() {
			parent.removeAct(id);
			actProxy.unsubscribe(this);
		}
		public void add(C2KLocalObject contents) {
			parent.removeAct(id);
		}
	
		public void remove(String id) {
			parent.removeAct(id);
		}
		
		public String toString() {
			return "ActDef cache entry: "+id; 
		}
	}
}