001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.protocol;
030:
031: import com.caucho.ejb.AbstractServer;
032: import com.caucho.ejb.RemoteExceptionWrapper;
033:
034: import javax.ejb.EJBObject;
035: import java.rmi.RemoteException;
036:
037: /**
038: * Handle implementation for EJB Objects.
039: *
040: * <code><pre>
041: * String url = "http://localhost:8080/ejb/houses?ejbid=Gryffindor";
042: * HandleImpl handle = new HandleImpl(url);
043: *
044: * test.RemoteBean bean = (test.RemoteBean) handle.getEJBObject();
045: * </pre></code>
046: */
047: public class HandleImpl extends AbstractHandle {
048: private String _serverId;
049: private String _objectId;
050:
051: private transient EJBObject _object;
052:
053: /**
054: * Null constructor for serialization.
055: */
056: public HandleImpl() {
057: }
058:
059: /**
060: * Create a new HandleImpl
061: *
062: * @param serverId the object's server
063: * @param objectKey the object id
064: */
065: public HandleImpl(String serverId, String objectId) {
066: _serverId = serverId;
067: _objectId = objectId;
068: }
069:
070: /**
071: * Return the URL prefix for the bean's home.
072: */
073: public String getServerId() {
074: return _serverId;
075: }
076:
077: /**
078: * Returns the URL suffix used as a primary key.
079: */
080: public String getObjectId() {
081: return _objectId;
082: }
083:
084: public String getURL(String protocol) {
085: AbstractServer server = EjbProtocolManager
086: .getJVMServer(_serverId);
087:
088: return server.getHandleEncoder(protocol).getURL(_objectId);
089: }
090:
091: public EJBObject getEJBObject() throws RemoteException {
092: if (_object == null) {
093: try {
094: /* XXX: webbeans
095: SameJVMClientContainer client;
096: client = SameJVMClientContainer.find(_serverId);
097:
098: if (client != null)
099: _object = client.createObjectStub(_objectId);
100: */
101: } catch (Exception e) {
102: RemoteExceptionWrapper.create(e);
103: }
104: }
105:
106: return _object;
107: }
108:
109: /**
110: * Returns true if the test handle refers to the same object.
111: * In this implementation, the handles are identical iff the urls
112: * are identical.
113: */
114: public boolean equals(Object b) {
115: if (!(b instanceof HandleImpl))
116: return false;
117:
118: HandleImpl handle = (HandleImpl) b;
119:
120: return (_serverId.equals(handle._serverId) && _objectId
121: .equals(handle._objectId));
122: }
123:
124: /**
125: * The handle's hashcode is the same as the url's hashcode
126: */
127: public int hashCode() {
128: return 65521 * _serverId.hashCode() + _objectId.hashCode();
129: }
130:
131: /**
132: * The printed representation of the handle is the url.
133: */
134: public String toString() {
135: return "HandleImpl[" + _serverId + "," + _objectId + "]";
136: }
137: }
|