001: package org.apache.ojb.otm.connector;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.PBKey;
019: import org.apache.ojb.otm.Kit;
020: import org.apache.ojb.otm.OTMConnection;
021: import org.apache.ojb.otm.kit.SimpleKit;
022:
023: import javax.resource.ResourceException;
024: import javax.resource.spi.ConnectionManager;
025: import javax.resource.spi.ConnectionRequestInfo;
026: import javax.resource.spi.ManagedConnection;
027: import javax.resource.spi.ManagedConnectionFactory;
028: import javax.security.auth.Subject;
029: import java.io.PrintWriter;
030: import java.io.Serializable;
031: import java.sql.DriverManager;
032: import java.util.Iterator;
033: import java.util.Set;
034:
035: /**
036: *
037: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird<a>
038: */
039:
040: public class OTMJCAManagedConnectionFactory implements
041: ManagedConnectionFactory, Serializable {
042: private Kit m_kit;
043:
044: private synchronized void initialize() {
045: if (m_kit == null) {
046: m_kit = SimpleKit.getInstance();
047: }
048: }
049:
050: public Kit getKit() throws ResourceException {
051: initialize();
052: return m_kit;
053: }
054:
055: public OTMJCAManagedConnectionFactory() {
056: Util.log("In OTMJCAManagedConnectionFactory.constructor");
057: }
058:
059: public Object createConnectionFactory(ConnectionManager cxManager)
060: throws ResourceException {
061: Util
062: .log("In OTMJCAManagedConnectionFactory.createConnectionFactory,1");
063: return new JCAKit(this , cxManager);
064: }
065:
066: public Object createConnectionFactory() throws ResourceException {
067: Util
068: .log("In OTMJCAManagedConnectionFactory.createManagedFactory,2");
069: return new JCAKit(this , null);
070: }
071:
072: /**
073: * return a new managed connection. This connection is wrapped around the real connection and delegates to it
074: * to get work done.
075: * @param subject
076: * @param info
077: * @return
078: */
079: public ManagedConnection createManagedConnection(Subject subject,
080: ConnectionRequestInfo info) {
081: Util
082: .log("In OTMJCAManagedConnectionFactory.createManagedConnection");
083: try {
084: Kit kit = getKit();
085: PBKey key = ((OTMConnectionRequestInfo) info).getPbKey();
086: OTMConnection connection = kit.acquireConnection(key);
087: return new OTMJCAManagedConnection(this , connection, key);
088: } catch (ResourceException e) {
089: throw new OTMConnectionRuntimeException(e.getMessage());
090: }
091: }
092:
093: public ManagedConnection matchManagedConnections(Set connectionSet,
094: Subject subject, ConnectionRequestInfo info)
095: throws ResourceException {
096: Util
097: .log("OTMJCAManagedConnectionFactory::matchManagedConnections called with "
098: + connectionSet.size() + " connections.");
099: for (Iterator i = connectionSet.iterator(); i.hasNext();) {
100: Object o = i.next();
101: if (o instanceof OTMJCAManagedConnection) {
102: // all idle connections are identical
103: return (OTMJCAManagedConnection) o;
104: }
105: }
106: Util
107: .log("OTMJCAManagedConnectionFactory::No matched connections");
108: return null;
109: }
110:
111: public void setLogWriter(PrintWriter out) throws ResourceException {
112: Util.log("In OTMJCAManagedConnectionFactory.setLogWriter");
113: }
114:
115: public PrintWriter getLogWriter() throws ResourceException {
116: Util.log("In OTMJCAManagedConnectionFactory.getLogWriter");
117: return DriverManager.getLogWriter();
118: }
119:
120: public boolean equals(Object obj) {
121: if (obj == null)
122: return false;
123: if (obj instanceof OTMJCAManagedConnectionFactory) {
124: int hash1 = ((OTMJCAManagedConnectionFactory) obj)
125: .hashCode();
126: int hash2 = hashCode();
127: return hash1 == hash2;
128: } else {
129: return false;
130: }
131: }
132:
133: public int hashCode() {
134: return 1;
135: }
136: }
|