KickJava   Java API By Example, From Geeks To Geeks.

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


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.protocol.Skeleton;
32 import com.caucho.ejb.xa.EjbTransactionManager;
33 import com.caucho.hessian.io.HessianInput;
34 import com.caucho.hessian.io.HessianOutput;
35 import com.caucho.hessian.io.HessianProtocolException;
36
37 import javax.transaction.xa.XAResource JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Skeleton for XA Resource.
45  */

46 public class XAResourceSkeleton extends Skeleton {
47   protected static final Logger JavaDoc log
48     = Logger.getLogger(XAResourceSkeleton.class.getName());
49
50   private EjbTransactionManager _tm;
51   
52   XAResourceSkeleton(EjbTransactionManager tm)
53   {
54     _tm = tm;
55   }
56
57   /**
58    * Services the request.
59    */

60   public void _service(InputStream JavaDoc is, OutputStream JavaDoc os)
61     throws Exception JavaDoc
62   {
63     HessianInput in = new HessianReader(is);
64     HessianOutput out = new HessianWriter(os);
65
66     in.startCall();
67
68     String JavaDoc method = in.getMethod();
69
70     try {
71       if (method.equals("commitOnePhase") ||
72           method.equals("commitOnePhase_string") ||
73           method.equals("commitOnePhase_1")) {
74         executeCommitOnePhase(in, out);
75       }
76       else if (method.equals("commit") ||
77            method.equals("commit_string") ||
78            method.equals("commit_1")) {
79         executeCommit(in, out);
80       }
81       else if (method.equals("rollback") ||
82            method.equals("rollback_string") ||
83            method.equals("rollback_1")) {
84         executeRollback(in, out);
85       }
86       else if (method.equals("prepare") ||
87            method.equals("prepare_string") ||
88            method.equals("prepare_1")) {
89         executePrepare(in, out);
90       }
91       else
92         executeUnknown(method, in, out);
93     } catch (HessianProtocolException e) {
94       throw e;
95     } catch (Throwable JavaDoc e) {
96       log.log(Level.WARNING, e.toString(), e);
97
98       out.startReply();
99       out.writeFault("ServiceException", e.getMessage(), e);
100       out.completeReply();
101     }
102   }
103
104   private void executeCommitOnePhase(HessianInput in, HessianOutput out)
105     throws Throwable JavaDoc
106   {
107     String JavaDoc xid = in.readString();
108     in.completeCall();
109
110     _tm.commitTransaction(xid);
111
112     out.startReply();
113     out.writeNull();
114     out.completeReply();
115   }
116
117   private void executeCommit(HessianInput in, HessianOutput out)
118     throws Throwable JavaDoc
119   {
120     String JavaDoc xid = in.readString();
121     in.completeCall();
122
123     _tm.commitTransaction(xid);
124
125     out.startReply();
126     out.writeNull();
127     out.completeReply();
128   }
129
130   private void executePrepare(HessianInput in, HessianOutput out)
131     throws Throwable JavaDoc
132   {
133     String JavaDoc xid = in.readString();
134     in.completeCall();
135
136     // XXX: _tm.commitTransaction(xid);
137

138     out.startReply();
139     out.writeInt(XAResource.XA_OK);
140     out.completeReply();
141   }
142
143   private void executeRollback(HessianInput in, HessianOutput out)
144     throws Throwable JavaDoc
145   {
146     String JavaDoc xid = in.readString();
147     in.completeCall();
148
149     _tm.rollbackTransaction(xid);
150     
151     out.startReply();
152     out.writeNull();
153     out.completeReply();
154   }
155        
156   /**
157    * Executes an unknown method.
158    *
159    * @param method the method name to match.
160    * @param in the hessian input stream
161    * @param out the hessian output stream
162    */

163   protected void executeUnknown(String JavaDoc method,
164                                 HessianInput in, HessianOutput out)
165     throws Exception JavaDoc
166   {
167     if (method.equals("_hessian_getAttribute")) {
168       String JavaDoc key = in.readString();
169       in.completeCall();
170
171       out.startReply();
172
173       /*
174       if ("java.api.class".equals(key))
175         out.writeString(NameServerRemote.class.getName());
176       else if ("java.home.class".equals(key))
177         out.writeString(NameServerRemote.class.getName());
178       else if ("java.object.class".equals(key))
179         out.writeString(NameServerRemote.class.getName());
180       else if ("home-class".equals(key))
181         out.writeString(NameServerRemote.class.getName());
182       else if ("remote-class".equals(key))
183         out.writeString(NameServerRemote.class.getName());
184       else
185         out.writeNull();
186       */

187       out.writeNull();
188       
189       out.completeReply();
190     }
191     else {
192       out.startReply();
193       out.writeFault("NoMethod", "no such method: " + method, null);
194       out.completeReply();
195     }
196   }
197 }
198
Popular Tags