KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > optimisticlock > ejb > FacadeBean


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.optimisticlock.ejb;
23
24 import org.jboss.test.cmp2.optimisticlock.interfaces.CmpEntityLocal;
25 import org.jboss.test.cmp2.optimisticlock.interfaces.CmpEntityLocalHome;
26 import org.jboss.test.cmp2.optimisticlock.interfaces.FacadeHome;
27 import org.jboss.logging.Logger;
28
29 import javax.ejb.SessionBean JavaDoc;
30 import javax.ejb.CreateException JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.ejb.FinderException JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35
36 /**
37  * @ejb.bean
38  * name="Facade"
39  * type="Stateless"
40  * jndi-name="FacadeBean"
41  * view-type="remote"
42  *
43  * @author <a HREF="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
44  */

45 public class FacadeBean
46    implements SessionBean JavaDoc
47 {
48    // Attributes --------------------------------------------
49
static Logger log = Logger.getLogger(FacadeBean.class);
50    private FacadeHome myHome;
51
52    // Business methods --------------------------------------
53
/**
54     * @ejb.interface-method
55     */

56    public void createCmpEntity(String JavaDoc jndiName,
57                                Integer JavaDoc id,
58                                String JavaDoc stringGroup1,
59                                Integer JavaDoc integerGroup1,
60                                Double JavaDoc doubleGroup1,
61                                String JavaDoc stringGroup2,
62                                Integer JavaDoc integerGroup2,
63                                Double JavaDoc doubleGroup2)
64       throws Exception JavaDoc
65    {
66       if(log.isDebugEnabled())
67       {
68          log.debug("createCmpEntity> jndiName=" + jndiName
69             + ", id=" + id
70             + ", stringGroup1=" + stringGroup1
71             + ", integerGroup1=" + integerGroup1
72             + ", doubleGroup1=" + doubleGroup1
73             + ", stringGroup2=" + stringGroup2
74             + ", integerGroup2=" + integerGroup2
75             + ", doubleGroup2=" + doubleGroup2);
76       }
77
78       CmpEntityLocalHome entityHome = getCmpEntityHome(jndiName);
79       entityHome.create(id, stringGroup1, integerGroup1, doubleGroup1,
80          stringGroup2, integerGroup2, doubleGroup2);
81    }
82
83    /**
84     * @ejb.interface-method
85     */

86    public void safeRemove(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
87    {
88       log.debug("safeRemove> jndiName=" + jndiName + ", id=" + id);
89       try
90       {
91          CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
92          entity.remove();
93       }
94       catch(FinderException JavaDoc e){}
95    }
96
97    /**
98     * @ejb.interface-method
99     */

100    public void testNullLockedFields(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
101    {
102       log.debug("testNullLockedFields> begin");
103       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
104       entity.setDoubleGroup1(new Double JavaDoc(11.11));
105       entity.setDoubleGroup2(new Double JavaDoc(22.22));
106       entity.setIntegerGroup1(new Integer JavaDoc(11));
107       entity.setIntegerGroup2(new Integer JavaDoc(22));
108       entity.setStringGroup1("str1 modified in testNullLockedFields");
109       entity.setStringGroup2("str2 modified in testNullLockedFields");
110       log.debug("testNullLockedFields> done");
111    }
112
113    /**
114     * @ejb.interface-method
115     */

116    public void testKeygenStrategyPass(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
117    {
118       log.debug("testKeygenStrategyPass> begin");
119       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
120       entity.setIntegerGroup1(new Integer JavaDoc(111));
121       entity.setStringGroup2("modified in testKeygenStrategyPass");
122       entity.getDoubleGroup1();
123       log.debug("testKeygenStrategyPass> done");
124    }
125
126    /**
127     * @ejb.interface-method
128     */

129    public void testKeygenStrategyFail(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
130    {
131       log.debug("testKeygenStrategyFail> begin");
132       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
133       entity.setIntegerGroup1(new Integer JavaDoc(111));
134       entity.setStringGroup2("modified in testKeygenStrategyFail");
135       entity.getDoubleGroup1();
136       myHome.create().modifyGroup1InRequiresNew(jndiName, id);
137       log.debug("testKeygenStrategyFail> done");
138    }
139
140    /**
141     * @ejb.interface-method
142     */

143    public void testTimestampStrategyPass(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
144    {
145       log.debug("testTimestampStrategyPass> begin");
146       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
147       entity.setIntegerGroup1(new Integer JavaDoc(111));
148       entity.setStringGroup2("modified in testTimestampStrategyPass");
149       entity.getDoubleGroup1();
150       log.debug("testTimestampStrategyPass> done");
151    }
152
153    /**
154     * @ejb.interface-method
155     */

156    public void testTimestampStrategyFail(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
157    {
158       log.debug("testTimestampStrategyFail> begin");
159       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
160       entity.setIntegerGroup1(new Integer JavaDoc(111));
161       entity.setStringGroup2("modified in testTimestampStrategyFail");
162       entity.getDoubleGroup1();
163       myHome.create().modifyGroup1InRequiresNew(jndiName, id);
164       log.debug("testTimestampStrategyFail> done");
165    }
166
167    /**
168     * @ejb.interface-method
169     */

170    public void testVersionStrategyPass(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
171    {
172       log.debug("testVersionStrategyPass> begin");
173       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
174       entity.setIntegerGroup1(new Integer JavaDoc(111));
175       entity.setStringGroup2("modified in testVersionStrategyPass");
176       entity.getDoubleGroup1();
177       log.debug("testVersionStrategyPass> done");
178    }
179
180    /**
181     * @ejb.interface-method
182     */

183    public void testVersionStrategyFail(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
184    {
185       log.debug("testVersionStrategyFail> begin");
186       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
187       entity.setIntegerGroup1(new Integer JavaDoc(111));
188       entity.setStringGroup2("modified in testVersionStrategyFail");
189       entity.getDoubleGroup1();
190       myHome.create().modifyGroup1InRequiresNew(jndiName, id);
191       log.debug("testVersionStrategyFail> done");
192    }
193
194    /**
195     * @ejb.interface-method
196     */

197    public void testGroupStrategyPass(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
198    {
199       log.debug("testGroupStrategyPass> begin");
200       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
201       entity.setIntegerGroup1(new Integer JavaDoc(111));
202       entity.setStringGroup2("modified in testGroupStrategyPass");
203       entity.getDoubleGroup1();
204       myHome.create().modifyGroup1InRequiresNew(jndiName, id);
205       log.debug("testGroupStrategyPass> done");
206    }
207
208    /**
209     * @ejb.interface-method
210     */

211    public void testGroupStrategyFail(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
212    {
213       log.debug("testGroupStrategyFail> begin");
214       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
215       entity.setIntegerGroup1(new Integer JavaDoc(111));
216       entity.setStringGroup2("modified in testGroupStrategyPass");
217       entity.getDoubleGroup1();
218       myHome.create().modifyGroup2InRequiresNew(jndiName, id);
219       log.debug("testGroupStrategyFail> done");
220    }
221
222    /**
223     * @ejb.interface-method
224     */

225    public void testReadStrategyPass(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
226    {
227       log.debug("testReadStrategyPass> begin");
228       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
229       entity.setIntegerGroup1(new Integer JavaDoc(111));
230       entity.getStringGroup1();
231       entity.getDoubleGroup1();
232       myHome.create().modifyGroup2InRequiresNew(jndiName, id);
233       log.debug("testReadStrategyPass> done");
234    }
235
236    /**
237     * @ejb.interface-method
238     */

239    public void testReadStrategyFail(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
240    {
241       log.debug("testReadStrategyFail> begin");
242       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
243       entity.setIntegerGroup1(new Integer JavaDoc(111));
244       entity.getStringGroup2();
245       entity.getDoubleGroup1();
246       myHome.create().modifyGroup2InRequiresNew(jndiName, id);
247       log.debug("testReadStrategyFail> done");
248    }
249
250    /**
251     * @ejb.interface-method
252     */

253    public void testModifiedStrategyPass(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
254    {
255       log.debug("testModifiedStrategyPass> begin");
256       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
257       entity.setIntegerGroup1(new Integer JavaDoc(111));
258       entity.setStringGroup1("modified in testModifiedStrategyPass");
259       entity.setDoubleGroup1(new Double JavaDoc(111.111));
260       myHome.create().modifyGroup2InRequiresNew(jndiName, id);
261       log.debug("testModifiedStrategyPass> done");
262    }
263
264    /**
265     * @ejb.interface-method
266     */

267    public void testModifiedStrategyFail(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
268    {
269       log.debug("testModifiedStrategyFail> begin");
270       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
271       entity.setStringGroup2("modified by testModifiedStrategyFail");
272       myHome.create().modifyGroup2InRequiresNew(jndiName, id);
273       log.debug("testModifiedStrategyFail> done");
274    }
275
276    /**
277     * @ejb.interface-method
278     * @ejb.transaction type="RequiresNew"
279     */

280    public void modifyGroup2InRequiresNew(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
281    {
282       log.debug("modifyGroup2InRequiresNew");
283       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
284       entity.setIntegerGroup2(new Integer JavaDoc(222));
285       entity.setStringGroup2("modified by modifyGroup2InRequiresNew");
286       entity.setDoubleGroup2(new Double JavaDoc(222.222));
287    }
288
289    /**
290     * @ejb.interface-method
291     * @ejb.transaction type="RequiresNew"
292     */

293    public void modifyGroup1InRequiresNew(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
294    {
295       log.debug("modifyGroup1InRequiresNew");
296       CmpEntityLocal entity = getCmpEntityHome(jndiName).findByPrimaryKey(id);
297       entity.setIntegerGroup1(new Integer JavaDoc(333));
298       entity.setStringGroup1("modified by modifyGroup1InRequiresNew");
299       entity.setDoubleGroup1(new Double JavaDoc(333.333));
300    }
301
302    /**
303     * @ejb.interface-method
304     */

305    public void testUpdateLockOnSync(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
306    {
307       log.debug("testUpdateLockOnSync> begin");
308       CmpEntityLocal entity = getCmpEntityHome(jndiName).findById(id);
309       entity.setStringGroup1("FIRST UPDATE");
310
311       entity = getCmpEntityHome(jndiName).findById(id);
312       entity.setStringGroup1("SECOND UPDATE");
313
314       log.debug("testUpdateLockOnSync> done");
315    }
316
317    /**
318     * @ejb.interface-method
319     */

320    public void testExplicitVersionUpdateOnSync(String JavaDoc jndiName, Integer JavaDoc id) throws Exception JavaDoc
321    {
322       log.debug("testExplicitVersionUpdateOnSync> begin");
323       CmpEntityLocal entity = getCmpEntityHome(jndiName).findById(id);
324       if(entity.getVersionField().longValue() != 1)
325          throw new Exception JavaDoc("entity.getVersionField().longValue() != 1");
326       entity.setStringGroup1("FIRST UPDATE");
327
328       entity = getCmpEntityHome(jndiName).findById(id);
329       if(entity.getVersionField().longValue() != 2)
330          throw new Exception JavaDoc("entity.getVersionField().longValue() != 2");
331       entity.setStringGroup1("SECOND UPDATE");
332
333       log.debug("testExplicitVersionUpdateOnSync> done");
334    }
335
336    // SessionBean implementation ----------------------------
337
public void setSessionContext(SessionContext JavaDoc ctx)
338    {
339       myHome = (FacadeHome)ctx.getEJBHome();
340    }
341    public void ejbCreate() throws CreateException JavaDoc {}
342    public void ejbRemove() {}
343    public void ejbActivate() {}
344    public void ejbPassivate() {}
345
346    // Private -----------------------------------------------
347
private CmpEntityLocalHome getCmpEntityHome(String JavaDoc entityJndiName)
348       throws NamingException JavaDoc
349    {
350       InitialContext JavaDoc ic = new InitialContext JavaDoc();
351       CmpEntityLocalHome cmpEntityHome = (CmpEntityLocalHome)
352          ic.lookup(entityJndiName);
353       return cmpEntityHome;
354    }
355 }
356
Popular Tags