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