KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > j2eeca > runtimeCASLR


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

4 package org.objectweb.jonas.jtests.beans.j2eeca;
5
6 import javax.ejb.CreateException JavaDoc;
7 import javax.ejb.SessionBean JavaDoc;
8 import javax.ejb.SessionContext JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.resource.spi.ConnectionEvent JavaDoc;
11
12 import org.objectweb.jonas.common.Log;
13 import org.objectweb.util.monolog.api.BasicLevel;
14 import org.objectweb.util.monolog.api.Logger;
15
16 import fictional.resourceadapter.CommonClient;
17 import fictional.resourceadapter.ConnectionImpl;
18 import fictional.resourceadapter.JtestResourceAdapter;
19
20
21 /*
22  */

23 public class runtimeCASLR implements SessionBean JavaDoc {
24
25     static private Logger logger = null;
26     SessionContext JavaDoc ejbContext;
27     private JtestResourceAdapter mcf = null; //Managed Connection Factory
28
private CommonClient cccf = null; //Common Client Connection Factory
29
private ConnectionImpl conn = null;
30     private CommonClient csp = null; //ConnectionSpec
31
InitialContext JavaDoc ic=null;
32     private String JavaDoc res_auth = "";
33     String JavaDoc cName = "runtimeCASLR";
34
35     // ------------------------------------------------------------------
36
// SessionBean implementation
37
// ------------------------------------------------------------------
38

39
40     public void setSessionContext(SessionContext JavaDoc ctx) {
41         if (logger == null) {
42             logger = Log.getLogger("org.objectweb.jonas.jtests.j2eeca");
43         }
44         logger.log(BasicLevel.DEBUG, "");
45         ejbContext = ctx;
46     }
47         
48
49     public void ejbRemove() {
50         logger.log(BasicLevel.DEBUG, "");
51     }
52         
53
54     public void ejbCreate() throws CreateException JavaDoc {
55         logger.log(BasicLevel.DEBUG, "");
56     }
57
58     public void ejbPassivate() {
59         logger.log(BasicLevel.DEBUG, "");
60     }
61
62     public void ejbActivate() {
63         logger.log(BasicLevel.DEBUG, "");
64     }
65     
66     // ------------------------------------------------------------------
67
// runtime implementation
68
// ------------------------------------------------------------------
69
public void setResAuth(String JavaDoc ra) {
70         res_auth=ra; // set to Application or Container
71
}
72
73     /**
74      * method1
75      */

76     public void method1(String JavaDoc rar_jndi_name, String JavaDoc testName)
77             throws Exception JavaDoc
78     {
79         logger.log(BasicLevel.DEBUG, "============================ "+testName);
80         logger.log(BasicLevel.DEBUG, cName+".method1 : lookup "+rar_jndi_name);
81         try {
82             ic = new InitialContext JavaDoc();
83         } catch (Exception JavaDoc e1) {
84             logger.log(BasicLevel.DEBUG, cName+".method1 error: InitialContext failed");
85             throw e1;
86         }
87         try {
88             cccf = (CommonClient)ic.lookup(rar_jndi_name);
89             logger.log(BasicLevel.DEBUG, cName+".method1 : found "+rar_jndi_name);
90         } catch (Exception JavaDoc e2) {
91             logger.log(BasicLevel.DEBUG, cName+".method1 error: lookup failed for "+rar_jndi_name);
92             throw e2;
93         }
94
95         //
96
// Component-managed sign-on when file "runtime.xml" contains line below
97
//
98
// <res-auth>Application</res-auth>
99
//
100
try {
101             csp = new CommonClient(); // get a new ConnectionSpec
102
csp.setUserName("Fictional_User_Name");
103             csp.setPassword("__Jtest_Pass_word__");
104             logger.log(BasicLevel.DEBUG, cName
105                   +".method1 : ConnectionSpec + Fictional_User_Name,__Jtest_Pass_word__");
106         } catch (Exception JavaDoc e3) {
107             logger.log(BasicLevel.DEBUG, cName+".method1 : new connection spec failed");
108             throw e3;
109         }
110         try {
111             conn = (ConnectionImpl)cccf.getConnection();
112             if (conn==null) {
113                 logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection returned null connection.");
114                 throw new Exception JavaDoc("");
115             }
116         } catch (Exception JavaDoc e4) {
117             logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection failed "
118                     +e4.toString());
119             throw e4;
120         }
121     }
122
123     /**
124      * closeUp
125      */

126     public void closeUp(int w) {
127         try {
128             if (w>0) {
129                 // The CONNECTION_ERROR_OCCURRED indicates that the associated
130
// ManagedConnection instance is now invalid and unusable.
131
conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
132                 logger.log(BasicLevel.DEBUG, cName+".closeUp : closed physical connection");
133             } else {
134                 // The CONNECTION_CLOSED indicates that connection handle
135
// is closed, but physical connection still exists
136
conn.close();
137                 logger.log(BasicLevel.DEBUG, cName+".closeUp : closed connection");
138             }
139         } catch (Exception JavaDoc e) {
140             logger.log(BasicLevel.DEBUG, cName+".closeUp error: close handle/physical connection failed");
141         }
142     }
143     /**
144      * JUnit tests
145      */

146     public int getMCF_Pwriter()
147     {
148         int here=2;
149         JtestResourceAdapter mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
150
try {
151             if (mcf.getLogWriter()==null) { // PrintWriter not null
152
logger.log(BasicLevel.DEBUG, cName+".getMCF_Pwriter No PrintWriter registered");
153                 here=0;
154             }
155             else {
156                 logger.log(BasicLevel.DEBUG, cName+".getMCF_Pwriter PrintWriter is o.k.");
157                 here=1;
158             }
159         } catch (Exception JavaDoc e) {
160             logger.log(BasicLevel.DEBUG, cName+
161                 ".getMCF_Pwriter error: "+e.toString());
162         }
163         return here;
164     }
165     public int getMC_Pwriter()
166     {
167         int here=2;
168         ConnectionImpl conni = (ConnectionImpl)conn;
169         try {
170             JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
171
if (mc.getLogWriter()==null) { // PrintWriter not null
172
logger.log(BasicLevel.DEBUG, cName+
173                     ".getMC_Pwriter No PrintWriter registered in ManagedConnection");
174                 here=0;
175             }
176             else {
177                 logger.log(BasicLevel.DEBUG, cName+
178                     ".getMC_Pwriter PrintWriter in ManagedConnection is o.k.");
179                 here=1;
180             }
181         } catch (Exception JavaDoc e) {
182             logger.log(BasicLevel.DEBUG, cName+
183                 ".getMC_Pwriter error: "+e.toString());
184         }
185         return here;
186     }
187     public String JavaDoc getResAuth() {
188         mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
189
try {
190             //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
191
String JavaDoc ra = mcf.getRes_Auth(); // get real "Application" or "Container"
192
logger.log(BasicLevel.DEBUG, cName+".getResAuth "
193                        +"<res-auth>"+ra+"</res-auth>");
194             return ra;
195         } catch (Exception JavaDoc e) {
196             logger.log(BasicLevel.DEBUG, cName
197                    +".getResAuth error: failed to find <res-auth> "
198                    +"in ManagedConnectionFactory");
199             return "";
200         }
201     }
202     public String JavaDoc getSecurityPassword() {
203         mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
204
//ConnectionImpl conni = (ConnectionImpl)conn;
205
try {
206             //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
207
String JavaDoc pw = mcf.getPassword();
208             logger.log(BasicLevel.DEBUG, cName+".getSecurityPassword ("
209                           +mcf.getRes_Auth()+")password="+pw);
210             return pw;
211         } catch (Exception JavaDoc e) {
212             String JavaDoc pw = mcf.getPassword(); // find default
213
logger.log(BasicLevel.DEBUG, cName
214                    +".getSecurityPassword error: failed to find ManagedConnectionFactory "
215                    +"instance containing password. Using pw="+pw);
216             return pw;
217         }
218     }
219     public String JavaDoc getSecurityUserName() {
220         mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
221
//ConnectionImpl conni = (ConnectionImpl)conn; // get ConnectionImpl
222
try {
223             //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
224
String JavaDoc u = mcf.getUserName();
225             logger.log(BasicLevel.DEBUG, cName+".getSecurityUserName ("
226                           +mcf.getRes_Auth()+")userName="+u);
227             return u;
228         } catch (Exception JavaDoc e) {
229             String JavaDoc u = mcf.getUserName(); // find default
230
logger.log(BasicLevel.DEBUG, cName
231                    +".getSecurityUserName error: failed to find ManagedConnectionFactory "
232                    +"instance containing userName. Using="+u);
233             return u;
234         }
235     }
236 }
237
238
Popular Tags