KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > cache > AbstractMetaCache


1 package org.apache.ojb.broker.cache;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.Identity;
19
20 /**
21  * An abstract 'meta' implementation of the {@link ObjectCache}
22  * interace.
23  * <br/>
24  * Implement the abstract {@link #getCache} method in sub-classes.
25  * All base Object/Identity validation is done by this class.
26  *
27  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
28  * @version $Id: AbstractMetaCache.java,v 1.5.2.2 2005/12/21 22:24:15 tomdz Exp $
29  */

30 public abstract class AbstractMetaCache implements ObjectCache
31 {
32     public static final int METHOD_CACHE = 1;
33     public static final int METHOD_LOOKUP = 2;
34     public static final int METHOD_REMOVE = 3;
35
36     /**
37      * This method handle all calls against the {@link ObjectCache} interface.
38      * Note: The parameter <code>obj</code> can be <code>null</code> - e.g. when
39      * lookup or remove method was called.
40      *
41      * @param oid Identity of the target object.
42      * @param obj The target object itself or <code>null</code> if not available.
43      * @param callingMethod Specifies the type of method call against the {@link ObjectCache}
44      * interface. {@link #METHOD_CACHE}, {@link #METHOD_LOOKUP}, {@link #METHOD_REMOVE}.
45      * @return The {@link ObjectCache} implementation.
46      */

47     public abstract ObjectCache getCache(Identity oid, Object JavaDoc obj, int callingMethod);
48
49     /**
50      * Caches the given object using the given Identity as key
51      *
52      * @param oid The Identity key
53      * @param obj The object o cache
54      */

55     public void cache(Identity oid, Object JavaDoc obj)
56     {
57         if (oid != null && obj != null)
58         {
59             ObjectCache cache = getCache(oid, obj, METHOD_CACHE);
60             if (cache != null)
61             {
62                 cache.cache(oid, obj);
63             }
64         }
65     }
66
67     /**
68      * We delegate this method to the standard cache method.
69      * <br/>
70      * ++ Override if needed ++
71      */

72     public boolean cacheIfNew(Identity oid, Object JavaDoc obj)
73     {
74         cache(oid, obj);
75         return true;
76     }
77
78     /**
79      * Looks up the object from the cache
80      *
81      * @param oid The Identity to look up the object for
82      * @return The object if found, otherwise null
83      */

84     public Object JavaDoc lookup(Identity oid)
85     {
86         Object JavaDoc ret = null;
87         if (oid != null)
88         {
89             ObjectCache cache = getCache(oid, null, METHOD_LOOKUP);
90             if (cache != null)
91             {
92                 ret = cache.lookup(oid);
93             }
94         }
95         return ret;
96     }
97
98     /**
99      * Removes the given object from the cache
100      *
101      * @param oid oid of the object to remove
102      */

103     public void remove(Identity oid)
104     {
105         if (oid == null) return;
106
107         ObjectCache cache = getCache(oid, null, METHOD_REMOVE);
108         if (cache != null)
109         {
110             cache.remove(oid);
111         }
112     }
113 }
114
Popular Tags