KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > cache > CachedObjectFactory


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool.cache;
5
6 import java.io.PrintWriter JavaDoc;
7
8 /**
9  * Creates objects for a cache. The cache is essentially a map,
10  * so this factory is given a "key" and creates the appropriate "value"
11  * which will be cached. There are a number of other functions that
12  * can be overridden for more specific control, but createObject is
13  * the only one that's required.
14  *
15  * @author Aaron Mulder ammulder@alumni.princeton.edu
16  */

17 public abstract class CachedObjectFactory {
18
19     public CachedObjectFactory() {
20     }
21
22     /**
23      * Creates a new object to be stored in an object cache. This is the
24      * instance that will actually be stored in the cache and reused. If you
25      * want to wrap it somehow, or return instances of a different type that
26      * refers to these, you can implement prepareObject.
27      * @see #prepareObject
28      */

29     public abstract Object JavaDoc createObject(Object JavaDoc identifier);
30
31     /*
32      * Indicates to the factory that the cache has started up. This will be
33      * called before any other methods of the factory are called (on behalf of
34      * this cache).
35      * @param cache The cache that is starting. You may decide to allow
36      * multiple cached you use your factory, or to restrict it to a one-to-one
37      * relationship.
38      * @param log A writer you can use to log messages. Use this in preference
39      * to System.xxx.println.
40      * @throws java.lang.IllegalArgumentException
41      * Occurs when the cache is null.
42      */

43     public void cacheStarted(ObjectCache cache, PrintWriter JavaDoc log) {
44         if (cache == null)
45             throw new IllegalArgumentException JavaDoc("Cannot start factory with null cache!");
46     }
47
48     /**
49      * Prepares an object to be returned to the client. This may be used to
50      * configure the object somehow, or actually return a completely different
51      * object (so long as the original can be recovered in translateObject.
52      * This will be called whenever an object is returned to the client, whether
53      * a new object or a cached object.
54      * @param cachedObject The object in the cache, as created by createObject.
55      * @return The object to return to the client. If different, the cached
56      * object must be recoverable by translateObject.
57      */

58     public Object JavaDoc prepareObject(Object JavaDoc cachedObject) {
59         return cachedObject;
60     }
61
62     /**
63      * If the objects supplied to the client are different than the objects in
64      * the cache, extracts a cache object from a client object. This may be
65      * called once after an object has been released if the garbage collector
66      * and a client attempt to release an object at the same time. In this
67      * case, this method may work, return null, or throw an exception and the
68      * cache will handle it gracefully. The default implementation returns the
69      * parameter object (assumes client and cached objects are the same).
70      * @param clientObject The client object, as returned by prepareObject
71      * @return The cached object, as originally returned by createObject
72      */

73     public Object JavaDoc translateObject(Object JavaDoc clientObject) {
74         return clientObject;
75     }
76
77     /**
78      * Indicates to the factory that the cache is closing down. This will be
79      * called before all the instances are destroyed. There may be calls to
80      * returnObject or translateObject after this, but no calls to
81      * createObject or prepareObject (on behalf of this cache).
82      * @param cache The cache that is closing. You may decide to allow
83      * multiple caches you use your factory, or to restrict it to a one-to-one
84      * relationship.
85      * @throws java.lang.IllegalArgumentException
86      * Occurs when the pool is null.
87      */

88     public void cacheClosing(ObjectCache cache) {
89         if (cache == null)
90             throw new IllegalArgumentException JavaDoc("Cannot close factory with a null cache!");
91     }
92
93     /**
94      * Permanently closes an object, after it is removed from the cache. The
95      * object will not be returned to the cache - after this, it is gone. This
96      * is called when the cache is full and new objects are added, and when
97      * the cache is closed.
98      */

99     public void deleteObject(Object JavaDoc pooledObject) {
100     }
101 }
102
Popular Tags