KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > j2eeca > connectorCASLR


1 // connectorCASLR.java
2
// Stateless Session bean
3

4 package org.objectweb.jonas.jtests.beans.j2eeca;
5
6 import javax.ejb.CreateException JavaDoc;
7 import javax.ejb.SessionBean JavaDoc;
8 import javax.ejb.SessionContext JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.resource.cci.ConnectionMetaData JavaDoc;
11 import javax.resource.spi.ConnectionEvent JavaDoc;
12
13 import org.objectweb.jonas.common.Log;
14 import org.objectweb.util.monolog.api.BasicLevel;
15 import org.objectweb.util.monolog.api.Logger;
16
17 import fictional.resourceadapter.CommonClient;
18 import fictional.resourceadapter.ConnectionImpl;
19 import fictional.resourceadapter.JtestInteraction;
20 import fictional.resourceadapter.JtestResourceAdapter;
21
22 /**
23  * -
24  */

25 public class connectorCASLR implements SessionBean JavaDoc {
26
27     static private Logger logger = null;
28     SessionContext JavaDoc ejbContext;
29     private CommonClient cccf = null; //Common Client Connection Factory
30
private JtestResourceAdapter mcf = null; //Managed Connection Factory
31
private CommonClient csp = null; //ConnectionSpec
32
private ConnectionImpl conn = null;
33     InitialContext JavaDoc ic=null;
34     JtestInteraction i = null;
35     ConnectionMetaData JavaDoc cMetaData = null;
36     String JavaDoc cName = "connectorCASLR";
37
38     // ------------------------------------------------------------------
39
// SessionBean implementation
40
// ------------------------------------------------------------------
41

42
43     public void setSessionContext(SessionContext JavaDoc ctx) {
44         if (logger == null) {
45             logger = Log.getLogger("org.objectweb.jonas.jtests.j2eeca");
46         }
47         logger.log(BasicLevel.DEBUG, cName+".setSessionContext");
48         ejbContext = ctx;
49     }
50         
51
52     public void ejbRemove() {
53         logger.log(BasicLevel.DEBUG, cName+".ejbRemove");
54     }
55         
56
57     public void ejbCreate() throws CreateException JavaDoc {
58         logger.log(BasicLevel.DEBUG, cName+".ejbCreate");
59     }
60
61     public void ejbPassivate() {
62         logger.log(BasicLevel.DEBUG, cName+".ejbPassivate");
63     }
64
65     public void ejbActivate() {
66         logger.log(BasicLevel.DEBUG, cName+".ejbActivate");
67     }
68     
69     // ------------------------------------------------------------------
70
// connectorCA implementation
71
// ------------------------------------------------------------------
72
/**
73      * method1
74      */

75     public void method1(String JavaDoc rar_jndi_name, String JavaDoc testName)
76             throws Exception JavaDoc
77     {
78         logger.log(BasicLevel.DEBUG, "============================ "+testName);
79         try {
80             ic = new InitialContext JavaDoc();
81         } catch (Exception JavaDoc e1) {
82             logger.log(BasicLevel.DEBUG, cName+".method1 error: InitialContext failed");
83             throw e1;
84         }
85         try {
86             cccf = (CommonClient)ic.lookup(rar_jndi_name);
87             logger.log(BasicLevel.DEBUG, cName+".method1 : found "+rar_jndi_name);
88         } catch (Exception JavaDoc e2) {
89             logger.log(BasicLevel.DEBUG, cName+".method1 error: lookup failed for "+rar_jndi_name);
90             throw e2;
91         }
92         
93         try {
94             csp = new CommonClient(); // get a new ConnectionSpec
95
logger.log(BasicLevel.DEBUG, cName
96                   +".method1 : ConnectionSpec o.k.");
97             
98         } catch (Exception JavaDoc e3) {
99             logger.log(BasicLevel.DEBUG, cName+".method1 : new connection spec failed");
100             throw e3;
101         }
102         try {
103             conn = (ConnectionImpl)cccf.getConnection();
104             logger.log(BasicLevel.DEBUG, cName+".method1 : getConnection conn="+conn);
105             if (conn==null) {
106                 logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection returned null connection.");
107                 throw new Exception JavaDoc("");
108             }
109         } catch (Exception JavaDoc e4) {
110             logger.log(BasicLevel.DEBUG, cName+".method1 error: getConnection failed "
111                     +e4.toString());
112             throw e4;
113         }
114         try {
115             i = (JtestInteraction)conn.createInteraction();
116         } catch (Exception JavaDoc e5) {
117             logger.log(BasicLevel.DEBUG, cName+".method1 error: create interaction failed");
118             throw e5;
119         }
120         try {
121             cMetaData = (ConnectionMetaData JavaDoc)conn.getMetaData();
122         } catch (Exception JavaDoc e6) {
123             logger.log(BasicLevel.DEBUG, cName+".method1 : get ConnectionMetaData failed");
124             throw e6;
125         }
126
127     }
128
129     /**
130      * closeUp
131      */

132     public void closeUp(int w) {
133         try {
134             if (w>0) {
135                 // The CONNECTION_ERROR_OCCURRED indicates that the associated
136
// ManagedConnection instance is now invalid and unusable.
137
conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
138                 logger.log(BasicLevel.DEBUG, cName+".closeUp : closed physical connection");
139             } else {
140                 // The CONNECTION_CLOSED indicates that connection handle
141
// is closed, but physical connection still exists
142
conn.close();
143                 logger.log(BasicLevel.DEBUG, cName+".closeUp : closed connection");
144             }
145         } catch (Exception JavaDoc e) {
146             logger.log(BasicLevel.DEBUG, cName+".closeUp error: close handle/physical connection failed");
147         }
148     }
149     /**
150      * JUnit tests
151      */

152     public boolean isConnectionSpec() {
153         if (csp==null) return false;
154         else return true;
155     }
156     public boolean isConnection() {
157         if (conn==null) return false;
158         else return true;
159     }
160     public boolean isInteraction() {
161         if (i==null) return false;
162         else return true;
163     }
164     public String JavaDoc getConnectionProduct()
165             throws Exception JavaDoc
166     {
167         String JavaDoc s;
168         try {
169             s = cMetaData.getEISProductName();
170             logger.log(BasicLevel.DEBUG, cName+".getConnectionProduct : ConnectionMetaData.product="+s);
171         } catch (Exception JavaDoc e) {
172             logger.log(BasicLevel.DEBUG, cName+".getConnectionProduct error: ConnectionMetaData.getEISProductName() failed");
173             throw e;
174         }
175         return s;
176     }
177     /**
178      * The application server calls setter methods on the ManagedConnectionFactory instance
179      * to set various configuration properties on this instance. These properties provide
180      * information required by the ManagedConnectionFactory instance to create physical
181      * connections to the underlying EIS. The application server uses an existing property set
182      * (configured during the deployment of a resource adapter) to set the required properties on
183      * the ManagedConnectionFactory instance.
184      *
185      */

186     public String JavaDoc getServerName()
187             throws Exception JavaDoc
188     {
189         logger.log(BasicLevel.DEBUG, cName+".getServerName");
190         mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
191
String JavaDoc cp1 = "null";
192         if (mcf==null) return cp1;
193         try {
194             cp1 = mcf.getServerName();
195         } catch (Exception JavaDoc e) {
196             logger.log(BasicLevel.DEBUG, cName+".getConfigurationProperty error: failed");
197             throw e;
198         }
199         return cp1;
200     }
201     public String JavaDoc getProtocolProperty()
202             throws Exception JavaDoc
203     {
204         logger.log(BasicLevel.DEBUG, cName+".getProtocolProperty");
205         mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
206
String JavaDoc p = "null";
207         if (mcf==null) return p;
208         try {
209             p = mcf.getProtocol();
210         } catch (Exception JavaDoc e) {
211             logger.log(BasicLevel.DEBUG, cName+".getProtocolProperty error: failed");
212             throw e;
213         }
214         return p;
215     }
216     /**
217      * After the newly created ManagedConnectionFactory instance has been configured with
218      * its property set, the application server creates a new ConnectionManager instance.
219      * true returned if ConnectionManager is valid
220      */

221     public boolean getCMInstance()
222     {
223         mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
224
if (mcf.getCM()==null) { // ConnectionManager not null
225
logger.log(BasicLevel.DEBUG, cName+".getCMInstance error: ConnectionManager is null");
226             return false;
227         }
228         else {
229             logger.log(BasicLevel.DEBUG, cName+".getCMInstance ConnectionManager is o.k.");
230             return true;
231         }
232     }
233     public int getMCF_Pwriter()
234     {
235         int here=2;
236         mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
237
try {
238             if (mcf.getLogWriter()==null) { // PrintWriter not null
239
logger.log(BasicLevel.DEBUG, cName+".getMCF_Pwriter No PrintWriter registered");
240                 here=0;
241             }
242             else {
243                 logger.log(BasicLevel.DEBUG, cName+".getMCF_Pwriter PrintWriter is o.k.");
244                 here=1;
245             }
246         } catch (Exception JavaDoc e) {
247             logger.log(BasicLevel.DEBUG, cName+
248                 ".getMCF_Pwriter error: "+e.toString());
249         }
250         return here;
251     }
252     public int getMC_Pwriter()
253     {
254         int here=2;
255         ConnectionImpl conni = (ConnectionImpl)conn;
256         try {
257             JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
258
if (mc.getLogWriter()==null) { // PrintWriter not null
259
logger.log(BasicLevel.DEBUG, cName+
260                     ".getMC_Pwriter No PrintWriter registered in ManagedConnection");
261                 here=0;
262             }
263             else {
264                 logger.log(BasicLevel.DEBUG, cName+
265                     ".getMC_Pwriter PrintWriter in ManagedConnection is o.k.");
266                 here=1;
267             }
268         } catch (Exception JavaDoc e) {
269             logger.log(BasicLevel.DEBUG, cName+
270                 ".getMC_Pwriter error: "+e.toString());
271         }
272         return here;
273     }
274     public void setMatchNull(boolean b) {
275         mcf = (JtestResourceAdapter) cccf.getMcf(); // ManagedConnectionFactory
276
mcf.setMatchNull(b);
277     }
278     public int cntListeners()
279     {
280         int i = 0;
281         ConnectionImpl conni = (ConnectionImpl)conn;
282         try {
283              JtestResourceAdapter mc = (JtestResourceAdapter)conni.getMC(); //get ManagedConnection
284
i = mc.cntListeners();
285             logger.log(BasicLevel.DEBUG, cName+".cntListeners cnt="+i);
286         } catch (Exception JavaDoc e) {
287             logger.log(BasicLevel.DEBUG, cName+".cntListeners error: failed to count Listeners");
288         }
289         return i;
290     }
291 }
292
293
Popular Tags