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.optimisticlock.ejb;
023:
024: import org.jboss.test.cmp2.optimisticlock.interfaces.CmpEntityLocal;
025: import org.jboss.test.cmp2.optimisticlock.interfaces.CmpEntityLocalHome;
026: import org.jboss.test.cmp2.optimisticlock.interfaces.FacadeHome;
027: import org.apache.log4j.Category;
028:
029: import javax.ejb.SessionBean;
030: import javax.ejb.CreateException;
031: import javax.ejb.SessionContext;
032: import javax.ejb.FinderException;
033: import javax.naming.NamingException;
034: import javax.naming.InitialContext;
035:
036: /**
037: * @ejb.bean
038: * name="Facade"
039: * type="Stateless"
040: * jndi-name="FacadeBean"
041: * view-type="remote"
042: *
043: * @author <a href="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
044: */
045: public class FacadeBean implements SessionBean {
046: // Attributes --------------------------------------------
047: static Category log = Category.getInstance(FacadeBean.class);
048: private FacadeHome myHome;
049:
050: // Business methods --------------------------------------
051: /**
052: * @ejb.interface-method
053: */
054: public void createCmpEntity(String jndiName, Integer id,
055: String stringGroup1, Integer integerGroup1,
056: Double doubleGroup1, String stringGroup2,
057: Integer integerGroup2, Double doubleGroup2)
058: throws Exception {
059: if (log.isDebugEnabled()) {
060: log.debug("createCmpEntity> jndiName=" + jndiName + ", id="
061: + id + ", stringGroup1=" + stringGroup1
062: + ", integerGroup1=" + integerGroup1
063: + ", doubleGroup1=" + doubleGroup1
064: + ", stringGroup2=" + stringGroup2
065: + ", integerGroup2=" + integerGroup2
066: + ", doubleGroup2=" + doubleGroup2);
067: }
068:
069: CmpEntityLocalHome entityHome = getCmpEntityHome(jndiName);
070: entityHome
071: .create(id, stringGroup1, integerGroup1, doubleGroup1,
072: stringGroup2, integerGroup2, doubleGroup2);
073: }
074:
075: /**
076: * @ejb.interface-method
077: */
078: public void safeRemove(String jndiName, Integer id)
079: throws Exception {
080: log.debug("safeRemove> jndiName=" + jndiName + ", id=" + id);
081: try {
082: CmpEntityLocal entity = getCmpEntityHome(jndiName)
083: .findByPrimaryKey(id);
084: entity.remove();
085: } catch (FinderException e) {
086: }
087: }
088:
089: /**
090: * @ejb.interface-method
091: */
092: public void testNullLockedFields(String jndiName, Integer id)
093: throws Exception {
094: log.debug("testNullLockedFields> begin");
095: CmpEntityLocal entity = getCmpEntityHome(jndiName)
096: .findByPrimaryKey(id);
097: entity.setDoubleGroup1(new Double(11.11));
098: entity.setDoubleGroup2(new Double(22.22));
099: entity.setIntegerGroup1(new Integer(11));
100: entity.setIntegerGroup2(new Integer(22));
101: entity.setStringGroup1("str1 modified in testNullLockedFields");
102: entity.setStringGroup2("str2 modified in testNullLockedFields");
103: log.debug("testNullLockedFields> done");
104: }
105:
106: /**
107: * @ejb.interface-method
108: */
109: public void testKeygenStrategyPass(String jndiName, Integer id)
110: throws Exception {
111: log.debug("testKeygenStrategyPass> begin");
112: CmpEntityLocal entity = getCmpEntityHome(jndiName)
113: .findByPrimaryKey(id);
114: entity.setIntegerGroup1(new Integer(111));
115: entity.setStringGroup2("modified in testKeygenStrategyPass");
116: entity.getDoubleGroup1();
117: log.debug("testKeygenStrategyPass> done");
118: }
119:
120: /**
121: * @ejb.interface-method
122: */
123: public void testKeygenStrategyFail(String jndiName, Integer id)
124: throws Exception {
125: log.debug("testKeygenStrategyFail> begin");
126: CmpEntityLocal entity = getCmpEntityHome(jndiName)
127: .findByPrimaryKey(id);
128: entity.setIntegerGroup1(new Integer(111));
129: entity.setStringGroup2("modified in testKeygenStrategyFail");
130: entity.getDoubleGroup1();
131: myHome.create().modifyGroup1InRequiresNew(jndiName, id, 0);
132: log.debug("testKeygenStrategyFail> done");
133: }
134:
135: /**
136: * @ejb.interface-method
137: */
138: public void testTimestampStrategyPass(String jndiName, Integer id)
139: throws Exception {
140: log.debug("testTimestampStrategyPass> begin");
141: CmpEntityLocal entity = getCmpEntityHome(jndiName)
142: .findByPrimaryKey(id);
143: entity.setIntegerGroup1(new Integer(111));
144: entity.setStringGroup2("modified in testTimestampStrategyPass");
145: entity.getDoubleGroup1();
146: log.debug("testTimestampStrategyPass> done");
147: }
148:
149: /**
150: * @ejb.interface-method
151: */
152: public void testTimestampStrategyFail(String jndiName, Integer id)
153: throws Exception {
154: log.debug("testTimestampStrategyFail> begin");
155: CmpEntityLocal entity = getCmpEntityHome(jndiName)
156: .findByPrimaryKey(id);
157: entity.setIntegerGroup1(new Integer(111));
158: entity.setStringGroup2("modified in testTimestampStrategyFail");
159: entity.getDoubleGroup1();
160: myHome.create().modifyGroup1InRequiresNew(jndiName, id, 1000);
161:
162: // to make a difference in the timestamp
163: Thread.sleep(1000);
164:
165: log.debug("testTimestampStrategyFail> done");
166: }
167:
168: /**
169: * @ejb.interface-method
170: */
171: public void testVersionStrategyPass(String jndiName, Integer id)
172: throws Exception {
173: log.debug("testVersionStrategyPass> begin");
174: CmpEntityLocal entity = getCmpEntityHome(jndiName)
175: .findByPrimaryKey(id);
176: entity.setIntegerGroup1(new Integer(111));
177: entity.setStringGroup2("modified in testVersionStrategyPass");
178: entity.getDoubleGroup1();
179: log.debug("testVersionStrategyPass> done");
180: }
181:
182: /**
183: * @ejb.interface-method
184: */
185: public void testVersionStrategyFail(String jndiName, Integer id)
186: throws Exception {
187: log.debug("testVersionStrategyFail> begin");
188: CmpEntityLocal entity = getCmpEntityHome(jndiName)
189: .findByPrimaryKey(id);
190: entity.setIntegerGroup1(new Integer(111));
191: entity.setStringGroup2("modified in testVersionStrategyFail");
192: entity.getDoubleGroup1();
193: myHome.create().modifyGroup1InRequiresNew(jndiName, id, 0);
194: log.debug("testVersionStrategyFail> done");
195: }
196:
197: /**
198: * @ejb.interface-method
199: */
200: public void testGroupStrategyPass(String jndiName, Integer id)
201: throws Exception {
202: log.debug("testGroupStrategyPass> begin");
203: CmpEntityLocal entity = getCmpEntityHome(jndiName)
204: .findByPrimaryKey(id);
205: entity.setIntegerGroup1(new Integer(111));
206: entity.setStringGroup2("modified in testGroupStrategyPass");
207: entity.getDoubleGroup1();
208: myHome.create().modifyGroup1InRequiresNew(jndiName, id, 0);
209: log.debug("testGroupStrategyPass> done");
210: }
211:
212: /**
213: * @ejb.interface-method
214: */
215: public void testGroupStrategyFail(String jndiName, Integer id)
216: throws Exception {
217: log.debug("testGroupStrategyFail> begin");
218: CmpEntityLocal entity = getCmpEntityHome(jndiName)
219: .findByPrimaryKey(id);
220: entity.setIntegerGroup1(new Integer(111));
221: entity.setStringGroup2("modified in testGroupStrategyPass");
222: entity.getDoubleGroup1();
223: myHome.create().modifyGroup2InRequiresNew(jndiName, id);
224: log.debug("testGroupStrategyFail> done");
225: }
226:
227: /**
228: * @ejb.interface-method
229: */
230: public void testReadStrategyPass(String jndiName, Integer id)
231: throws Exception {
232: log.debug("testReadStrategyPass> begin");
233: CmpEntityLocal entity = getCmpEntityHome(jndiName)
234: .findByPrimaryKey(id);
235: entity.setIntegerGroup1(new Integer(111));
236: entity.getStringGroup1();
237: entity.getDoubleGroup1();
238: myHome.create().modifyGroup2InRequiresNew(jndiName, id);
239: log.debug("testReadStrategyPass> done");
240: }
241:
242: /**
243: * @ejb.interface-method
244: */
245: public void testReadStrategyFail(String jndiName, Integer id)
246: throws Exception {
247: log.debug("testReadStrategyFail> begin");
248: CmpEntityLocal entity = getCmpEntityHome(jndiName)
249: .findByPrimaryKey(id);
250: entity.setIntegerGroup1(new Integer(111));
251: entity.getStringGroup2();
252: entity.getDoubleGroup1();
253: myHome.create().modifyGroup2InRequiresNew(jndiName, id);
254: log.debug("testReadStrategyFail> done");
255: }
256:
257: /**
258: * @ejb.interface-method
259: */
260: public void testModifiedStrategyPass(String jndiName, Integer id)
261: throws Exception {
262: log.debug("testModifiedStrategyPass> begin");
263: CmpEntityLocal entity = getCmpEntityHome(jndiName)
264: .findByPrimaryKey(id);
265: entity.setIntegerGroup1(new Integer(111));
266: entity.setStringGroup1("modified in testModifiedStrategyPass");
267: entity.setDoubleGroup1(new Double(111.111));
268: myHome.create().modifyGroup2InRequiresNew(jndiName, id);
269: log.debug("testModifiedStrategyPass> done");
270: }
271:
272: /**
273: * @ejb.interface-method
274: */
275: public void testModifiedStrategyFail(String jndiName, Integer id)
276: throws Exception {
277: log.debug("testModifiedStrategyFail> begin");
278: CmpEntityLocal entity = getCmpEntityHome(jndiName)
279: .findByPrimaryKey(id);
280: entity.setStringGroup2("modified by testModifiedStrategyFail");
281: myHome.create().modifyGroup2InRequiresNew(jndiName, id);
282: log.debug("testModifiedStrategyFail> done");
283: }
284:
285: /**
286: * @ejb.interface-method
287: * @ejb.transaction type="RequiresNew"
288: */
289: public void modifyGroup2InRequiresNew(String jndiName, Integer id)
290: throws Exception {
291: log.debug("modifyGroup2InRequiresNew");
292: CmpEntityLocal entity = getCmpEntityHome(jndiName)
293: .findByPrimaryKey(id);
294: entity.setIntegerGroup2(new Integer(222));
295: entity.setStringGroup2("modified by modifyGroup2InRequiresNew");
296: entity.setDoubleGroup2(new Double(222.222));
297: }
298:
299: /**
300: * @ejb.interface-method
301: * @ejb.transaction type="RequiresNew"
302: */
303: public void modifyGroup1InRequiresNew(String jndiName, Integer id,
304: long sleep) throws Exception {
305: log.debug("modifyGroup1InRequiresNew");
306: CmpEntityLocal entity = getCmpEntityHome(jndiName)
307: .findByPrimaryKey(id);
308: entity.setIntegerGroup1(new Integer(333));
309: entity.setStringGroup1("modified by modifyGroup1InRequiresNew");
310: entity.setDoubleGroup1(new Double(333.333));
311:
312: if (sleep > 0) {
313: Thread.sleep(sleep);
314: }
315: }
316:
317: /**
318: * @ejb.interface-method
319: */
320: public void testUpdateLockOnSync(String jndiName, Integer id)
321: throws Exception {
322: log.debug("testUpdateLockOnSync> begin");
323: CmpEntityLocal entity = getCmpEntityHome(jndiName).findById(id);
324: entity.setStringGroup1("FIRST UPDATE");
325:
326: entity = getCmpEntityHome(jndiName).findById(id);
327: entity.setStringGroup1("SECOND UPDATE");
328:
329: log.debug("testUpdateLockOnSync> done");
330: }
331:
332: /**
333: * @ejb.interface-method
334: */
335: public void testExplicitVersionUpdateOnSync(String jndiName,
336: Integer id) throws Exception {
337: log.debug("testExplicitVersionUpdateOnSync> begin");
338: CmpEntityLocal entity = getCmpEntityHome(jndiName).findById(id);
339: if (entity.getVersionField().longValue() != 1)
340: throw new Exception(
341: "entity.getVersionField().longValue() != 1");
342: entity.setStringGroup1("FIRST UPDATE");
343:
344: entity = getCmpEntityHome(jndiName).findById(id);
345: if (entity.getVersionField().longValue() != 2)
346: throw new Exception(
347: "entity.getVersionField().longValue() != 2");
348: entity.setStringGroup1("SECOND UPDATE");
349:
350: log.debug("testExplicitVersionUpdateOnSync> done");
351: }
352:
353: // SessionBean implementation ----------------------------
354: public void setSessionContext(SessionContext ctx) {
355: myHome = (FacadeHome) ctx.getEJBHome();
356: }
357:
358: public void ejbCreate() throws CreateException {
359: }
360:
361: public void ejbRemove() {
362: }
363:
364: public void ejbActivate() {
365: }
366:
367: public void ejbPassivate() {
368: }
369:
370: // Private -----------------------------------------------
371: private CmpEntityLocalHome getCmpEntityHome(String entityJndiName)
372: throws NamingException {
373: InitialContext ic = new InitialContext();
374: CmpEntityLocalHome cmpEntityHome = (CmpEntityLocalHome) ic
375: .lookup(entityJndiName);
376: return cmpEntityHome;
377: }
378: }
|