KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > aop > InternalDelegate


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.aop;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.aop.InstanceAdvisor;
12 import org.jboss.aop.Advised;
13 import org.jboss.aop.advice.Interceptor;
14 import org.jboss.aop.joinpoint.Invocation;
15 import org.jboss.aop.joinpoint.MethodInvocation;
16 import org.jboss.aop.proxy.ClassProxy;
17 import org.jboss.aop.proxy.ClassProxyFactory;
18 import org.jboss.aop.util.MethodHashing;
19 import org.jboss.util.NestedRuntimeException;
20 import org.jboss.cache.Fqn;
21 import org.jboss.cache.CacheException;
22 import org.jboss.cache.GlobalTransaction;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29 /**
30  * AopCache delegation to handle internal cache sotre, that is, the portion that is not part of user's data.
31  *
32  * @author Ben Wang
33  */

34 public class InternalDelegate
35 {
36    static Log log=LogFactory.getLog(InternalDelegate.class.getName());
37    public static final String JavaDoc CLASS_INTERNAL = "__jboss:internal:class__";
38    public static final Fqn JBOSS_INTERNAL = new Fqn("__JBossInternal__");
39    // We will try to break up the AopInstance inside the cache store.
40
public static final String JavaDoc AOPINSTANCE_REF_COUNT = "__AOPINSTANCE_COUNT__";
41    public static final String JavaDoc AOPINSTANCE_REF_FQN = "__AOPINSTANCE_FQN__";
42    // This is the key to store the pojo instance. But this is not replicated instead local only.
43
public static final String JavaDoc AOPINSTANCE_POJO = "__AOPINSTANCE_POJO__";
44
45    protected TreeCacheAop cache_;
46
47    InternalDelegate(TreeCacheAop cache)
48    {
49       cache_ = cache;
50    }
51
52 // Object getObjectInstance(AOPInstance aopInstance) {
53
// return aopInstance.get();
54
// }
55

56    void resetRefCount(Fqn fqn) throws CacheException
57    {
58       cache_.put(fqn, AOPINSTANCE_REF_COUNT, new Integer JavaDoc(1));
59    }
60
61    /**
62     * Increment reference count for the pojo. Note that this is not thread safe or atomic.
63     */

64    int incrementRefCount(Fqn fqn) throws CacheException
65    {
66       /**
67        * We used to bundle ref count and fqn with aop instance. But now split them up to avoid
68        * the class loading problem.
69        */

70       // Needs to be atomic.
71
Integer JavaDoc count = (Integer JavaDoc)cache_.peek(fqn, AOPINSTANCE_REF_COUNT);
72       if( count == null )
73          throw new RuntimeException JavaDoc("InternalDelegate.incrementRefCount(): REF_COUNT for reference counting is null at: " +fqn);
74
75       int counter = count.intValue() +1;
76       cache_.put(fqn, AOPINSTANCE_REF_COUNT, new Integer JavaDoc(counter));
77       return counter;
78    }
79
80    /**
81     * decrement reference count for the pojo. Note that this is not thread safe or atomic.
82     */

83    int decrementRefCount(Fqn fqn) throws CacheException
84    {
85       // Needs to be atomic.
86
Integer JavaDoc count = (Integer JavaDoc)cache_.peek(fqn, AOPINSTANCE_REF_COUNT);
87       if( count == null )
88          throw new RuntimeException JavaDoc("InternalDelegate.decrementRefCount(): REF_COUNT for reference counting is null at: " +fqn);
89
90       int counter = count.intValue();
91       if(counter == 0)
92          throw new RuntimeException JavaDoc("InternalDelegate.decrementRefCount(): REF_COUNT for reference counting is 0 at: " +fqn);
93
94       counter--;
95       cache_.put(fqn, AOPINSTANCE_REF_COUNT, new Integer JavaDoc(counter));
96       return counter;
97    }
98
99    String JavaDoc getRefFqn(Fqn fqn) throws CacheException {
100       return (String JavaDoc)cache_.peek(fqn, AOPINSTANCE_REF_FQN);
101    }
102
103    void setRefFqn(Fqn fqn, String JavaDoc internalFqn) throws CacheException
104    {
105       cache_.put(fqn, AOPINSTANCE_REF_FQN, internalFqn);
106    }
107
108    void removeRefFqn(Fqn fqn) throws CacheException
109    {
110       cache_.remove(fqn, AOPINSTANCE_REF_FQN);
111    }
112
113    Object JavaDoc getObjectInstance(Fqn fqn) throws CacheException
114    {
115       return getPOJO(fqn);
116    }
117
118    Object JavaDoc getPOJO(Fqn fqn) throws CacheException
119    {
120       return cache_.peek(fqn, AOPINSTANCE_POJO);
121    }
122
123    void setPOJO(Fqn fqn, Object JavaDoc obj) throws CacheException
124    {
125       // Should do this in TreeCache.
126
GlobalTransaction tx = cache_.getCurrentTransaction();
127       cache_._put(tx, fqn, AOPINSTANCE_POJO, obj, true);
128    }
129
130    /**
131     * We store the class name in string.
132     */

133    void putAopClazz(Fqn fqn, Class JavaDoc clazz) throws CacheException {
134       cache_.marshalledPut(fqn, CLASS_INTERNAL, clazz);
135    }
136
137    Class JavaDoc peekAopClazz(Fqn fqn) throws CacheException {
138       return (Class JavaDoc)cache_.marshalledGet(fqn, CLASS_INTERNAL);
139    }
140
141    public static boolean isInternalNode(Fqn fqn)
142    {
143      if(fqn.isChildOf(JBOSS_INTERNAL)) return true;
144      return false;
145    }
146
147    // TODO Need to mangle the name to obtain uniqueness?
148
Fqn createInternalNode(Fqn storedFqn)
149    {
150       Fqn fqn = new Fqn(JBOSS_INTERNAL, storedFqn);
151       return fqn;
152    }
153
154    public boolean isAopNode(Fqn fqn) throws CacheException
155    {
156       // We use REF_COUNT as a marker to denote aop node now.
157
if( cache_.peek(fqn, AOPINSTANCE_REF_COUNT) != null )
158          return true;
159       else
160          return false;
161    }
162
163 }
164
Popular Tags