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.burlap;
030:
031: import com.caucho.ejb.EJBExceptionWrapper;
032: import com.caucho.ejb.RemoteExceptionWrapper;
033: import com.caucho.ejb.protocol.AbstractHandle;
034: import com.caucho.ejb.protocol.HandleEncoder;
035:
036: import javax.ejb.EJBObject;
037: import java.rmi.RemoteException;
038:
039: /**
040: * Handle implementation for Burlap Objects.
041: *
042: * <code><pre>
043: * String url = "http://localhost:8080/ejb/houses/Gryffindor";
044: * BurlapHandle handle = new BurlapHandle(url);
045: *
046: * test.RemoteBean bean = (test.RemoteBean) handle.getEJBObject();
047: * </pre></code>
048: */
049: public class BurlapHandle extends AbstractHandle {
050: private String url;
051:
052: private transient String serverId;
053: private transient String objectId;
054: private transient Object objectKey;
055: private transient EJBObject object;
056:
057: /**
058: * Null-arg constructor for serialization.
059: */
060: public BurlapHandle() {
061: }
062:
063: /**
064: * Create a new handle.
065: */
066: public BurlapHandle(String url) {
067: this .url = url;
068: }
069:
070: /**
071: * Create a new handle.
072: */
073: public BurlapHandle(String url, Object key) {
074: this .url = url;
075: this .objectKey = key;
076: }
077:
078: /**
079: * Returns the server id
080: */
081: public String getServerId() {
082: if (serverId == null) {
083: int p = url.lastIndexOf('?');
084: serverId = url.substring(0, p);
085: }
086:
087: return serverId;
088: }
089:
090: /**
091: * Returns the object id
092: */
093: public String getObjectId() {
094: if (objectId == null) {
095: int p = url.lastIndexOf('?');
096: objectId = url.substring(p + 7);
097: }
098:
099: return objectId;
100: }
101:
102: void setEJBObject(EJBObject obj) {
103: this .object = obj;
104: }
105:
106: public EJBObject getEJBObject() throws RemoteException {
107: if (object == null) {
108: try {
109: BurlapClientContainer client;
110: client = BurlapClientContainer.find(getServerId());
111: object = (EJBObject) client.createObjectStub(url);
112: } catch (Exception e) {
113: throw RemoteExceptionWrapper.create(e);
114: }
115: }
116:
117: return object;
118: }
119:
120: /**
121: * Returns the object id
122: */
123: public Object getObjectKey() {
124: if (objectKey == null) {
125: try {
126: HandleEncoder encoder = BurlapClientContainer.find(
127: getServerId()).getHandleEncoder();
128: objectKey = encoder.objectIdToKey(getObjectId());
129: } catch (Exception e) {
130: throw new EJBExceptionWrapper(e);
131: }
132: }
133:
134: return objectKey;
135: }
136:
137: /**
138: * Returns the url
139: */
140: public String getURL() {
141: return url;
142: }
143:
144: /**
145: * Returns the url
146: */
147: public String getURL(String protocol) {
148: return url;
149: }
150:
151: /**
152: * Returns the hash code.
153: */
154: public int hashCode() {
155: return url.hashCode();
156: }
157:
158: /**
159: * Returns true if equals.
160: */
161: public boolean equals(Object obj) {
162: if (!(obj instanceof BurlapHandle))
163: return false;
164:
165: BurlapHandle handle = (BurlapHandle) obj;
166:
167: return this .url.equals(handle.url);
168: }
169:
170: /**
171: * Returns the string.
172: */
173: public String toString() {
174: return url;
175: }
176: }
|