001: // securedCASLR.java
002: // Stateless Session bean
003:
004: package org.objectweb.jonas.jtests.beans.j2eeca;
005:
006: import javax.ejb.CreateException;
007: import javax.ejb.SessionBean;
008: import javax.ejb.SessionContext;
009: import javax.naming.InitialContext;
010: import javax.resource.spi.ConnectionEvent;
011:
012: import org.objectweb.jonas.common.Log;
013: import org.objectweb.util.monolog.api.BasicLevel;
014: import org.objectweb.util.monolog.api.Logger;
015:
016: import fictional.resourceadapter.CommonClient;
017: import fictional.resourceadapter.ConnectionImpl;
018: import fictional.resourceadapter.JtestResourceAdapter;
019:
020: /**
021: *
022: */
023: public class securedCASLR implements SessionBean {
024:
025: static private Logger logger = null;
026: SessionContext ejbContext;
027: private JtestResourceAdapter mcf = null; //Managed Connection Factory
028: private CommonClient cccf = null; //Common Client Connection Factory
029: private ConnectionImpl conn = null;
030: InitialContext ic = null;
031: private String res_auth = "";
032: String cName = "securedCASLR";
033:
034: // ------------------------------------------------------------------
035: // SessionBean implementation
036: // ------------------------------------------------------------------
037:
038: public void setSessionContext(SessionContext ctx) {
039: if (logger == null) {
040: logger = Log.getLogger("org.objectweb.jonas.jtests.j2eeca");
041: }
042: logger.log(BasicLevel.DEBUG, cName + ".setSessionContext");
043: ejbContext = ctx;
044: }
045:
046: public void ejbRemove() {
047: logger.log(BasicLevel.DEBUG, "");
048: }
049:
050: public void ejbCreate() throws CreateException {
051: logger.log(BasicLevel.DEBUG, "");
052: }
053:
054: public void ejbPassivate() {
055: logger.log(BasicLevel.DEBUG, "");
056: }
057:
058: public void ejbActivate() {
059: logger.log(BasicLevel.DEBUG, "");
060: }
061:
062: /**
063: * closeUp
064: */
065: public void closeUp(int w) {
066: try {
067: if (w > 0) {
068: // The CONNECTION_ERROR_OCCURRED indicates that the associated
069: // ManagedConnection instance is now invalid and unusable.
070: conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
071: logger.log(BasicLevel.DEBUG, cName
072: + ".closeUp : closed physical connection");
073: } else {
074: // The CONNECTION_CLOSED indicates that connection handle
075: // is closed, but physical connection still exists
076: conn.close();
077: logger.log(BasicLevel.DEBUG, cName
078: + ".closeUp : closed connection");
079: }
080: } catch (Exception e) {
081: logger
082: .log(
083: BasicLevel.DEBUG,
084: cName
085: + ".closeUp error: close handle/physical connection failed");
086: }
087: }
088:
089: public void setResAuth(String ra) {
090: res_auth = ra; // set to Application or Container
091: }
092:
093: public void setMatchNull(boolean b) {
094: mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
095: mcf.setMatchNull(b);
096: }
097:
098: // ------------------------------------------------------------------
099: // secured implementation
100: // ------------------------------------------------------------------
101:
102: /**
103: * method1
104: */
105: public void method1(String rar_jndi_name, String testName)
106: throws Exception {
107: logger.log(BasicLevel.DEBUG, "============================ "
108: + testName);
109: try {
110: ic = new InitialContext();
111: } catch (Exception e1) {
112: logger.log(BasicLevel.DEBUG, cName
113: + ".method1 error: InitialContext failed");
114: throw e1;
115: }
116: try {
117: cccf = (CommonClient) ic.lookup(rar_jndi_name);
118: logger.log(BasicLevel.DEBUG, cName + ".method1 : found "
119: + rar_jndi_name);
120: } catch (Exception e2) {
121: logger.log(BasicLevel.DEBUG, cName
122: + ".method1 error: lookup failed for "
123: + rar_jndi_name);
124: throw e2;
125: }
126:
127: //
128: // Container-managed sign-on when file "secured.xml" contains line below
129: //
130: // <res-auth>Container</res-auth>
131: //
132: try {
133: conn = (ConnectionImpl) cccf.getConnection();
134: logger.log(BasicLevel.DEBUG, cName
135: + ".method1 : getConnection() 'Container' conn="
136: + conn);
137:
138: if (conn == null) {
139: logger
140: .log(
141: BasicLevel.DEBUG,
142: cName
143: + ".method1 error: getConnection returned null connection.");
144: throw new Exception("");
145: }
146: } catch (Exception e4) {
147: logger.log(BasicLevel.DEBUG, cName
148: + ".method1 error: getConnection failed "
149: + e4.toString());
150: throw e4;
151: }
152: }
153:
154: public String getResAuth() {
155: mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
156: try {
157: //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
158: String ra = mcf.getRes_Auth(); // get real "Application" or "Container"
159: logger.log(BasicLevel.DEBUG, cName + ".getResAuth "
160: + "<res-auth>" + ra + "</res-auth>");
161: return ra;
162: } catch (Exception e) {
163: logger.log(BasicLevel.DEBUG, cName
164: + ".getResAuth error: failed to find <res-auth> "
165: + "in ManagedConnectionFactory");
166: return "";
167: }
168: }
169:
170: public String getSecurityPassword() {
171: mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
172: //ConnectionImpl conni = (ConnectionImpl)conn;
173: try {
174: //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
175: String pw = mcf.getPassword();
176: logger.log(BasicLevel.DEBUG, cName
177: + ".getSecurityPassword (" + mcf.getRes_Auth()
178: + ")password=" + pw);
179: return pw;
180: } catch (Exception e) {
181: String pw = mcf.getPassword(); // find default
182: logger
183: .log(
184: BasicLevel.DEBUG,
185: cName
186: + ".getSecurityPassword error: failed to find ManagedConnectionFactory "
187: + "instance containing password. Using pw="
188: + pw);
189: return pw;
190: }
191: }
192:
193: public String getSecurityUserName() {
194: mcf = (JtestResourceAdapter) cccf.getMcf(); // get ManagedConnectionFactory
195: //ConnectionImpl conni = (ConnectionImpl)conn; // get ConnectionImpl
196: try {
197: //JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
198: String u = mcf.getUserName();
199: logger.log(BasicLevel.DEBUG, cName
200: + ".getSecurityUserName (" + mcf.getRes_Auth()
201: + ")userName=" + u);
202: return u;
203: } catch (Exception e) {
204: String u = mcf.getUserName(); // find default
205: logger
206: .log(
207: BasicLevel.DEBUG,
208: cName
209: + ".getSecurityUserName error: failed to find ManagedConnectionFactory "
210: + "instance containing userName. Using="
211: + u);
212: return u;
213: }
214: }
215:
216: }
|