KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > NullCallbackStore


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.remoting;
8
9 import org.jboss.logging.Logger;
10
11 import java.io.IOException JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.util.Map JavaDoc;
14
15 /**
16  * This implementation does nothing other than throw away persisted objects and throw exceptions.
17  * This is to be use when don't have a proper store or don't care about throwing away callbacks when
18  * starting to run out of memory.
19  *
20  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
21  */

22 public class NullCallbackStore implements SerializableStore, Serializable JavaDoc
23 {
24    static final long serialVersionUID = -8182007953992756845L;
25    private boolean isCallbackLost = false;
26
27    private static final Logger log = Logger.getLogger(NullCallbackStore.class);
28
29    /**
30     * Getst the number of objects stored and available.
31     *
32     * @return
33     */

34    public int size()
35    {
36       return isCallbackLost ? 1 : 0;
37    }
38
39    /**
40     * Will look through the files in the store directory for the oldest object serialized to disk, load it,
41     * delete the file, and return the deserialized object.
42     * Important to note that once this object is returned from this method, it is gone forever from this
43     * store and will not be able to retrieve it again without adding it back.
44     *
45     * @return
46     * @throws java.io.IOException
47     */

48    public Object JavaDoc getNext() throws IOException JavaDoc
49    {
50       if(isCallbackLost)
51       {
52          isCallbackLost = false;
53          return new FailedCallback("This is an invalid callback. The server ran out of memory, so callbacks were lost.");
54       }
55       else
56       {
57          return null;
58       }
59    }
60
61    /**
62     * Persists the serializable object passed to the directory specified. The file name will be the current time
63     * in milliseconds (vis System.currentTimeMillis()) with the specified suffix. This object can later be
64     * retrieved using the getNext() method, but objects will be returned in the order that they were added (FIFO).
65     *
66     * @param object
67     * @throws java.io.IOException
68     */

69    public void add(Serializable JavaDoc object) throws IOException JavaDoc
70    {
71       isCallbackLost = true;
72       log.error("Lost callback because not enough free memory available. Callback lost was " + object);
73       throw new IOException JavaDoc("Callback has been lost because not enough free memory to hold object.");
74    }
75
76    /**
77     * No op
78     *
79     * @param config
80     */

81    public void setConfig(Map JavaDoc config)
82    {
83    }
84
85    /**
86     * No op
87     *
88     * @throws Exception
89     */

90    public void start() throws Exception JavaDoc
91    {
92    }
93
94    /**
95     * No op
96     */

97    public void stop()
98    {
99    }
100
101    /**
102     * This is a no op method, but needed in order to be used as a service within JBoss AS.
103     *
104     * @throws Exception
105     */

106    public void create() throws Exception JavaDoc
107    {
108    }
109
110    /**
111     * This is a no op method, but needed in order to be used as a service within JBoss AS.
112     */

113    public void destroy()
114    {
115    }
116
117    /**
118     * Sets if store should clean up persisted files when shutdown (destroy()).
119     *
120     * @param purgeOnShutdown
121     */

122    public void setPurgeOnShutdown(boolean purgeOnShutdown)
123    {
124    }
125
126    /**
127     * Returns if store will clean up persisted files when shutdown (destroy()).
128     *
129     * @return
130     */

131    public boolean getPurgeOnShutdown()
132    {
133       return false;
134    }
135
136    public void purgeFiles()
137    {
138    }
139
140    public class FailedCallback extends Callback
141    {
142
143       public FailedCallback(Object JavaDoc callbackPayload)
144       {
145          super(callbackPayload);
146       }
147
148       public Object JavaDoc getCallbackObject()
149       {
150          throw new RuntimeException JavaDoc("This is an invalid callback. The server ran out of memory, so callbacks were lost.");
151       }
152    }
153 }
154
Popular Tags