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: * Initial developer(s):
022: * --------------------------------------------------------------------------
023: * $Id: MyStatefulSFR.java 9349 2006-08-03 14:43:47Z japaz $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.sampleCluster2.ejb;
026:
027: import java.rmi.RemoteException;
028: import java.util.ArrayList;
029: import java.util.Date;
030: import java.util.Iterator;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035: import javax.ejb.SessionSynchronization;
036: import javax.naming.InitialContext;
037: import javax.naming.NamingException;
038:
039: import org.objectweb.jonas.common.JProp;
040: import org.objectweb.jonas.common.Log;
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: *
046: */
047: public class MyStatefulSFR implements SessionBean,
048: SessionSynchronization {
049:
050: /**
051: *
052: */
053: private static final long serialVersionUID = 1L;
054:
055: /**
056: * After the transaction begun
057: */
058: public void afterBegin() {
059: logger.log(BasicLevel.DEBUG, "");
060: }
061:
062: /**
063: * before the commit is executed
064: */
065: public void beforeCompletion() {
066: logger.log(BasicLevel.DEBUG, "");
067: }
068:
069: /**
070: * after the commit or rollback
071: * @param committed true -> commit, false -> rollback
072: */
073: public void afterCompletion(boolean committed) {
074: logger.log(BasicLevel.DEBUG, "");
075: }
076:
077: /**
078: * Set the session context
079: * @param ctx the session context
080: */
081: public void setSessionContext(SessionContext ctx) {
082: if (logger == null) {
083: logger = Log.getLogger("org.objectweb.jonas_tests");
084: }
085: logger.log(BasicLevel.DEBUG, "");
086: }
087:
088: /**
089: * removes the ejb
090: */
091: public void ejbRemove() {
092: logger.log(BasicLevel.DEBUG, "");
093: }
094:
095: /**
096: * creation of the ejb
097: */
098: public void ejbCreate() {
099: logger.log(BasicLevel.DEBUG, "");
100: creatorJonasInstanceName = "unknown";
101:
102: try {
103: JProp jp = JProp.getInstance();
104: creatorJonasInstanceName = jp.getValue("jonas.name");
105: } catch (Exception e) {
106: logger.log(BasicLevel.FATAL, e.getMessage());
107: }
108:
109: log = new ArrayList(MAXSIZE);
110:
111: logger.log(BasicLevel.DEBUG, "ejbCreate()->" + this .toString());
112: isModified = true;
113: }
114:
115: /**
116: * Passivate of the ejb
117: */
118: public void ejbPassivate() {
119: logger.log(BasicLevel.DEBUG, "");
120: }
121:
122: /**
123: * activation of the ejb
124: */
125: public void ejbActivate() {
126: logger.log(BasicLevel.DEBUG, "");
127: }
128:
129: /**
130: * Initializes the inner beans
131: */
132: public void initialize() {
133: logger.log(BasicLevel.DEBUG, "");
134:
135: localStateful = createLocalStateful();
136: Date date = new Date();
137: localEntity = createEntity(Long.toString(date.getTime()));
138: isModified = true;
139: }
140:
141: /**
142: * Keep the parameter in a list. Note : the parameter represents a line in
143: * the "session servlet output" screen
144: * @param s The string to keep.
145: * @throws RemoteException
146: */
147: public void log(java.lang.String s) {
148: logger.log(BasicLevel.DEBUG, "");
149:
150: if (log.size() == MAXSIZE) {
151: Iterator iter = log.iterator();
152: iter.next();
153: iter.remove();
154: }
155:
156: log.add(s);
157:
158: // Call local stateful
159: getLocalStateful().increment();
160:
161: isModified = true;
162: }
163:
164: /**
165: * Keep the parameter in a list with the source node
166: * @param s The string to keep.
167: * @throws RemoteException
168: */
169: public void logWithJOnASInstance(java.lang.String s) {
170: logger.log(BasicLevel.DEBUG, "");
171:
172: if (log.size() == MAXSIZE) {
173: Iterator iter = log.iterator();
174: iter.next();
175: iter.remove();
176: }
177:
178: log.add("JOnAS instance=" + creatorJonasInstanceName + " - "
179: + s);
180:
181: // Call local stateful
182: getLocalStateful().increment();
183:
184: isModified = true;
185: }
186:
187: /**
188: * Retreive all the data in the log table Note : The return value is the
189: * data shown in the "session servlet output" screen
190: * @return All the logged data
191: */
192: public java.lang.StringBuffer getLogDump() {
193: logger.log(BasicLevel.DEBUG, "");
194:
195: StringBuffer sb = new StringBuffer();
196: sb.append("JOnAS instance=");
197: sb.append(creatorJonasInstanceName);
198: sb.append(" ; EJB=");
199: sb.append(this .toString());
200: sb.append("\n");
201: sb.append("Owner: ");
202: sb.append(callerHTTPSessionId);
203: sb.append("\n");
204:
205: sb.append("Inner count: ");
206: sb.append(getLocalStateful().getCount());
207: sb.append("\n");
208:
209: sb.append("Local entity: ");
210: sb.append(getLocalEntity().getNumber());
211: sb.append("\n");
212:
213: Iterator iter = log.iterator();
214:
215: sb
216: .append("<table border=1><tr><td>servlet running on</td><td>stateless bean created on</td><td>statless bean total calls</td><td>entity bean created on</td></tr>");
217: while (iter.hasNext()) {
218: sb.append(iter.next());
219: }
220: sb.append("</table>");
221:
222: logger.log(BasicLevel.INFO, sb.toString());
223: return sb;
224: }
225:
226: /**
227: * Retreive all the data in the log table Note : The return value is the
228: * text format (calls from the java client)
229: * @return All the logged data
230: */
231: public java.lang.StringBuffer getLogTextDump() {
232: logger.log(BasicLevel.DEBUG, "");
233:
234: StringBuffer sb = new StringBuffer();
235: sb.append("JOnAS instance=");
236: sb.append(creatorJonasInstanceName);
237: sb.append(" ; EJB=");
238: sb.append(this .toString());
239: sb.append("\n");
240:
241: sb.append("Inner count: ");
242: sb.append(getLocalStateful().getCount());
243: sb.append("\n");
244:
245: sb.append("Local entity: ");
246: sb.append(getLocalEntity().getNumber());
247: sb.append("\n");
248:
249: Iterator iter = log.iterator();
250: int i = 1;
251: sb.append("Log entries -> \n");
252: while (iter.hasNext()) {
253: sb.append(" " + i + " : ");
254: sb.append((String) iter.next());
255: sb.append("\n");
256: i++;
257: }
258: sb.append("End\n");
259:
260: logger.log(BasicLevel.INFO, sb.toString());
261: return sb;
262: }
263:
264: /**
265: * Set the http sessionid of the caller.
266: * @param s The sessionid of the caller.
267: * @throws RemoteException
268: */
269: public void setHTTPSessionId(java.lang.String s) {
270: logger.log(BasicLevel.DEBUG, "");
271: callerHTTPSessionId = s;
272:
273: isModified = true;
274: }
275:
276: /**
277: * Get the stored http sessionid of the caller.
278: * @return The stored sessionid.
279: * @throws RemoteException
280: */
281: public java.lang.String getHTTPSessionId() {
282: logger.log(BasicLevel.DEBUG, "");
283: return callerHTTPSessionId;
284: }
285:
286: /**
287: * The JOnAS node name where the bean is executed
288: * the field is transient because we don't want to passivate or replicate it
289: */
290: private transient String creatorJonasInstanceName = "unknown";
291:
292: /**
293: * The logger
294: */
295: private static Logger logger = null;
296:
297: /**
298: * The maximum number of values to conserve
299: */
300: private static final int MAXSIZE = 20;
301:
302: /**
303: * The log output
304: */
305: private ArrayList log = null;
306:
307: /**
308: * caller http session id
309: */
310: private String callerHTTPSessionId = null;
311:
312: private MyStatefulInnerLocal localStateful = null;
313:
314: private MyEntityLocal localEntity = null;
315:
316: private transient boolean isModified = true;
317:
318: /**
319: * Creates a new Entity bean with the time as parameter
320: * @param valeur The current time as string
321: * @return A new Entity Bean
322: */
323: private MyStatefulInnerLocal createLocalStateful() {
324: InitialContext cntx;
325: MyStatefulInnerLocal result = null;
326: try {
327: cntx = new InitialContext();
328: MyStatefulInnerLocalHome statefulHome = (MyStatefulInnerLocalHome) cntx
329: .lookup("MyStatefulInnerHome_L");
330: result = statefulHome.create();
331: } catch (NamingException e) {
332: logger.log(BasicLevel.FATAL, "Naming exception : "
333: + e.getMessage());
334: } catch (CreateException e) {
335: logger.log(BasicLevel.FATAL, "Create exception : "
336: + e.getMessage());
337: }
338: return result;
339: }
340:
341: /**
342: * Returns the local stateful bean
343: * @return the local stateful bean
344: */
345: private MyStatefulInnerLocal getLocalStateful() {
346: if (localStateful == null) {
347: localStateful = createLocalStateful();
348: isModified = true;
349: }
350:
351: return localStateful;
352: }
353:
354: /**
355: * Creates a new Entity bean with the time as parameter
356: * @param valeur The current time as string
357: * @return A new Entity Bean
358: */
359: private MyEntityLocal createEntity(String valeur) {
360: InitialContext cntx;
361: MyEntityLocal result = null;
362: try {
363: cntx = new InitialContext();
364: MyEntityLocalHome entityHome = (MyEntityLocalHome) cntx
365: .lookup("MyEntityHome_L");
366: result = entityHome.create(valeur);
367: } catch (NamingException e) {
368: logger.log(BasicLevel.FATAL, "Naming exception : "
369: + e.getMessage());
370: } catch (CreateException e) {
371: logger.log(BasicLevel.FATAL, "Create exception : "
372: + e.getMessage());
373: }
374: return result;
375: }
376:
377: /**
378: * Returns the local entity bean
379: * @return the local entity bean
380: */
381: private MyEntityLocal getLocalEntity() {
382: if (localEntity == null) {
383: Date date = new Date();
384: localEntity = createEntity(Long.toString(date.getTime()));
385: isModified = true;
386: }
387: return localEntity;
388: }
389:
390: public boolean isModified() {
391: boolean result = isModified;
392: isModified = false;
393: return result;
394: }
395: }
|