001: // ConnectorCASLR.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.cci.ConnectionMetaData;
018: import javax.resource.spi.ManagedConnection;
019: import ersatz.resourceadapter.*;
020: import javax.resource.spi.ConnectionEvent;
021:
022: /**
023: * -
024: */
025: public class ConnectorCASLR implements SessionBean {
026:
027: SessionContext ejbContext;
028: private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
029: private ManagedConnectionFactoryImpl mcf = null; //Managed Connection Factory
030: private ConnectionSpecImpl csp = null; //ConnectionSpec
031: private ConnectionImpl conn = null;
032: InitialContext ic = null;
033: InteractionImpl i = null;
034: ConnectionMetaDataImpl cMetaData = null;
035: String cName = "ConnectorCASLR";
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(cName + ".ejbRemove");
048: }
049:
050: public void ejbCreate() throws CreateException {
051: Utility.log(cName + ".ejbCreate");
052: }
053:
054: public void ejbPassivate() {
055: Utility.log(cName + ".ejbPassivate");
056: }
057:
058: public void ejbActivate() {
059: Utility.log(cName + ".ejbActivate");
060: }
061:
062: // ------------------------------------------------------------------
063: // ConnectorCA implementation
064: // ------------------------------------------------------------------
065: /**
066: * method1
067: */
068: public void method1(String rar_jndi_name, String testName)
069: throws Exception {
070: Utility.log("============================ " + testName);
071: try {
072: ic = new InitialContext();
073: } catch (Exception e1) {
074: Utility
075: .log(cName
076: + ".method1 error: InitialContext failed");
077: throw e1;
078: }
079: try {
080: cccf = (ConnectionFactoryImpl) ic.lookup(rar_jndi_name);
081: Utility.log(cName + ".method1 : found " + rar_jndi_name);
082: } catch (Exception e2) {
083: Utility.log(cName + ".method1 error: lookup failed for "
084: + rar_jndi_name);
085: throw e2;
086: }
087:
088: try {
089: csp = new ConnectionSpecImpl(); // get a new ConnectionSpec
090: Utility.log(cName + ".method1 : ConnectionSpec o.k.");
091:
092: } catch (Exception e3) {
093: Utility
094: .log(cName
095: + ".method1 : new connection spec failed");
096: throw e3;
097: }
098: try {
099: conn = (ConnectionImpl) cccf.getConnection();
100: Utility
101: .log(cName + ".method1 : getConnection conn="
102: + conn);
103: if (conn == null) {
104: Utility
105: .log(cName
106: + ".method1 error: getConnection returned null connection.");
107: throw new Exception("");
108: }
109: } catch (Exception e4) {
110: Utility.log(cName + ".method1 error: getConnection failed "
111: + e4.toString());
112: throw e4;
113: }
114: try {
115: i = (InteractionImpl) conn.createInteraction();
116: } catch (Exception e5) {
117: Utility.log(cName
118: + ".method1 error: create interaction failed");
119: throw e5;
120: }
121: try {
122: cMetaData = (ConnectionMetaDataImpl) conn.getMetaData();
123: } catch (Exception e6) {
124: Utility.log(cName
125: + ".method1 : get ConnectionMetaData failed");
126: throw e6;
127: }
128:
129: }
130:
131: /**
132: * closeUp
133: */
134: public void closeUp(int w) {
135: try {
136: if (w > 0) {
137: // The CONNECTION_ERROR_OCCURRED indicates that the associated
138: // ManagedConnection instance is now invalid and unusable.
139: conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
140: Utility.log(cName
141: + ".closeUp : closed physical connection");
142: } else {
143: // The CONNECTION_CLOSED indicates that connection handle
144: // is closed, but physical connection still exists
145: conn.close();
146: Utility.log(cName + ".closeUp : closed connection");
147: }
148: } catch (Exception e) {
149: Utility
150: .log(cName
151: + ".closeUp error: close handle/physical connection failed");
152: }
153: }
154:
155: /**
156: * JUnit tests
157: */
158: public boolean isConnectionSpec() {
159: if (csp == null)
160: return false;
161: else
162: return true;
163: }
164:
165: public boolean isConnection() {
166: if (conn == null)
167: return false;
168: else
169: return true;
170: }
171:
172: public boolean isInteraction() {
173: if (i == null)
174: return false;
175: else
176: return true;
177: }
178:
179: public String getConnectionProduct() throws Exception {
180: String s;
181: try {
182: s = cMetaData.getEISProductName();
183: Utility
184: .log(cName
185: + ".getConnectionProduct : ConnectionMetaData.product="
186: + s);
187: } catch (Exception e) {
188: Utility
189: .log(cName
190: + ".getConnectionProduct error: ConnectionMetaData.getEISProductName() failed");
191: throw e;
192: }
193: return s;
194: }
195:
196: /**
197: * The application server calls setter methods on the ManagedConnectionFactory instance
198: * to set various configuration properties on this instance. These properties provide
199: * information required by the ManagedConnectionFactory instance to create physical
200: * connections to the underlying EIS. The application server uses an existing property set
201: * (configured during the deployment of a resource adapter) to set the required properties on
202: * the ManagedConnectionFactory instance.
203: *
204: */
205: public String getServerName() throws Exception {
206: Utility.log(cName + ".getServerName");
207: mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactoryImpl
208: String cp1 = "null";
209: if (mcf == null)
210: return cp1;
211: try {
212: cp1 = mcf.getServerName();
213: } catch (Exception e) {
214: Utility.log(cName
215: + ".getConfigurationProperty error: failed");
216: throw e;
217: }
218: return cp1;
219: }
220:
221: public String getProtocolProperty() throws Exception {
222: Utility.log(cName + ".getProtocolProperty");
223: mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactory
224: String p = "null";
225: if (mcf == null)
226: return p;
227: try {
228: p = mcf.getProtocol();
229: } catch (Exception e) {
230: Utility.log(cName + ".getProtocolProperty error: failed");
231: throw e;
232: }
233: return p;
234: }
235:
236: /**
237: * After the newly created ManagedConnectionFactory instance has been configured with
238: * its property set, the application server creates a new ConnectionManager instance.
239: * true returned if ConnectionManager is valid
240: */
241: public boolean getCMInstance() {
242: mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactory
243: if (mcf.getCM() == null) { // ConnectionManager not null
244: Utility
245: .log(cName
246: + ".getCMInstance error: ConnectionManager is null");
247: return false;
248: } else {
249: Utility.log(cName
250: + ".getCMInstance ConnectionManager is o.k.");
251: return true;
252: }
253: }
254:
255: public int getMCF_Pwriter() {
256: int here = 2;
257: mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactory
258: try {
259: if (mcf.getLogWriter() == null) { // PrintWriter not null
260: Utility.log(cName
261: + ".getMCF_Pwriter No PrintWriter registered");
262: here = 0;
263: } else {
264: Utility.log(cName
265: + ".getMCF_Pwriter PrintWriter is o.k.");
266: here = 1;
267: }
268: } catch (Exception e) {
269: Utility.log(cName + ".getMCF_Pwriter error: "
270: + e.toString());
271: }
272: return here;
273: }
274:
275: public int getMC_Pwriter() {
276: int here = 2;
277: ConnectionImpl conni = (ConnectionImpl) conn;
278: try {
279: ManagedConnectionImpl mc = (ManagedConnectionImpl) conni
280: .getMC(); //get ManagedConnection
281: if (mc.getLogWriter() == null) { // PrintWriter not null
282: Utility
283: .log(cName
284: + ".getMC_Pwriter No PrintWriter registered in ManagedConnection");
285: here = 0;
286: } else {
287: Utility
288: .log(cName
289: + ".getMC_Pwriter PrintWriter in ManagedConnection is o.k.");
290: here = 1;
291: }
292: } catch (Exception e) {
293: Utility
294: .log(cName + ".getMC_Pwriter error: "
295: + e.toString());
296: }
297: return here;
298: }
299:
300: public void setMatchNull(boolean b) {
301: mcf = (ManagedConnectionFactoryImpl) cccf.getMcf(); // ManagedConnectionFactory
302: mcf.setMatchNull(b);
303: }
304:
305: public int cntListeners() {
306: int i = 0;
307: ConnectionImpl conni = (ConnectionImpl) conn;
308: try {
309: ManagedConnectionImpl mc = (ManagedConnectionImpl) conni
310: .getMC(); //get ManagedConnection
311: i = mc.cntListeners();
312: Utility.log(cName + ".cntListeners cnt=" + i);
313: } catch (Exception e) {
314: Utility.log(cName
315: + ".cntListeners error: failed to count Listeners");
316: }
317: return i;
318: }
319: }
|