001: /*
002: * Created on December 11, 2003
003: *
004: * ManagedConnectionFactoryImpl.java is part of a Resource Adapter to test
005: * J2EE Connector 1.5 as implemented by JOnAS.
006: */
007: package ersatz.resourceadapter;
008:
009: import javax.naming.Reference;
010: import java.io.PrintWriter;
011: import java.util.Set;
012: import java.util.Timer;
013: import java.util.Iterator;
014: import java.util.Date;
015: import javax.security.auth.Subject;
016: import javax.resource.ResourceException;
017: import javax.resource.NotSupportedException;
018: import javax.resource.spi.ResourceAllocationException;
019: import javax.resource.spi.ManagedConnectionFactory;
020: import javax.resource.spi.ResourceAdapterAssociation;
021: import javax.resource.spi.ConnectionManager;
022: import javax.resource.spi.ConnectionRequestInfo;
023: import javax.resource.spi.ManagedConnection;
024: import javax.resource.spi.ResourceAdapter;
025: import javax.resource.spi.BootstrapContext;
026: import javax.resource.spi.security.PasswordCredential;
027: import java.io.Serializable;
028:
029: /**
030: * @author Bob Kruse
031: *
032: * used to test the J2EE Connector as implemented by JOnAS.
033: *
034: */
035: public class ManagedConnectionFactoryImpl implements
036: ManagedConnectionFactory, ResourceAdapterAssociation,
037: Serializable {
038: Reference reference;
039: public String defaultUserName = ""; // used in ManagedConnectionImpl, ConnectionRequestInfo
040: public String defaultPassword = ""; // set by "getter" method
041:
042: // loaded by Application Server
043: ResourceAdapter ra = null;
044: String ServerName = "";
045: String PortNumber = "";
046: String userName = "";
047: String password = "";
048: String protocol = "";
049:
050: public ConnectionManager cm; // loaded by ManagedConnectionFactory
051: private ManagedConnectionFactoryImpl mcf;
052: private ConnectionRequestInfoImpl crii;
053: private ResourceAdapterImpl rai = null; // re-cast at the same time
054: private BootstrapContext bootstrapCtx; // used to test timer
055: private boolean raLoaded = false;
056: PrintWriter pw; // App Server sets to null by default
057: boolean forceMatchNull; // in matchManagedConnections() when debugging
058: public String res_auth; // set by ConnectionFactory.getConnection()
059: // then put into ManagedConnection when created
060: String mfId;
061: String cName = "ManagedConnectionFactoryImpl";
062:
063: //
064: public ManagedConnectionFactoryImpl() {
065: crii = new ConnectionRequestInfoImpl();
066: pw = null;
067: }
068:
069: public Object createConnectionFactory(
070: ConnectionManager connectionmanager)
071: throws ResourceException {
072: cm = connectionmanager;
073: Utility.log(cName + ".createConnectionFactory cm=" + cm);
074: Object obj;
075: obj = new ConnectionFactoryImpl(this , cm); // creates ConnectionFactory
076: return obj;
077: }
078:
079: /**
080: *
081: * When the createConnectionFactory method takes no arguments, the resource adapter
082: * provides a default ConnectionManager instance. This case is used in a non-managed
083: * application scenario.
084: */
085: public Object createConnectionFactory() throws ResourceException {
086: Utility.log(cName + ": error - non-managed detected");
087: throw new NotSupportedException(
088: "A non-managed two-tier application environment is not supported in J2EE testing.");
089: }
090:
091: public ManagedConnection createManagedConnection(Subject subject,
092: ConnectionRequestInfo CRI) throws ResourceException {
093: ConnectionRequestInfoImpl info = (ConnectionRequestInfoImpl) CRI;
094: Utility.log(cName + ".createManagedConnection subject="
095: + subject + ", cri=" + info);
096: mcf = this ;
097: ManagedConnectionImpl mc;
098: if (subject != null) {
099: //
100: // The application server user's password to the EIS is carried in Subject
101: //
102: try {
103: PasswordCredential pc = Utility.getPasswordCredential(
104: this , subject, info);
105: if (pc == null) {
106: Utility
107: .log(cName
108: + ".createManagedConnection PasswordCredential = null, Subject not null.");
109: } else {
110: if (pc.getUserName() == null
111: || pc.getUserName().length() == 0) {
112: Utility
113: .log(cName
114: + ".createManagedConnection userName=empty");
115: } else {
116: userName = new String(pc.getUserName());
117: Utility.log(cName + ".createManagedConnection"
118: + " PasswordCredential userName="
119: + userName);
120: password = new String(pc.getPassword()); // pc.password is char[]
121: Utility.log(cName + ".createManagedConnection"
122: + " PasswordCredential password="
123: + password);
124: }
125: }
126: } catch (Exception e) {
127: Utility
128: .log(cName
129: + ".createManagedConnection getPasswordCredential "
130: + "error: e=" + e.toString());
131: }
132: }
133:
134: if (info != null) {
135: //
136: // The client application component user's password is carried in info
137: // Use defaults if userName or password is empty
138: //
139: String s = info.getUserName();
140: if (s.length() > 0)
141: userName = s;
142: else
143: Utility.log(cName + ".createManagedConnection"
144: + " ConnectionRequestInfo userName=" + s
145: + ". Use default=" + userName);
146: Utility.log(cName + ".createManagedConnection"
147: + " ConnectionRequestInfo userName=" + userName);
148: s = info.getPassword();
149: if (s.length() > 0)
150: password = s;
151: else
152: Utility.log(cName + ".createManagedConnection"
153: + " ConnectionRequestInfo password=" + s
154: + ". Use default=" + password);
155: Utility.log(cName + ".createManagedConnection"
156: + " ConnectionRequestInfo password=" + password);
157: }
158:
159: if ((subject == null && info == null) || userName == "") {
160: //
161: // The default user's password is carried in the ManagedConnectionFactory instance
162: //
163: userName = defaultUserName;
164: password = defaultPassword;
165: Utility.log(cName
166: + ".createManagedConnection default userName="
167: + userName);
168: Utility.log(cName
169: + ".createManagedConnection default password="
170: + password);
171: }
172: ConnectionRequestInfoImpl mcCrii = new ConnectionRequestInfoImpl(
173: crii);
174: mcCrii.setPassword(password);
175: mcCrii.setUserName(userName);
176: try {
177: //
178: // Create the ManagedConnectionImpl Instance
179: //
180: mc = new ManagedConnectionImpl(mcCrii);
181: mc.setMcf(mcf);
182: mc.setRes_Auth(res_auth);
183: mc.setLogWriter(pw);
184: Utility.log(cName + ".createManagedConnection for eis="
185: + rai.getEIS_URL() + " mc=" + mc);
186: return mc;
187: } catch (Exception ex) {
188: Utility
189: .log(cName
190: + ".createManagedConnection : error - Exception ex="
191: + ex.getMessage());
192: throw new ResourceAllocationException(ex.getMessage());
193: }
194: }
195:
196: public void setMatchNull(boolean truefalse) { // nobody calls this
197: forceMatchNull = truefalse;
198: }
199:
200: public ManagedConnection matchManagedConnections(Set connectionSet,
201: Subject subject, ConnectionRequestInfo info)
202: throws ResourceException {
203: String mName = ".matchManagedConnections";
204: Utility.log(cName + mName + " Set=" + connectionSet
205: + " Subject=" + subject + " Crinfo=" + info);
206: ManagedConnectionImpl mc = null;
207: if (forceMatchNull) {
208: Utility.log(cName + mName + " force new connection");
209: return mc; // forces new connection
210: }
211:
212: ConnectionRequestInfoImpl mcCrii = null;
213: ConnectionRequestInfoImpl curCrii = new ConnectionRequestInfoImpl(
214: crii);
215: ManagedConnectionImpl wmc = null;
216: Iterator it = connectionSet.iterator();
217: int cnt = 0;
218: while (it.hasNext()) {
219: Object obj = it.next();
220: Utility.log(cName + mName
221: + " find next ManagedConnectionImpl in Set cnt="
222: + (++cnt) + " obj=" + obj);
223: if (obj instanceof ManagedConnectionImpl) {
224: // see if ManagedConnectionImpl is available
225: // i.e., no connection handle exists for mc instance
226: // i.e., the connection is closed
227: // TODO choose one of the above
228: wmc = (ManagedConnectionImpl) obj;
229: mcCrii = wmc.getCrii();
230: if (curCrii.equals(mcCrii)) {
231: if (wmc.getCHandle() == null || wmc.isClosed()) {
232: mc = wmc;
233: Utility
234: .log(cName
235: + mName
236: + " connection handle == null. Set cnt="
237: + cnt + " matched mc=" + wmc);
238:
239: } else {
240: // error: connection should not exist J2ee 1.0 pg. 32
241: String s = "connection handle should not exist";
242: Utility.log(cName + mName + "error: " + s
243: + ". connectionSet cnt=" + cnt);
244: }
245: }
246:
247: }
248: }
249: if (mc == null)
250: Utility.log(cName + mName
251: + " mc=null No match found. Set cnt=" + cnt);
252: else
253: Utility.log(cName + mName + " match found mc=" + mc);
254: return mc;
255: }
256:
257: //
258: // The setLogWriter and getLogWriter
259: // the Application Server calls ManagedConnectionImpl.setLogWriter(pw)
260: //
261: public void setLogWriter(PrintWriter printwriter)
262: throws ResourceException {
263: Utility.log(cName + ".setLogWriter mcfi.pw=" + pw
264: + " ApplicationServer's PrintWriter=" + printwriter);
265: if (printwriter != null)
266: pw = printwriter;
267: }
268:
269: public PrintWriter getLogWriter() throws ResourceException {
270: return pw;
271: }
272:
273: public ResourceAdapter getResourceAdapter() {
274: return ra;
275: }
276:
277: /** Set the ResourceAdapter association.
278: *
279: * @param ra ResourceAdapter passed in by Application Server (JOnAS)
280: * @exception javax.resource.ResourceException
281: **/
282: public void setResourceAdapter(ResourceAdapter ra)
283: throws ResourceException {
284: Utility.log(cName + ".setResourceAdapter arg=" + ra);
285: if (ra == null) {
286: String s = "Application Server cannot set ResourceAdapter to null";
287: Utility.log(cName + ".setResourceAdapter error: " + s);
288: throw new ResourceException(s);
289: }
290: if (!raLoaded) {
291: this .ra = ra;
292: rai = (ResourceAdapterImpl) ra;
293: crii.setRaName(rai.getEIS_URL());
294: raLoaded = true; // load exactly once
295: } else {
296: if (this .ra == ra) {
297: String s = "setResoruceAdapter called again by the Application "
298: + "Server with same ResourceAdapter value.";
299: Utility.log(cName + ".setResourceAdapter error: " + s);
300: throw new ResourceException(s);
301: } else {
302: String s = "ResourceAdapter already set to " + this .ra
303: + ". Must not change association";
304: Utility.log(cName + ".setResourceAdapter error: " + s);
305: throw new ResourceException(s);
306: }
307: }
308: }
309:
310: //
311: // config-property processing "setter" properties
312: //
313: /** Set the JOnAS config-property.. Default value is "".
314: *
315: * @param c1 String of config-property.
316: * for testng RA.
317: **/
318: public void setServerName(String c1) {
319: ServerName = c1;
320: crii.setServerName(ServerName);
321: //Utility.log(cName+".setServerName="+ServerName);
322: }
323:
324: public String getServerName() {
325: return ServerName;
326: }
327:
328: public void setPortNumber(String s) {
329: PortNumber = s;
330: crii.setPortNumber(PortNumber);
331: //Utility.log(cName+".setPortNumber="+PortNumber);
332: }
333:
334: public String getPortNumber() {
335: return PortNumber;
336: }
337:
338: public void setDUserName(String s) {
339: defaultUserName = s;
340: crii.setUserName(s);
341: //Utility.log(cName+".setDUserName="+defaultUserName);
342: }
343:
344: public String getDUserName() {
345: return defaultUserName;
346: }
347:
348: public void setDPassword(String s) {
349: defaultPassword = s;
350: crii.setPassword(s);
351: //Utility.log(cName+".setDPassword="+defaultPassword);
352: }
353:
354: public String getDPassword() {
355: return defaultPassword;
356: }
357:
358: public void setProtocol(String s) {
359: protocol = s;
360: crii.setProtocol(protocol);
361: //Utility.log(cName+".setProtocol="+protocol);
362: }
363:
364: public String getProtocol() {
365: return protocol;
366: }
367:
368: public ConnectionManager getCM() {
369: return cm;
370: }
371:
372: public String getRes_Auth() {
373: return res_auth;
374: }
375:
376: public void setRes_Auth(String r) {
377: res_auth = r;
378: }
379:
380: //
381: // hashCode and equals method used in ConnectionRequestInfo,
382: // ManagedConnectionFactory
383: public int hashCode() {
384: if (mfId == null || mfId.length() == 0) {
385: crii.hashCode();
386: mfId = crii.getId();
387: }
388: int hash = mfId.hashCode();
389: return hash;
390: }
391:
392: public boolean equals(Object other) {
393: boolean match = false;
394: if (other == null) {
395: return match;
396: }
397: if (other instanceof ManagedConnectionFactoryImpl) {
398: ManagedConnectionFactoryImpl otherMcf = (ManagedConnectionFactoryImpl) other;
399: try {
400: if (hashCode() != otherMcf.hashCode()) {
401: Utility.log(cName + ".equals(" + otherMcf
402: + ") return=" + match);
403: return false;
404: }
405: match = crii.equals(otherMcf.crii);
406: } catch (Exception e) {
407: Utility.log(cName + ".equals(" + otherMcf
408: + ") Exception=" + e);
409: return false;
410: }
411: }
412: return match;
413: }
414:
415: /* 10.3.7
416: A resource adapter may not be able to directly create a Timer instance, if it does
417: not have adequate runtime permissions to create threads. This is because the Timer
418: instance starts a background thread. In such a case, the resource adapter can
419: instead use the BootstrapContext instance to obtain a Timer instance from the
420: application server.
421: */
422: public boolean isTimerWorking() {
423: ResourceAdapterImpl rai = (ResourceAdapterImpl) ra;
424: try {
425: if (rai.bootstrapCtx == null) {
426: throw new Exception("bootstrapCtx=null");
427: }
428: Timer timer = rai.bootstrapCtx.createTimer(); // get a Timer instance
429: Utility.log(cName + ".isTimerWorking Timer was created. ");
430: return true;
431: } catch (Exception e) {
432: Utility.log(cName + ".isTimerWorking Error: " + e);
433: return false;
434: }
435: }
436: }
|