KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > base > timer > BaseTimeoutCallbackAccess


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BaseTimeoutCallbackAccess.java 827 2006-07-10 14:12:17Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.base.timer;
26
27 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType.TIMEOUT;
28 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTERPRISE_BEAN;
29 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER;
30 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER_FACTORY;
31 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.RESOURCE_MANAGER;
32 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.TIMER;
33 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.USER_TRANSACTION;
34 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.isEqual;
35 import static org.objectweb.easybeans.tests.common.helper.ContextHelper.checkResource;
36
37 import java.io.Serializable JavaDoc;
38
39 import javax.annotation.Resource;
40 import javax.ejb.EJB JavaDoc;
41 import javax.ejb.EJBContext JavaDoc;
42 import javax.ejb.Timeout JavaDoc;
43 import javax.ejb.Timer JavaDoc;
44 import javax.ejb.TimerService JavaDoc;
45 import javax.jms.JMSException JavaDoc;
46 import javax.jms.Message JavaDoc;
47 import javax.persistence.EntityManager;
48 import javax.persistence.EntityManagerFactory;
49 import javax.persistence.PersistenceContext;
50 import javax.persistence.PersistenceUnit;
51 import javax.sql.DataSource JavaDoc;
52 import javax.transaction.UserTransaction JavaDoc;
53
54 import org.objectweb.easybeans.log.JLog;
55 import org.objectweb.easybeans.log.JLogFactory;
56 import org.objectweb.easybeans.tests.common.ejbs.base.ItfOneMethod01;
57 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.BaseInsertOperation;
58 import org.objectweb.easybeans.tests.common.jms.MessageProperty;
59 import org.objectweb.easybeans.tests.common.resources.EMFactoryTester;
60 import org.objectweb.easybeans.tests.common.resources.EntityManagerTester;
61
62 /**
63  * Used as base of beans to test timeout callback.
64  * @author Eduardo Studzinski Estima de Castro
65  * @author Gisele Pinheiro Souza
66  */

67 @EJB JavaDoc(name = "ejb/bean01", beanInterface = ItfOneMethod01.class, beanName = "EJBInjectionBean")
68 @Resource(name = "jdbc/ds01", type = javax.sql.DataSource JavaDoc.class, mappedName = "jdbc_1")
69 @PersistenceUnit(name = "persistence/pu01")
70 @PersistenceContext(name = "persistence/pctx01")
71 public abstract class BaseTimeoutCallbackAccess extends BaseInsertOperation implements ItfCreateTimer{
72
73     /**
74      * Log helper.
75      */

76     private JLog logger = JLogFactory.getLog(BaseTimeoutCallbackAccess.class);
77
78     /**
79      * Gets the bean name.
80      * @return name
81      */

82     public abstract String JavaDoc getName();
83
84     /**
85      * Tests the UserTransaction.
86      * @param utx instance
87      * @return true if the instance is working properly, otherwise false.
88      */

89     public abstract boolean testUserTransaction(final UserTransaction JavaDoc utx);
90
91     /**
92      * TimerService.
93      */

94     @Resource
95     private TimerService JavaDoc ts;
96
97     /**
98      * Context.
99      */

100     @Resource
101     private EJBContext JavaDoc ctx;
102
103     /**
104      * UserTransaction must not be injected.
105      */

106     @Resource
107     private UserTransaction JavaDoc utx;
108
109     /**
110      * Starts a timer. The timeout will perform the operation specifed in the MessageProperty.OPERATION property.
111      * @param message msg
112      */

113     public void onMessage(final Message JavaDoc message) {
114         String JavaDoc op = null;
115
116         try {
117             op = message.getStringProperty(MessageProperty.OPERATION.toString());
118             startTimer(DURATION, op);
119         } catch (JMSException JavaDoc e) {
120             logger.debug("Error getting operation type: {0}", e);
121         }
122     }
123
124     /**
125      * This timer performs the operation defined in the info.
126      * @param timer timer with information.
127      */

128     @Timeout JavaDoc
129     public void timeout(final Timer JavaDoc timer) {
130         String JavaDoc op = "";
131         Serializable JavaDoc info = timer.getInfo();
132
133         if (info != null){
134             op = info.toString();
135         }
136
137         if (isEqual(RESOURCE_MANAGER, op)) {
138             DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("ds/ds01");
139             checkResource(ds);
140             log(getName(), TIMEOUT, getName(), RESOURCE_MANAGER);
141
142         } else if (isEqual(ENTERPRISE_BEAN, op)) {
143             ItfOneMethod01 bean = (ItfOneMethod01) ctx.lookup("ejb/bean01");
144             bean.getBool();
145             log(getName(), TIMEOUT, getName(), ENTERPRISE_BEAN);
146
147         } else if (isEqual(ENTITY_MANAGER_FACTORY, op)) {
148             EntityManagerFactory emf = (EntityManagerFactory) ctx.lookup("persistance/pu01");
149             try {
150                 EMFactoryTester.checkInstance(emf, "tmpTable" + this.hashCode());
151                 log(getName(), TIMEOUT, getName(), ENTITY_MANAGER_FACTORY);
152             } catch (Exception JavaDoc e) {
153                 logger.debug("Error in EntityManagerFactory use: {0}", e);
154                 e.printStackTrace();
155             }
156
157         } else if (isEqual(ENTITY_MANAGER, op)) {
158             EntityManager em = (EntityManager) ctx.lookup("persistance/pctx01");
159             try {
160                 EntityManagerTester.checkInstance(em, "tmpTable" + this.hashCode());
161                 log(getName(), TIMEOUT, getName(), ENTITY_MANAGER);
162             } catch (Exception JavaDoc e) {
163                 logger.debug("Error in EntityManagerFactory use: {0}", e);
164                 e.printStackTrace();
165             }
166
167         } else if (isEqual(TIMER, op)) {
168             ts.createTimer(DURATION, "").cancel();
169             log(getName(), TIMEOUT, getName(), TIMER);
170
171         } else if (isEqual(USER_TRANSACTION, op)){
172             if (testUserTransaction(utx)){
173                 log(getName(), TIMEOUT, getName(), USER_TRANSACTION);
174             }
175         }else{
176             logger.debug("Invalid operation: {0}", op);
177         }
178     }
179
180     /**
181      * Starts a timer.
182      * @param duration timer duration
183      * @param op operation that the timeout callback must perform
184      */

185     public void startTimer(final int duration, final Serializable JavaDoc op) {
186         ts.createTimer(duration, op.toString());
187     }
188 }
189
Popular Tags