KickJava   Java API By Example, From Geeks To Geeks.

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


1 // securedCASLR.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  */

24 public class securedCASLR implements SessionBean JavaDoc {
25
26     static private Logger logger = null;
27     SessionContext JavaDoc ejbContext;
28     private JtestResourceAdapter mcf = null; //Managed Connection Factory
29
private CommonClient cccf = null; //Common Client Connection Factory
30
private ConnectionImpl conn = null;
31     InitialContext JavaDoc ic=null;
32     private String JavaDoc res_auth = "";
33     String JavaDoc cName = "securedCASLR";
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, cName+".setSessionContext");
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      * closeUp
68      */

69     public void closeUp(int w) {
70         try {
71             if (w>0) {
72                 // The CONNECTION_ERROR_OCCURRED indicates that the associated
73
// ManagedConnection instance is now invalid and unusable.
74
conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
75                 logger.log(BasicLevel.DEBUG, 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                 logger.log(BasicLevel.DEBUG, cName+".closeUp : closed connection");
81             }
82         } catch (Exception JavaDoc e) {
83             logger.log(BasicLevel.DEBUG, 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 = (JtestResourceAdapter) 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         logger.log(BasicLevel.DEBUG, "============================ "+testName);
104         try {
105             ic = new InitialContext JavaDoc();
106         } catch (Exception JavaDoc e1) {
107             logger.log(BasicLevel.DEBUG, cName+".method1 error: InitialContext failed");
108             throw e1;
109         }
110         try {
111             cccf = (CommonClient)ic.lookup(rar_jndi_name);
112             logger.log(BasicLevel.DEBUG, cName+".method1 : found "+rar_jndi_name);
113         } catch (Exception JavaDoc e2) {
114             logger.log(BasicLevel.DEBUG, 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             logger.log(BasicLevel.DEBUG, cName+".method1 : getConnection() 'Container' conn="+conn);
126
127             if (conn==null) {
128                 logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection returned null connection.");
129                 throw new Exception JavaDoc("");
130             }
131         } catch (Exception JavaDoc e4) {
132             logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection failed "
133                     +e4.toString());
134             throw e4;
135         }
136     }
137
138     public String JavaDoc getResAuth() {
139         mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
140
try {
141             //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
142
String JavaDoc ra = mcf.getRes_Auth(); // get real "Application" or "Container"
143
logger.log(BasicLevel.DEBUG, cName+".getResAuth "
144                        +"<res-auth>"+ra+"</res-auth>");
145             return ra;
146         } catch (Exception JavaDoc e) {
147             logger.log(BasicLevel.DEBUG, cName
148                    +".getResAuth error: failed to find <res-auth> "
149                    +"in ManagedConnectionFactory");
150             return "";
151         }
152     }
153     public String JavaDoc getSecurityPassword() {
154         mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
155
//ConnectionImpl conni = (ConnectionImpl)conn;
156
try {
157             //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
158
String JavaDoc pw = mcf.getPassword();
159             logger.log(BasicLevel.DEBUG, cName+".getSecurityPassword ("
160                           +mcf.getRes_Auth()+")password="+pw);
161             return pw;
162         } catch (Exception JavaDoc e) {
163             String JavaDoc pw = mcf.getPassword(); // find default
164
logger.log(BasicLevel.DEBUG, cName
165                    +".getSecurityPassword error: failed to find ManagedConnectionFactory "
166                    +"instance containing password. Using pw="+pw);
167             return pw;
168         }
169     }
170     public String JavaDoc getSecurityUserName() {
171         mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
172
//ConnectionImpl conni = (ConnectionImpl)conn; // get ConnectionImpl
173
try {
174             //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
175
String JavaDoc u = mcf.getUserName();
176             logger.log(BasicLevel.DEBUG, cName+".getSecurityUserName ("
177                           +mcf.getRes_Auth()+")userName="+u);
178             return u;
179         } catch (Exception JavaDoc e) {
180             String JavaDoc u = mcf.getUserName(); // find default
181
logger.log(BasicLevel.DEBUG, cName
182                    +".getSecurityUserName error: failed to find ManagedConnectionFactory "
183                    +"instance containing userName. Using="+u);
184             return u;
185         }
186     }
187
188 }
189
190
Popular Tags