001: /*
002: * Created on December 12, 2003
003: *
004: * ConnectionFactoryImpl.java is used to test the J2EE Connector
005: * as implemented by JOnAS.
006: *
007: */
008: package ersatz.resourceadapter;
009:
010: import javax.naming.Reference;
011: import javax.naming.NamingException;
012: import javax.resource.ResourceException;
013: import javax.resource.NotSupportedException;
014: import javax.resource.spi.ConnectionManager; // interfaces implemented in this class
015: import javax.resource.cci.ConnectionFactory;
016: import javax.resource.cci.Connection;
017: import javax.resource.cci.ConnectionSpec; // j2ee 1.5
018: import javax.resource.cci.RecordFactory;
019: import javax.resource.cci.ResourceAdapterMetaData;
020: import java.io.Serializable;
021: import javax.resource.Referenceable;
022:
023: /**
024: * @author Bob Kruse
025: *
026: * used to test the J2EE Connector as implemented by JOnAS.
027: *
028: */
029: public class ConnectionFactoryImpl implements ConnectionFactory,
030: Referenceable, Serializable
031:
032: {
033: Reference reference;
034: private ConnectionManager cm;
035: private ManagedConnectionFactoryImpl mcf; // used in cm.allocateConnection
036: private ConnectionSpecImpl cs; // ConnectionSpec
037: protected boolean managed = true;
038: private String userName = "";
039: private String passWord = "";
040: String cName = "ConnectionFactoryImpl";
041:
042: //
043: // ConnectionFactory instance created during lookup() by Application Server
044: //
045: public ConnectionFactoryImpl() {
046: }
047:
048: public ConnectionFactoryImpl(ManagedConnectionFactoryImpl mcf,
049: ConnectionManager cm) {
050: this .mcf = mcf;
051: this .cm = cm;
052:
053: }
054:
055: //
056: // Container Managed Sign-on calls getConnection() from the Application Component
057: //
058: // Container-managed sign-on when deployment file contains line below
059: //
060: // <res-auth>Container</res-auth>
061: //
062: public Connection getConnection() throws ResourceException {
063: Utility.log(cName + ".getConnection"
064: + " (Container Managed Sign-on)");
065: ConnectionImpl conn = null;
066: try {
067: conn = (ConnectionImpl) getConnection(null);
068: return conn;
069: } catch (ResourceException ex) {
070: throw ex;
071: }
072: }
073:
074: //
075: // Component Managed Sign-on calls getConnection(cs) directly from the Application Component
076: //
077: // Component-managed sign-on when file "connector.xml / secured.jar" contains line below
078: //
079: // <res-auth>Application</res-auth>
080: //
081: public Connection getConnection(ConnectionSpec connectionspec)
082: throws ResourceException {
083: ConnectionImpl conn = null;
084: ConnectionSpecImpl cs = null;
085: ConnectionRequestInfoImpl jcri = null; //
086:
087: if (connectionspec == null) {
088: mcf.setRes_Auth("Container");
089: Utility.log(cName + ".getConnection detected res-auth='"
090: + mcf.getRes_Auth() + "'");
091: // TODO what now? anything?
092: // now pass null to cm.allocateConnection(mcf, null);
093: } else {
094: mcf.setRes_Auth("Application");
095: Utility.log(cName + ".getConnection detected res-auth='"
096: + mcf.getRes_Auth() + "'");
097: cs = (ConnectionSpecImpl) connectionspec;
098: // load user and password into ConnectionRequestInfo instance
099: jcri = new ConnectionRequestInfoImpl();
100: jcri.setUserName(cs.getUserName());
101: jcri.setPassword(cs.getPassword());
102:
103: }
104: Utility.log(cName
105: + ".getConnection calling cm.allocateConnection");
106: try {
107: conn = (ConnectionImpl) cm.allocateConnection(mcf, jcri);
108: if (conn == null) {
109: Utility.log(cName
110: + ". getConnection, cm.allocateConnection"
111: + " error: Null connection object returned");
112: throw new ResourceException(
113: "Null connection object returned by allocateConnection");
114: }
115: conn.crii = jcri;
116: return conn;
117: } catch (IllegalStateException is) {
118: Utility.log(cName + ".getConnection IllegalStateException"
119: + is);
120: throw is;
121: } catch (ResourceException re) {
122: Utility.log(cName + ".getConnection ResourceException="
123: + re);
124: throw re;
125: }
126: }
127:
128: public RecordFactory getRecordFactory() throws ResourceException {
129: NotSupportedException nse = new NotSupportedException(
130: "RecordFactory is not currently supported");
131: Utility.log(cName + ".getRecordFactory " + nse);
132: throw nse;
133: }
134:
135: public ResourceAdapterMetaData getMetaData()
136: throws ResourceException {
137: Utility.log(cName + ".getMetaData");
138: ResourceAdapterMetaData rd = null; // TODO do something
139: return rd;
140: }
141:
142: public ConnectionManager getCM() {
143: return cm;
144: }
145:
146: public ManagedConnectionFactoryImpl getMcf() {
147: return mcf;
148: }
149:
150: //
151: // *****************
152: // Reference
153: // *****************
154: //
155: /** Required by the referencable attribute
156: * @param ref Reference object
157: **/
158: public void setReference(javax.naming.Reference ref) {
159: this .reference = ref;
160: }
161:
162: /** Required by the referencable attribute
163: * @return Reference object
164: **/
165: public Reference getReference() throws NamingException {
166: return reference;
167: }
168: }
|