KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.remoting.callback;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.util.Map JavaDoc;
6 import org.jboss.logging.Logger;
7 import org.jboss.remoting.SerializableStore;
8
9 /**
10  * This implementation does nothing other than throw away persisted objects and throw exceptions.
11  * This is to be use when don't have a proper store or don't care about throwing away callbacks when
12  * starting to run out of memory.
13  *
14  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
15  */

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

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

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

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

76    public void setConfig(Map JavaDoc config)
77    {
78    }
79
80    /**
81     * No op
82     *
83     * @throws Exception
84     */

85    public void start() throws Exception JavaDoc
86    {
87    }
88
89    /**
90     * No op
91     */

92    public void stop()
93    {
94    }
95
96    /**
97     * This is a no op method, but needed in order to be used as a service within JBoss AS.
98     *
99     * @throws Exception
100     */

101    public void create() throws Exception JavaDoc
102    {
103    }
104
105    /**
106     * This is a no op method, but needed in order to be used as a service within JBoss AS.
107     */

108    public void destroy()
109    {
110    }
111
112    /**
113     * Sets if store should clean up persisted files when shutdown (destroy()).
114     *
115     * @param purgeOnShutdown
116     */

117    public void setPurgeOnShutdown(boolean purgeOnShutdown)
118    {
119    }
120
121    /**
122     * Returns if store will clean up persisted files when shutdown (destroy()).
123     *
124     * @return
125     */

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