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.ejb.AbstractServer;
032: import com.caucho.ejb.protocol.EjbProtocolManager;
033: import com.caucho.ejb.protocol.Skeleton;
034: import com.caucho.hessian.io.HessianInput;
035: import com.caucho.hessian.io.HessianOutput;
036: import com.caucho.hessian.io.HessianProtocolException;
037: import com.caucho.log.Log;
038: import com.caucho.services.name.NameServerRemote;
039:
040: import javax.ejb.EJBHome;
041: import java.io.InputStream;
042: import java.io.OutputStream;
043: import java.util.ArrayList;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046:
047: /**
048: * Base class for any bean skeleton capable of handling a Hessian request.
049: *
050: * <p/>Once selected, the calling servlet will dispatch the request through
051: * the <code>_service</code> call. After parsing the request headers,
052: * <code>_service</code> calls the generated entry <code>_execute</code>
053: * to execute the request.
054: */
055: public class NameContextSkeleton extends Skeleton {
056: private static final Logger log = Logger
057: .getLogger(NameContextSkeleton.class.getName());
058:
059: private HessianProtocol _protocol;
060: private String _prefix;
061:
062: NameContextSkeleton(HessianProtocol protocol, String prefix) {
063: _protocol = protocol;
064: _prefix = prefix;
065: }
066:
067: /**
068: * Services the request.
069: */
070: public void _service(InputStream is, OutputStream os)
071: throws Exception {
072: HessianInput in = new HessianReader(is);
073: HessianOutput out = new HessianWriter(os);
074:
075: in.startCall();
076:
077: String method = in.getMethod();
078:
079: try {
080: if (method.equals("lookup")
081: || method.equals("lookup_string")
082: || method.equals("lookup_1"))
083: executeLookup(in, out);
084: else if (method.equals("list"))
085: executeList(in, out);
086: else
087: executeUnknown(method, in, out);
088: } catch (HessianProtocolException e) {
089: throw e;
090: } catch (Throwable e) {
091: log.log(Level.WARNING, e.toString(), e);
092:
093: out.startReply();
094: out.writeFault("ServiceException", e.getMessage(), e);
095: out.completeReply();
096: }
097: }
098:
099: private void executeLookup(HessianInput in, HessianOutput out)
100: throws Throwable {
101: String name = in.readString();
102: in.completeCall();
103:
104: while (name.startsWith("/"))
105: name = name.substring(1);
106:
107: EjbProtocolManager container = _protocol.getProtocolManager();
108:
109: AbstractServer server;
110:
111: server = container.getServerByServerId(name);
112:
113: if (server == null)
114: server = container.getServerByEJBName(name);
115:
116: if (server != null) {
117: EJBHome home = server.getEJBHome();
118:
119: out.startReply();
120:
121: if (home != null)
122: out.writeObject(home);
123: else
124: // if (server instanceof
125: out.writeObject(server.getRemoteObject(server
126: .getRemoteHomeClass(), "hessian"));
127:
128: out.completeReply();
129: } else if (container.getRemoteChildren(name) != null) {
130: out.startReply();
131:
132: String serverId;
133:
134: if (_prefix.endsWith("/") || name.startsWith("/"))
135: serverId = _prefix + name;
136: else
137: serverId = _prefix + '/' + name;
138:
139: if (serverId.startsWith("/"))
140: serverId = serverId.substring(1);
141:
142: String url;
143: String prefix = _protocol.getURLPrefix();
144: if (prefix.endsWith("/"))
145: url = prefix + serverId;
146: else
147: url = prefix + '/' + serverId;
148:
149: out.writeRemote(NameServerRemote.class.getName(), url);
150:
151: out.completeReply();
152: } else {
153: out.startReply();
154:
155: out.writeNull();
156: out.completeReply();
157: }
158: }
159:
160: private void executeList(HessianInput in, HessianOutput out)
161: throws Throwable {
162: in.completeCall();
163:
164: EjbProtocolManager container = _protocol.getProtocolManager();
165:
166: AbstractServer server = container.getServerByEJBName(_prefix);
167:
168: ArrayList children;
169:
170: if (server != null) {
171: EJBHome home = server.getEJBHome();
172:
173: out.startReply();
174:
175: out.writeNull();
176:
177: out.completeReply();
178: } else if ((children = container.getRemoteChildren(_prefix)) != null) {
179: out.startReply();
180:
181: out.writeObject(children
182: .toArray(new String[children.size()]));
183:
184: out.completeReply();
185: } else {
186: out.startReply();
187:
188: out.writeNull();
189: out.completeReply();
190: }
191: }
192:
193: /**
194: * Executes an unknown method.
195: *
196: * @param method the method name to match.
197: * @param in the hessian input stream
198: * @param out the hessian output stream
199: */
200: protected void executeUnknown(String method, HessianInput in,
201: HessianOutput out) throws Exception {
202: if (method.equals("_hessian_getAttribute")) {
203: String key = in.readString();
204: in.completeCall();
205:
206: out.startReply();
207:
208: if ("java.api.class".equals(key))
209: out.writeString(NameServerRemote.class.getName());
210: else if ("java.home.class".equals(key))
211: out.writeString(NameServerRemote.class.getName());
212: else if ("java.object.class".equals(key))
213: out.writeString(NameServerRemote.class.getName());
214: else if ("home-class".equals(key))
215: out.writeString(NameServerRemote.class.getName());
216: else if ("remote-class".equals(key))
217: out.writeString(NameServerRemote.class.getName());
218: else
219: out.writeNull();
220:
221: out.completeReply();
222: } else {
223: out.startReply();
224: out.writeFault("NoMethod", "no such method: " + method,
225: null);
226: out.completeReply();
227: }
228: }
229: }
|