001: /*
002: * Created on Jun 12, 2003
003: *
004: * CommonClient.java is used to test the J2EE Connector
005: * as implemented by JOnAS. This class implements some the Common Client Interface
006: * (cci) classes. Each cci method simulates actual functionality and returns test
007: * results to the caller which is a JUnit test program.
008: *
009: */
010: package fictional.resourceadapter;
011:
012: import javax.naming.Reference;
013: import javax.naming.NamingException;
014: import javax.resource.ResourceException;
015: import javax.resource.NotSupportedException;
016: import javax.resource.spi.ConnectionManager;
017: import javax.resource.spi.ManagedConnectionFactory;
018: import javax.resource.spi.ConnectionRequestInfo; // interfaces implemented in this class
019: import javax.resource.cci.ConnectionFactory;
020: import javax.resource.cci.Connection;
021: import javax.resource.cci.ConnectionSpec; // j2ee 1.5
022: import javax.resource.cci.RecordFactory;
023: import javax.resource.cci.ResourceAdapterMetaData;
024: import java.io.Serializable;
025: import javax.resource.Referenceable; // logger imports
026: import org.objectweb.jonas.common.Log;
027: import org.objectweb.util.monolog.api.Logger;
028: import org.objectweb.util.monolog.api.BasicLevel;
029:
030: /**
031: * @author Bob Kruse
032: *
033: * used to test the J2EE Connector as implemented by JOnAS.
034: *
035: */
036: public class CommonClient implements ConnectionFactory, ConnectionSpec, // j2ee 1.5
037: ResourceAdapterMetaData, Referenceable, Serializable
038:
039: {
040: Reference reference;
041: private ConnectionManager cm;
042: private ManagedConnectionFactory mcf; // used in cm.allocateConnection
043: private CommonClient cs; // ConnectionSpec
044: protected boolean managed = true;
045: private Logger logger = null;
046: private String userName = "";
047: private String passWord = "";
048: String cName = "";
049:
050: public CommonClient() {
051: if (logger == null) {
052: logger = Log.getLogger("fictional.resourceadapter");
053: }
054: }
055:
056: //
057: // *****************
058: // ConnectionSpec methods
059: // *****************
060: //
061: public void setUserName(String u) {
062: userName = u;
063: cName = "ConnectionSpec";
064: logger.log(BasicLevel.DEBUG, cName + ".setUserName=" + u);
065: }
066:
067: public void setPassword(String p) {
068: passWord = p;
069: cName = "ConnectionSpec";
070: logger.log(BasicLevel.DEBUG, cName + ".setPassword=" + p);
071: }
072:
073: public String getUserName() {
074: cName = "ConnectionSpec";
075: logger
076: .log(BasicLevel.DEBUG, cName + ".getUserName="
077: + userName);
078: return userName;
079: }
080:
081: public String getPassword() {
082: cName = "ConnectionSpec";
083: logger
084: .log(BasicLevel.DEBUG, cName + ".getPassword="
085: + passWord);
086: return passWord;
087: }
088:
089: //
090: // *****************
091: // ConnectionFactory methods
092: // *****************
093: //
094: // Referenced classes of package javax.resource.cci:
095: // Connection, ConnectionSpec, RecordFactory, ResourceAdapterMetaData
096: // (below is constructor for ConnectionFactory
097: //
098: // ConnectionFactory instance created during lookup() by Application Server
099: //
100: public CommonClient(ManagedConnectionFactory mcf,
101: ConnectionManager cm) {
102: if (logger == null) {
103: logger = Log.getLogger("fictional.resourceadapter");
104: }
105: this .mcf = mcf;
106: this .cm = cm;
107:
108: }
109:
110: private String impl(Object obj) {
111: if (obj instanceof ConnectionFactory) {
112: return "ConnectionFactory";
113: } else if (obj instanceof ConnectionSpec) {
114: return "ConnectionSpec";
115: } else if (obj instanceof ResourceAdapterMetaData) {
116: return "ResourceAdapterMetaData";
117: } else
118: return "CommonClient. Is this an error";
119:
120: }
121:
122: //
123: // Container Managed Sign-on calls getConnection() from the Application Component
124: //
125: // Container-managed sign-on when file "connector.xml / secured.jar" contains line below
126: //
127: // <res-auth>Container</res-auth>
128: //
129: public Connection getConnection() throws ResourceException {
130: cName = "ConnectionFactory";
131: logger.log(BasicLevel.DEBUG, cName + ".getConnection"
132: + " (Container Managed Sign-on)");
133: Connection conn = null;
134: try {
135: conn = (Connection) getConnection(null);
136: return conn;
137: } catch (ResourceException ex) {
138: throw ex;
139: }
140: }
141:
142: //
143: // Component Managed Sign-on calls getConnection(cs) directly from the Application Component
144: //
145: // Component-managed sign-on when file "connector.xml / secured.jar" contains line below
146: //
147: // <res-auth>Application</res-auth>
148: //
149: public Connection getConnection(ConnectionSpec connectionspec)
150: throws ResourceException {
151: cName = "ConnectionFactory";
152: JtestResourceAdapter jmcf = (JtestResourceAdapter) mcf; // ManagedConnectionFactory
153: Connection conn = null;
154: CommonClient cs; // ConnectionSpec
155: JtestResourceAdapter jcri = null; // ConnectionRequestInfo
156:
157: if (connectionspec == null) {
158: jmcf.setRes_Auth("Container");
159: logger.log(BasicLevel.DEBUG, cName
160: + ".getConnection detected res-auth='"
161: + jmcf.getRes_Auth() + "'");
162: // TODO what now? anything?
163: // now pass null to cm.allocateConnection(mcf, null);
164: } else {
165: jmcf.setRes_Auth("Application");
166: logger.log(BasicLevel.DEBUG, cName
167: + ".getConnection detected res-auth='"
168: + jmcf.getRes_Auth() + "'");
169: cs = (CommonClient) connectionspec;
170: // load user and password into ConnectionRequestInfo instance
171: jcri = new JtestResourceAdapter(); // ConnectionRequestInfo
172: jcri.setUserName(cs.getUserName());
173: jcri.setPassword(cs.getPassword());
174:
175: }
176: logger.log(BasicLevel.DEBUG, cName
177: + ".getConnection calling cm.allocateConnection");
178: try {
179: ConnectionRequestInfo cri = (ConnectionRequestInfo) jcri;
180: conn = (Connection) cm.allocateConnection(mcf, cri);
181: if (conn == null) {
182: logger.log(BasicLevel.DEBUG, cName
183: + ". getConnection, cm.allocateConnection"
184: + " error: Null connection object returned");
185: throw new ResourceException(
186: "Null connection object returned by allocateConnection");
187: }
188: return conn;
189: } catch (IllegalStateException is) {
190: logger.log(BasicLevel.DEBUG, cName
191: + ".getConnection IllegalStateException" + is);
192: throw is;
193: } catch (ResourceException re) {
194: logger.log(BasicLevel.DEBUG, cName
195: + ".getConnection ResourceException=" + re);
196: throw re;
197: }
198: }
199:
200: public RecordFactory getRecordFactory() throws ResourceException {
201: cName = "ConnectionFactory";
202: logger.log(BasicLevel.DEBUG, cName + ".getRecordFactory");
203: NotSupportedException nse = new NotSupportedException(
204: "RecordFactory is not currently supported");
205: throw nse;
206: }
207:
208: public ResourceAdapterMetaData getMetaData()
209: throws ResourceException {
210: cName = "ConnectionFactory";
211: logger.log(BasicLevel.DEBUG, cName + ".getMetaData");
212: ResourceAdapterMetaData rd = null; // TODO do something
213: return rd;
214: }
215:
216: public ManagedConnectionFactory getMcf() {
217: ManagedConnectionFactory MCF = (ManagedConnectionFactory) mcf;
218: return MCF;
219: }
220:
221: //
222: // *****************
223: // Reference
224: // *****************
225: //
226: /** Required by the referencable attribute
227: * @param ref Reference object
228: **/
229: public void setReference(javax.naming.Reference ref) {
230: this .reference = ref;
231: }
232:
233: /** Required by the referencable attribute
234: * @return Reference object
235: **/
236: public Reference getReference() throws NamingException {
237: return reference;
238: }
239:
240: //
241: // ***********************
242: // ResourceAdapterMetaData methods
243: // ***********************
244: //
245: public String getAdapterVersion() {
246: return "1.0";
247: }
248:
249: public String getAdapterVendorName() {
250: return "Bull";
251: }
252:
253: public String getAdapterName() {
254: return "JOnAS Test Resource Adapter";
255: }
256:
257: public String getAdapterShortDescription() {
258: return "Test JOnAS Application Server compliance to J2EE Java Community Process (JSR112)";
259: }
260:
261: public String getSpecVersion() {
262: return "J2EE Java Community Process (JSR112)";
263: }
264:
265: public String[] getInteractionSpecsSupported() {
266: String[] s = { "JSR016", "JSR112" };
267: return s;
268: }
269:
270: public boolean supportsExecuteWithInputAndOutputRecord() {
271: return true;
272: }
273:
274: public boolean supportsExecuteWithInputRecordOnly() {
275: return true;
276: }
277:
278: public boolean supportsLocalTransactionDemarcation() {
279: return false;
280: }
281: }
|