KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > recover > bean > DummyRecoverableProxyService


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.test.recover.bean;
23
24 import java.io.Serializable JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.transaction.xa.XAException JavaDoc;
29 import javax.transaction.xa.XAResource JavaDoc;
30 import javax.transaction.xa.Xid JavaDoc;
31
32 import org.jboss.naming.Util;
33 import org.jboss.system.ServiceMBeanSupport;
34 import org.jboss.tm.recovery.Recoverable;
35 import org.jboss.tm.recovery.RecoveryManagerServiceMBean;
36 import org.jboss.tm.recovery.RecoveryTestingException;
37
38 /**
39  * Service MBean that registers a proxy to a remote DummyRecoverable in
40  * the local JNDI space.
41  *
42  * @author <a HREF="reverbel@ime.usp.br">Francisco Reverbel</a>
43  * @version $Revision: 37406 $
44  */

45 public class DummyRecoverableProxyService
46       extends ServiceMBeanSupport
47       implements DummyRecoverableProxyServiceMBean
48 {
49    private String JavaDoc jndiName;
50    private String JavaDoc remoteJndiName;
51    private RecoveryManagerServiceMBean manager;
52    private Recoverable proxy;
53    
54    public String JavaDoc getJndiName()
55    {
56       return jndiName;
57    }
58
59    public void setJndiName(String JavaDoc jndiName)
60    {
61       this.jndiName = jndiName;
62    }
63
64    public String JavaDoc getRemoteJndiName()
65    {
66       return remoteJndiName;
67    }
68
69    public void setRemoteJndiName(String JavaDoc remoteJndiName)
70    {
71       this.remoteJndiName = remoteJndiName;
72    }
73
74    public void setManager(RecoveryManagerServiceMBean manager)
75    {
76       this.manager = manager;
77    }
78
79    public Recoverable getProxy()
80    {
81       return proxy;
82    }
83    
84    public void startService() throws Exception JavaDoc
85    {
86       proxy = (Recoverable) Util.lookup(remoteJndiName, Recoverable.class);
87       log.debug("Got Recoverable proxy from remoteJndiName=" + remoteJndiName);
88       manager.registerRecoverable(proxy);
89
90       Context JavaDoc iniCtx = new InitialContext JavaDoc();
91       Util.bind(iniCtx, jndiName, new RecoverableWrapper(proxy));
92       log.debug("Bound wrapped proxy under jndiName=" + jndiName);
93    }
94
95    public void stopService() throws Exception JavaDoc
96    {
97       Context JavaDoc iniCtx = new InitialContext JavaDoc();
98       Util.unbind(iniCtx, jndiName);
99       manager = null;
100       proxy = null;
101    }
102    
103    public static class RecoverableWrapper
104          implements Recoverable, Serializable JavaDoc
105    {
106       private Recoverable inner;
107       private transient XAResource JavaDoc resourceWrapper = null;
108       
109       RecoverableWrapper(Recoverable inner)
110       {
111          this.inner = inner;
112       }
113       
114       public String JavaDoc getId()
115       {
116          return inner.getId();
117       }
118
119       public XAResource JavaDoc getResource()
120       {
121          if (resourceWrapper == null)
122             resourceWrapper = new XAResourceWrapper(inner.getResource());
123          
124          return resourceWrapper;
125       }
126
127       public Xid JavaDoc[] scan() throws XAException JavaDoc
128       {
129          return inner.scan();
130       }
131
132       public void cleanupResource()
133       {
134          inner.cleanupResource();
135       }
136       
137       public XAResource JavaDoc getUnwrappedResource()
138       {
139          return inner.getResource();
140       }
141
142    }
143    
144    private static class XAResourceWrapper
145          implements XAResource JavaDoc, Serializable JavaDoc
146    {
147       private XAResource JavaDoc inner;
148       
149       XAResourceWrapper(XAResource JavaDoc xaRes)
150       {
151          inner = xaRes;
152       }
153       
154       public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc
155       {
156          try
157          {
158             inner.commit(xid, onePhase);
159          }
160          catch (RecoveryTestingException e)
161          {
162             crash();
163          }
164       }
165
166       public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc
167       {
168          inner.end(xid, flags);
169       }
170
171       public void forget(Xid JavaDoc xid) throws XAException JavaDoc
172       {
173          inner.forget(xid);
174       }
175
176       public int getTransactionTimeout() throws XAException JavaDoc
177       {
178          return inner.getTransactionTimeout();
179       }
180
181       public boolean isSameRM(XAResource JavaDoc xaRes) throws XAException JavaDoc
182       {
183          return inner.isSameRM(xaRes);
184       }
185
186       public int prepare(Xid JavaDoc xid) throws XAException JavaDoc
187       {
188          try
189          {
190             return inner.prepare(xid);
191          }
192          catch (RecoveryTestingException e)
193          {
194             crash();
195             return 0;
196          }
197       }
198
199       public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc
200       {
201          return inner.recover(flag);
202       }
203
204       public void rollback(Xid JavaDoc xid) throws XAException JavaDoc
205       {
206          try
207          {
208             inner.rollback(xid);
209          }
210          catch (RecoveryTestingException e)
211          {
212             crash();
213          }
214          
215       }
216
217       public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc
218       {
219          return inner.setTransactionTimeout(seconds);
220       }
221
222       public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc
223       {
224          inner.start(xid, flags);
225       }
226       
227    }
228
229    private static void crash()
230    {
231       Runtime.getRuntime().halt(1);
232    }
233
234 }
235
Popular Tags