KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > jca > JalistoManagedConnectionFactory


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.jca;
25
26 import org.objectweb.jalisto.se.impl.trace.Trace;
27 import org.objectweb.jalisto.se.impl.trace.TraceNull;
28 import org.objectweb.jalisto.se.JalistoFactory;
29 import org.objectweb.jalisto.se.api.JalistoProperties;
30
31 import javax.resource.ResourceException JavaDoc;
32 import javax.resource.spi.*;
33 import javax.security.auth.Subject JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Set JavaDoc;
37
38 public class JalistoManagedConnectionFactory implements ManagedConnectionFactory {
39
40     public void setHashCode(int code) {
41         hashCode = code;
42     }
43
44     public Trace getTrace() {
45         return trace;
46     }
47
48     private String JavaDoc getCfIdentification() {
49         return String.valueOf(connectionDriverName) +
50                String.valueOf(connectionURL) +
51                String.valueOf(connectionUserName) +
52                String.valueOf(connectionPassword);
53     }
54
55     private void initTrace() {
56         if (connectionURL != null) {
57             JalistoProperties prop = JalistoFactory.getInternalFactory().getProperties(connectionURL);
58             trace = JalistoFactory.getInternalFactory().getTracer(prop);
59         } else {
60             trace = new TraceNull();
61         }
62         JalistoXARPool.setTrace(trace);
63         JalistoObjectFactory.setTrace(trace);
64     }
65
66     private void out(Object JavaDoc o) {
67         String JavaDoc message = TRACE_ID + " " + o.toString();
68         trace.println(Trace.JCA, message);
69     }
70
71
72     /*********************************** Properties methods *******************************************/
73
74     public void setConnectionDriverName(String JavaDoc s) {
75         connectionDriverName = s;
76         if (jalistoCf != null) {
77             jalistoCf.setConnectionDriverName(s);
78         }
79     }
80
81     public String JavaDoc getConnectionDriverName() {
82         return jalistoCf.getConnectionDriverName();
83     }
84
85     public void setConnectionPassword(String JavaDoc connectionPassword) {
86         this.connectionPassword = connectionPassword;
87         if (jalistoCf != null) {
88             jalistoCf.setConnectionPassword(connectionPassword);
89         }
90     }
91
92     public String JavaDoc getConnectionPassword() {
93         return connectionPassword;
94     }
95
96     public void setConnectionURL(String JavaDoc s) {
97         connectionURL = s;
98
99         if (jalistoCf != null) {
100             jalistoCf.setConnectionURL(s);
101         }
102     }
103
104     public String JavaDoc getConnectionURL() {
105         return jalistoCf.getConnectionURL();
106     }
107
108     public void setConnectionUserName(String JavaDoc connectionUserName) {
109         this.connectionUserName = connectionUserName;
110         if (jalistoCf != null) {
111             jalistoCf.setConnectionUserName(connectionUserName);
112         }
113     }
114
115     public String JavaDoc getConnectionUserName() {
116         return connectionUserName;
117     }
118
119     /************************* ManagedConnectionFactory implementation *******************************/
120
121     public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc {
122         return createConnectionFactory(new JalistoConnectionManager());
123     }
124
125     public Object JavaDoc createConnectionFactory(ConnectionManager manager) throws ResourceException JavaDoc {
126         try {
127             initTrace();
128
129             if (jalistoCf == null) {
130                 jalistoCf = new JalistoConnectionFactory(manager, this);
131
132                 if (connectionDriverName != null) {
133                     jalistoCf.setConnectionDriverName(connectionDriverName);
134                 }
135                 if (connectionURL != null) {
136                     jalistoCf.setConnectionURL(connectionURL);
137                 }
138                 if (connectionUserName != null) {
139                     jalistoCf.setConnectionUserName(connectionUserName);
140                 }
141                 if (connectionPassword != null) {
142                     jalistoCf.setConnectionPassword(connectionPassword);
143                 }
144             }
145             out("createConnectionFactory(ConnectionManager)");
146             out("connectionDriverName = " + connectionDriverName);
147             out("connectionURL = " + connectionURL);
148             out("connectionUserName = " + connectionUserName);
149         } catch (Exception JavaDoc e) {
150             e.printStackTrace();
151             throw new ResourceException JavaDoc("Cannot create ConnectionFactory");
152         }
153         JalistoXARPool.setJdoConnectionFactory(getCfIdentification(), jalistoCf);
154         return jalistoCf;
155     }
156
157     // info may be null
158
public ManagedConnection createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo info)
159             throws ResourceException JavaDoc {
160         out("createManagedConnection");
161         out("with Subject : " + String.valueOf(subject));
162         out("with info : " + String.valueOf(info));
163         JalistoManagedConnection managedConnection = new JalistoManagedConnection(connectionURL, info, trace);
164         managedConnection.setLogWriter(getLogWriter());
165         managedConnection.setCfIdentification(getCfIdentification());
166         return managedConnection;
167     }
168
169     public ManagedConnection matchManagedConnections(Set JavaDoc set, Subject JavaDoc subject, ConnectionRequestInfo info)
170             throws ResourceException JavaDoc {
171         out("matchManagedConnections");
172         out("with info : " + String.valueOf(info));
173         out("with set : " + String.valueOf(set));
174         JalistoManagedConnection jdoManagedConnection;
175         for (Iterator JavaDoc iterator = set.iterator(); iterator.hasNext();) {
176             jdoManagedConnection = (JalistoManagedConnection) iterator.next();
177             if (jdoManagedConnection.matchConnectionRequestInfo(info) && jdoManagedConnection.isAvalaible()) {
178                 jdoManagedConnection.setNotAvalaible();
179                 out("matchManagedConnections : return " + String.valueOf(jdoManagedConnection));
180                 return jdoManagedConnection;
181             }
182         }
183         out("matchManagedConnections : return null");
184         return null;
185     }
186
187     public void setLogWriter(PrintWriter JavaDoc writer) throws ResourceException JavaDoc {
188         if (writer != null) {
189             logger = writer;
190         }
191     }
192
193     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
194         return logger;
195     }
196
197     // hashcode devrait être égal si les connection... le sont
198
public int hashCode() {
199         if (hashCode == 0) {
200             hashCode = super.hashCode();
201         }
202         return hashCode;
203     }
204
205     public boolean equals(Object JavaDoc o) {
206         if (o instanceof JalistoManagedConnectionFactory) {
207             JalistoManagedConnectionFactory candidat = (JalistoManagedConnectionFactory) o;
208             return (connectionURL.equals(candidat.connectionURL) &&
209                     connectionDriverName.equals(candidat.connectionDriverName) &&
210                     connectionUserName.equals(candidat.connectionUserName) &&
211                     connectionPassword.equals(candidat.connectionPassword));
212         }
213         return false;
214     }
215
216
217     private JalistoConnectionFactory jalistoCf = null;
218     private PrintWriter JavaDoc logger = null;
219     private Trace trace = new TraceNull();
220
221     private String JavaDoc connectionDriverName;
222     private String JavaDoc connectionURL; // properties file name
223
private String JavaDoc connectionUserName;
224     private String JavaDoc connectionPassword;
225
226     private int hashCode = 0;
227
228     private static final String JavaDoc TRACE_ID = "[JalistoManagedConnectionFactory]";
229 }
230
Popular Tags