001: // RuntimeCASLR.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.resource.cci.Connection;
017: import javax.resource.spi.ManagedConnection;
018: import javax.resource.spi.ConnectionManager;
019: import ersatz.resourceadapter.*;
020: import javax.resource.spi.ConnectionEvent;
021:
022: /*
023: */
024: public class RuntimeCASLR implements SessionBean {
025:
026: SessionContext ejbContext;
027: private ManagedConnectionFactoryImpl mcf = null; //Managed Connection Factory
028: private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
029: private ConnectionImpl conn = null;
030: private ConnectionSpecImpl csp = null; //ConnectionSpec
031: private ConnectionRequestInfoImpl crii = null;
032: InitialContext ic = null;
033: private String res_auth = "";
034: String cName = "RuntimeCASLR";
035:
036: // ------------------------------------------------------------------
037: // SessionBean implementation
038: // ------------------------------------------------------------------
039:
040: public void setSessionContext(SessionContext ctx) {
041: Utility.log(cName + ".setSessionContext");
042: ejbContext = ctx;
043: }
044:
045: public void ejbRemove() {
046: Utility.log(cName + ".ejbRemove");
047: }
048:
049: public void ejbCreate() throws CreateException {
050: Utility.log(cName + ".ejbCreate");
051: }
052:
053: public void ejbPassivate() {
054: Utility.log(cName + ".ejbPassivate");
055: }
056:
057: public void ejbActivate() {
058: Utility.log(cName + ".ejbActivate");
059: }
060:
061: // ------------------------------------------------------------------
062: // Runtime implementation
063: // ------------------------------------------------------------------
064: public void setResAuth(String ra) {
065: res_auth = ra; // set to Application or Container
066: }
067:
068: /**
069: * method1
070: */
071: public void method1(String rar_jndi_name, String testName)
072: throws Exception {
073: Utility.log("============================ " + testName);
074: Utility.log(cName + ".method1 : lookup " + rar_jndi_name);
075: try {
076: ic = new InitialContext();
077: } catch (Exception e1) {
078: Utility
079: .log(cName
080: + ".method1 error: InitialContext failed");
081: throw e1;
082: }
083: try {
084: cccf = (ConnectionFactoryImpl) ic.lookup(rar_jndi_name);
085: Utility.log(cName + ".method1 : found " + rar_jndi_name);
086: } catch (Exception e2) {
087: Utility.log(cName + ".method1 error: lookup failed for "
088: + rar_jndi_name);
089: throw e2;
090: }
091:
092: //
093: // Component-managed sign-on when file "runtime.xml" contains line below
094: //
095: // <res-auth>Application</res-auth>
096: //
097: try {
098: csp = new ConnectionSpecImpl(); // get a new ConnectionSpecImpl
099: csp.setUserName("Ersatz_User_Name");
100: csp.setPassword("__Jtest_Pass_word__");
101: Utility
102: .log(cName
103: + ".method1 : ConnectionSpecImpl + Ersatz_User_Name,__Jtest_Pass_word__");
104: } catch (Exception e3) {
105: Utility
106: .log(cName
107: + ".method1 : new connection spec failed");
108: throw e3;
109: }
110: try {
111: conn = (ConnectionImpl) cccf.getConnection();
112: if (conn == null) {
113: Utility
114: .log(cName
115: + ".method1 error: getConnection returned null connection.");
116: throw new Exception("");
117: }
118: } catch (Exception e4) {
119: Utility.log(cName + ".method1 error: getConnection failed "
120: + e4.toString());
121: throw e4;
122: }
123: }
124:
125: /**
126: * closeUp
127: */
128: public void closeUp(int w) {
129: try {
130: if (w > 0) {
131: // The CONNECTION_ERROR_OCCURRED indicates that the associated
132: // ManagedConnectionImpl instance is now invalid and unusable.
133: conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
134: Utility.log(cName
135: + ".closeUp : closed physical connection");
136: } else {
137: // The CONNECTION_CLOSED indicates that connection handle
138: // is closed, but physical connection still exists
139: conn.close();
140: Utility.log(cName + ".closeUp : closed connection");
141: }
142: } catch (Exception e) {
143: Utility
144: .log(cName
145: + ".closeUp error: close handle/physical connection failed");
146: }
147: }
148:
149: /**
150: * JUnit tests
151: */
152: public int getMCF_Pwriter() {
153: int here = 2;
154: mcf = cccf.getMcf(); // ManagedConnectionFactory
155: try {
156: if (mcf.getLogWriter() == null) { // PrintWriter is null
157: Utility
158: .log(cName
159: + ".getMCF_Pwriter No PrintWriter registered in "
160: + "ManagedConnectionFactoryImpl");
161: here = 0;
162: } else {
163: Utility.log(cName
164: + ".getMCF_Pwriter PrintWriter is o.k.in "
165: + "ManagedConnectionFactoryImpl");
166: here = 1;
167: }
168: } catch (Exception e) {
169: Utility.log(cName + ".getMCF_Pwriter error: "
170: + e.toString());
171: }
172: return here;
173: }
174:
175: public int getMC_Pwriter() {
176: int here = 2;
177: try {
178: ManagedConnectionImpl mc = conn.getMC();
179: if (mc.getLogWriter() == null) { // PrintWriter not null
180: Utility
181: .log(cName
182: + ".getMC_Pwriter No PrintWriter registered in ManagedConnectionImpl");
183: here = 0;
184: } else {
185: Utility
186: .log(cName
187: + ".getMC_Pwriter PrintWriter in ManagedConnectionImpl is o.k.");
188: here = 1;
189: }
190: } catch (Exception e) {
191: Utility
192: .log(cName + ".getMC_Pwriter error: "
193: + e.toString());
194: }
195: return here;
196: }
197:
198: public String getResAuth() {
199: try {
200: ManagedConnectionImpl mc = conn.getMC();
201: String ra = mc.res_auth; // get real "Application" or "Container"
202: Utility.log(cName + ".getResAuth " + "<res-auth>" + ra
203: + "</res-auth>");
204: if (ra == null || ra.length() == 0)
205: throw new Exception("");
206: return ra;
207: } catch (Exception e) {
208: Utility.log(cName
209: + ".getResAuth error: failed to find <res-auth> "
210: + "in ManagedConnectionImpl");
211: return "";
212: }
213: }
214:
215: public String getSecurityPassword() {
216: crii = conn.crii; // look at ConnectionRequestInfoImpl
217: try {
218: ManagedConnectionImpl mc = conn.getMC();
219: String pw = crii.getPassword();
220: Utility.log(cName + ".getSecurityPassword (" + mc.res_auth
221: + ")password=" + pw);
222: if (pw == null || pw.length() == 0)
223: throw new Exception("");
224: return pw;
225: } catch (Exception e) {
226: mcf = cccf.getMcf();
227: String pw = mcf.defaultPassword; // find default
228: Utility
229: .log(cName
230: + ".getSecurityPassword error: failed to find ConnectionRequestInfoImpl "
231: + "instance containing password. Using default="
232: + pw);
233: return pw;
234: }
235: }
236:
237: public String getSecurityUserName() {
238: crii = conn.crii; // look at ConnectionRequestInfoImpl
239: try {
240: ManagedConnectionImpl mc = conn.getMC();
241: String u = crii.getUserName();
242: Utility.log(cName + ".getSecurityUserName ("
243: + mcf.getRes_Auth() + ")userName=" + u);
244: if (u == null || u.length() == 0)
245: throw new Exception("");
246: return u;
247: } catch (Exception e) {
248: mcf = cccf.getMcf();
249: String u = mcf.defaultUserName; // find default
250: Utility
251: .log(cName
252: + ".getSecurityUserName error: failed to find ConnectionRequestInfoImpl "
253: + "instance containing userName. Using default="
254: + u);
255: return u;
256: }
257: }
258: }
|