KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fictional > resourceadapter > ConnectionImpl


1 /*
2  * Created on Jul 10, 2003
3  *
4  * ConnectionImpl.java is used to test the J2EE Connector
5  * as implemented by JOnAS. This class implements the Connection Interface
6  * and ConnectionMetaData Interface (cci) classes.
7  *
8  */

9 package fictional.resourceadapter;
10
11 import javax.resource.ResourceException;
12 import javax.resource.NotSupportedException;
13 import javax.resource.cci.*;
14 import javax.resource.spi.ManagedConnection;
15 import javax.resource.spi.ConnectionEvent;
16 import org.objectweb.jonas.common.Log;
17 import org.objectweb.util.monolog.api.Logger;
18 import org.objectweb.util.monolog.api.BasicLevel;
19
20 /**
21  * @author Bob Kruse
22  *
23  * Jtest Resource Adapter
24  *
25  * used to test the J2EE Connector as implemented by JOnAS.
26  *
27  */

28 public class ConnectionImpl
29         implements Connection,
30         ConnectionMetaData,
31         java.io.Serializable
32 {
33     public String product="Fictional EIS"; // see J2eeConnectorTestBeanClient
34
String version="1.1";
35     String UserName="Fictional_User_Name"; //
36
private ManagedConnection mc;
37     public LocalTransactionImpl lt;
38     boolean closed = false; // connection is closed
39
private Logger logger = null;
40     String cName = "";
41     private boolean autoCommitMode = false;
42
43     //
44
// *****************
45
// Connection methods
46
// *****************
47
//
48
public ConnectionImpl(Object mc) { // constructor for Connection
49
if (logger == null) {
50             logger = Log.getLogger("fictional.resourceadapter");
51         }
52         logger.log(BasicLevel.DEBUG, impl(this)+".constructor");
53         this.mc=(ManagedConnection)mc;
54     }
55     private String impl(Object obj) {
56         if (obj instanceof Connection) {
57             return "ConnectionImpl";
58         } else if (obj instanceof ConnectionImpl) {
59             return "Connection";
60         } else if (obj instanceof ConnectionMetaData) {
61             return "ConnectionMetaData";
62         } else
63             return "ConnectionImpl. Is this an error";
64     }
65     //
66
// close connection handle
67
//
68
public void close() throws ResourceException
69     {
70         logger.log(BasicLevel.DEBUG, impl(this)
71             +".close with ConnectionEvent.CONNECTION_CLOSED="
72             +ConnectionEvent.CONNECTION_CLOSED);
73         closed = true;
74         
75         if (mc != null) {
76             try {
77                 JtestResourceAdapter omc = (JtestResourceAdapter) mc;
78                 omc.sendEvent(ConnectionEvent.CONNECTION_CLOSED, null, this);
79                 mc=null;
80                 logger.log(BasicLevel.DEBUG, impl(this)
81                        +".close sendevent 'CONNECTION_CLOSED'");
82             } catch (ResourceException e) {
83                 logger.log(BasicLevel.DEBUG, impl(this)+".close error: unable to close");
84                 throw e;
85             }
86         } else {
87             logger.log(BasicLevel.DEBUG, impl(this)+".close error: mc=null");
88         }
89         return;
90     }
91     //
92
// close physical connection
93
//
94
// The connectionErrorOccurred method indicates that the associated
95
// ManagedConnection instance is now invalid and unusable.
96
// mc.sendEvent method makes the call to connectionErrorOccurred
97
//
98
public void close(int eType) throws ResourceException
99     {
100         logger.log(BasicLevel.DEBUG, impl(this)+".close("+eType+")");
101         closed = true;
102         
103         if (mc != null) {
104             try {
105                 JtestResourceAdapter omc = (JtestResourceAdapter) mc;
106                 omc.sendEvent(eType, null, this);
107                 mc=null;
108                 logger.log(BasicLevel.DEBUG, impl(this)
109                        +".close(CONNECTION_ERROR_OCCURRED="
110                        +ConnectionEvent.CONNECTION_ERROR_OCCURRED+") to "
111                        +"close physical connection");
112             } catch (ResourceException e) {
113                 logger.log(BasicLevel.DEBUG, impl(this)
114                        +".close error: unable to close physical connection"
115                        +" with 'CONNECTION_ERROR_OCCURRED'");
116                 throw e;
117             }
118         } else {
119             logger.log(BasicLevel.DEBUG, impl(this)+".close error: mc=null already");
120         }
121         return;
122     }
123     public Interaction createInteraction() throws ResourceException
124     {
125       logger.log(BasicLevel.DEBUG, impl(this)+".createInteraction");
126       Interaction gi=new JtestInteraction(this, mc);
127       // TODO Interaction gi = new Interaction(this, this.mc, lw);
128
return gi;
129     }
130     public ConnectionMetaData getMetaData() throws ResourceException
131     {
132         logger.log(BasicLevel.DEBUG, impl(this)+".getMetaData");
133         ConnectionMetaData eisInfo = (ConnectionMetaData)new ConnectionImpl(mc);
134         return eisInfo;
135     }
136     public LocalTransaction getLocalTransaction()
137                 throws ResourceException
138     {
139         try {
140             JtestResourceAdapter jmc = (JtestResourceAdapter)mc;
141             lt = (LocalTransactionImpl) jmc.getLocalTransaction(true);
142             logger.log(BasicLevel.DEBUG, impl(this)+".getLocalTransaction lt="+lt);
143         } catch (Exception e) {
144             logger.log(BasicLevel.DEBUG, impl(this)+".getLocalTransaction error: "
145                 +e.getMessage()+" <<<<<<<<<<<<");
146         }
147         return (lt);
148     }
149     public ResultSetInfo getResultSetInfo() throws ResourceException
150     {
151         logger.log(BasicLevel.DEBUG, impl(this)+".getResultSetInfo");
152         NotSupportedException nse = new NotSupportedException("getResultSetInfo is not supported");
153         throw nse;
154     }
155     public void associateConnection(ManagedConnection mc) throws IllegalStateException
156     {
157         logger.log(BasicLevel.DEBUG, impl(this)+".associateConnection");
158         this.mc=mc;
159     }
160     public ManagedConnection getMC() throws ResourceException
161     {
162         logger.log(BasicLevel.DEBUG, impl(this)+".getMC mc="+this.mc);
163         return this.mc;
164     }
165     public void setAutoCommit(boolean a) {
166         autoCommitMode=a;
167     }
168     public boolean getAutoCommit() {
169         return autoCommitMode;
170     }
171     //
172
// **********************
173
// ConnectionMetaData methods
174
// **********************
175
//
176
// The method getUserName returns the user name for an active connection as
177
// known to the underlying EIS instance. The name corresponds the resource
178
// principal under whose security context a connection to the EIS
179
// instance has been established. (page 109 CA 1.0)
180
//
181
public String getEISProductName()
182                                        throws ResourceException
183     {
184         return (product);
185     }
186     public String getEISProductVersion()
187                                           throws ResourceException
188     {
189         return (version);
190     }
191     public String getUserName()
192                                           throws ResourceException
193     {
194         return (UserName);
195     }
196 }
197
Popular Tags