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