KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > jca15 > ConnectorCASLR


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

4 package org.objectweb.jonas.jtests.beans.jca15;
5
6 import java.rmi.RemoteException JavaDoc;
7 import javax.ejb.CreateException JavaDoc;
8 import javax.ejb.EJBException JavaDoc;
9 import javax.ejb.RemoveException JavaDoc;
10 import javax.ejb.EJBObject JavaDoc;
11 import javax.ejb.SessionBean JavaDoc;
12 import javax.ejb.SessionContext JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16 import javax.resource.cci.Connection JavaDoc;
17 import javax.resource.cci.ConnectionMetaData JavaDoc;
18 import javax.resource.spi.ManagedConnection JavaDoc;
19 import ersatz.resourceadapter.*;
20 import javax.resource.spi.ConnectionEvent JavaDoc;
21
22 /**
23  * -
24  */

25 public class ConnectorCASLR implements SessionBean JavaDoc {
26
27     SessionContext JavaDoc ejbContext;
28     private ConnectionFactoryImpl cccf = null; //Common Client Connection Factory
29
private ManagedConnectionFactoryImpl mcf = null; //Managed Connection Factory
30
private ConnectionSpecImpl csp = null; //ConnectionSpec
31
private ConnectionImpl conn = null;
32     InitialContext JavaDoc ic=null;
33     InteractionImpl i = null;
34     ConnectionMetaDataImpl cMetaData = null;
35     String JavaDoc cName = "ConnectorCASLR";
36
37     // ------------------------------------------------------------------
38
// SessionBean implementation
39
// ------------------------------------------------------------------
40

41
42     public void setSessionContext(SessionContext JavaDoc ctx) {
43         Utility.log(cName+".setSessionContext");
44         ejbContext = ctx;
45     }
46         
47
48     public void ejbRemove() {
49         Utility.log(cName+".ejbRemove");
50     }
51         
52
53     public void ejbCreate() throws CreateException JavaDoc {
54         Utility.log(cName+".ejbCreate");
55     }
56
57     public void ejbPassivate() {
58         Utility.log(cName+".ejbPassivate");
59     }
60
61     public void ejbActivate() {
62         Utility.log(cName+".ejbActivate");
63     }
64     
65     // ------------------------------------------------------------------
66
// ConnectorCA implementation
67
// ------------------------------------------------------------------
68
/**
69      * method1
70      */

71     public void method1(String JavaDoc rar_jndi_name, String JavaDoc testName)
72             throws Exception JavaDoc
73     {
74         Utility.log("============================ "+testName);
75         try {
76             ic = new InitialContext JavaDoc();
77         } catch (Exception JavaDoc e1) {
78             Utility.log(cName+".method1 error: InitialContext failed");
79             throw e1;
80         }
81         try {
82             cccf = (ConnectionFactoryImpl)ic.lookup(rar_jndi_name);
83             Utility.log(cName+".method1 : found "+rar_jndi_name);
84         } catch (Exception JavaDoc e2) {
85             Utility.log(cName+".method1 error: lookup failed for "+rar_jndi_name);
86             throw e2;
87         }
88         
89         try {
90             csp = new ConnectionSpecImpl(); // get a new ConnectionSpec
91
Utility.log(cName
92                   +".method1 : ConnectionSpec o.k.");
93             
94         } catch (Exception JavaDoc e3) {
95             Utility.log(cName+".method1 : new connection spec failed");
96             throw e3;
97         }
98         try {
99             conn = (ConnectionImpl)cccf.getConnection();
100             Utility.log(cName+".method1 : getConnection conn="+conn);
101             if (conn==null) {
102                 Utility.log(cName+".method1 error: getConnection returned null connection.");
103                 throw new Exception JavaDoc("");
104             }
105         } catch (Exception JavaDoc e4) {
106             Utility.log(cName+".method1 error: getConnection failed "
107                     +e4.toString());
108             throw e4;
109         }
110         try {
111             i = (InteractionImpl)conn.createInteraction();
112         } catch (Exception JavaDoc e5) {
113             Utility.log(cName+".method1 error: create interaction failed");
114             throw e5;
115         }
116         try {
117             cMetaData = (ConnectionMetaDataImpl)conn.getMetaData();
118         } catch (Exception JavaDoc e6) {
119             Utility.log(cName+".method1 : get ConnectionMetaData failed");
120             throw e6;
121         }
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
// ManagedConnection instance is now invalid and unusable.
133
conn.close(ConnectionEvent.CONNECTION_ERROR_OCCURRED);
134                 Utility.log(cName+".closeUp : closed physical connection");
135             } else {
136                 // The CONNECTION_CLOSED indicates that connection handle
137
// is closed, but physical connection still exists
138
conn.close();
139                 Utility.log(cName+".closeUp : closed connection");
140             }
141         } catch (Exception JavaDoc e) {
142             Utility.log(cName+".closeUp error: close handle/physical connection failed");
143         }
144     }
145     /**
146      * JUnit tests
147      */

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

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

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