KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > Invocation


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.invocation;
23
24 import java.io.Serializable JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.security.Principal JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import javax.transaction.Transaction JavaDoc;
36
37 /**
38  * The Invocation object is the generic object flowing through our interceptors.
39  *
40  * <p>The heart of it is the payload map that can contain anything we then
41  * put readers on them. The first <em>reader</em> is this
42  * <em>Invocation</em> object that can interpret the data in it.
43  *
44  * <p>Essentially we can carry ANYTHING from the client to the server, we keep
45  * a series of of predifined variables and method calls to get at the
46  * pointers. But really it is just a repository of objects.
47  *
48  * @author <a HREF="mailto:marc@jboss.org">Marc Fleury</a>
49  * @author <a HREF="mailto:christoph.jung@infor.de">Christoph G. Jung</a>
50  * @version $Revision: 37459 $
51  */

52 public class Invocation
53 {
54    /** The signature of the invoke() method */
55    public static final String JavaDoc[] INVOKE_SIGNATURE = { "org.jboss.invocation.Invocation" };
56
57    // The payload is a repository of everything associated with the invocation
58
// It is information that will need to travel
59

60    /**
61     * Contextual information to the invocation that is not part of the payload.
62     */

63    public Map JavaDoc transient_payload;
64
65    /**
66     * as_is classes that will not be marshalled by the invocation
67     * (java.* and javax.* or anything in system classpath is OK)
68     */

69    public Map JavaDoc as_is_payload;
70
71    /** Payload will be marshalled for type hiding at the RMI layers. */
72    public Map JavaDoc payload;
73
74    public InvocationContext invocationContext;
75    public Object JavaDoc[] args;
76    public Object JavaDoc objectName;
77    public Method JavaDoc method;
78    public InvocationType invocationType;
79
80    // The variables used to indicate what type of data and where to put it.
81

82    //
83
// We are using the generic payload to store some of our data, we define
84
// some integer entries. These are just some variables that we define for
85
// use in "typed" getters and setters. One can define anything either in
86
// here explicitely or through the use of external calls to getValue
87
//
88

89    /**
90     * No-args constructor exposed for externalization only.
91     */

92    public Invocation()
93    {
94    }
95
96    public Invocation( Object JavaDoc id, Method JavaDoc m, Object JavaDoc[] args, Transaction JavaDoc tx,
97       Principal JavaDoc identity, Object JavaDoc credential )
98    {
99       setId(id);
100       setMethod(m);
101       setArguments(args);
102       setTransaction(tx);
103       setPrincipal(identity);
104       setCredential(credential);
105    }
106    
107    /**
108     * The generic store of variables.
109     *
110     * <p>
111     * The generic getter and setter is really all that one needs to talk
112     * to this object. We introduce typed getters and setters for
113     * convenience and code readability in the codeba
114     */

115    public void setValue(Object JavaDoc key, Object JavaDoc value)
116    {
117       setValue(key, value, PayloadKey.PAYLOAD);
118    }
119    
120    /**
121     * Advanced store
122     * Here you can pass a TYPE that indicates where to put the value.
123     * TRANSIENT: the value is put in a map that WON'T be passed
124     * AS_IS: no need to marshall the value when passed (use for all JDK
125     * java types)
126     * PAYLOAD: we need to marshall the value as its type is application specific
127     */

128    public void setValue(Object JavaDoc key, Object JavaDoc value, PayloadKey type)
129    {
130       if(type == PayloadKey.TRANSIENT)
131       {
132           getTransientPayload().put(key,value);
133       }
134       else if(type == PayloadKey.AS_IS)
135       {
136           getAsIsPayload().put(key,value);
137       }
138       else if(type == PayloadKey.PAYLOAD)
139       {
140           getPayload().put(key,value);
141       }
142       else
143       {
144          throw new IllegalArgumentException JavaDoc("Unknown PayloadKey: " + type);
145       }
146    }
147  
148    /**
149     * Get a value from the stores.
150     */

151    public Object JavaDoc getValue(Object JavaDoc key)
152    {
153       // find where it is
154
Object JavaDoc rtn = getPayloadValue(key);
155       if (rtn != null) return rtn;
156
157       rtn = getAsIsValue(key);
158       if (rtn != null) return rtn;
159
160       rtn = getTransientValue(key);
161       return rtn;
162    }
163    
164    public Object JavaDoc getPayloadValue(Object JavaDoc key)
165    {
166       if (payload == null) return null;
167       return payload.get(key);
168    }
169
170    public Object JavaDoc getTransientValue(Object JavaDoc key)
171    {
172       if (transient_payload == null) return null;
173       return transient_payload.get(key);
174    }
175
176    public Object JavaDoc getAsIsValue(Object JavaDoc key)
177    {
178       if (as_is_payload == null) return null;
179       return as_is_payload.get(key);
180    }
181
182
183
184    //
185
// Convenience typed getters, use pre-declared keys in the store,
186
// but it all comes back to the payload, here you see the usage of the
187
// different payloads. Anything that has a well defined type can go in as_is
188
// Anything that is arbitrary and depends on the application needs to go in
189
// in the serialized payload. The "Transaction" is known, the type of the
190
// method arguments are not for example and are part of the EJB jar.
191
//
192

193    /**
194     * set the transaction.
195     */

196    public void setTransaction(Transaction JavaDoc tx)
197    {
198       if( tx instanceof Serializable JavaDoc )
199          getAsIsPayload().put(InvocationKey.TRANSACTION, tx);
200       else
201          getTransientPayload().put(InvocationKey.TRANSACTION, tx);
202    }
203    
204    /**
205     * get the transaction.
206     */

207    public Transaction JavaDoc getTransaction()
208    {
209       Transaction JavaDoc tx = (Transaction JavaDoc) getAsIsPayload().get(InvocationKey.TRANSACTION);
210       if( tx == null )
211          tx = (Transaction JavaDoc) getTransientPayload().get(InvocationKey.TRANSACTION);
212       return tx;
213    }
214
215    /**
216     * Change the security identity of this invocation.
217     */

218    public void setPrincipal(Principal JavaDoc principal)
219    {
220       getAsIsPayload().put(InvocationKey.PRINCIPAL, principal);
221    }
222    
223    public Principal JavaDoc getPrincipal()
224    {
225       return (Principal JavaDoc) getAsIsPayload().get(InvocationKey.PRINCIPAL);
226    }
227    
228    /**
229     * Change the security credentials of this invocation.
230     */

231    public void setCredential(Object JavaDoc credential)
232    {
233       getPayload().put(InvocationKey.CREDENTIAL, credential);
234    }
235    
236    public Object JavaDoc getCredential()
237    {
238       return getPayloadValue(InvocationKey.CREDENTIAL);
239    }
240    
241    /**
242     * container for server side association.
243     */

244    public void setObjectName(Object JavaDoc objectName)
245    {
246       this.objectName = objectName;
247    }
248    
249    public Object JavaDoc getObjectName()
250    {
251       return objectName;
252    }
253    
254    /**
255     * An arbitrary type.
256     */

257    public void setType(InvocationType type)
258    {
259       invocationType = type;
260    }
261    
262    public InvocationType getType()
263    {
264       if (invocationType == null) return InvocationType.LOCAL;
265       return invocationType;
266    }
267
268    /**
269     * Return the invocation target ID. Can be used to identify a cached object
270     */

271    public void setId(Object JavaDoc id)
272    {
273       getPayload().put(InvocationKey.CACHE_ID, id);
274    }
275    
276    public Object JavaDoc getId()
277    {
278       return getPayloadValue(InvocationKey.CACHE_ID);
279    }
280    
281    /**
282     * set on method Return the invocation method.
283     */

284    public void setMethod(Method JavaDoc method)
285    {
286       this.method = method;
287    }
288    
289    /**
290     * get on method Return the invocation method.
291     */

292    public Method JavaDoc getMethod()
293    {
294       return method;
295    }
296    
297    /**
298     * A list of arguments for the method.
299     */

300    public void setArguments(Object JavaDoc[] arguments)
301    {
302       this.args = arguments;
303    }
304    
305    public Object JavaDoc[] getArguments()
306    {
307       return this.args;
308    }
309    
310    /**
311     * marcf: SCOTT WARNING! I removed the "setPrincipal" that was called here
312     */

313    public InvocationContext getInvocationContext()
314    {
315       return invocationContext;
316    }
317
318    public void setInvocationContext(InvocationContext ctx)
319    {
320       this.invocationContext = ctx;
321    }
322    
323    public void setEnterpriseContext(Object JavaDoc ctx)
324    {
325       getTransientPayload().put(InvocationKey.ENTERPRISE_CONTEXT, ctx);
326    }
327       
328    public Object JavaDoc getEnterpriseContext()
329    {
330       return getTransientPayload().get(InvocationKey.ENTERPRISE_CONTEXT);
331    }
332
333    public Map JavaDoc getTransientPayload()
334    {
335       if (transient_payload == null) transient_payload = new HashMap JavaDoc();
336       return transient_payload;
337    }
338
339    public Map JavaDoc getAsIsPayload()
340    {
341       if (as_is_payload == null) as_is_payload = new HashMap JavaDoc();
342       return as_is_payload;
343    }
344
345    public Map JavaDoc getPayload()
346    {
347       if (payload == null) payload = new HashMap JavaDoc();
348       return payload;
349    }
350
351    /**
352     * This method will be called by the container(ContainerInterceptor) to issue the
353     * ultimate method call represented by this invocation. It is overwritten, e.g., by the
354     * WS4EE invocation in order to realize JAXRPC pre- and postprocessing.
355     */

356    public Object JavaDoc performCall(Object JavaDoc instance, Method JavaDoc m, Object JavaDoc[] arguments)
357            throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, Exception JavaDoc
358    {
359       return m.invoke(instance,arguments);
360    }
361
362    /**
363     * Helper method to determine whether an invocation is local
364     *
365     * @return true when local, false otherwise
366     */

367    public boolean isLocal()
368    {
369       InvocationType type = getType();
370       return (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
371    }
372 }
373
Popular Tags