001: // SessSLR.java
002: // Stateless Session bean
003:
004: package samplehttp.beans;
005:
006: import java.rmi.RemoteException;
007: import java.sql.Connection;
008: import java.sql.SQLException;
009: import java.sql.Statement;
010: import javax.ejb.CreateException;
011: import javax.ejb.EJBException;
012: import javax.ejb.RemoveException;
013: import javax.ejb.EJBObject;
014: import javax.ejb.SessionBean;
015: import javax.ejb.SessionContext;
016: import javax.naming.Context;
017: import javax.naming.InitialContext;
018: import javax.naming.NamingException;
019: import javax.sql.DataSource;
020:
021: import org.objectweb.jonas.common.Log;
022: import org.objectweb.util.monolog.api.Logger;
023: import org.objectweb.util.monolog.api.BasicLevel;
024:
025: /**
026: *
027: */
028: public class SessSLR implements SessionBean {
029:
030: static private Logger logger = null;
031: SessionContext ejbContext;
032: private DataSource dataSource = null;
033:
034: private Connection getConnection() throws EJBException,
035: SQLException {
036: if (dataSource == null) {
037: // Finds DataSource from JNDI
038: Context initialContext = null;
039: try {
040: initialContext = new InitialContext();
041: dataSource = (DataSource) initialContext
042: .lookup("java:comp/env/jdbc/SessDs");
043: } catch (Exception e) {
044: System.out.println("Cannot lookup dataSource" + e);
045: throw new javax.ejb.EJBException(
046: "Cannot lookup dataSource ");
047: }
048: }
049: return dataSource.getConnection();
050: }
051:
052: // ------------------------------------------------------------------
053: // SessionBean implementation
054: // ------------------------------------------------------------------
055:
056: /**
057: * Set the associated session context. The container calls this method
058: * after the instance creation.
059: * The enterprise Bean instance should store the reference to the context
060: * object in an instance variable.
061: * This method is called with no transaction context.
062: *
063: * @param sessionContext A SessionContext interface for the instance.
064: * @throws EJBException Thrown by the method to indicate a failure caused by
065: * a system-level error.
066: */
067: public void setSessionContext(SessionContext ctx) {
068: if (logger == null) {
069: logger = Log.getLogger("org.objectweb.jonas_tests");
070: }
071: logger.log(BasicLevel.DEBUG, "");
072: ejbContext = ctx;
073: }
074:
075: /**
076: * A container invokes this method before it ends the life of the session object.
077: * This happens as a result of a client's invoking a remove operation, or when a
078: * container decides to terminate the session object after a timeout.
079: * This method is called with no transaction context.
080: *
081: * @throws EJBException Thrown by the method to indicate a failure caused by
082: * a system-level error.
083: */
084: public void ejbRemove() {
085: logger.log(BasicLevel.DEBUG, "");
086: }
087:
088: /**
089: * The Session bean must define 1 or more ejbCreate methods.
090: *
091: * @throws CreateException Failure to create a session EJB object.
092: */
093: public void ejbCreate() throws CreateException {
094: logger.log(BasicLevel.DEBUG, "");
095: }
096:
097: /**
098: * A container invokes this method on an instance before the instance
099: * becomes disassociated with a specific EJB object.
100: */
101: public void ejbPassivate() {
102: logger.log(BasicLevel.DEBUG, "");
103: }
104:
105: /**
106: * A container invokes this method when the instance is taken out of
107: * the pool of available instances to become associated with a specific
108: * EJB object.
109: */
110: public void ejbActivate() {
111: logger.log(BasicLevel.DEBUG, "");
112: }
113:
114: // ------------------------------------------------------------------
115: // Sess implementation
116: // ------------------------------------------------------------------
117:
118: /**
119: * process
120: * @ param long treatment time
121: */
122: public void process(long time) {
123: logger.log(BasicLevel.DEBUG, "treatment time = " + time);
124: try {
125: Thread.currentThread().sleep(time);
126: } catch (InterruptedException e) {
127: System.out.println("sleep:" + e);
128: }
129: }
130:
131: /**
132: * process
133: * @ param long treatment time
134: */
135: public void processwithconn(long time) {
136: logger.log(BasicLevel.DEBUG, "treatment time = " + time);
137: try {
138: Connection conn = getConnection();
139: Thread.currentThread().sleep(time);
140: conn.close();
141: } catch (InterruptedException e) {
142: System.out.println("sleep:" + e);
143: } catch (Exception e) {
144: System.out.println("exception caught: " + e);
145: }
146: }
147:
148: }
|