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