KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > HessianHandle


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.hessian;
30
31 import com.caucho.ejb.EJBExceptionWrapper;
32 import com.caucho.ejb.RemoteExceptionWrapper;
33 import com.caucho.ejb.protocol.AbstractHandle;
34 import com.caucho.ejb.protocol.HandleEncoder;
35
36 import javax.ejb.EJBObject JavaDoc;
37 import java.rmi.RemoteException JavaDoc;
38
39 /**
40  * Handle implementation for Hessian Objects.
41  *
42  * <code><pre>
43  * String url = "http://localhost:8080/ejb/houses/Gryffindor";
44  * HessianHandle handle = new HessianHandle(url);
45  *
46  * test.RemoteBean bean = (test.RemoteBean) handle.getEJBObject();
47  * </pre></code>
48  */

49 public class HessianHandle extends AbstractHandle {
50   private String JavaDoc url;
51
52   private transient String JavaDoc serverId;
53   private transient String JavaDoc objectId;
54   private transient Object JavaDoc objectKey;
55   private transient EJBObject JavaDoc object;
56   
57   /**
58    * Null-arg constructor for serialization.
59    */

60   public HessianHandle() {}
61   
62   /**
63    * Create a new handle.
64    */

65   public HessianHandle(String JavaDoc url)
66   {
67     this.url = url;
68   }
69   
70   /**
71    * Create a new handle.
72    */

73   public HessianHandle(String JavaDoc url, Object JavaDoc key)
74   {
75     this.url = url;
76     this.objectKey = key;
77   }
78
79   /**
80    * Returns the server id
81    */

82   public String JavaDoc getServerId()
83   {
84     if (serverId == null) {
85       int p = url.lastIndexOf('?');
86       serverId = url.substring(0, p);
87     }
88
89     return serverId;
90   }
91   
92   /**
93    * Returns the object id
94    */

95   public String JavaDoc getObjectId()
96   {
97     if (objectId == null) {
98       int p = url.lastIndexOf('?');
99       objectId = url.substring(p + 7);
100     }
101
102     return objectId;
103   }
104
105   void setEJBObject(EJBObject JavaDoc obj)
106   {
107     this.object = obj;
108   }
109
110   public EJBObject JavaDoc getEJBObject()
111     throws RemoteException JavaDoc
112   {
113     if (object == null) {
114       try {
115         HessianClientContainer client;
116         client = HessianClientContainer.find(getServerId());
117         object = client.createObjectStub(url);
118       } catch (Exception JavaDoc e) {
119         throw RemoteExceptionWrapper.create(e);
120       }
121     }
122     
123     return object;
124   }
125   
126   /**
127    * Returns the object id
128    */

129   public Object JavaDoc getObjectKey()
130   {
131     if (objectKey == null) {
132       try {
133         HandleEncoder encoder = HessianClientContainer.find(getServerId()).getHandleEncoder();
134         objectKey = encoder.objectIdToKey(getObjectId());
135       } catch (Exception JavaDoc e) {
136         throw new EJBExceptionWrapper(e);
137       }
138     }
139
140     return objectKey;
141   }
142
143   /**
144    * Returns the url
145    */

146   public String JavaDoc getURL()
147   {
148     return url;
149   }
150
151   /**
152    * Returns the url
153    */

154   public String JavaDoc getURL(String JavaDoc protocol)
155   {
156     return url;
157   }
158
159   /**
160    * Returns the hash code.
161    */

162   public int hashCode()
163   {
164     return url.hashCode();
165   }
166
167   /**
168    * Returns true if equals.
169    */

170   public boolean equals(Object JavaDoc obj)
171   {
172     if (! (obj instanceof HessianHandle))
173       return false;
174
175     HessianHandle handle = (HessianHandle) obj;
176
177     return url.equals(handle.url);
178   }
179
180   /**
181    * Returns the string.
182    */

183   public String JavaDoc toString()
184   {
185     return url;
186   }
187 }
188
Popular Tags