001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005-2006 Bull S.A.S
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MyEjb1SLR.java 7938 2006-01-25 17:15:26Z pelletib $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.sampleCluster2.ejb;
025:
026: import java.util.Date;
027: import java.util.Properties;
028:
029: import javax.ejb.CreateException;
030: import javax.ejb.SessionBean;
031: import javax.ejb.SessionContext;
032: import javax.naming.InitialContext;
033: import javax.naming.NamingException;
034:
035: import org.objectweb.jonas.common.JProp;
036: import org.objectweb.jonas.common.Log;
037:
038: import org.objectweb.util.monolog.api.BasicLevel;
039: import org.objectweb.util.monolog.api.Logger;
040:
041: /**
042: * @author goebelg
043: * Implementation of a stateless session bean
044: */
045: public class MyEjb1SLR implements SessionBean {
046:
047: /**
048: *
049: */
050: private static final long serialVersionUID = 1L;
051:
052: /**
053: * After how many invokes an entity bean is created
054: */
055: private static final int DIV = 10;
056:
057: /**
058: * Sets the session Context
059: * @param ctx the session context
060: */
061: public void setSessionContext(SessionContext ctx) {
062: if (logger == null) {
063: logger = Log.getLogger("org.objectweb.jonas_tests");
064: }
065: logger.log(BasicLevel.DEBUG, "");
066: }
067:
068: /**
069: * removes the ejb
070: */
071: public void ejbRemove() {
072: logger.log(BasicLevel.DEBUG, "");
073: }
074:
075: /**
076: * creates the ejb
077: */
078: public void ejbCreate() {
079:
080: logger.log(BasicLevel.DEBUG, "");
081: jonasInstanceName = "unknown";
082:
083: try {
084: JProp jp = JProp.getInstance();
085: jonasInstanceName = jp.getValue("jonas.name");
086: } catch (Exception e) {
087: logger.log(BasicLevel.FATAL,
088: "Error while creating MyEjb1SLR : "
089: + e.getMessage());
090: }
091:
092: logger.log(BasicLevel.DEBUG, "ejbCreate()->" + this .toString());
093: }
094:
095: /**
096: * Passivate of the ejb
097: */
098: public void ejbPassivate() {
099: logger.log(BasicLevel.DEBUG, "");
100: }
101:
102: /**
103: * Activation of the ejb
104: */
105: public void ejbActivate() {
106: logger.log(BasicLevel.DEBUG, "");
107: }
108:
109: /**
110: * Access a property stored in the JOnAS instance executing the EJB
111: * container.
112: * @param prop Name of the property
113: * @return The value of the property
114: */
115: public String getProperty(String prop) {
116: String s = "unknown";
117: try {
118: JProp jp = JProp.getInstance();
119: s = jp.getValue(prop);
120: } catch (Exception e) {
121: logger.log(BasicLevel.FATAL, "Error while getProperty : "
122: + e.getMessage());
123: }
124: return s;
125: }
126:
127: /**
128: * Throw a MyException
129: * @throws MyException application exception
130: */
131: public void throwMyException() throws MyException {
132: throw new MyException("From EjbSLR1(" + jonasInstanceName + ")");
133: }
134:
135: /**
136: * Retreive some information of the JOnAS instance executing the EJB
137: * container The names of the properties are : EJB server, EJB id, EJB
138: * instance calls, EJB total calls - EJB server : name of the JOnAS instance -
139: * EJB id : toString() of the MyEjb1 - EJB instance calls : number of calls
140: * to this EJB instance - EJB total calls : number of calls to this EJB for
141: * all instance of class MyEjb1
142: * @return The Properties
143: */
144: public Properties getInfoProps() {
145: updateStatistics();
146: Properties p = new Properties();
147: p.setProperty("EJB server", jonasInstanceName);
148: p.setProperty("EJB id", this .toString());
149: p.setProperty("EJB total calls", (new Integer(
150: allInstancesTotalCallsCount)).toString());
151: p.setProperty("EJB server entity created",
152: entityJonasInstanceName);
153: return p;
154: }
155:
156: /**
157: * Updates the properties described above
158: */
159: private void updateStatistics() {
160: allInstancesTotalCallsCount++;
161:
162: // create an entity bean every 10 times
163: if ((allInstancesTotalCallsCount % DIV) == 0) {
164: // creating a new entity bean
165: logger.log(BasicLevel.INFO, "Trying to create the entity");
166: Date date = new Date();
167: MyEntityLocal entity = createEntity(Long.toString(date
168: .getTime()));
169: logger
170: .log(BasicLevel.INFO,
171: "Finished to create the entity");
172: entityJonasInstanceName = entity.getJOnASName();
173: } else {
174: entityJonasInstanceName = "No entity Bean created";
175: }
176:
177: logger.log(BasicLevel.INFO, "JOnAS=" + jonasInstanceName
178: + " ; ejb=" + this .toString() + " ; total calls="
179: + allInstancesTotalCallsCount + " ; entity created on="
180: + entityJonasInstanceName);
181: }
182:
183: /**
184: * Creates a new Entity bean with the time as parameter
185: * @param valeur The current time as string
186: * @return A new Entity Bean
187: */
188: private MyEntityLocal createEntity(String valeur) {
189: InitialContext cntx;
190: MyEntityLocal result = null;
191: try {
192: cntx = new InitialContext();
193: MyEntityLocalHome entityHome = (MyEntityLocalHome) cntx
194: .lookup("MyEntityHome_L");
195: result = entityHome.create(valeur);
196: } catch (NamingException e) {
197: logger.log(BasicLevel.FATAL, "Naming exception : "
198: + e.getMessage());
199: } catch (CreateException e) {
200: logger.log(BasicLevel.FATAL, "Create exception : "
201: + e.getMessage());
202: }
203: return result;
204: }
205:
206: /**
207: * The logger
208: */
209: private static Logger logger = null;
210:
211: /**
212: * the node name of the jonas instance where an
213: * entity bean has bean created
214: */
215: private String entityJonasInstanceName = "unknown";
216:
217: /**
218: * the node name of the jonas instance where the
219: * session bean is executed
220: */
221: private String jonasInstanceName = "unknown";
222:
223: /**
224: * counts all the instances created of this class in
225: * that JVM
226: */
227: private static int allInstancesTotalCallsCount = 0;
228: }
|