KickJava   Java API By Example, From Geeks To Geeks.

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


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 SoftwareFoundation, 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.vfs.Path;
33 import com.caucho.vfs.Vfs;
34
35 import javax.transaction.xa.XAException JavaDoc;
36 import javax.transaction.xa.XAResource JavaDoc;
37 import javax.transaction.xa.Xid JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Resource
43  */

44 public class HessianXAResource implements XAResource JavaDoc {
45   private static final Logger JavaDoc log
46     = Logger.getLogger(HessianXAResource.class.getName());
47   
48   private String JavaDoc _url;
49   private Path _path;
50   
51   public HessianXAResource(String JavaDoc url)
52   {
53     _url = url;
54
55     _path = Vfs.lookup(url);
56   }
57
58   public boolean isSameRM(XAResource JavaDoc xares)
59   {
60     if (! (xares instanceof HessianXAResource))
61       return false;
62     
63     HessianXAResource rm = (HessianXAResource) xares;
64
65     return _url.equals(rm._url);
66   }
67
68   public void start(Xid JavaDoc xid, int flags)
69     throws XAException JavaDoc
70   {
71   }
72   
73   public void end(Xid JavaDoc xid, int flags)
74     throws XAException JavaDoc
75   {
76   }
77   
78   public boolean setTransactionTimeout(int seconds)
79     throws XAException JavaDoc
80   {
81     return true;
82   }
83   
84   public int getTransactionTimeout()
85     throws XAException JavaDoc
86   {
87     return 0;
88   }
89
90   public int prepare(Xid JavaDoc xid)
91     throws XAException JavaDoc
92   {
93     try {
94       Object JavaDoc value = MetaStub.call(_path, "prepare", xidToString(xid));
95     } catch (Throwable JavaDoc e) {
96       log.log(Level.WARNING, e.toString(), e);
97
98       throw new XAException JavaDoc(e.toString());
99     }
100     
101     // also rdonly
102

103     return XA_OK;
104   }
105
106   public Xid JavaDoc[]recover(int flag)
107     throws XAException JavaDoc
108   {
109     // XXX: should query
110

111     return null;
112   }
113
114   public void forget(Xid JavaDoc xid)
115     throws XAException JavaDoc
116   {
117   }
118
119   public void rollback(Xid JavaDoc xid)
120     throws XAException JavaDoc
121   {
122     try {
123       MetaStub.call(_path, "rollback", xidToString(xid));
124     } catch (Throwable JavaDoc e) {
125       log.log(Level.WARNING, e.toString(), e);
126
127       throw new XAException JavaDoc(e.toString());
128     }
129   }
130
131   public void commit(Xid JavaDoc xid, boolean onephase)
132     throws XAException JavaDoc
133   {
134     try {
135       if (onephase)
136     MetaStub.call(_path, "commitOnePhase", xidToString(xid));
137       else
138     MetaStub.call(_path, "commit", xidToString(xid));
139     } catch (Throwable JavaDoc e) {
140       log.log(Level.WARNING, e.toString(), e);
141
142       throw new XAException JavaDoc(e.toString());
143     }
144   }
145
146   private static String JavaDoc xidToString(Xid JavaDoc xid)
147   {
148     byte []id = xid.getGlobalTransactionId();
149
150     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
151
152     for (int i = 0; i < id.length; i++) {
153       byte b = id[i];
154
155       sb.append(toHex((b >> 4) & 0xf));
156       sb.append(toHex(b & 0xf));
157     }
158
159     return sb.toString();
160   }
161
162   private static char toHex(int d)
163   {
164     if (d < 10)
165       return (char) ('0' + d);
166     else
167       return (char) ('a' + d - 10);
168   }
169 }
170
Popular Tags