KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > iiop > wrapper > OTSResourceWrapper


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.tm.iiop.wrapper;
23
24 import java.rmi.RemoteException JavaDoc;
25
26 import javax.transaction.HeuristicCommitException JavaDoc;
27 import javax.transaction.HeuristicMixedException JavaDoc;
28 import javax.transaction.HeuristicRollbackException JavaDoc;
29
30 import org.jboss.iiop.CorbaORB;
31 import org.jboss.tm.remoting.interfaces.HeuristicHazardException;
32 import org.jboss.tm.remoting.interfaces.Resource;
33 import org.jboss.tm.remoting.interfaces.TransactionNotPreparedException;
34 import org.jboss.tm.remoting.interfaces.TransactionAlreadyPreparedException;
35 import org.jboss.tm.remoting.interfaces.Vote;
36
37
38 /**
39  * Implements <code>org.jboss.tm.remoting.interfaces.Resource</code> by
40  * wrapping an OTS resource and forwarding all method calls to the wrapped
41  * resource.
42  *
43  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
44  * @version $Revision: 37459 $
45  */

46 public class OTSResourceWrapper implements Resource
47 {
48    // Private field -------------------------------------------------
49

50    /**
51     * The wrapped OTS resource.
52     */

53    private org.omg.CosTransactions.Resource otsResource;
54    
55    // Constructor ---------------------------------------------------
56

57    /**
58     * Creates an <code>OTSResourceWrapper</code> for a given OTS resource.
59     * @param otsResource the OTS resource to be wrapped.
60     */

61    public OTSResourceWrapper(org.omg.CosTransactions.Resource otsResource)
62    {
63       this.otsResource = otsResource;
64    }
65    
66    // Attribute -----------------------------------------------------
67

68    public org.omg.CosTransactions.Resource getWrappedResource()
69    {
70       return otsResource;
71    }
72    
73    // Conversion to string ------------------------------------------
74

75    public String JavaDoc toString()
76    {
77       return CorbaORB.getInstance().object_to_string(otsResource);
78    }
79    
80    // Static --------------------------------------------------------
81

82    public static OTSResourceWrapper fromString(String JavaDoc s)
83    {
84       org.omg.CORBA.Object JavaDoc obj = CorbaORB.getInstance().string_to_object(s);
85       org.omg.CosTransactions.Resource otsRes =
86          org.omg.CosTransactions.ResourceHelper.narrow(obj);
87       return new OTSResourceWrapper(otsRes);
88    }
89    
90    // org.jboss.tm.remoting.interfaces.Resource methods -------------
91

92    /**
93     * Calls <code>prepare</code> on the wrapped OTS resource.
94     * @see org.jboss.tm.remoting.interfaces.Resource#prepare()
95     */

96    public Vote prepare()
97          throws RemoteException JavaDoc,
98                 TransactionAlreadyPreparedException,
99                 HeuristicMixedException JavaDoc,
100                 HeuristicHazardException
101    {
102       try
103       {
104          org.omg.CosTransactions.Vote otsVote = otsResource.prepare();
105          if (otsVote == org.omg.CosTransactions.Vote.VoteCommit)
106             return Vote.COMMIT;
107          else if (otsVote == org.omg.CosTransactions.Vote.VoteReadOnly)
108             return Vote.READONLY;
109          else // (otsVote == org.omg.CosTransactions.Vote.VoteRollback)
110
return Vote.ROLLBACK;
111       }
112       catch (org.omg.CosTransactions.HeuristicMixed hm)
113       {
114          throw new HeuristicMixedException JavaDoc();
115       }
116       catch (org.omg.CosTransactions.HeuristicHazard hz)
117       {
118          throw new HeuristicHazardException();
119       }
120       catch (org.omg.CORBA.BAD_INV_ORDER JavaDoc bio)
121       {
122          throw new TransactionAlreadyPreparedException();
123       }
124       catch (org.omg.CORBA.SystemException JavaDoc se)
125       {
126          throw javax.rmi.CORBA.Util.mapSystemException(se);
127       }
128    }
129
130    /**
131     * Calls <code>rollback</code> on the wrapper OTS resource.
132     * @see org.jboss.tm.remoting.interfaces.Resource#rollback()
133     */

134    public void rollback()
135          throws RemoteException JavaDoc,
136                 HeuristicCommitException JavaDoc,
137                 HeuristicMixedException JavaDoc,
138                 HeuristicHazardException
139    {
140       try
141       {
142          otsResource.rollback();
143       }
144       catch (org.omg.CosTransactions.HeuristicCommit hc)
145       {
146          throw new HeuristicCommitException JavaDoc();
147       }
148       catch (org.omg.CosTransactions.HeuristicMixed hm)
149       {
150          throw new HeuristicMixedException JavaDoc();
151       }
152       catch (org.omg.CosTransactions.HeuristicHazard hz)
153       {
154          throw new HeuristicHazardException();
155       }
156       catch (org.omg.CORBA.SystemException JavaDoc se)
157       {
158          throw javax.rmi.CORBA.Util.mapSystemException(se);
159       }
160    }
161
162    /**
163     * Calls <code>commit</code> on the wrapped OTS resource.
164     * @see org.jboss.tm.remoting.interfaces.Resource#commit()
165     */

166    public void commit() throws RemoteException JavaDoc,
167          TransactionNotPreparedException, HeuristicRollbackException JavaDoc,
168          HeuristicMixedException JavaDoc, HeuristicHazardException
169    {
170       try
171       {
172          otsResource.commit();
173       }
174       catch (org.omg.CosTransactions.NotPrepared np)
175       {
176          throw new TransactionNotPreparedException();
177       }
178       catch (org.omg.CosTransactions.HeuristicRollback hr)
179       {
180          throw new HeuristicRollbackException JavaDoc();
181       }
182       catch (org.omg.CosTransactions.HeuristicMixed hm)
183       {
184          throw new HeuristicMixedException JavaDoc();
185       }
186       catch (org.omg.CosTransactions.HeuristicHazard hz)
187       {
188          throw new HeuristicHazardException();
189       }
190       catch (org.omg.CORBA.SystemException JavaDoc se)
191       {
192          throw javax.rmi.CORBA.Util.mapSystemException(se);
193       }
194    }
195
196    /**
197     * Calls <code>commit_one_phase</code> on the wrapped OTS resource.
198     * @see org.jboss.tm.remoting.interfaces.Resource#commitOnePhase()
199     */

200    public void commitOnePhase() throws RemoteException JavaDoc,
201          HeuristicHazardException
202    {
203       try
204       {
205          otsResource.commit_one_phase();
206       }
207       catch (org.omg.CosTransactions.HeuristicHazard hz)
208       {
209          throw new HeuristicHazardException();
210       }
211       catch (org.omg.CORBA.SystemException JavaDoc se)
212       {
213          throw javax.rmi.CORBA.Util.mapSystemException(se);
214       }
215    }
216
217    /**
218     * Calls <code>forget</code> on the wrapped OTS resource.
219     * @see org.jboss.tm.remoting.interfaces.Resource#forget()
220     */

221    public void forget() throws RemoteException JavaDoc
222    {
223       try
224       {
225          otsResource.forget();
226       }
227       catch (org.omg.CORBA.SystemException JavaDoc se)
228       {
229          throw javax.rmi.CORBA.Util.mapSystemException(se);
230       }
231    }
232
233 }
234
Popular Tags