KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > unified > interfaces > UnifiedInvokerProxy


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.interfaces;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.io.StreamCorruptedException JavaDoc;
29 import java.rmi.MarshalledObject JavaDoc;
30 import java.rmi.RemoteException JavaDoc;
31 import java.rmi.ServerException JavaDoc;
32 import org.jboss.invocation.Invocation;
33 import org.jboss.invocation.Invoker;
34 import org.jboss.logging.Logger;
35 import org.jboss.remoting.Client;
36 import org.jboss.remoting.InvokerLocator;
37
38 /**
39  * This represents the client side of the EJB invoker. This invoker uses
40  * the remoting framework for making invocations.
41  *
42  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
43  */

44 public class UnifiedInvokerProxy implements Invoker, Externalizable JavaDoc
45 {
46    static final long serialVersionUID = -1108158470271861548L;
47
48    private transient Client client;
49
50    private InvokerLocator locator;
51
52    private boolean strictRMIException = false;
53
54    private String JavaDoc subsystem = "invoker";
55
56    protected final Logger log = Logger.getLogger(getClass());
57
58    static final int VERSION_5_0 = 500;
59    static final int CURRENT_VERSION = VERSION_5_0;
60
61
62    public UnifiedInvokerProxy()
63    {
64       super();
65    }
66
67    public UnifiedInvokerProxy(InvokerLocator locator)
68    {
69       init(locator);
70    }
71
72    public UnifiedInvokerProxy(InvokerLocator locator, boolean isStrictRMIException)
73    {
74       this.strictRMIException = isStrictRMIException;
75       init(locator);
76    }
77
78    protected void init(InvokerLocator locator)
79    {
80       this.locator = locator;
81
82       try
83       {
84          client = new Client(locator, getSubSystem());
85          client.connect();
86       }
87       catch(Exception JavaDoc e)
88       {
89          log.fatal("Could not initialize UnifiedInvokerProxy.", e);
90       }
91
92    }
93
94    public String JavaDoc getSubSystem()
95    {
96       return subsystem;
97    }
98
99    public void setSubSystem(String JavaDoc subsystem)
100    {
101       this.subsystem = subsystem;
102    }
103
104
105    public boolean isStrictRMIException()
106    {
107       return strictRMIException;
108    }
109
110    protected Client getClient()
111    {
112       return client;
113    }
114
115    protected InvokerLocator getLocator()
116    {
117       return locator;
118    }
119
120    protected void setLocator(InvokerLocator locator)
121    {
122       this.locator = locator;
123    }
124
125    protected void setStrictRMIException(boolean strictRMIException)
126    {
127       this.strictRMIException = strictRMIException;
128    }
129
130
131    /**
132     * A free form String identifier for this delegate invoker, can be clustered or target node
133     * This should evolve in a more advanced meta-inf object.
134     * <p/>
135     * This will return the host supplied by the invoker locator if locator is not null. Otherwise, if the locator is null, will
136     * return null.
137     */

138    public String JavaDoc getServerHostName() throws Exception JavaDoc
139    {
140       if(locator != null)
141       {
142          return locator.getHost();
143       }
144       else
145       {
146          return null;
147       }
148    }
149
150    /**
151     * @param invocation A pointer to the invocation object
152     * @return Return value of method invocation.
153     * @throws Exception Failed to invoke method.
154     */

155    public Object JavaDoc invoke(Invocation invocation) throws Exception JavaDoc
156    {
157       Object JavaDoc response = null;
158
159       try
160       {
161          response = client.invoke(invocation, null);
162
163          if(response instanceof Exception JavaDoc)
164          {
165             throw ((Exception JavaDoc) response);
166          }
167          if(response instanceof MarshalledObject JavaDoc)
168          {
169             return ((MarshalledObject JavaDoc) response).get();
170          }
171          return response;
172
173       }
174       catch(RemoteException JavaDoc aex)
175       {
176          // per Jira issue JBREM-61
177
if(strictRMIException)
178          {
179             throw new ServerException JavaDoc(aex.getMessage(), aex);
180          }
181          else
182          {
183             throw aex;
184          }
185       }
186       catch(Throwable JavaDoc throwable)
187       {
188          // this is somewhat of a hack as remoting throws throwable,
189
// so will let Exception types bubble up, but if Throwable type,
190
// then have to wrap in new Exception, as this is the signature
191
// of this invoke method.
192
if(throwable instanceof Exception JavaDoc)
193          {
194             throw (Exception JavaDoc) throwable;
195          }
196          throw new Exception JavaDoc(throwable);
197       }
198    }
199
200    /**
201     * Externalize this instance and handle obtaining the remoteInvoker stub
202     */

203    public void writeExternal(final ObjectOutput JavaDoc out)
204          throws IOException JavaDoc
205    {
206       out.writeInt(CURRENT_VERSION);
207
208       out.writeUTF(locator.getOriginalURI());
209       out.writeBoolean(strictRMIException);
210    }
211
212    /**
213     * Un-externalize this instance.
214     */

215    public void readExternal(final ObjectInput JavaDoc in)
216          throws IOException JavaDoc, ClassNotFoundException JavaDoc
217    {
218       int version = in.readInt();
219       // Read in and map the version of the serialized data seen
220
switch(version)
221       {
222          case VERSION_5_0:
223             locator = new InvokerLocator(in.readUTF());
224             strictRMIException = in.readBoolean();
225             init(locator);
226             break;
227          default:
228             throw new StreamCorruptedException JavaDoc("Unknown version seen: " + version);
229       }
230    }
231
232 }
233
Popular Tags