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