KickJava   Java API By Example, From Geeks To Geeks.

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


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.hessian.io.HessianRemoteResolver;
37 import com.caucho.log.Log;
38 import com.caucho.util.CharBuffer;
39
40 import java.io.InputStream JavaDoc;
41 import java.io.OutputStream JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Base class for any bean skeleton capable of handling a Burlap request.
47  *
48  * <p/>Once selected, the calling servlet will dispatch the request through
49  * the <code>_service</code> call. After parsing the request headers,
50  * <code>_service</code> calls the generated entry <code>_execute</code>
51  * to execute the request.
52  */

53 abstract public class BurlapSkeleton extends Skeleton {
54   protected static Logger JavaDoc log = Log.open(BurlapSkeleton.class);
55
56   private AbstractServer _server;
57   private HessianRemoteResolver _resolver;
58
59   void _setServer(AbstractServer server)
60   {
61     _server = server;
62   }
63   
64   /**
65    * Sets the burlap resolver.
66    */

67   void _setResolver(HessianRemoteResolver resolver)
68   {
69     _resolver = resolver;
70   }
71
72   abstract protected void _setObject(Object JavaDoc object);
73
74   public void _service(InputStream JavaDoc is, OutputStream JavaDoc os)
75     throws Exception JavaDoc
76   {
77     BurlapInput in = new BurlapReader(is);
78     BurlapOutput out = new BurlapWriter(os);
79
80     in.setRemoteResolver(_resolver);
81     in.startCall();
82
83     String JavaDoc method = in.getMethod();
84     CharBuffer cb = new CharBuffer();
85     cb.append(method);
86
87     String JavaDoc oldProtocol = EjbProtocolManager.setThreadProtocol("burlap");
88     
89     try {
90       _execute(cb, in, out);
91     } catch (BurlapProtocolException e) {
92       throw e;
93     } catch (Throwable JavaDoc e) {
94       log.log(Level.FINER, e.toString(), e);
95
96       out.startReply();
97       out.writeFault("ServiceException", e.getMessage(), e);
98       out.completeReply();
99     } finally {
100       EjbProtocolManager.setThreadProtocol(oldProtocol);
101     }
102   }
103
104   abstract protected void _execute(CharBuffer method,
105                                    BurlapInput in,
106                                    BurlapOutput out)
107     throws Throwable JavaDoc;
108   
109   protected void _executeUnknown(CharBuffer method,
110                                  BurlapInput in,
111                                  BurlapOutput out)
112     throws Exception JavaDoc
113   {
114     if (method.matches("_burlap_getAttribute")) {
115       String JavaDoc key = in.readString();
116       in.completeCall();
117
118       out.startReply();
119
120       if ("java.api.class".equals(key))
121         out.writeString(_server.getRemoteHomeClass().getName());
122       else if ("java.home.class".equals(key))
123         out.writeString(_server.getRemoteHomeClass().getName());
124       else if ("java.object.class".equals(key))
125         out.writeString(_server.getRemoteObjectClass().getName());
126       else if ("home-class".equals(key))
127         out.writeString(_server.getRemoteHomeClass().getName());
128       else if ("remote-class".equals(key))
129         out.writeString(_server.getRemoteObjectClass().getName());
130       else
131         out.writeNull();
132       
133       out.completeReply();
134     }
135     else {
136       out.startReply();
137       out.writeFault("NoMethod", "no such method: " + method, null);
138       out.completeReply();
139     }
140   }
141 }
142
143
144
Popular Tags