KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > burlap > NameContextSkeleton


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.burlap;
30
31 import com.caucho.burlap.io.BurlapInput;
32 import com.caucho.burlap.io.BurlapOutput;
33 import com.caucho.ejb.AbstractServer;
34 import com.caucho.ejb.protocol.EjbProtocolManager;
35 import com.caucho.ejb.protocol.Skeleton;
36 import com.caucho.log.Log;
37 import com.caucho.services.name.NameServerRemote;
38
39 import javax.ejb.EJBHome JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.OutputStream JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Base class for any bean skeleton capable of handling a Burlap request.
48  *
49  * <p/>Once selected, the calling servlet will dispatch the request through
50  * the <code>_service</code> call. After parsing the request headers,
51  * <code>_service</code> calls the generated entry <code>_execute</code>
52  * to execute the request.
53  */

54 public class NameContextSkeleton extends Skeleton {
55   protected static final Logger JavaDoc log = Log.open(NameContextSkeleton.class);
56
57   private BurlapProtocol _protocol;
58   private String JavaDoc _prefix;
59
60   NameContextSkeleton(BurlapProtocol protocol, String JavaDoc prefix)
61   {
62     _protocol = protocol;
63     _prefix = prefix;
64   }
65
66   /**
67    * Services the request.
68    */

69   public void _service(InputStream JavaDoc is, OutputStream JavaDoc os)
70     throws Exception JavaDoc
71   {
72     BurlapInput in = new BurlapReader(is);
73     BurlapOutput out = new BurlapWriter(os);
74
75     in.startCall();
76
77     String JavaDoc method = in.getMethod();
78
79     try {
80       if (method.equals("lookup") ||
81           method.equals("lookup_string") ||
82           method.equals("lookup_1"))
83         executeLookup(in, out);
84       else if (method.equals("list"))
85         executeList(in, out);
86       else
87         executeUnknown(method, in, out);
88     } catch (BurlapProtocolException e) {
89       throw e;
90     } catch (Throwable JavaDoc e) {
91       log.log(Level.FINE, e.toString(), e);
92
93       out.startReply();
94       out.writeFault("ServiceException", e.getMessage(), e);
95       out.completeReply();
96     }
97   }
98
99   private void executeLookup(BurlapInput in, BurlapOutput out)
100     throws Throwable JavaDoc
101   {
102     String JavaDoc name = in.readString();
103     in.completeCall();
104
105     EjbProtocolManager container = _protocol.getProtocolManager();
106     
107     AbstractServer server = container.getServerByEJBName(name);
108
109     if (server != null) {
110       EJBHome JavaDoc home = server.getEJBHome();
111       
112       out.startReply();
113       
114       out.writeObject(home);
115
116       out.completeReply();
117     }
118     else if (container.getRemoteChildren(name) != null) {
119       out.startReply();
120
121       String JavaDoc serverId = _prefix + name;
122       if (serverId.startsWith("/"))
123         serverId = serverId.substring(1);
124       
125       out.writeRemote(NameServerRemote.class.getName(),
126                       _protocol.getURLPrefix() + serverId);
127       
128       out.completeReply();
129     }
130     else {
131       out.startReply();
132
133       out.writeNull();
134       out.completeReply();
135     }
136   }
137
138   private void executeList(BurlapInput in, BurlapOutput out)
139     throws Throwable JavaDoc
140   {
141     in.completeCall();
142
143     EjbProtocolManager container = _protocol.getProtocolManager();
144     
145     AbstractServer server = container.getServerByEJBName(_prefix);
146
147     ArrayList JavaDoc children;
148     
149     if (server != null) {
150       EJBHome JavaDoc home = server.getEJBHome();
151       
152       out.startReply();
153       
154       out.writeNull();
155
156       out.completeReply();
157     }
158     else if ((children = container.getRemoteChildren(_prefix)) != null) {
159       out.startReply();
160       
161       out.writeObject(children.toArray(new String JavaDoc[children.size()]));
162       
163       out.completeReply();
164     }
165     else {
166       out.startReply();
167
168       out.writeNull();
169       out.completeReply();
170     }
171   }
172
173   /**
174    * Executes an unknown method.
175    *
176    * @param method the method name to match.
177    * @param in the burlap input stream
178    * @param out the burlap output stream
179    */

180   protected void executeUnknown(String JavaDoc method,
181                                 BurlapInput in, BurlapOutput out)
182     throws Exception JavaDoc
183   {
184     if (method.equals("_burlap_getAttribute")) {
185       String JavaDoc key = in.readString();
186       in.completeCall();
187
188       out.startReply();
189
190       if ("java.api.class".equals(key))
191         out.writeString(NameServerRemote.class.getName());
192       else if ("java.home.class".equals(key))
193         out.writeString(NameServerRemote.class.getName());
194       else if ("java.object.class".equals(key))
195         out.writeString(NameServerRemote.class.getName());
196       else if ("home-class".equals(key))
197         out.writeString(NameServerRemote.class.getName());
198       else if ("remote-class".equals(key))
199         out.writeString(NameServerRemote.class.getName());
200       else
201         out.writeNull();
202       
203       out.completeReply();
204     }
205     else {
206       out.startReply();
207       out.writeFault("NoMethod", "no such method: " + method, null);
208       out.completeReply();
209     }
210   }
211 }
212
Popular Tags