KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > InvocationContext


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache;
8
9 import org.jboss.cache.config.Option;
10 import org.jboss.cache.marshall.MethodCall;
11
12 import javax.transaction.Transaction JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.LinkedList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * This context holds information specific to a method invocation.
19  *
20  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
21  */

22 public class InvocationContext implements Cloneable JavaDoc
23 {
24    private transient List JavaDoc<MethodCall> cacheListenerEvents = new LinkedList JavaDoc<MethodCall>();
25    private Transaction JavaDoc transaction;
26    private GlobalTransaction globalTransaction;
27    private Option optionOverrides;
28    // defaults to true.
29
private boolean originLocal = true;
30    private boolean txHasMods;
31    private boolean localRollbackOnly;
32
33    InvocationContext()
34    {
35    }
36
37    public void setLocalRollbackOnly(boolean localRollbackOnly)
38    {
39       this.localRollbackOnly = localRollbackOnly;
40    }
41
42
43    /**
44     * Retrieves the transaction associated with this invocation
45     *
46     * @return The transaction associated with this invocation
47     */

48    public Transaction JavaDoc getTransaction()
49    {
50       return transaction;
51    }
52
53    /**
54     * Sets the transaction associated with this invocation
55     *
56     * @param transaction
57     */

58    public void setTransaction(Transaction JavaDoc transaction)
59    {
60       this.transaction = transaction;
61    }
62
63    /**
64     * Retrieves the global transaction associated with this invocation
65     *
66     * @return the global transaction associated with this invocation
67     */

68    public GlobalTransaction getGlobalTransaction()
69    {
70       return globalTransaction;
71    }
72
73    /**
74     * Sets the global transaction associated with this invocation
75     *
76     * @param globalTransaction
77     */

78    public void setGlobalTransaction(GlobalTransaction globalTransaction)
79    {
80       this.globalTransaction = globalTransaction;
81    }
82
83    /**
84     * Retrieves the option overrides associated with this invocation
85     *
86     * @return the option overrides associated with this invocation
87     */

88    public Option getOptionOverrides()
89    {
90       if (optionOverrides == null)
91       {
92          optionOverrides = new Option();
93       }
94       return optionOverrides;
95    }
96
97    /**
98     * Sets the option overrides associated with this invocation
99     *
100     * @param optionOverrides
101     */

102    public void setOptionOverrides(Option optionOverrides)
103    {
104       this.optionOverrides = optionOverrides;
105    }
106
107    /**
108     * Tests if this invocation originated locally or from a remote cache.
109     *
110     * @return true if the invocation originated locally.
111     */

112    public boolean isOriginLocal()
113    {
114       return originLocal;
115    }
116
117    /**
118     * If set to true, the invocation is assumed to have originated locally. If set to false,
119     * assumed to have originated from a remote cache.
120     *
121     * @param originLocal
122     */

123    public void setOriginLocal(boolean originLocal)
124    {
125       this.originLocal = originLocal;
126    }
127
128    /**
129     * @return a list of cache listener events set during the course of this invocation
130     */

131    public List JavaDoc<MethodCall> getCacheListenerEvents()
132    {
133       return new ArrayList JavaDoc<MethodCall>(cacheListenerEvents);
134    }
135
136    /**
137     * Adds a cache listener event to the list of events created by the current invocation.
138     *
139     * @param event
140     */

141    public void addCacheListenerEvent(MethodCall event)
142    {
143       cacheListenerEvents.add(event);
144    }
145
146    public void clearCacheListenerEvents()
147    {
148       cacheListenerEvents.clear();
149    }
150
151    public String JavaDoc toString()
152    {
153       return "InvocationContext{" +
154               "transaction=" + transaction +
155               ", globalTransaction=" + globalTransaction +
156               ", optionOverrides=" + optionOverrides +
157               ", originLocal=" + originLocal +
158               ", txHasMods=" + txHasMods +
159               '}';
160    }
161
162    public boolean isTxHasMods()
163    {
164       return txHasMods;
165    }
166
167    public void setTxHasMods(boolean b)
168    {
169       txHasMods = b;
170    }
171
172    public boolean isLocalRollbackOnly()
173    {
174       return localRollbackOnly;
175    }
176
177    /**
178     * Resets this to the defaults used when constructing an invocation context object
179     */

180    public void reset()
181    {
182       transaction = null;
183       globalTransaction = null;
184       optionOverrides = null;
185       originLocal = true;
186       txHasMods = false;
187       cacheListenerEvents.clear();
188    }
189
190    public InvocationContext clone() throws CloneNotSupportedException JavaDoc
191    {
192       InvocationContext clone = (InvocationContext) super.clone();
193       clone.setOptionOverrides(getOptionOverrides().clone());
194       return clone;
195    }
196
197    /**
198     * Sets the state of the InvocationContext based on the template context passed in
199     *
200     * @param template
201     */

202    public void setState(InvocationContext template)
203    {
204       if (template == null)
205       {
206          throw new NullPointerException JavaDoc("Template InvocationContext passed in to InvocationContext.setState() passed in is null");
207       }
208
209       this.setGlobalTransaction(template.getGlobalTransaction());
210       this.setLocalRollbackOnly(template.isLocalRollbackOnly());
211       this.setOptionOverrides(template.getOptionOverrides());
212       this.setOriginLocal(template.isOriginLocal());
213       this.setTransaction(template.getTransaction());
214       this.setTxHasMods(template.isTxHasMods());
215    }
216
217    public boolean equals(Object JavaDoc o)
218    {
219       if (this == o) return true;
220       if (o == null || getClass() != o.getClass()) return false;
221
222       final InvocationContext that = (InvocationContext) o;
223
224       if (localRollbackOnly != that.localRollbackOnly) return false;
225       if (originLocal != that.originLocal) return false;
226       if (txHasMods != that.txHasMods) return false;
227       if (globalTransaction != null ? !globalTransaction.equals(that.globalTransaction) : that.globalTransaction != null)
228       {
229          return false;
230       }
231       if (optionOverrides != null ? !optionOverrides.equals(that.optionOverrides) : that.optionOverrides != null)
232       {
233          return false;
234       }
235       if (transaction != null ? !transaction.equals(that.transaction) : that.transaction != null) return false;
236
237       return true;
238    }
239
240    public int hashCode()
241    {
242       int result;
243       result = (transaction != null ? transaction.hashCode() : 0);
244       result = 29 * result + (globalTransaction != null ? globalTransaction.hashCode() : 0);
245       result = 29 * result + (optionOverrides != null ? optionOverrides.hashCode() : 0);
246       result = 29 * result + (originLocal ? 1 : 0);
247       result = 29 * result + (txHasMods ? 1 : 0);
248       result = 29 * result + (localRollbackOnly ? 1 : 0);
249       return result;
250    }
251 }
252
Popular Tags