KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > recover > derby > EmbeddedDerbyRecoverable


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.derby;
23
24 import org.jboss.tm.recovery.Recoverable;
25 import org.jboss.tm.recovery.RecoveryManagerServiceMBean;
26 import org.jboss.system.ServiceMBeanSupport;
27
28 import javax.transaction.xa.XAResource JavaDoc;
29 import javax.transaction.xa.Xid JavaDoc;
30 import javax.transaction.xa.XAException JavaDoc;
31 import javax.sql.XAConnection JavaDoc;
32 import java.sql.DriverManager JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 import org.apache.derby.jdbc.EmbeddedXADataSource;
36
37 /**
38  * Test for embedded Derby.
39  *
40  * @author <a HREF="reverbel@ime.usp.br">Francisco Reverbel</a>
41  * @version $Revision: 37406 $
42  */

43 public class EmbeddedDerbyRecoverable
44       extends ServiceMBeanSupport
45       implements Recoverable, EmbeddedDerbyRecoverableMBean
46 {
47    private String JavaDoc id;
48    private String JavaDoc databaseName;
49    private String JavaDoc user;
50    private String JavaDoc password;
51    private String JavaDoc createDatabase;
52    private RecoveryManagerServiceMBean manager;
53    private EmbeddedXADataSource xads;
54    private XAConnection JavaDoc connection;
55    private XAResource JavaDoc resource;
56
57
58    protected void startService() throws Exception JavaDoc
59    {
60       super.startService();
61       xads = new EmbeddedXADataSource();
62       xads.setDatabaseName(databaseName);
63       xads.setUser(user);
64       xads.setPassword(password);
65       xads.setCreateDatabase(createDatabase);
66       manager.registerRecoverable(this);
67    }
68
69    public void setManager(RecoveryManagerServiceMBean manager)
70    {
71       this.manager = manager;
72    }
73
74
75    public String JavaDoc getId()
76    {
77       return id;
78    }
79
80    public void setId(String JavaDoc id)
81    {
82       this.id = id;
83    }
84
85    public String JavaDoc getDatabaseName()
86    {
87       return databaseName;
88    }
89
90    public void setDatabaseName(String JavaDoc databaseName)
91    {
92       this.databaseName = databaseName;
93    }
94
95    public String JavaDoc getUser()
96    {
97       return user;
98    }
99
100    public void setUser(String JavaDoc user)
101    {
102       this.user = user;
103    }
104
105    public String JavaDoc getPassword()
106    {
107       return password;
108    }
109
110    public void setPassword(String JavaDoc password)
111    {
112       this.password = password;
113    }
114
115    public String JavaDoc getCreateDatabase()
116    {
117       return createDatabase;
118    }
119
120    public void setCreateDatabase(String JavaDoc createDatabase)
121    {
122       this.createDatabase = createDatabase;
123    }
124
125    public XAResource JavaDoc getResource()
126    {
127       try
128       {
129          connection = xads.getXAConnection();
130          resource = connection.getXAResource();
131          return resource;
132       }
133       catch (SQLException JavaDoc e)
134       {
135          throw new RuntimeException JavaDoc(e);
136       }
137    }
138
139    public Xid JavaDoc[] scan() throws XAException JavaDoc
140    {
141       return resource.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN);
142    }
143
144    public void cleanupResource()
145    {
146       try
147       {
148          System.out.println("EmbeddedDerbyRecoverable \"" + getServiceName() + "\": closing XA connection");
149          connection.close();
150       }
151       catch (SQLException JavaDoc e)
152       {
153          throw new RuntimeException JavaDoc(e);
154       }
155    }
156 }
157
Popular Tags