KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > remoting > interceptors > invoker > RemotingInterceptorFactory


1 package org.jboss.aspects.remoting.interceptors.invoker;
2
3 import org.jboss.aop.advice.Interceptor;
4 import org.jboss.aop.joinpoint.Invocation;
5 import org.jboss.aspects.remoting.interceptors.marshall.MarshallInterceptor;
6 import org.jboss.aspects.remoting.interceptors.transport.TransportInterceptor;
7 import org.jboss.remoting.Client;
8 import org.jboss.remoting.InvokerLocator;
9 import org.jboss.remoting.marshal.MarshalFactory;
10 import org.jboss.remoting.marshal.Marshaller;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Map JavaDoc;
14
15 /**
16  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
17  */

18 public class RemotingInterceptorFactory
19 {
20    public static final String JavaDoc REMOTING = "REMOTING";
21    public static final String JavaDoc INVOKER_LOCATOR = "INVOKER_LOCATOR";
22    public static final String JavaDoc SUBSYSTEM = "SUBSYSTEM";
23    public static final String JavaDoc MARSHALLER = "MARSHALLER";
24    public static final String JavaDoc LOADER = "LOADER";
25
26
27    public static Invocation injectRemotingInterceptors(Invocation invocation)
28    {
29       Invocation newInvocation = null;
30
31       // Have to first get the locator from the invocation
32
InvokerLocator locator = (InvokerLocator) invocation.getMetaData(REMOTING, INVOKER_LOCATOR);
33       if (locator != null)
34       {
35          Interceptor[] newInterceptor = null;
36          ArrayList JavaDoc interceptorList = new ArrayList JavaDoc();
37
38          // First need to determine which marshalling interceptors are needed
39
String JavaDoc dataType = (String JavaDoc) invocation.getMetaData(REMOTING, InvokerLocator.DATATYPE);
40          if (dataType == null)
41          {
42             dataType = getDataType(locator);
43          }
44
45          if (dataType != null)
46          {
47             // If found data type, will get marshaller and create interceptor wrapper for it
48
// and then insert into interceptor chain
49
Marshaller marshaller = MarshalFactory.getMarshaller(dataType);
50             MarshallInterceptor marshallInterceptor = new MarshallInterceptor(marshaller);
51             interceptorList.add(marshallInterceptor);
52          }
53
54          // Now onto creating transport interceptor
55
ClassLoader JavaDoc loader = (ClassLoader JavaDoc) invocation.getMetaData(REMOTING, LOADER);
56          String JavaDoc subsystem = (String JavaDoc) invocation.getMetaData(REMOTING, SUBSYSTEM);
57
58          Client client = null;
59          try
60          {
61             if (loader != null)
62             {
63                client = new Client(loader, locator, subsystem, null);
64             }
65             else
66             {
67                client = new Client(locator, subsystem);
68             }
69          }
70          catch (Exception JavaDoc e)
71          {
72             throw new RuntimeException JavaDoc("Could not create remoting client.", e);
73          }
74
75          TransportInterceptor transportInterceptor = new TransportInterceptor(client);
76          interceptorList.add(transportInterceptor);
77
78          //TODO -TME Important to note that this will actually be appending new interceptors
79
// to end of chain based on my understanding of implementation. This is fine unless
80
// the InvokerInterceptor is NOT the final interceptor in the chain as there may
81
// be side effects that will cause problems.
82

83          // Should have all new remoting interceptors, so create new invocation
84
newInterceptor = (Interceptor[]) interceptorList.toArray(new Interceptor[interceptorList.size()]);
85          newInvocation = invocation.getWrapper(newInterceptor);
86       }
87       else
88       {
89          throw new RuntimeException JavaDoc("Require InvokerLocator to make remote invocations.");
90       }
91
92       return newInvocation;
93    }
94
95    private static String JavaDoc getDataType(InvokerLocator locator)
96    {
97       String JavaDoc type = null;
98
99       if (locator != null)
100       {
101          Map JavaDoc params = locator.getParameters();
102          if (params != null)
103          {
104             type = (String JavaDoc) params.get(InvokerLocator.DATATYPE);
105          }
106       }
107       return type;
108    }
109 }
110
Popular Tags