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.aspects.remoting.interceptors.invoker;
023:
024: import org.jboss.aop.advice.Interceptor;
025: import org.jboss.aop.joinpoint.Invocation;
026: import org.jboss.aspects.remoting.interceptors.marshall.MarshallInterceptor;
027: import org.jboss.aspects.remoting.interceptors.transport.TransportInterceptor;
028: import org.jboss.remoting.Client;
029: import org.jboss.remoting.InvokerLocator;
030: import org.jboss.remoting.marshal.MarshalFactory;
031: import org.jboss.remoting.marshal.Marshaller;
032:
033: import java.util.ArrayList;
034: import java.util.Map;
035:
036: /**
037: * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
038: */
039: public class RemotingInterceptorFactory {
040: public static final String REMOTING = "REMOTING";
041: public static final String INVOKER_LOCATOR = "INVOKER_LOCATOR";
042: public static final String SUBSYSTEM = "SUBSYSTEM";
043: public static final String MARSHALLER = "MARSHALLER";
044: public static final String LOADER = "LOADER";
045:
046: public static Invocation injectRemotingInterceptors(
047: Invocation invocation) {
048: Invocation newInvocation = null;
049:
050: // Have to first get the locator from the invocation
051: InvokerLocator locator = (InvokerLocator) invocation
052: .getMetaData(REMOTING, INVOKER_LOCATOR);
053: if (locator != null) {
054: Interceptor[] newInterceptor = null;
055: ArrayList interceptorList = new ArrayList();
056:
057: // First need to determine which marshalling interceptors are needed
058: String dataType = (String) invocation.getMetaData(REMOTING,
059: InvokerLocator.DATATYPE);
060: if (dataType == null) {
061: dataType = getDataType(locator);
062: }
063:
064: if (dataType != null) {
065: // If found data type, will get marshaller and create interceptor wrapper for it
066: // and then insert into interceptor chain
067: Marshaller marshaller = MarshalFactory
068: .getMarshaller(dataType);
069: MarshallInterceptor marshallInterceptor = new MarshallInterceptor(
070: marshaller);
071: interceptorList.add(marshallInterceptor);
072: }
073:
074: // Now onto creating transport interceptor
075: ClassLoader loader = (ClassLoader) invocation.getMetaData(
076: REMOTING, LOADER);
077: String subsystem = (String) invocation.getMetaData(
078: REMOTING, SUBSYSTEM);
079:
080: Client client = null;
081: try {
082: if (loader != null) {
083: client = new Client(loader, locator, subsystem,
084: null);
085: } else {
086: client = new Client(locator, subsystem);
087: }
088: } catch (Exception e) {
089: throw new RuntimeException(
090: "Could not create remoting client.", e);
091: }
092:
093: TransportInterceptor transportInterceptor = new TransportInterceptor(
094: client);
095: interceptorList.add(transportInterceptor);
096:
097: //TODO -TME Important to note that this will actually be appending new interceptors
098: // to end of chain based on my understanding of implementation. This is fine unless
099: // the InvokerInterceptor is NOT the final interceptor in the chain as there may
100: // be side effects that will cause problems.
101:
102: // Should have all new remoting interceptors, so create new invocation
103: newInterceptor = (Interceptor[]) interceptorList
104: .toArray(new Interceptor[interceptorList.size()]);
105: newInvocation = invocation.getWrapper(newInterceptor);
106: } else {
107: throw new RuntimeException(
108: "Require InvokerLocator to make remote invocations.");
109: }
110:
111: return newInvocation;
112: }
113:
114: private static String getDataType(InvokerLocator locator) {
115: String type = null;
116:
117: if (locator != null) {
118: Map params = locator.getParameters();
119: if (params != null) {
120: type = (String) params.get(InvokerLocator.DATATYPE);
121: }
122: }
123: return type;
124: }
125: }
|