KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > sampleCluster2 > ejb > MyEjb1SLR


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@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: MyEjb1SLR.java,v 1.4 2005/04/05 06:26:27 goebelg Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.sampleCluster2.ejb;
27
28 import java.util.Date JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.ejb.CreateException JavaDoc;
32 import javax.ejb.SessionBean JavaDoc;
33 import javax.ejb.SessionContext JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 import org.objectweb.jonas.common.JProp;
38 import org.objectweb.jonas.common.Log;
39
40 import org.objectweb.util.monolog.api.BasicLevel;
41 import org.objectweb.util.monolog.api.Logger;
42
43 /**
44  * @author goebelg
45  * Implementation of a stateless session bean
46  */

47 public class MyEjb1SLR implements SessionBean JavaDoc {
48
49     /**
50      *
51      */

52     private static final long serialVersionUID = 1L;
53
54
55     /**
56      * After how many invokes an entity bean is created
57      */

58     private static final int DIV = 10;
59     /**
60      * Sets the session Context
61      * @param ctx the session context
62      */

63     public void setSessionContext(SessionContext JavaDoc ctx) {
64         if (logger == null) {
65             logger = Log.getLogger("org.objectweb.jonas_tests");
66         }
67         logger.log(BasicLevel.DEBUG, "");
68     }
69
70     /**
71      * removes the ejb
72      */

73     public void ejbRemove() {
74         logger.log(BasicLevel.DEBUG, "");
75     }
76
77     /**
78      * creates the ejb
79      */

80     public void ejbCreate() {
81         logger.log(BasicLevel.DEBUG, "");
82         jonasInstanceName = "unknown";
83
84         try {
85             JProp jp = JProp.getInstance();
86             jonasInstanceName = jp.getValue("jonas.name");
87         } catch (Exception JavaDoc e) {
88             logger.log(BasicLevel.FATAL, "Error while creating MyEjb1SLR : " + e.getMessage());
89         }
90
91         logger.log(BasicLevel.DEBUG, "ejbCreate()->" + this.toString());
92     }
93
94     /**
95      * Passivate of the ejb
96      */

97     public void ejbPassivate() {
98         logger.log(BasicLevel.DEBUG, "");
99     }
100
101     /**
102      * Activation of the ejb
103      */

104     public void ejbActivate() {
105         logger.log(BasicLevel.DEBUG, "");
106     }
107
108     /**
109      * Access a property stored in the JOnAS instance executing the EJB
110      * container.
111      * @param prop Name of the property
112      * @return The value of the property
113      */

114     public String JavaDoc getProperty(String JavaDoc prop) {
115         String JavaDoc s = "unknown";
116         try {
117             JProp jp = JProp.getInstance();
118             s = jp.getValue(prop);
119         } catch (Exception JavaDoc e) {
120             logger.log(BasicLevel.FATAL, "Error while getProperty : " + e.getMessage());
121         }
122         return s;
123     }
124
125     /**
126      * Retreive some information of the JOnAS instance executing the EJB
127      * container The names of the properties are : EJB server, EJB id, EJB
128      * instance calls, EJB total calls - EJB server : name of the JOnAS instance -
129      * EJB id : toString() of the MyEjb1 - EJB instance calls : number of calls
130      * to this EJB instance - EJB total calls : number of calls to this EJB for
131      * all instance of class MyEjb1
132      * @return The Properties
133      */

134     public Properties JavaDoc getInfoProps() {
135         updateStatistics();
136         Properties JavaDoc p = new Properties JavaDoc();
137         p.setProperty("EJB server", jonasInstanceName);
138         p.setProperty("EJB id", this.toString());
139         p.setProperty("EJB total calls", (new Integer JavaDoc(allInstancesTotalCallsCount)).toString());
140         p.setProperty("EJB server entity created", entityJonasInstanceName);
141         return p;
142     }
143
144     /**
145      * Updates the properties described above
146      */

147     private void updateStatistics() {
148         allInstancesTotalCallsCount++;
149
150         // create an entity bean every 10 times
151
if ((allInstancesTotalCallsCount % DIV) == 0) {
152             // creating a new entity bean
153
logger.log(BasicLevel.INFO, "Trying to create the entity");
154             Date JavaDoc date = new Date JavaDoc();
155             MyEntityLocal entity = createEntity(Long.toString(date.getTime()));
156             logger.log(BasicLevel.INFO, "Finished to create the entity");
157             entityJonasInstanceName = entity.getJOnASName();
158         } else {
159             entityJonasInstanceName = "No entity Bean created";
160         }
161
162         logger.log(BasicLevel.INFO, "JOnAS=" + jonasInstanceName + " ; ejb=" + this.toString() + " ; total calls="
163                 + allInstancesTotalCallsCount + " ; entity created on=" + entityJonasInstanceName);
164     }
165
166     /**
167      * Creates a new Entity bean with the time as parameter
168      * @param valeur The current time as string
169      * @return A new Entity Bean
170      */

171     private MyEntityLocal createEntity(String JavaDoc valeur) {
172         InitialContext JavaDoc cntx;
173         MyEntityLocal result = null;
174         try {
175             cntx = new InitialContext JavaDoc();
176             MyEntityLocalHome entityHome = (MyEntityLocalHome) cntx.lookup("MyEntityHome_L");
177             result = entityHome.create(valeur);
178         } catch (NamingException JavaDoc e) {
179             logger.log(BasicLevel.FATAL, "Naming exception : " + e.getMessage());
180         } catch (CreateException JavaDoc e) {
181             logger.log(BasicLevel.FATAL, "Create exception : " + e.getMessage());
182         }
183         return result;
184     }
185
186     /**
187      * The logger
188      */

189     private static Logger logger = null;
190
191
192     /**
193      * the node name of the jonas instance where an
194      * entity bean has bean created
195      */

196     private String JavaDoc entityJonasInstanceName = "unknown";
197
198     /**
199      * the node name of the jonas instance where the
200      * session bean is executed
201      */

202     private String JavaDoc jonasInstanceName = "unknown";
203
204     /**
205      * counts all the instances created of this class in
206      * that JVM
207      */

208     private static int allInstancesTotalCallsCount = 0;
209 }
210
211
Popular Tags