1
2
3
4
5
6
7
8
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
34
35
36 public class MuleManagedConnectionFactory implements ManagedConnectionFactory
37 {
38
39
40
41 private static final long serialVersionUID = -1460847590293644271L;
42
43 private transient PrintWriter out;
44 private transient PropertyChangeSupport changes = new PropertyChangeSupport(this);
45
46
47 private String username = null;
48
49
50 private String password = null;
51
52
53
54
55 protected transient Log logger = LogFactory.getLog(this.getClass());
56
57 public MuleManagedConnectionFactory()
58 {
59 super();
60 }
61
62 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
63 {
64 in.defaultReadObject();
65 this.logger = LogFactory.getLog(this.getClass());
66 this.changes = new PropertyChangeSupport(this);
67 this.out = null;
68 }
69
70 public int hashCode()
71 {
72 final int PRIME = 31;
73 int result = 1;
74 result = PRIME * result + ((password == null) ? 0 : password.hashCode());
75 return PRIME * result + ((username == null) ? 0 : username.hashCode());
76 }
77
78 public boolean equals(Object obj)
79 {
80 if (this == obj)
81 {
82 return true;
83 }
84
85 if (obj == null)
86 {
87 return false;
88 }
89
90 if (this.getClass() != obj.getClass())
91 {
92 return false;
93 }
94
95 final MuleManagedConnectionFactory other = (MuleManagedConnectionFactory)obj;
96
97 if (password == null)
98 {
99 if (other.password != null)
100 {
101 return false;
102 }
103 }
104 else if (!password.equals(other.password))
105 {
106 return false;
107 }
108
109 if (username == null)
110 {
111 if (other.username != null)
112 {
113 return false;
114 }
115 }
116 else if (!username.equals(other.username))
117 {
118 return false;
119 }
120
121 return true;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public Object createConnectionFactory(ConnectionManager cxManager) throws ResourceException
137 {
138 try
139 {
140 return new DefaultMuleConnectionFactory(this, cxManager, null);
141 }
142 catch (Exception e)
143 {
144 throw new ResourceException(e);
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157 public Object createConnectionFactory() throws ResourceException
158 {
159 return new DefaultMuleConnectionFactory(this, null, null);
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cxRequestInfo)
175 throws ResourceException
176 {
177 return new MuleManagedConnection(this, subject, cxRequestInfo);
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public ManagedConnection matchManagedConnections(Set connectionSet,
197 Subject subject,
198 ConnectionRequestInfo cxRequestInfo)
199 throws ResourceException
200 {
201 PasswordCredential pc = RaHelper.getPasswordCredential(this, subject, cxRequestInfo);
202
203 Iterator it = connectionSet.iterator();
204 while (it.hasNext())
205 {
206 Object obj = it.next();
207 if (obj instanceof MuleManagedConnection)
208 {
209 MuleManagedConnection mc = (MuleManagedConnection)obj;
210 PasswordCredential mcpc = mc.getPasswordCredential();
211 if (mcpc != null && pc != null && mcpc.equals(pc))
212 {
213 return mc;
214 }
215 }
216 }
217 return null;
218 }
219
220
221
222
223
224
225
226
227
228
229 public void setLogWriter(PrintWriter out) throws ResourceException
230 {
231 this.out = out;
232 }
233
234
235
236
237
238
239
240
241 public PrintWriter getLogWriter() throws ResourceException
242 {
243 return this.out;
244 }
245
246
247
248
249
250
251
252
253
254 public void addPropertyChangeListener(PropertyChangeListener lis)
255 {
256 changes.addPropertyChangeListener(lis);
257 }
258
259
260
261
262
263
264
265
266 public void removePropertyChangeListener(PropertyChangeListener lis)
267 {
268 changes.removePropertyChangeListener(lis);
269 }
270
271
272
273
274
275
276
277 public String getUsername()
278 {
279 return this.username;
280 }
281
282
283
284
285
286
287
288 public void setUsername(String username)
289 {
290 this.username = username;
291 }
292
293
294
295
296
297
298
299 public String getPassword()
300 {
301 return this.password;
302 }
303
304
305
306
307
308
309
310 public void setPassword(String password)
311 {
312 this.password = password;
313 }
314 }