Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MuleManagedConnectionFactory |
|
| 2.4375;2.438 |
1 | /* | |
2 | * $Id: MuleManagedConnectionFactory.java 19191 2010-08-25 21:05:23Z tcarlson $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | ||
11 | package org.mule.module.jca; | |
12 | ||
13 | import java.beans.PropertyChangeListener; | |
14 | import java.beans.PropertyChangeSupport; | |
15 | import java.io.IOException; | |
16 | import java.io.ObjectInputStream; | |
17 | import java.io.PrintWriter; | |
18 | import java.util.Iterator; | |
19 | import java.util.Set; | |
20 | ||
21 | import javax.resource.ResourceException; | |
22 | import javax.resource.spi.ConnectionManager; | |
23 | import javax.resource.spi.ConnectionRequestInfo; | |
24 | import javax.resource.spi.ManagedConnection; | |
25 | import javax.resource.spi.ManagedConnectionFactory; | |
26 | import javax.resource.spi.security.PasswordCredential; | |
27 | import javax.security.auth.Subject; | |
28 | ||
29 | import org.apache.commons.logging.Log; | |
30 | import org.apache.commons.logging.LogFactory; | |
31 | ||
32 | /** | |
33 | * <code>MuleManagedConnectionFactory</code> TODO | |
34 | */ | |
35 | ||
36 | public class MuleManagedConnectionFactory implements ManagedConnectionFactory | |
37 | { | |
38 | /** | |
39 | * Serial version | |
40 | */ | |
41 | private static final long serialVersionUID = -1460847590293644271L; | |
42 | ||
43 | private transient PrintWriter out; | |
44 | 0 | private transient PropertyChangeSupport changes = new PropertyChangeSupport(this); |
45 | ||
46 | // userName property value | |
47 | 0 | private String username = null; |
48 | ||
49 | // password property value | |
50 | 0 | private String password = null; |
51 | ||
52 | /** | |
53 | * logger used by this class | |
54 | */ | |
55 | 0 | protected transient Log logger = LogFactory.getLog(this.getClass()); |
56 | ||
57 | public MuleManagedConnectionFactory() | |
58 | { | |
59 | 0 | super(); |
60 | 0 | } |
61 | ||
62 | private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException | |
63 | { | |
64 | 0 | in.defaultReadObject(); |
65 | 0 | this.logger = LogFactory.getLog(this.getClass()); |
66 | 0 | this.changes = new PropertyChangeSupport(this); |
67 | 0 | this.out = null; |
68 | 0 | } |
69 | ||
70 | public int hashCode() | |
71 | { | |
72 | 0 | final int PRIME = 31; |
73 | 0 | int result = 1; |
74 | 0 | result = PRIME * result + ((password == null) ? 0 : password.hashCode()); |
75 | 0 | return PRIME * result + ((username == null) ? 0 : username.hashCode()); |
76 | } | |
77 | ||
78 | public boolean equals(Object obj) | |
79 | { | |
80 | 0 | if (this == obj) |
81 | { | |
82 | 0 | return true; |
83 | } | |
84 | ||
85 | 0 | if (obj == null) |
86 | { | |
87 | 0 | return false; |
88 | } | |
89 | ||
90 | 0 | if (this.getClass() != obj.getClass()) |
91 | { | |
92 | 0 | return false; |
93 | } | |
94 | ||
95 | 0 | final MuleManagedConnectionFactory other = (MuleManagedConnectionFactory)obj; |
96 | ||
97 | 0 | if (password == null) |
98 | { | |
99 | 0 | if (other.password != null) |
100 | { | |
101 | 0 | return false; |
102 | } | |
103 | } | |
104 | 0 | else if (!password.equals(other.password)) |
105 | { | |
106 | 0 | return false; |
107 | } | |
108 | ||
109 | 0 | if (username == null) |
110 | { | |
111 | 0 | if (other.username != null) |
112 | { | |
113 | 0 | return false; |
114 | } | |
115 | } | |
116 | 0 | else if (!username.equals(other.username)) |
117 | { | |
118 | 0 | return false; |
119 | } | |
120 | ||
121 | 0 | return true; |
122 | } | |
123 | ||
124 | /** | |
125 | * Creates a Connection Factory instance. The ConnectionFactory instance is | |
126 | * initialized with the passed ConnectionManager. In the managed scenario, | |
127 | * ConnectionManager is provided by the application server. | |
128 | * | |
129 | * @param cxManager ConnectionManager to be associated with created EIS | |
130 | * connection factory instance | |
131 | * @return EIS-specific Connection Factory instance | |
132 | * @throws javax.resource.ResourceException if the attempt to create a connection | |
133 | * factory fails | |
134 | */ | |
135 | ||
136 | public Object createConnectionFactory(ConnectionManager cxManager) throws ResourceException | |
137 | { | |
138 | try | |
139 | { | |
140 | 0 | return new DefaultMuleConnectionFactory(this, cxManager, null); |
141 | } | |
142 | 0 | catch (Exception e) |
143 | { | |
144 | 0 | throw new ResourceException(e); |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
149 | * Creates a Connection Factory instance. The Connection Factory instance is | |
150 | * initialized with a default ConnectionManager. In the non-managed scenario, the | |
151 | * ConnectionManager is provided by the resource adapter. | |
152 | * | |
153 | * @return EIS-specific Connection Factory instance | |
154 | * @throws ResourceException if the attempt to create a connection factory fails | |
155 | */ | |
156 | ||
157 | public Object createConnectionFactory() throws ResourceException | |
158 | { | |
159 | 0 | return new DefaultMuleConnectionFactory(this, null, null); |
160 | } | |
161 | ||
162 | /** | |
163 | * ManagedConnectionFactory uses the security information (passed as Subject) and | |
164 | * additional ConnectionRequestInfo (which is specific to ResourceAdapter and | |
165 | * opaque to application server) to create this new connection. | |
166 | * | |
167 | * @param subject caller's security information | |
168 | * @param cxRequestInfo additional resource adapter specific connection request | |
169 | * information | |
170 | * @return ManagedConnection instance | |
171 | * @throws ResourceException if the attempt to create a connection fails | |
172 | */ | |
173 | ||
174 | public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) | |
175 | throws ResourceException | |
176 | { | |
177 | 0 | return new MuleManagedConnection(this, subject, cxRequestInfo); |
178 | } | |
179 | ||
180 | /** | |
181 | * Returns a matched managed connection from the candidate set of connections. | |
182 | * ManagedConnectionFactory uses the security info (as in Subject) and | |
183 | * information provided through ConnectionRequestInfo and additional Resource | |
184 | * Adapter specific criteria to do matching. A MC that has the requested store is | |
185 | * returned as a match | |
186 | * | |
187 | * @param connectionSet candidate connection set | |
188 | * @param subject caller's security information | |
189 | * @param cxRequestInfo additional resource adapter specific connection request | |
190 | * information | |
191 | * @return ManagedConnection if resource adapter finds an acceptable match, | |
192 | * otherwise null | |
193 | * @throws ResourceException if the match fails | |
194 | */ | |
195 | ||
196 | public ManagedConnection matchManagedConnections(Set connectionSet, | |
197 | Subject subject, | |
198 | ConnectionRequestInfo cxRequestInfo) | |
199 | throws ResourceException | |
200 | { | |
201 | 0 | PasswordCredential pc = RaHelper.getPasswordCredential(this, subject, cxRequestInfo); |
202 | ||
203 | 0 | Iterator it = connectionSet.iterator(); |
204 | 0 | while (it.hasNext()) |
205 | { | |
206 | 0 | Object obj = it.next(); |
207 | 0 | if (obj instanceof MuleManagedConnection) |
208 | { | |
209 | 0 | MuleManagedConnection mc = (MuleManagedConnection)obj; |
210 | 0 | PasswordCredential mcpc = mc.getPasswordCredential(); |
211 | 0 | if (mcpc != null && pc != null && mcpc.equals(pc)) |
212 | { | |
213 | 0 | return mc; |
214 | } | |
215 | } | |
216 | 0 | } |
217 | 0 | return null; |
218 | } | |
219 | ||
220 | /** | |
221 | * Sets the log writer for this ManagedConnectionFactory instance. The log writer | |
222 | * is a character output stream to which all logging and tracing messages for | |
223 | * this ManagedConnectionfactory instance will be printed. | |
224 | * | |
225 | * @param out an output stream for error logging and tracing | |
226 | * @throws ResourceException if the method fails | |
227 | */ | |
228 | ||
229 | public void setLogWriter(PrintWriter out) throws ResourceException | |
230 | { | |
231 | 0 | this.out = out; |
232 | 0 | } |
233 | ||
234 | /** | |
235 | * Gets the log writer for this ManagedConnectionFactory instance. | |
236 | * | |
237 | * @return PrintWriter an output stream for error logging and tracing | |
238 | * @throws ResourceException if the method fails | |
239 | */ | |
240 | ||
241 | public PrintWriter getLogWriter() throws ResourceException | |
242 | { | |
243 | 0 | return this.out; |
244 | } | |
245 | ||
246 | /** | |
247 | * Associate PropertyChangeListener with the ManagedConnectionFactory, in order | |
248 | * to notify about properties changes. | |
249 | * | |
250 | * @param lis the PropertyChangeListener to be associated with the | |
251 | * ManagedConnectionFactory | |
252 | */ | |
253 | ||
254 | public void addPropertyChangeListener(PropertyChangeListener lis) | |
255 | { | |
256 | 0 | changes.addPropertyChangeListener(lis); |
257 | 0 | } |
258 | ||
259 | /** | |
260 | * Delete association of PropertyChangeListener with the | |
261 | * ManagedConnectionFactory. | |
262 | * | |
263 | * @param lis the PropertyChangeListener to be removed | |
264 | */ | |
265 | ||
266 | public void removePropertyChangeListener(PropertyChangeListener lis) | |
267 | { | |
268 | 0 | changes.removePropertyChangeListener(lis); |
269 | 0 | } |
270 | ||
271 | /** | |
272 | * Returns the value of the userName property. | |
273 | * | |
274 | * @return the value of the userName property | |
275 | */ | |
276 | ||
277 | public String getUsername() | |
278 | { | |
279 | 0 | return this.username; |
280 | } | |
281 | ||
282 | /** | |
283 | * Sets the value of the userName property. | |
284 | * | |
285 | * @param username String containing the value to be assigned to userName | |
286 | */ | |
287 | ||
288 | public void setUsername(String username) | |
289 | { | |
290 | 0 | this.username = username; |
291 | 0 | } |
292 | ||
293 | /** | |
294 | * Returns the value of the password property. | |
295 | * | |
296 | * @return the value of the password property | |
297 | */ | |
298 | ||
299 | public String getPassword() | |
300 | { | |
301 | 0 | return this.password; |
302 | } | |
303 | ||
304 | /** | |
305 | * Sets the value of the password property. | |
306 | * | |
307 | * @param password String containing the value to be assigned to password | |
308 | */ | |
309 | ||
310 | public void setPassword(String password) | |
311 | { | |
312 | 0 | this.password = password; |
313 | 0 | } |
314 | } |