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.hessian;
030:
031: import com.caucho.hessian.server.HessianSkeleton;
032: import com.caucho.config.ConfigException;
033: import com.caucho.ejb.AbstractServer;
034: import com.caucho.ejb.message.MessageServer;
035: import com.caucho.ejb.protocol.HandleEncoder;
036: import com.caucho.ejb.protocol.ProtocolContainer;
037: import com.caucho.ejb.protocol.Skeleton;
038: import com.caucho.hessian.io.HessianRemoteResolver;
039: import com.caucho.util.L10N;
040:
041: import java.util.ArrayList;
042: import java.util.HashMap;
043: import java.util.logging.Logger;
044: import java.util.*;
045:
046: /**
047: * Server containing all the EJBs for a given configuration.
048: *
049: * <p>Each protocol will extend the container to override Handle creation.
050: */
051: public class HessianProtocol extends ProtocolContainer {
052: private static final L10N L = new L10N(HessianProtocol.class);
053: private static final Logger log = Logger
054: .getLogger(HessianProtocol.class.getName());
055:
056: private HashMap<String, AbstractServer> _serverMap = new HashMap<String, AbstractServer>();
057:
058: private WeakHashMap<Class, HessianSkeleton> _skeletonMap = new WeakHashMap<Class, HessianSkeleton>();
059:
060: private HessianRemoteResolver _resolver;
061:
062: /**
063: * Create a server with the given prefix name.
064: */
065: public HessianProtocol() {
066: _resolver = new HessianStubFactory();
067: }
068:
069: public String getName() {
070: return "hessian";
071: }
072:
073: /**
074: * Adds a server to the protocol.
075: */
076: public void addServer(AbstractServer server) {
077: log.finer(this + " add " + server);
078:
079: _serverMap.put(server.getProtocolId(), server);
080: }
081:
082: /**
083: * Removes a server from the protocol.
084: */
085: public void removeServer(AbstractServer server) {
086: log.finer(this + " remove " + server);
087:
088: _serverMap.remove(server.getProtocolId());
089: }
090:
091: @Override
092: protected HandleEncoder createHandleEncoder(AbstractServer server,
093: Class primaryKeyClass) throws ConfigException {
094: return new HessianHandleEncoder(server, getURLPrefix()
095: + server.getProtocolId(), primaryKeyClass);
096: }
097:
098: /**
099: * Returns the skeleton
100: */
101: public Skeleton getSkeleton(String uri, String queryString)
102: throws Exception {
103: String serverId = uri;
104: String objectId = null;
105:
106: // decode ?id=my-instance-id
107: if (queryString != null) {
108: int p = queryString.indexOf('=');
109:
110: if (p >= 0)
111: objectId = queryString.substring(p + 1);
112: else
113: objectId = queryString;
114: }
115:
116: AbstractServer server = _serverMap.get(serverId);
117:
118: if (server == null)
119: server = getProtocolManager().getServerByEJBName(serverId);
120:
121: if (server == null) {
122: ArrayList children = getProtocolManager()
123: .getRemoteChildren(serverId);
124:
125: if (children != null && children.size() > 0)
126: return new NameContextSkeleton(this , serverId);
127: else {
128: log.fine(this + " can't find server for " + serverId);
129:
130: return null; // XXX: should return error skeleton
131: }
132: /*
133: ArrayList children = getServerContainer().getRemoteChildren(serverId);
134:
135: if (children != null && children.size() > 0)
136: return new NameContextSkeleton(this, serverId);
137: else
138: return null; // XXX: should return error skeleton
139: */
140: } else if (objectId != null) {
141: Object key = server.getHandleEncoder("hessian")
142: .objectIdToKey(objectId);
143:
144: // ejb/0604 vs ejb/0500
145: Object obj = server.getRemoteObject(key);
146:
147: Class remoteApi = server.getRemoteObjectClass();
148: Class homeApi = server.getRemoteHomeClass();
149:
150: com.caucho.hessian.server.HessianSkeleton skel = getSkeleton(
151: remoteApi, homeApi, remoteApi);
152:
153: return new HessianEjbSkeleton(obj, skel, _resolver);
154: } else if (server instanceof MessageServer) {
155: throw new IllegalStateException(getClass().getName());
156: } else {
157: Class homeApi;
158: Class remoteApi;
159:
160: homeApi = server.getRemoteHomeClass();
161: remoteApi = server.getRemoteObjectClass();
162:
163: if (homeApi != null) {
164: Object remote = server.getRemoteObject(homeApi,
165: "hessian");
166:
167: com.caucho.hessian.server.HessianSkeleton skel = getSkeleton(
168: homeApi, homeApi, remoteApi);
169:
170: return new HessianEjbSkeleton(remote, skel, _resolver);
171: }
172:
173: if (remoteApi != null) {
174: Object remote = server.getRemoteObject(remoteApi,
175: "hessian");
176:
177: com.caucho.hessian.server.HessianSkeleton skel = getSkeleton(
178: remoteApi, remoteApi, remoteApi);
179:
180: return new HessianEjbSkeleton(remote, skel, _resolver);
181: }
182: }
183:
184: return null;
185: }
186:
187: /**
188: * Returns the skeleton to use to return configuration exceptions
189: */
190: @Override
191: public Skeleton getExceptionSkeleton() throws Exception {
192: return new ExceptionSkeleton();
193: }
194:
195: /**
196: * Returns the class for home skeletons.
197: */
198: protected HessianSkeleton getSkeleton(Class api, Class homeApi,
199: Class remoteApi) throws Exception {
200: HessianSkeleton skel;
201:
202: synchronized (_skeletonMap) {
203: skel = _skeletonMap.get(api);
204:
205: if (skel == null) {
206: skel = new HessianSkeleton(api);
207:
208: skel.setHomeClass(homeApi);
209: skel.setObjectClass(remoteApi);
210:
211: _skeletonMap.put(api, skel);
212: }
213:
214: return skel;
215: }
216: }
217: }
|