KickJava   Java API By Example, From Geeks To Geeks.

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


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.http.HTTPMarshaller;
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 HTTPInvocationMarshaller extends HTTPMarshaller
46 {
47    public final static String JavaDoc DATATYPE = "invocationhttp";
48
49    private static final Logger log = Logger.getLogger(HTTPInvocationMarshaller.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       if(dataObject instanceof InvocationRequest)
66       {
67          InvocationRequest remoteInv = (InvocationRequest) dataObject;
68
69          if(remoteInv.getParameter() instanceof Invocation)
70          {
71             Invocation inv = (Invocation) remoteInv.getParameter();
72
73             MarshalledInvocation marshInv = new MarshalledInvocation(inv);
74
75             if(inv != null)
76             {
77                // now that have invocation object related to ejb invocations,
78
// need to get the possible known payload objects and make sure
79
// they get serialized.
80

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