KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > unified > marshall > InvocationMarshaller


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.unified.marshall;
23
24 import org.jboss.invocation.Invocation;
25 import org.jboss.invocation.MarshalledInvocation;
26 import org.jboss.logging.Logger;
27 import org.jboss.remoting.InvocationRequest;
28 import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
29 import org.jboss.remoting.marshal.Marshaller;
30 import org.jboss.remoting.marshal.MarshallerDecorator;
31 import org.jboss.tm.TransactionPropagationContextFactory;
32 import org.jboss.tm.TransactionPropagationContextUtil;
33
34 import javax.transaction.SystemException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37
38 /**
39  * This marshaller is to be used in conjunction with the UnifiedInvoker and will
40  * look for an InvocationRequest to be passed to it, which is specific to EJB
41  * invocations.
42  *
43  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
44  */

45 public class InvocationMarshaller extends SerializableMarshaller implements MarshallerDecorator
46 {
47    public final static String JavaDoc DATATYPE = "invocation";
48
49    private static final Logger log = Logger.getLogger(InvocationMarshaller.class);
50
51    /**
52     * Marshaller will need to take the dataObject and convert
53     * into primitive java data types and write to the
54     * given output. Will check to see if dataObject being passed is
55     * an InvocationRequest, and if is, process it (including handling propagation of
56     * transaction). If is not an instance of InvocationRequest, will default back to
57     * SerializableMarshaller for processing.
58     *
59     * @param dataObject Object to be writen to output
60     * @param output The data output to write the object
61     * data to.
62     */

63    public void write(Object JavaDoc dataObject, OutputStream JavaDoc output) throws IOException JavaDoc
64    {
65
66        super.write(addDecoration(dataObject), output);
67    }
68
69     public Object JavaDoc addDecoration(Object JavaDoc dataObject) throws IOException JavaDoc {
70         if(dataObject instanceof InvocationRequest)
71         {
72            InvocationRequest remoteInv = (InvocationRequest) dataObject;
73
74            if(remoteInv.getParameter() instanceof Invocation)
75            {
76               Invocation inv = (Invocation) remoteInv.getParameter();
77
78               MarshalledInvocation marshInv = new MarshalledInvocation(inv);
79
80               if(inv != null)
81               {
82                  // now that have invocation object related to ejb invocations,
83
// need to get the possible known payload objects and make sure
84
// they get serialized.
85

86                  try
87                  {
88                     marshInv.setTransactionPropagationContext(getTransactionPropagationContext());
89                  }
90                  catch(SystemException JavaDoc e)
91                  {
92                     log.error("Error setting transaction propagation context.", e);
93                     throw new IOException JavaDoc("Error setting transaction context. Message: " + e.getMessage());
94                  }
95
96                  // reset the invocation parameter within remote invocation
97
remoteInv.setParameter(marshInv);
98               }
99               else
100               {
101                  //Should never get here, but will check anyways
102
log.error("Attempting to marshall Invocation but is null. Can not proceed.");
103                  throw new IOException JavaDoc("Can not process data object due to the InvocationRequest's parameter being null.");
104               }
105
106            }
107         }
108         return dataObject;
109     }
110
111     public Object JavaDoc getTransactionPropagationContext()
112           throws SystemException JavaDoc
113     {
114        TransactionPropagationContextFactory tpcFactory = TransactionPropagationContextUtil.getTPCFactoryClientSide();
115        return (tpcFactory == null) ? null : tpcFactory.getTransactionPropagationContext();
116     }
117
118    public Marshaller cloneMarshaller() throws CloneNotSupportedException JavaDoc
119    {
120       return new InvocationMarshaller();
121    }
122
123 }
Popular Tags