KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > pooled > interfaces > PooledMarshalledInvocation


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.pooled.interfaces;
23
24 import org.jboss.invocation.Invocation;
25 import org.jboss.invocation.InvocationType;
26 import org.jboss.invocation.MarshalledInvocation;
27 import org.jboss.invocation.MarshalledValue;
28
29 import javax.transaction.Transaction JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.security.Principal JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * The MarshalledInvocation is an invocation that travels. As such it serializes
38  * its payload because of lack of ClassLoader visibility.
39  * As such it contains Marshalled data representing the byte[] of the Invocation object it extends
40  * Besides handling the specifics of "marshalling" the payload, which could be done at the Invocation level
41  * the Marshalled Invocation can hold optimization and needed code for distribution for example the
42  * TransactionPropagationContext which is a serialization of the TX for distribution purposes as
43  * well as the "hash" for the methods that we send, as opposed to sending Method objects.
44  * Serialization "optimizations" should be coded here in the externalization implementation of the class
45  *
46  * @author <a HREF="mailto:marc@jboss.org">Marc Fleury</a>
47  * @version $Revision: 37459 $
48  */

49 public class PooledMarshalledInvocation
50         extends MarshalledInvocation
51         implements java.io.Externalizable JavaDoc
52 {
53    // Constants -----------------------------------------------------
54

55    /** Serial Version Identifier. */
56    static final long serialVersionUID = -728630295444149842L;
57
58    private transient Transaction JavaDoc tx;
59    private transient Object JavaDoc credential;
60    private transient Principal JavaDoc principal;
61    private transient Object JavaDoc enterpriseContext;
62    private transient Object JavaDoc id;
63    private transient PooledMarshalledValue pooledMarshalledArgs;
64
65
66    // Constructors --------------------------------------------------
67
public PooledMarshalledInvocation()
68    {
69       // For externalization to work
70
}
71
72    public PooledMarshalledInvocation(Invocation invocation)
73    {
74       this.payload = invocation.payload;
75       this.as_is_payload = invocation.as_is_payload;
76       this.method = invocation.getMethod();
77       this.objectName = invocation.getObjectName();
78       this.args = invocation.getArguments();
79       this.invocationType = invocation.getType();
80    }
81
82
83    /*
84    public MarshalledInvocation(Map payload)
85    {
86       super(payload);
87    }
88
89    public MarshalledInvocation(Map payload, Map as_is_payload)
90    {
91       super(payload);
92       this.as_is_payload = as_is_payload;
93    }
94    */

95    public PooledMarshalledInvocation(
96            Object JavaDoc id,
97            Method JavaDoc m,
98            Object JavaDoc[] args,
99            Transaction JavaDoc tx,
100            Principal JavaDoc identity,
101            Object JavaDoc credential)
102    {
103       super(id, m, args, tx, identity, credential);
104    }
105    // Public --------------------------------------------------------
106

107
108
109    public Object JavaDoc getEnterpriseContext()
110    {
111       return enterpriseContext;
112    }
113
114    public void setEnterpriseContext(Object JavaDoc enterpriseContext)
115    {
116       this.enterpriseContext = enterpriseContext;
117    }
118
119    public Object JavaDoc getId()
120    {
121       if (id == null) id = super.getId();
122       return id;
123    }
124
125    public void setId(Object JavaDoc id)
126    {
127       super.setId(id);
128       this.id = id;
129    }
130
131    public void setTransaction(Transaction JavaDoc tx)
132    {
133       super.setTransaction(tx);
134      this.tx = tx;
135    }
136
137    public Transaction JavaDoc getTransaction()
138    {
139       if (tx == null) tx = super.getTransaction();
140       return this.tx;
141    }
142
143    public Object JavaDoc getCredential()
144    {
145       if (credential == null) credential = super.getCredential();
146       return credential;
147    }
148
149    public void setCredential(Object JavaDoc credential)
150    {
151       super.setCredential(credential);
152       this.credential = credential;
153    }
154
155    public Principal JavaDoc getPrincipal()
156    {
157       if (principal == null) principal = super.getPrincipal();
158       return principal;
159    }
160
161    public void setPrincipal(Principal JavaDoc principal)
162    {
163       super.setPrincipal(principal);
164       this.principal = principal;
165    }
166
167
168    public Object JavaDoc[] getArguments()
169    {
170       if (this.args == null)
171       {
172          try
173          {
174             this.args = (Object JavaDoc[]) pooledMarshalledArgs.get();
175          }
176          catch (Exception JavaDoc e)
177          {
178             e.printStackTrace();
179          }
180       }
181       return args;
182    }
183
184    // Externalizable implementation ---------------------------------
185
public void writeExternal(java.io.ObjectOutput JavaDoc out)
186            throws IOException JavaDoc
187    {
188       out.writeObject(invocationType);
189       // FIXME marcf: the "specific" treatment of Transactions should be abstracted.
190
// Write the TPC, not the local transaction
191
out.writeObject(tpc);
192
193       long methodHash = calculateHash(this.method);
194       out.writeLong(methodHash);
195
196       out.writeInt(((Integer JavaDoc)this.objectName).intValue());
197       out.writeObject(new PooledMarshalledValue(this.args));
198
199
200       // Write out payload hashmap
201
// Don't use hashmap serialization to avoid not-needed data being
202
// marshalled
203
// The map contains only serialized representations of every other object
204
// Everything else is possibly tied to classloaders that exist inside the
205
// server but not in the generic JMX land. they will travel in the payload
206
// as MarshalledValue objects, see the Invocation getter logic
207
//
208
if (payload == null)
209          out.writeInt(0);
210       else
211       {
212          out.writeInt(payload.size());
213          Iterator JavaDoc keys = payload.keySet().iterator();
214          while (keys.hasNext())
215          {
216             Object JavaDoc currentKey = keys.next();
217
218             // This code could be if (object.getClass().getName().startsWith("java")) then don't serialize.
219
// Bench the above for speed.
220

221             out.writeObject(currentKey);
222             out.writeObject(new MarshalledValue(payload.get(currentKey)));
223          }
224       }
225
226       // This map is "safe" as is
227
//out.writeObject(as_is_payload);
228
if (as_is_payload == null)
229          out.writeInt(0);
230       else
231       {
232          out.writeInt(as_is_payload.size());
233
234          Iterator JavaDoc keys = as_is_payload.keySet().iterator();
235          while (keys.hasNext())
236          {
237             Object JavaDoc currentKey = keys.next();
238             out.writeObject(currentKey);
239             out.writeObject(as_is_payload.get(currentKey));
240          }
241       }
242    }
243
244    public void readExternal(java.io.ObjectInput JavaDoc in)
245            throws IOException JavaDoc, ClassNotFoundException JavaDoc
246    {
247       invocationType = (InvocationType)in.readObject();
248       tpc = in.readObject();
249       this.methodHash = in.readLong();
250
251       this.objectName = new Integer JavaDoc(in.readInt());
252
253       pooledMarshalledArgs = (PooledMarshalledValue) in.readObject();
254
255       int payloadSize = in.readInt();
256       if (payloadSize > 0)
257       {
258          payload = new HashMap JavaDoc();
259          for (int i = 0; i < payloadSize; i++)
260          {
261             Object JavaDoc key = in.readObject();
262             Object JavaDoc value = in.readObject();
263             payload.put(key, value);
264          }
265       }
266
267       int as_is_payloadSize = in.readInt();
268       if (as_is_payloadSize > 0)
269       {
270          as_is_payload = new HashMap JavaDoc();
271          for (int i = 0; i < as_is_payloadSize; i++)
272          {
273             Object JavaDoc key = in.readObject();
274             Object JavaDoc value = in.readObject();
275             as_is_payload.put(key, value);
276          }
277       }
278    }
279 }
280
Popular Tags