KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samplehttp > beans > SessSLR


1 // SessSLR.java
2
// Stateless Session bean
3

4 package samplehttp.beans;
5
6 import java.rmi.RemoteException;
7 import java.sql.Connection;
8 import java.sql.SQLException;
9 import java.sql.Statement;
10 import javax.ejb.CreateException;
11 import javax.ejb.EJBException;
12 import javax.ejb.RemoveException;
13 import javax.ejb.EJBObject;
14 import javax.ejb.SessionBean;
15 import javax.ejb.SessionContext;
16 import javax.naming.Context;
17 import javax.naming.InitialContext;
18 import javax.naming.NamingException;
19 import javax.sql.DataSource;
20
21 import org.objectweb.jonas.common.Log;
22 import org.objectweb.util.monolog.api.Logger;
23 import org.objectweb.util.monolog.api.BasicLevel;
24
25
26 /**
27  *
28  */

29 public class SessSLR implements SessionBean {
30
31     static private Logger logger = null;
32     SessionContext ejbContext;
33     private DataSource dataSource = null;
34
35
36
37
38     private Connection getConnection() throws EJBException, SQLException {
39         if (dataSource == null) {
40             // Finds DataSource from JNDI
41
Context initialContext = null;
42             try {
43                 initialContext = new InitialContext();
44                 dataSource = (DataSource)initialContext.lookup("java:comp/env/jdbc/SessDs");
45             } catch (Exception e) {
46                 System.out.println("Cannot lookup dataSource"+e);
47                 throw new javax.ejb.EJBException("Cannot lookup dataSource ");
48             }
49         }
50         return dataSource.getConnection();
51     }
52
53
54     // ------------------------------------------------------------------
55
// SessionBean implementation
56
// ------------------------------------------------------------------
57

58     /**
59      * Set the associated session context. The container calls this method
60      * after the instance creation.
61      * The enterprise Bean instance should store the reference to the context
62      * object in an instance variable.
63      * This method is called with no transaction context.
64      *
65      * @param sessionContext A SessionContext interface for the instance.
66      * @throws EJBException Thrown by the method to indicate a failure caused by
67      * a system-level error.
68      */

69     public void setSessionContext(SessionContext ctx) {
70         if (logger == null) {
71             logger = Log.getLogger("org.objectweb.jonas_tests");
72         }
73         logger.log(BasicLevel.DEBUG, "");
74         ejbContext = ctx;
75     }
76     
77     /**
78      * A container invokes this method before it ends the life of the session object.
79      * This happens as a result of a client's invoking a remove operation, or when a
80      * container decides to terminate the session object after a timeout.
81      * This method is called with no transaction context.
82      *
83      * @throws EJBException Thrown by the method to indicate a failure caused by
84      * a system-level error.
85      */

86     public void ejbRemove() {
87         logger.log(BasicLevel.DEBUG, "");
88     }
89     
90     /**
91      * The Session bean must define 1 or more ejbCreate methods.
92      *
93      * @throws CreateException Failure to create a session EJB object.
94      */

95     public void ejbCreate() throws CreateException {
96         logger.log(BasicLevel.DEBUG, "");
97     }
98
99
100     /**
101      * A container invokes this method on an instance before the instance
102      * becomes disassociated with a specific EJB object.
103      */

104     public void ejbPassivate() {
105         logger.log(BasicLevel.DEBUG, "");
106     }
107
108     /**
109      * A container invokes this method when the instance is taken out of
110      * the pool of available instances to become associated with a specific
111      * EJB object.
112      */

113     public void ejbActivate() {
114         logger.log(BasicLevel.DEBUG, "");
115     }
116     
117     // ------------------------------------------------------------------
118
// Sess implementation
119
// ------------------------------------------------------------------
120

121     /**
122      * process
123      * @ param long treatment time
124      */

125     public void process(long time) {
126         logger.log(BasicLevel.DEBUG, "treatment time = "+time);
127         try {
128             Thread.currentThread().sleep(time);
129         } catch (InterruptedException e) {
130             System.out.println("sleep:"+e);
131         }
132     }
133
134     /**
135      * process
136      * @ param long treatment time
137      */

138     public void processwithconn(long time) {
139         logger.log(BasicLevel.DEBUG, "treatment time = "+time);
140         try {
141             Connection conn = getConnection();
142             Thread.currentThread().sleep(time);
143             conn.close();
144         } catch (InterruptedException e) {
145             System.out.println("sleep:"+e);
146         } catch (Exception e) {
147             System.out.println("exception caught: "+e);
148         }
149     }
150
151 }
152
Popular Tags