KickJava   Java API By Example, From Geeks To Geeks.

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


1 // SecuredCASLR.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.sql.DataSource JavaDoc;
17 import ersatz.resourceadapter.*;
18 import javax.resource.cci.Connection JavaDoc;
19 import javax.resource.spi.ManagedConnection JavaDoc;
20 import javax.resource.spi.ConnectionEvent JavaDoc;
21 import javax.resource.spi.ConnectionManager JavaDoc;
22
23
24 /**
25  *
26  */

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

42
43     public void setSessionContext(SessionContext JavaDoc ctx) {
44         Utility.log(cName+".setSessionContext");
45         ejbContext = ctx;
46     }
47         
48
49     public void ejbRemove() {
50         Utility.log("");
51     }
52         
53
54     public void ejbCreate() throws CreateException JavaDoc {
55         Utility.log("");
56     }
57
58     public void ejbPassivate() {
59         Utility.log("");
60     }
61
62     public void ejbActivate() {
63         Utility.log("");
64     }
65     
66     /**
67      * closeUp
68      */

69     public void closeUp(int w) {
70         try {
71             if (w>0) {
72                 // The CONNECTION_ERROR_OCCURRED indicates that the associated
73
// ManagedConnectionImpl instance is now invalid and unusable.
74
conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
75                 Utility.log(cName+".closeUp : closed physical connection");
76             } else {
77                 // The CONNECTION_CLOSED indicates that connection handle
78
// is closed, but physical connection still exists
79
conn.close();
80                 Utility.log(cName+".closeUp : closed connection");
81             }
82         } catch (Exception JavaDoc e) {
83             Utility.log(cName+".closeUp error: close handle/physical connection failed");
84         }
85     }
86     public void setResAuth(String JavaDoc ra) {
87         res_auth=ra; // set to Application or Container
88
}
89     public void setMatchNull(boolean b) {
90         mcf = cccf.getMcf(); // ManagedConnectionFactory
91
mcf.setMatchNull(b);
92     }
93     // ------------------------------------------------------------------
94
// secured implementation
95
// ------------------------------------------------------------------
96

97     /**
98      * method1
99      */

100     public void method1(String JavaDoc rar_jndi_name, String JavaDoc testName)
101             throws Exception JavaDoc
102     {
103         Utility.log("============================ "+testName);
104         try {
105             ic = new InitialContext JavaDoc();
106         } catch (Exception JavaDoc e1) {
107             Utility.log(cName+".method1 error: InitialContext failed");
108             throw e1;
109         }
110         try {
111             cccf = (ConnectionFactoryImpl)ic.lookup(rar_jndi_name);
112             Utility.log(cName+".method1 : found "+rar_jndi_name);
113         } catch (Exception JavaDoc e2) {
114             Utility.log(cName+".method1 error: lookup failed for "+rar_jndi_name);
115             throw e2;
116         }
117         
118             //
119
// Container-managed sign-on when file "secured.xml" contains line below
120
//
121
// <res-auth>Container</res-auth>
122
//
123
try {
124             conn = (ConnectionImpl)cccf.getConnection();
125             Utility.log(cName+".method1 : getConnection() 'Container' conn="+conn);
126
127             if (conn==null) {
128                 Utility.log(cName+".method1 error: getConnection returned null connection.");
129                 throw new Exception JavaDoc("");
130             }
131         } catch (Exception JavaDoc e4) {
132             Utility.log(cName+".method1 error: getConnection failed "
133                     +e4.toString());
134             throw e4;
135         }
136     }
137
138     public String JavaDoc getResAuth() {
139         try {
140             ManagedConnectionImpl mc = conn.getMC();
141             String JavaDoc ra = mc.res_auth; // get real "Application" or "Container"
142
Utility.log(cName+".getResAuth "
143                        +"<res-auth>"+ra+"</res-auth>");
144             if (ra==null || ra.length()==0)
145                 throw new Exception JavaDoc("");
146             return ra;
147         } catch (Exception JavaDoc e) {
148             Utility.log(cName
149                    +".getResAuth error: failed to find <res-auth> "
150                    +"in ManagedConnectionImpl");
151             return "";
152         }
153     }
154     public String JavaDoc getSecurityPassword() {
155         crii = conn.crii; // look at ConnectionRequestInfoImpl
156
try {
157             ManagedConnectionImpl mc = conn.getMC();
158             String JavaDoc pw = crii.getPassword();
159             Utility.log(cName+".getSecurityPassword ("
160                           +mc.res_auth+")password="+pw);
161             if (pw==null || pw.length()==0)
162                 throw new Exception JavaDoc("");
163             return pw;
164         } catch (Exception JavaDoc e) {
165             mcf = cccf.getMcf();
166             String JavaDoc pw = mcf.defaultPassword; // find default
167
Utility.log(cName
168                    +".getSecurityPassword error: failed to find ConnectionRequestInfoImpl "
169                    +"instance containing password. Using default="+pw);
170             return pw;
171         }
172     }
173     public String JavaDoc getSecurityUserName() {
174         crii = conn.crii; // look at ConnectionRequestInfoImpl
175
try {
176             ManagedConnectionImpl mc = conn.getMC();
177             String JavaDoc u = crii.getUserName();
178             Utility.log(cName+".getSecurityUserName ("
179                           +mcf.getRes_Auth()+")userName="+u);
180             if (u==null || u.length()==0)
181                 throw new Exception JavaDoc("");
182             return u;
183         } catch (Exception JavaDoc e) {
184             mcf = cccf.getMcf();
185             String JavaDoc u = mcf.defaultUserName; // find default
186
Utility.log(cName
187                    +".getSecurityUserName error: failed to find ConnectionRequestInfoImpl "
188                    +"instance containing userName. Using default="+u);
189             return u;
190         }
191     }
192
193 }
194
195
Popular Tags