KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > jca15 > RuntimeCASLR


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

4 package org.objectweb.jonas.jtests.beans.jca15;
5
6 import java.rmi.RemoteException JavaDoc;
7 import javax.ejb.CreateException JavaDoc;
8 import javax.ejb.EJBException JavaDoc;
9 import javax.ejb.RemoveException JavaDoc;
10 import javax.ejb.EJBObject JavaDoc;
11 import javax.ejb.SessionBean JavaDoc;
12 import javax.ejb.SessionContext JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.resource.cci.Connection JavaDoc;
17 import javax.resource.spi.ManagedConnection JavaDoc;
18 import javax.resource.spi.ConnectionManager JavaDoc;
19 import ersatz.resourceadapter.*;
20 import javax.resource.spi.ConnectionEvent JavaDoc;
21
22
23 /*
24  */

25 public class RuntimeCASLR implements SessionBean JavaDoc {
26
27     SessionContext JavaDoc ejbContext;
28     private ManagedConnectionFactoryImpl mcf = null; //Managed Connection Factory
29
private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
30
private ConnectionImpl conn = null;
31     private ConnectionSpecImpl csp = null; //ConnectionSpec
32
private ConnectionRequestInfoImpl crii = null;
33     InitialContext JavaDoc ic=null;
34     private String JavaDoc res_auth = "";
35     String JavaDoc cName = "RuntimeCASLR";
36
37     // ------------------------------------------------------------------
38
// SessionBean implementation
39
// ------------------------------------------------------------------
40

41
42     public void setSessionContext(SessionContext JavaDoc ctx) {
43         Utility.log(cName+".setSessionContext");
44         ejbContext = ctx;
45     }
46         
47
48     public void ejbRemove() {
49         Utility.log(cName+".ejbRemove");
50     }
51         
52
53     public void ejbCreate() throws CreateException JavaDoc {
54         Utility.log(cName+".ejbCreate");
55     }
56
57     public void ejbPassivate() {
58         Utility.log(cName+".ejbPassivate");
59     }
60
61     public void ejbActivate() {
62         Utility.log(cName+".ejbActivate");
63     }
64     
65     // ------------------------------------------------------------------
66
// Runtime implementation
67
// ------------------------------------------------------------------
68
public void setResAuth(String JavaDoc ra) {
69         res_auth=ra; // set to Application or Container
70
}
71
72     /**
73      * method1
74      */

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

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

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