001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.invocation.unified.marshall;
023:
024: import org.jboss.invocation.Invocation;
025: import org.jboss.invocation.MarshalledInvocation;
026: import org.jboss.logging.Logger;
027: import org.jboss.remoting.InvocationRequest;
028: import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
029: import org.jboss.remoting.marshal.Marshaller;
030: import org.jboss.remoting.marshal.http.HTTPMarshaller;
031: import org.jboss.tm.TransactionPropagationContextFactory;
032: import org.jboss.tm.TransactionPropagationContextUtil;
033:
034: import javax.transaction.SystemException;
035: import java.io.IOException;
036: import java.io.OutputStream;
037:
038: /**
039: * This marshaller is to be used in conjunction with the UnifiedInvoker and will
040: * look for an InvocationRequest to be passed to it, which is specific to EJB
041: * invocations.
042: *
043: * @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
044: */
045: public class HTTPInvocationMarshaller extends HTTPMarshaller {
046: /** @since 4.2.0 */
047: static final long serialVersionUID = 1611946070051056241L;
048:
049: public final static String DATATYPE = "invocationhttp";
050:
051: private static final Logger log = Logger
052: .getLogger(HTTPInvocationMarshaller.class);
053:
054: /**
055: * Marshaller will need to take the dataObject and convert
056: * into primitive java data types and write to the
057: * given output. Will check to see if dataObject being passed is
058: * an InvocationRequest, and if is, process it (including handling propagation of
059: * transaction). If is not an instance of InvocationRequest, will default back to
060: * SerializableMarshaller for processing.
061: *
062: * @param dataObject Object to be writen to output
063: * @param output The data output to write the object
064: * data to.
065: */
066: public void write(Object dataObject, OutputStream output,
067: int version) throws IOException {
068: if (dataObject instanceof InvocationRequest) {
069: InvocationRequest remoteInv = (InvocationRequest) dataObject;
070:
071: if (remoteInv.getParameter() instanceof Invocation) {
072: Invocation inv = (Invocation) remoteInv.getParameter();
073:
074: MarshalledInvocation marshInv = new MarshalledInvocation(
075: inv);
076:
077: if (inv != null) {
078: // now that have invocation object related to ejb invocations,
079: // need to get the possible known payload objects and make sure
080: // they get serialized.
081:
082: try {
083: marshInv
084: .setTransactionPropagationContext(getTransactionPropagationContext());
085: } catch (SystemException e) {
086: log
087: .error(
088: "Error setting transaction propagation context.",
089: e);
090: throw new IOException(
091: "Error setting transaction context. Message: "
092: + e.getMessage());
093: }
094:
095: // reset the invocation parameter within remote invocation
096: remoteInv.setParameter(marshInv);
097: } else {
098: //Should never get here, but will check anyways
099: log
100: .error("Attempting to marshall Invocation but is null. Can not proceed.");
101: throw new IOException(
102: "Can not process data object due to the InvocationRequest's parameter being null.");
103: }
104:
105: }
106:
107: super .write(dataObject, output, version);
108:
109: } else // assume this is going to be the response
110: {
111: super .write(dataObject, output);
112: }
113: }
114:
115: public Object getTransactionPropagationContext()
116: throws SystemException {
117: TransactionPropagationContextFactory tpcFactory = TransactionPropagationContextUtil
118: .getTPCFactoryClientSide();
119: return (tpcFactory == null) ? null : tpcFactory
120: .getTransactionPropagationContext();
121: }
122:
123: public Marshaller cloneMarshaller()
124: throws CloneNotSupportedException {
125: return new HTTPInvocationMarshaller();
126: }
127:
128: }
|