KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > local > LocalClientInvoker


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.remoting.transport.local;
10
11 import org.jboss.remoting.AbstractInvoker;
12 import org.jboss.remoting.ConnectionFailedException;
13 import org.jboss.remoting.InvocationRequest;
14 import org.jboss.remoting.InvokerLocator;
15 import org.jboss.remoting.InvokerRegistry;
16 import org.jboss.remoting.ServerInvoker;
17 import org.jboss.remoting.marshal.Marshaller;
18 import org.jboss.remoting.marshal.UnMarshaller;
19 import org.jboss.remoting.transport.ClientInvoker;
20
21 /**
22  * LocalClientInvoker does not use any transport protocol for invoking
23  * the ServerInvoker, instead will make call directly on it locally.
24  * This increases performance since no serialization required as well
25  * as needed for push callbacks where InvokerCallbackHandler is in
26  * same JVM as the callback server.
27  *
28  * @author <a HREF="mailto:telrod@vocalocity.net">Tom Elrod</a>
29  * @version $Revision: 1.4 $
30  */

31 public class LocalClientInvoker extends AbstractInvoker implements ClientInvoker
32 {
33    private ServerInvoker serverInvoker;
34
35    public LocalClientInvoker(InvokerLocator locator)
36    {
37       super(locator);
38    }
39
40    /**
41     * transport a request against a remote ServerInvoker
42     *
43     * @param invocation
44     * @return
45     * @throws Throwable
46     */

47    public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
48    {
49       if(log.isTraceEnabled())
50       {
51          log.trace("Using local client invoker for invocation.");
52       }
53
54       Object JavaDoc ret = null;
55       if(serverInvoker != null)
56       {
57          ret = serverInvoker.invoke(invocation);
58       }
59       else
60       {
61          throw new ConnectionFailedException("Error invoking on server because " +
62                                              "no local server to call upon.");
63       }
64
65       return ret;
66    }
67
68    /**
69     * subclasses must provide this method to return true if their remote connection is connected and
70     * false if disconnected. in some transports, such as SOAP, this method may always return true, since the
71     * remote connectivity is done on demand and not kept persistent like other transports (such as socket-based
72     * transport).
73     *
74     * @return boolean true if connected, false if not
75     */

76    public boolean isConnected()
77    {
78       return serverInvoker != null;
79    }
80
81    /**
82     * connect to the remote invoker
83     *
84     * @throws ConnectionFailedException
85     */

86    public void connect() throws ConnectionFailedException
87    {
88       // nothing to be done here. Should already have reference to serverInvoker
89
}
90
91    /**
92     * disconnect from the remote invoker. Once disconnect called
93     * will not be able to re-connect by calling connect since will
94     * loose reference to server invoker.
95     */

96    public void disconnect()
97    {
98       /**
99        * Need to remove myself from registry so will not keep
100        * reference to me since I am of no use now (since serverInvoker
101        * reference is null). Will have to create a new one.
102        */

103       InvokerRegistry.destroyClientInvoker(getLocator());
104       serverInvoker = null;
105    }
106
107    public void setMarshaller(Marshaller marshaller)
108    {
109       // No op since is local, do not need marshaller
110
}
111
112    public Marshaller getMarshaller()
113    {
114       return null;
115    }
116
117    public void setUnMarshaller(UnMarshaller unmarshaller)
118    {
119       // No op since is local, do not need unmarshaller
120
}
121
122    public UnMarshaller getUnMarshaller()
123    {
124       return null;
125    }
126
127    /**
128     * This will set the local reference to the server invoker.
129     * This is needed to so can make calls directly against server.
130     *
131     * @param svrInvoker
132     */

133    public void setServerInvoker(ServerInvoker svrInvoker)
134    {
135       this.serverInvoker = svrInvoker;
136    }
137 }
138
Popular Tags