KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > protocol > HandleImpl


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.protocol;
30
31 import com.caucho.ejb.AbstractServer;
32 import com.caucho.ejb.RemoteExceptionWrapper;
33
34 import javax.ejb.EJBObject JavaDoc;
35 import java.rmi.RemoteException JavaDoc;
36   
37 /**
38  * Handle implementation for EJB Objects.
39  *
40  * <code><pre>
41  * String url = "http://localhost:8080/ejb/houses?ejbid=Gryffindor";
42  * HandleImpl handle = new HandleImpl(url);
43  *
44  * test.RemoteBean bean = (test.RemoteBean) handle.getEJBObject();
45  * </pre></code>
46  */

47 public class HandleImpl extends AbstractHandle {
48   private String JavaDoc _serverId;
49   private String JavaDoc _objectId;
50
51   private transient EJBObject JavaDoc _object;
52
53   /**
54    * Null constructor for serialization.
55    */

56   public HandleImpl() {}
57
58   /**
59    * Create a new HandleImpl
60    *
61    * @param serverId the object's server
62    * @param objectKey the object id
63    */

64   public HandleImpl(String JavaDoc serverId, String JavaDoc objectId)
65   {
66     _serverId = serverId;
67     _objectId = objectId;
68   }
69
70   /**
71    * Return the URL prefix for the bean's home.
72    */

73   public String JavaDoc getServerId()
74   {
75     return _serverId;
76   }
77
78   /**
79    * Returns the URL suffix used as a primary key.
80    */

81   public String JavaDoc getObjectId()
82   {
83     return _objectId;
84   }
85
86   public String JavaDoc getURL(String JavaDoc protocol)
87   {
88     AbstractServer server = EjbProtocolManager.getJVMServer(_serverId);
89     
90     return server.getHandleEncoder(protocol).getURL(_objectId);
91   }
92
93   public EJBObject JavaDoc getEJBObject()
94     throws RemoteException JavaDoc
95   {
96     if (_object == null) {
97       try {
98         SameJVMClientContainer client;
99         client = SameJVMClientContainer.find(_serverId);
100
101         if (client != null)
102           _object = client.createObjectStub(_objectId);
103       } catch (Exception JavaDoc e) {
104         RemoteExceptionWrapper.create(e);
105       }
106     }
107
108     return _object;
109   }
110
111   /**
112    * Returns true if the test handle refers to the same object.
113    * In this implementation, the handles are identical iff the urls
114    * are identical.
115    */

116   public boolean equals(Object JavaDoc b)
117   {
118     if (! (b instanceof HandleImpl))
119       return false;
120
121     HandleImpl handle = (HandleImpl) b;
122
123     return (_serverId.equals(handle._serverId) &&
124             _objectId.equals(handle._objectId));
125   }
126
127   /**
128    * The handle's hashcode is the same as the url's hashcode
129    */

130   public int hashCode()
131   {
132     return 65521 * _serverId.hashCode() + _objectId.hashCode();
133   }
134
135   /**
136    * The printed representation of the handle is the url.
137    */

138   public String JavaDoc toString()
139   {
140     return "HandleImpl[" + _serverId + "," + _objectId + "]";
141   }
142 }
143
Popular Tags