KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > jbas979 > FacadeSessionBean


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.cmp2.jbas979;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35 import org.jboss.mx.util.MBeanServerLocator;
36 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 44013 $</tt>
41  */

42 public class FacadeSessionBean
43    implements SessionBean JavaDoc
44 {
45    // Business methods
46

47    public void create(String JavaDoc ejbJndiName, Integer JavaDoc pk, String JavaDoc name) throws Exception JavaDoc
48    {
49       ALocalHome ah = getALocalHome(ejbJndiName);
50       ah.create(pk, name);
51    }
52
53    public void remove(String JavaDoc ejbJndiName, Integer JavaDoc pk) throws Exception JavaDoc
54    {
55       ALocalHome ah = getALocalHome(ejbJndiName);
56       ah.remove(pk);
57    }
58
59    public String JavaDoc getName(String JavaDoc ejbJndiName, Integer JavaDoc pk) throws Exception JavaDoc
60    {
61       ALocalHome ah = getALocalHome(ejbJndiName);
62       return ah.findByPrimaryKey(pk).getName();
63    }
64
65    public String JavaDoc getNameFlushCacheGetName(String JavaDoc ejbJndiName, Integer JavaDoc pk) throws Exception JavaDoc
66    {
67       ALocalHome ah = getALocalHome(ejbJndiName);
68       String JavaDoc nameBeforeFlush = ah.findByPrimaryKey(pk).getName();
69       flushCache(ejbJndiName);
70       String JavaDoc nameAfterFlush = ah.findByPrimaryKey(pk).getName();
71       if(!nameBeforeFlush.equals(nameAfterFlush))
72       {
73          throw new EJBException JavaDoc(
74             "The value of the name field before flush (" + nameBeforeFlush +
75             ") is not equal to the value after flush (" + nameAfterFlush + ")!");
76       }
77       return nameAfterFlush;
78    }
79
80    public String JavaDoc getNameFlushCacheSetName(String JavaDoc ejbJndiName, Integer JavaDoc pk, String JavaDoc value) throws Exception JavaDoc
81    {
82       ALocalHome ah = getALocalHome(ejbJndiName);
83       ah.findByPrimaryKey(pk).getName();
84       flushCache(ejbJndiName);
85       ALocal a = ah.findByPrimaryKey(pk);
86       a.setName(value);
87       String JavaDoc name = a.getName();
88       if(!name.equals(value))
89       {
90          throw new EJBException JavaDoc("setName(" + value + ") was ignored: " + name);
91       }
92       return name;
93    }
94
95    public void updateDB(String JavaDoc tableName, Integer JavaDoc pk, String JavaDoc value) throws Exception JavaDoc
96    {
97       DataSource JavaDoc ds = (DataSource JavaDoc)lookup("java:/DefaultDS");
98       Connection JavaDoc con = null;
99       PreparedStatement JavaDoc st = null;
100       try
101       {
102          con = ds.getConnection();
103          st = con.prepareStatement("update " + tableName + " set name=? where id=?");
104          st.setString(1, value);
105          st.setInt(2, pk.intValue());
106          int rowsAffected = st.executeUpdate();
107          if(rowsAffected != 1)
108          {
109             throw new EJBException JavaDoc("Failed to update column name in the row with pk " + pk +
110                " in the table " + tableName + ": expected 1 updated row but got " + rowsAffected);
111          }
112       }
113       finally
114       {
115          JDBCUtil.safeClose(st);
116          JDBCUtil.safeClose(con);
117       }
118    }
119
120    public void flushCache(String JavaDoc ejbJndiName) throws Exception JavaDoc
121    {
122       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
123       ObjectName JavaDoc container = new ObjectName JavaDoc("jboss.j2ee:service=EJB,jndiName=" + ejbJndiName);
124       server.invoke(container, "flushCache", null, null);
125    }
126
127    public void longTx(String JavaDoc ejbJndiName, Integer JavaDoc pk, long ms) throws Exception JavaDoc
128    {
129       ALocalHome ah = getALocalHome(ejbJndiName);
130       ah.findByPrimaryKey(pk).longTx();
131       try
132       {
133          Thread.sleep(ms);
134       }
135       catch(InterruptedException JavaDoc e)
136       {
137       }
138    }
139
140    // SessionBean implementation
141

142    /**
143     * @exception javax.ejb.CreateException Description of Exception
144     * @ejb.create-method
145     */

146    public void ejbCreate() throws CreateException JavaDoc
147    {
148    }
149
150    public void ejbActivate()
151    {
152    }
153
154    public void ejbPassivate()
155    {
156    }
157
158    public void ejbRemove()
159    {
160    }
161
162    public void setSessionContext(SessionContext JavaDoc ctx)
163    {
164    }
165
166    // Private
167

168    private ALocalHome getALocalHome(String JavaDoc name)
169       throws NamingException JavaDoc
170    {
171       return (ALocalHome)lookup(name);
172    }
173
174    private Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc
175    {
176       InitialContext JavaDoc ic = null;
177       try
178       {
179          ic = new InitialContext JavaDoc();
180          return ic.lookup(name);
181       }
182       finally
183       {
184          if(ic != null)
185          {
186             ic.close();
187          }
188       }
189    }
190 }
191
Popular Tags