KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > ejb > LocalCache


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.ejb;
13
14 import com.versant.core.common.OID;
15 import com.versant.core.common.State;
16 import com.versant.core.common.BindingSupportImpl;
17 import com.versant.core.ejb.common.EntrySet;
18
19 import java.util.Iterator JavaDoc;
20
21 /**
22  * This is a cache that keeps references to instances that are managed by an EntityManager.
23  * The type of reference can be weak/soft/hard.
24  */

25 public class LocalCache {
26     /**
27      * This is the set of managed instances.
28      */

29     private EntrySet set = new EntrySet();
30
31     /**
32      * Add a sm to the cache. This method does not expect to find a existing
33      * mapping by this id in the cache.
34      * @param oid
35      * @param sm
36      * @return
37      */

38     public StateManagerImp add(OID oid, StateManagerImp sm) {
39         CacheEntry e = (CacheEntry) set.get(oid);
40         if (e == null) {
41             set.addEntry(createEntry(oid, sm));
42         } else {
43             if (e.val == null) {
44                 throw BindingSupportImpl.getInstance().internal("Cache entry with no value");
45             } else if (!(e.val instanceof State)) {
46                 /**
47                  * This exception migh occor if the user assigned a already existing
48                  * id to a new instance and persisted it.
49                  */

50                 throw new RuntimeException JavaDoc("There is already an instance in the cache with same id");
51             } else {
52                 e.val = sm;
53             }
54         }
55         return sm;
56     }
57
58     public void add(OID oid, State state) {
59         CacheEntry e = (CacheEntry) set.get(oid);
60         if (e == null) {
61             set.addEntry(createEntry(oid, state));
62         } else {
63             Object JavaDoc value = e.val;
64             if (value instanceof StateManagerImp) {
65                 StateManagerImp sm = (StateManagerImp) value;
66                 sm.updateState(state);
67             } else {
68                 ((State) value).updateNonFilled(state);
69             }
70         }
71     }
72
73     public Object JavaDoc get(OID oid) {
74         CacheEntry e = (CacheEntry) set.get(oid);
75         if (e != null) {
76             return e.getValue();
77         }
78         return null;
79     }
80
81     /**
82      * Create a entry instance to be used. Must determine if this should be a weak/soft/hard entry.
83      * @param oid
84      * @param value
85      * @return
86      */

87     private CacheEntry createEntry(OID oid, Object JavaDoc value) {
88         return new CacheEntry(oid, value);
89     }
90
91     public void clear() {
92         set.clear();
93     }
94
95
96
97     public class CacheEntry extends EntrySet.Entry {
98         private Object JavaDoc val;
99
100         public CacheEntry(OID key, Object JavaDoc val) {
101             super(key);
102             this.val = val;
103         }
104
105         public Object JavaDoc getValue() {
106             return val;
107         }
108     }
109
110     public void processReferenceQueue() {
111     }
112
113     public Iterator JavaDoc getCacheIterator() {
114         processReferenceQueue();
115         return set.newEntryIterator();
116     }
117 }
118
Popular Tags