KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bmp > beans > BMPHelperSessionBean


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.bmp.beans;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29
30 import javax.ejb.CreateException JavaDoc;
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.SessionBean JavaDoc;
33 import javax.ejb.SessionContext JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.sql.DataSource JavaDoc;
37
38 import org.jboss.logging.Logger;
39 import org.jboss.test.bmp.interfaces.SimpleBMP;
40 import org.jboss.test.bmp.interfaces.SimpleBMPHome;
41
42 public class BMPHelperSessionBean implements SessionBean JavaDoc
43 {
44    /** The serialVersionUID */
45    private static final long serialVersionUID = 1L;
46
47    Logger log = Logger.getLogger(getClass());
48    
49    SessionContext JavaDoc ctx = null;
50    private DataSource JavaDoc ds = null;
51    
52    public void ejbCreate () throws CreateException JavaDoc, RemoteException JavaDoc
53    {
54       try
55       {
56          ds = (DataSource JavaDoc)new InitialContext JavaDoc ().lookup ("java:comp/env/datasource");
57       }
58       catch (NamingException JavaDoc _ne)
59       {
60          throw new CreateException JavaDoc ("Datasource not found: "+_ne.getMessage ());
61       }
62    }
63    
64    public boolean existsSimpleBeanTable ()
65    {
66       return tableExists ("SIMPLEBEAN");
67    }
68    
69    public void createSimpleBeanTable ()
70    {
71       createTable ("CREATE TABLE SIMPLEBEAN (id INTEGER, name VARCHAR(200))");
72    }
73    
74    public void dropSimpleBeanTable ()
75    {
76       dropTable ("SIMPLEBEAN");
77    }
78    
79    public String JavaDoc doTest () throws RemoteException JavaDoc
80    {
81       StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
82       SimpleBMP b;
83       try
84       {
85          SimpleBMPHome home = (SimpleBMPHome) new InitialContext JavaDoc ().lookup ("java:comp/env/bean");
86          b = home.findByPrimaryKey(new Integer JavaDoc (1));
87       }
88       catch (Exception JavaDoc _ne)
89       {
90          throw new EJBException JavaDoc ("couldnt find entity: "+_ne.getMessage ());
91       }
92       sb.append ("found: "+b.getName ()+"\n");
93       sb.append ("set name to \"Name for rollback\"\n");
94       b.setName ("Name for rollback");
95       sb.append ("current name is: "+b.getName ()+"\n");
96       try
97       {
98          sb.append ("now rolling back...\n");
99          
100          ctx.setRollbackOnly();
101       }
102       catch (Exception JavaDoc _e)
103       {
104          sb.append ("Error on rolling back: "+_e.getMessage ()+"\n");
105       }
106       sb.append ("done.");
107    
108       return sb.toString ();
109    }
110    
111    public String JavaDoc doTestAfterRollback () throws RemoteException JavaDoc
112    {
113       StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
114       SimpleBMP b;
115       try
116       {
117          SimpleBMPHome home = (SimpleBMPHome) new InitialContext JavaDoc ().lookup ("java:comp/env/bean");
118          b = home.findByPrimaryKey(new Integer JavaDoc (1));
119       }
120       catch (Exception JavaDoc _ne)
121       {
122          throw new EJBException JavaDoc ("couldnt find entity: "+_ne.getMessage ());
123       }
124       sb.append ("found: "+b.getName ()+"\n");
125       sb.append ("done.");
126    
127       return sb.toString ();
128    }
129    
130    private boolean tableExists (String JavaDoc _tableName)
131    {
132       boolean result = false;
133       Connection JavaDoc con = null;
134       try
135       {
136          con = ds.getConnection ();
137          DatabaseMetaData JavaDoc dmd = con.getMetaData ();
138          ResultSet JavaDoc rs = dmd.getTables (con.getCatalog (), null, _tableName, null);
139          if (rs.next ())
140             result = true;
141          
142          rs.close ();
143       }
144       catch (Exception JavaDoc _e)
145       {
146          throw new EJBException JavaDoc ("Error while looking up table: "+_e.getMessage ());
147       }
148       finally
149       {
150          try
151          {
152             if (con != null)
153                con.close ();
154          }
155          catch (Exception JavaDoc _sqle)
156          {
157          }
158       }
159       return result;
160    }
161    
162    
163    private void createTable (String JavaDoc _sql)
164    {
165       Connection JavaDoc con = null;
166       try
167       {
168          con = ds.getConnection ();
169          Statement JavaDoc s = con.createStatement ();
170          s.executeUpdate (_sql);
171          s.close ();
172       }
173       catch (Exception JavaDoc _e)
174       {
175          throw new EJBException JavaDoc ("Error while creating table: "+_e.getMessage ());
176       }
177       finally
178       {
179          try
180          {
181             if (con != null)
182                con.close ();
183          }
184          catch (Exception JavaDoc _sqle)
185          {
186          }
187       }
188    }
189    
190    private void dropTable (String JavaDoc _tableName)
191    {
192       Connection JavaDoc con = null;
193       try
194       {
195          con = ds.getConnection ();
196          Statement JavaDoc s = con.createStatement ();
197          s.executeUpdate ("DROP TABLE "+_tableName);
198          s.close ();
199       }
200       catch (Exception JavaDoc _e)
201       {
202          throw new EJBException JavaDoc ("Error while dropping table: "+_e.getMessage ());
203       }
204       finally
205       {
206          try
207          {
208             if (con != null)
209                con.close ();
210          }
211          catch (Exception JavaDoc _sqle)
212          {
213          }
214       }
215    }
216    
217    
218    public void ejbActivate () {}
219    public void ejbPassivate () {}
220    public void ejbRemove () {}
221    public void setSessionContext (SessionContext JavaDoc _ctx) {ctx = _ctx;}
222 }
223
Popular Tags