KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > 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.hessian;
30
31 import com.caucho.ejb.AbstractServer;
32 import com.caucho.ejb.protocol.EjbProtocolManager;
33 import com.caucho.ejb.protocol.Skeleton;
34 import com.caucho.hessian.io.HessianInput;
35 import com.caucho.hessian.io.HessianOutput;
36 import com.caucho.hessian.io.HessianProtocolException;
37 import com.caucho.log.Log;
38 import com.caucho.services.name.NameServerRemote;
39
40 import javax.ejb.EJBHome JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.io.OutputStream JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Base class for any bean skeleton capable of handling a Hessian request.
49  *
50  * <p/>Once selected, the calling servlet will dispatch the request through
51  * the <code>_service</code> call. After parsing the request headers,
52  * <code>_service</code> calls the generated entry <code>_execute</code>
53  * to execute the request.
54  */

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

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

204   protected void executeUnknown(String JavaDoc method,
205                                 HessianInput in, HessianOutput out)
206     throws Exception JavaDoc
207   {
208     if (method.equals("_hessian_getAttribute")) {
209       String JavaDoc key = in.readString();
210       in.completeCall();
211
212       out.startReply();
213
214       if ("java.api.class".equals(key))
215         out.writeString(NameServerRemote.class.getName());
216       else if ("java.home.class".equals(key))
217         out.writeString(NameServerRemote.class.getName());
218       else if ("java.object.class".equals(key))
219         out.writeString(NameServerRemote.class.getName());
220       else if ("home-class".equals(key))
221         out.writeString(NameServerRemote.class.getName());
222       else if ("remote-class".equals(key))
223         out.writeString(NameServerRemote.class.getName());
224       else
225         out.writeNull();
226       
227       out.completeReply();
228     }
229     else {
230       out.startReply();
231       out.writeFault("NoMethod", "no such method: " + method, null);
232       out.completeReply();
233     }
234   }
235 }
236
Popular Tags