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