001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.jbas979;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import javax.ejb.SessionBean;
027: import javax.ejb.SessionContext;
028: import javax.ejb.CreateException;
029: import javax.ejb.EJBException;
030: import javax.management.MBeanServer;
031: import javax.management.ObjectName;
032: import javax.naming.NamingException;
033: import javax.naming.InitialContext;
034: import javax.sql.DataSource;
035: import org.jboss.mx.util.MBeanServerLocator;
036: import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
037:
038: /**
039: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
040: * @version <tt>$Revision: 57211 $</tt>
041: */
042: public class FacadeSessionBean implements SessionBean {
043: // Business methods
044:
045: public void create(String ejbJndiName, Integer pk, String name)
046: throws Exception {
047: ALocalHome ah = getALocalHome(ejbJndiName);
048: ah.create(pk, name);
049: }
050:
051: public void remove(String ejbJndiName, Integer pk) throws Exception {
052: ALocalHome ah = getALocalHome(ejbJndiName);
053: ah.remove(pk);
054: }
055:
056: public String getName(String ejbJndiName, Integer pk)
057: throws Exception {
058: ALocalHome ah = getALocalHome(ejbJndiName);
059: return ah.findByPrimaryKey(pk).getName();
060: }
061:
062: public String getNameFlushCacheGetName(String ejbJndiName,
063: Integer pk) throws Exception {
064: ALocalHome ah = getALocalHome(ejbJndiName);
065: String nameBeforeFlush = ah.findByPrimaryKey(pk).getName();
066: flushCache(ejbJndiName);
067: String nameAfterFlush = ah.findByPrimaryKey(pk).getName();
068: if (!nameBeforeFlush.equals(nameAfterFlush)) {
069: throw new EJBException(
070: "The value of the name field before flush ("
071: + nameBeforeFlush
072: + ") is not equal to the value after flush ("
073: + nameAfterFlush + ")!");
074: }
075: return nameAfterFlush;
076: }
077:
078: public String getNameFlushCacheSetName(String ejbJndiName,
079: Integer pk, String value) throws Exception {
080: ALocalHome ah = getALocalHome(ejbJndiName);
081: ah.findByPrimaryKey(pk).getName();
082: flushCache(ejbJndiName);
083: ALocal a = ah.findByPrimaryKey(pk);
084: a.setName(value);
085: String name = a.getName();
086: if (!name.equals(value)) {
087: throw new EJBException("setName(" + value
088: + ") was ignored: " + name);
089: }
090: return name;
091: }
092:
093: public void updateDB(String tableName, Integer pk, String value)
094: throws Exception {
095: DataSource ds = (DataSource) lookup("java:/DefaultDS");
096: Connection con = null;
097: PreparedStatement st = null;
098: try {
099: con = ds.getConnection();
100: st = con.prepareStatement("update " + tableName
101: + " set name=? where id=?");
102: st.setString(1, value);
103: st.setInt(2, pk.intValue());
104: int rowsAffected = st.executeUpdate();
105: if (rowsAffected != 1) {
106: throw new EJBException(
107: "Failed to update column name in the row with pk "
108: + pk + " in the table " + tableName
109: + ": expected 1 updated row but got "
110: + rowsAffected);
111: }
112: } finally {
113: JDBCUtil.safeClose(st);
114: JDBCUtil.safeClose(con);
115: }
116: }
117:
118: public void flushCache(String ejbJndiName) throws Exception {
119: MBeanServer server = MBeanServerLocator.locateJBoss();
120: ObjectName container = new ObjectName(
121: "jboss.j2ee:service=EJB,jndiName=" + ejbJndiName);
122: server.invoke(container, "flushCache", null, null);
123: }
124:
125: public void longTx(String ejbJndiName, Integer pk, long ms)
126: throws Exception {
127: ALocalHome ah = getALocalHome(ejbJndiName);
128: ah.findByPrimaryKey(pk).longTx();
129: try {
130: Thread.sleep(ms);
131: } catch (InterruptedException e) {
132: }
133: }
134:
135: // SessionBean implementation
136:
137: /**
138: * @exception javax.ejb.CreateException Description of Exception
139: * @ejb.create-method
140: */
141: public void ejbCreate() throws CreateException {
142: }
143:
144: public void ejbActivate() {
145: }
146:
147: public void ejbPassivate() {
148: }
149:
150: public void ejbRemove() {
151: }
152:
153: public void setSessionContext(SessionContext ctx) {
154: }
155:
156: // Private
157:
158: private ALocalHome getALocalHome(String name)
159: throws NamingException {
160: return (ALocalHome) lookup(name);
161: }
162:
163: private Object lookup(String name) throws NamingException {
164: InitialContext ic = null;
165: try {
166: ic = new InitialContext();
167: return ic.lookup(name);
168: } finally {
169: if (ic != null) {
170: ic.close();
171: }
172: }
173: }
174: }
|