KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > HessianSkeleton


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.hessian;
31
32 import com.caucho.ejb.AbstractServer;
33 import com.caucho.ejb.protocol.EjbProtocolManager;
34 import com.caucho.ejb.protocol.Skeleton;
35 import com.caucho.ejb.xa.TransactionContext;
36 import com.caucho.hessian.io.HessianDebugInputStream;
37 import com.caucho.hessian.io.HessianInput;
38 import com.caucho.hessian.io.HessianOutput;
39 import com.caucho.hessian.io.HessianProtocolException;
40 import com.caucho.hessian.io.HessianRemoteResolver;
41 import com.caucho.log.Log;
42 import com.caucho.util.CharBuffer;
43
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.io.PrintWriter JavaDoc;
48 import java.util.logging.Level JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50
51 /**
52  * Base class for any bean skeleton capable of handling a Hessian request.
53  *
54  * <p/>Once selected, the calling servlet will dispatch the request through
55  * the <code>_service</code> call. After parsing the request headers,
56  * <code>_service</code> calls the generated entry <code>_execute</code>
57  * to execute the request.
58  */

59 abstract public class HessianSkeleton extends Skeleton {
60   protected static final Logger JavaDoc log = Log.open(HessianSkeleton.class);
61
62   private AbstractServer _server;
63   private HessianRemoteResolver _resolver;
64
65   private boolean _isDebug;
66
67   void _setServer(AbstractServer server)
68   {
69     _server = server;
70   }
71
72   public void setDebug(boolean isDebug)
73   {
74     _isDebug = isDebug;
75   }
76   
77   /**
78    * Sets the hessian resolver.
79    */

80   void _setResolver(HessianRemoteResolver resolver)
81   {
82     _resolver = resolver;
83   }
84
85   abstract protected void _setObject(Object JavaDoc object);
86
87   public void _service(InputStream is, OutputStream JavaDoc os)
88     throws Exception JavaDoc
89   {
90     java.io.StringWriter JavaDoc debugWriter = null;
91
92     if (_isDebug) {
93       debugWriter = new java.io.StringWriter JavaDoc();
94       is = new HessianDebugInputStream(is, new PrintWriter JavaDoc(debugWriter));
95     }
96     
97     HessianInput in = new HessianReader(is);
98     HessianOutput out = new HessianWriter(os);
99
100     in.setRemoteResolver(_resolver);
101     in.readCall();
102
103     String JavaDoc xid = null;
104     String JavaDoc header;
105     while ((header = in.readHeader()) != null) {
106       Object JavaDoc value = in.readObject();
107
108       if ("xid".equals(header)) {
109     xid = (String JavaDoc) value;
110       }
111     }
112
113     String JavaDoc method = in.readMethod();
114
115     CharBuffer cb = new CharBuffer();
116     cb.append(method);
117     
118     String JavaDoc oldProtocol = EjbProtocolManager.setThreadProtocol("hessian");
119     
120     try {
121       TransactionContext xa = null;
122       
123       if (xid != null)
124     xa = _server.getTransactionManager().startTransaction(xid);
125     
126       _execute(cb, in, out, xa);
127     } catch (HessianProtocolException e) {
128       throw e;
129     } catch (Throwable JavaDoc e) {
130       log.log(Level.FINER, e.toString(), e);
131
132       out.startReply();
133       out.writeFault("ServiceException", e.getMessage(), e);
134       out.completeReply();
135     } finally {
136       EjbProtocolManager.setThreadProtocol(oldProtocol);
137
138       if (debugWriter != null)
139     log.fine(debugWriter.toString());
140       
141       if (xid != null)
142     _server.getTransactionManager().finishTransaction(xid);
143     }
144   }
145
146   protected void startReply(HessianOutput out, TransactionContext xa)
147     throws IOException JavaDoc
148   {
149     out.startReply();
150
151     if (xa != null && ! xa.isEmpty()) {
152       EjbProtocolManager pm = _server.getServerManager().getProtocolManager();
153       HessianProtocol hessian = (HessianProtocol) pm.getProtocol("hessian");
154
155       if (hessian != null) {
156     out.writeHeader("xa-resource");
157     out.writeString(hessian.calculateURL("/_ejb_xa_resource"));
158       }
159     }
160   }
161
162   abstract protected void _execute(CharBuffer method,
163                                    HessianInput in,
164                                    HessianOutput out,
165                    TransactionContext xa)
166     throws Throwable JavaDoc;
167   
168   protected void _executeUnknown(CharBuffer method,
169                                  HessianInput in,
170                                  HessianOutput out)
171     throws Exception JavaDoc
172   {
173     if (method.matches("_hessian_getAttribute")) {
174       String JavaDoc key = in.readString();
175       in.completeCall();
176
177       out.startReply();
178
179       if ("java.api.class".equals(key))
180         out.writeString(_server.getRemoteHomeClass().getName());
181       else if ("java.home.class".equals(key))
182         out.writeString(_server.getRemoteHomeClass().getName());
183       else if ("java.object.class".equals(key))
184         out.writeString(_server.getRemoteObjectClass().getName());
185       else if ("home-class".equals(key))
186         out.writeString(_server.getRemoteHomeClass().getName());
187       else if ("remote-class".equals(key))
188         out.writeString(_server.getRemoteObjectClass().getName());
189       else
190         out.writeNull();
191       
192       out.completeReply();
193     }
194     else {
195       out.startReply();
196       out.writeFault("NoMethod", "no such method: " + method, null);
197       out.completeReply();
198     }
199   }
200 }
201
202
203
Popular Tags