KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > connector > OTMJCAManagedConnectionFactory


1 package org.apache.ojb.otm.connector;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.PBKey;
19 import org.apache.ojb.otm.Kit;
20 import org.apache.ojb.otm.OTMConnection;
21 import org.apache.ojb.otm.kit.SimpleKit;
22
23 import javax.resource.ResourceException JavaDoc;
24 import javax.resource.spi.ConnectionManager JavaDoc;
25 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
26 import javax.resource.spi.ManagedConnection JavaDoc;
27 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
28 import javax.security.auth.Subject JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.sql.DriverManager JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Set JavaDoc;
34
35 /**
36  *
37  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird<a>
38  */

39
40 public class OTMJCAManagedConnectionFactory
41         implements ManagedConnectionFactory JavaDoc, Serializable JavaDoc
42 {
43     private Kit m_kit;
44
45     private synchronized void initialize()
46     {
47         if (m_kit == null)
48         {
49             m_kit = SimpleKit.getInstance();
50         }
51     }
52
53     public Kit getKit() throws ResourceException JavaDoc
54     {
55         initialize();
56         return m_kit;
57     }
58
59     public OTMJCAManagedConnectionFactory()
60     {
61         Util.log("In OTMJCAManagedConnectionFactory.constructor");
62     }
63
64     public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cxManager) throws ResourceException JavaDoc
65     {
66         Util.log("In OTMJCAManagedConnectionFactory.createConnectionFactory,1");
67         return new JCAKit(this, cxManager);
68     }
69
70     public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc
71     {
72         Util.log("In OTMJCAManagedConnectionFactory.createManagedFactory,2");
73         return new JCAKit(this, null);
74     }
75
76     /**
77      * return a new managed connection. This connection is wrapped around the real connection and delegates to it
78      * to get work done.
79      * @param subject
80      * @param info
81      * @return
82      */

83     public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info)
84     {
85         Util.log("In OTMJCAManagedConnectionFactory.createManagedConnection");
86         try
87         {
88             Kit kit = getKit();
89             PBKey key = ((OTMConnectionRequestInfo) info).getPbKey();
90             OTMConnection connection = kit.acquireConnection(key);
91             return new OTMJCAManagedConnection(this, connection, key);
92         }
93         catch (ResourceException JavaDoc e)
94         {
95             throw new OTMConnectionRuntimeException(e.getMessage());
96         }
97     }
98
99     public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc connectionSet, Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info)
100             throws ResourceException JavaDoc
101     {
102         Util.log("OTMJCAManagedConnectionFactory::matchManagedConnections called with " + connectionSet.size() + " connections.");
103         for (Iterator JavaDoc i = connectionSet.iterator(); i.hasNext();)
104         {
105             Object JavaDoc o = i.next();
106             if (o instanceof OTMJCAManagedConnection)
107             {
108                 // all idle connections are identical
109
return (OTMJCAManagedConnection) o;
110             }
111         }
112         Util.log("OTMJCAManagedConnectionFactory::No matched connections");
113         return null;
114     }
115
116     public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc
117     {
118         Util.log("In OTMJCAManagedConnectionFactory.setLogWriter");
119     }
120
121     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
122     {
123         Util.log("In OTMJCAManagedConnectionFactory.getLogWriter");
124         return DriverManager.getLogWriter();
125     }
126
127     public boolean equals(Object JavaDoc obj)
128     {
129         if (obj == null)
130             return false;
131         if (obj instanceof OTMJCAManagedConnectionFactory)
132         {
133             int hash1 = ((OTMJCAManagedConnectionFactory) obj).hashCode();
134             int hash2 = hashCode();
135             return hash1 == hash2;
136         }
137         else
138         {
139             return false;
140         }
141     }
142
143     public int hashCode()
144     {
145         return 1;
146     }
147 }
148
Popular Tags