KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > ra > MuleManagedConnection


1 /*
2  * $Id: MuleManagedConnection.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.ra;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import javax.resource.NotSupportedException JavaDoc;
21 import javax.resource.ResourceException JavaDoc;
22 import javax.resource.spi.ConnectionEvent JavaDoc;
23 import javax.resource.spi.ConnectionEventListener JavaDoc;
24 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
25 import javax.resource.spi.ManagedConnection JavaDoc;
26 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
27 import javax.resource.spi.security.PasswordCredential JavaDoc;
28 import javax.security.auth.Subject JavaDoc;
29 import javax.transaction.xa.XAResource JavaDoc;
30
31 import org.mule.config.i18n.Message;
32 import org.mule.config.i18n.Messages;
33 import org.mule.impl.security.MuleCredentials;
34
35 /**
36  * <code>MuleManagedConnection</code> TODO
37  */

38 public class MuleManagedConnection implements ManagedConnection JavaDoc
39 {
40     private MuleManagedConnectionFactory mcf;
41     private List JavaDoc listeners = new ArrayList JavaDoc();
42     private Set JavaDoc connectionSet;
43     private PrintWriter JavaDoc logWriter;
44     private boolean destroyed;
45
46     private PasswordCredential JavaDoc passCred;
47
48     /**
49      * Constructor.
50      *
51      * @param mcf the ManagedConnectionFactory that created this instance
52      * @param subject security context as JAAS subject
53      * @param cxRequestInfo ConnectionRequestInfo instance
54      * @throws javax.resource.ResourceException in case of any error
55      */

56
57     MuleManagedConnection(MuleManagedConnectionFactory mcf,
58                           Subject JavaDoc subject,
59                           ConnectionRequestInfo JavaDoc cxRequestInfo) throws ResourceException JavaDoc
60     {
61         this.mcf = mcf;
62
63         // Note: this will select the credential that matches this MC's MCF.
64
// The credential's MCF is set by the application server.
65
this.passCred = RaHelper.getPasswordCredential(mcf, subject, cxRequestInfo);
66
67         connectionSet = new HashSet JavaDoc();
68     }
69
70     /**
71      * Creates a new connection handle to the Mail Server represented by the
72      * ManagedConnection instance. This connection handle is used by the application
73      * code to refer to the underlying physical connection.
74      *
75      * @param subject security context as JAAS subject
76      * @param connectionRequestInfo ConnectionRequestInfo instance
77      * @return Connection instance representing the connection handle
78      * @throws ResourceException if the method fails to get a connection
79      */

80
81     public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc connectionRequestInfo)
82         throws ResourceException JavaDoc
83     {
84
85         checkIfDestroyed();
86
87         PasswordCredential JavaDoc pc = RaHelper.getPasswordCredential(mcf, subject, connectionRequestInfo);
88
89         if (!passCred.equals(pc))
90         {
91             // TODO change the message, we are not dealing with an endpoint here
92
throw new javax.resource.spi.SecurityException JavaDoc(new Message(Messages.AUTH_DENIED_ON_ENDPOINT_X,
93                 this).getMessage());
94         }
95
96         String JavaDoc user;
97         String JavaDoc password;
98         MuleConnectionRequestInfo info = (MuleConnectionRequestInfo)connectionRequestInfo;
99
100         user = info.getUserName();
101         password = info.getPassword();
102         if (user == null)
103         {
104             // Use default values
105
user = mcf.getUsername();
106             password = mcf.getPassword();
107         }
108         MuleCredentials creds = null;
109         if (user != null)
110         {
111             if (password == null)
112             {
113                 password = "";
114             }
115             creds = new MuleCredentials(user, password.toCharArray());
116         }
117
118         MuleConnection connection = new DefaultMuleConnection(this, info.getManager(), creds);
119         addConnection(connection);
120         return connection;
121     }
122
123     /**
124      * Destroys the physical connection.
125      *
126      * @throws ResourceException if the method fails to destroy the connection
127      */

128
129     public void destroy() throws ResourceException JavaDoc
130     {
131         if (destroyed)
132         {
133             return;
134         }
135         destroyed = true;
136
137         invalidateConnections();
138     }
139
140     /**
141      * Initiates a cleanup of the client-specific state maintained by a
142      * ManagedConnection instance. The cleanup should invalidate all connection
143      * handles created using this ManagedConnection instance.
144      *
145      * @throws ResourceException if the cleanup fails
146      */

147
148     public void cleanup() throws ResourceException JavaDoc
149     {
150         checkIfDestroyed();
151
152         invalidateConnections();
153     }
154
155     private void invalidateConnections()
156     {
157         Iterator JavaDoc it = connectionSet.iterator();
158         while (it.hasNext())
159         {
160             DefaultMuleConnection connection = (DefaultMuleConnection)it.next();
161             connection.invalidate();
162         }
163         connectionSet.clear();
164     }
165
166     /**
167      * Used by the container to change the association of an application-level
168      * connection handle with a ManagedConnection instance. The container should find
169      * the right ManagedConnection instance and call the associateConnection method.
170      *
171      * @param connection application-level connection handle
172      * @throws ResourceException if the attempt to change the association fails
173      */

174
175     public void associateConnection(Object JavaDoc connection) throws ResourceException JavaDoc
176     {
177         checkIfDestroyed();
178
179         if (connection instanceof MuleConnection)
180         {
181             MuleConnection cnn = (MuleConnection)connection;
182             cnn.associateConnection(this);
183         }
184         else
185         {
186             throw new IllegalStateException JavaDoc(new Message(Messages.OBJECT_X_MARKED_INVALID,
187                 DefaultMuleConnection.class.getName() + ": "
188                                 + (connection == null ? "null" : connection.getClass().getName())).toString());
189         }
190     }
191
192     /**
193      * Adds a connection event listener to the ManagedConnection instance. The
194      * registered ConnectionEventListener instances are notified of connection close
195      * and error events as well as local-transaction-related events on the Managed
196      * Connection.
197      *
198      * @param listener a new ConnectionEventListener to be registered
199      */

200
201     public void addConnectionEventListener(ConnectionEventListener JavaDoc listener)
202     {
203         listeners.add(listener);
204     }
205
206     /**
207      * Removes an already registered connection event listener from the
208      * ManagedConnection instance.
209      *
210      * @param listener already registered connection event listener to be removed
211      */

212
213     public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener)
214     {
215         listeners.remove(listener);
216     }
217
218     /**
219      * Returns a javax.transaction.xa.XAresource instance. An application server
220      * enlists this XAResource instance with the Transaction Manager if the
221      * ManagedConnection instance is being used in a JTA transaction that is being
222      * coordinated by the Transaction Manager. <p/> Because this implementation does
223      * not support transactions, the method throws an exception.
224      *
225      * @return the XAResource instance
226      * @throws ResourceException if transactions are not supported
227      */

228     // TODO
229
public XAResource JavaDoc getXAResource() throws ResourceException JavaDoc
230     {
231         throw new NotSupportedException JavaDoc("getXAResource");
232     }
233
234     /**
235      * Returns a javax.resource.spi.LocalTransaction instance. The LocalTransaction
236      * interface is used by the container to manage local transactions for a RM
237      * instance. <p/> Because this implementation does not support transactions, the
238      * method throws an exception.
239      *
240      * @return javax.resource.spi.LocalTransaction instance
241      * @throws ResourceException if transactions are not supported
242      */

243
244     public javax.resource.spi.LocalTransaction JavaDoc getLocalTransaction() throws ResourceException JavaDoc
245     {
246         throw new NotSupportedException JavaDoc("getLocalTransaction");
247     }
248
249     /**
250      * Gets the metadata information for this connection's underlying EIS resource
251      * manager instance. The ManagedConnectionMetaData interface provides information
252      * about the underlying EIS instance associated with the ManagedConnection
253      * instance.
254      *
255      * @return ManagedConnectionMetaData ManagedConnectionMetaData instance
256      * @throws ResourceException if the metadata cannot be retrieved
257      */

258
259     public ManagedConnectionMetaData JavaDoc getMetaData() throws ResourceException JavaDoc
260     {
261         checkIfDestroyed();
262         return new MuleManagedConnectionMetaData(this);
263     }
264
265     /**
266      * Sets the log writer for this ManagedConnection instance. The log writer is a
267      * character output stream to which all logging and tracing messages for this
268      * ManagedConnection instance will be printed.
269      *
270      * @param out character output stream to be associated
271      * @throws ResourceException if the method fails
272      */

273
274     public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc
275     {
276         this.logWriter = out;
277     }
278
279     /**
280      * Gets the log writer for this ManagedConnection instance.
281      *
282      * @return the character output stream associated with this ManagedConnection
283      * instance
284      * @throws ResourceException if the method fails
285      */

286
287     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
288     {
289         return logWriter;
290     }
291
292     /**
293      * Gets the user name of the user associated with the ManagedConnection instance.
294      *
295      * @return the username for this connection
296      */

297
298     public String JavaDoc getUsername()
299     {
300         if (passCred != null)
301         {
302             return passCred.getUserName();
303         }
304         else
305         {
306             return null;
307         }
308     }
309
310     /**
311      * Gets the password for the user associated with the ManagedConnection instance.
312      *
313      * @return the password for this connection
314      */

315
316     public PasswordCredential JavaDoc getPasswordCredential()
317     {
318         return passCred;
319     }
320
321     /**
322      * Associate connection handle with the physical connection.
323      *
324      * @param connection connection handle
325      */

326
327     public void addConnection(MuleConnection connection)
328     {
329         connectionSet.add(connection);
330     }
331
332     /**
333      * Check validation of the physical connection.
334      *
335      * @throws ResourceException if the connection has been destroyed
336      */

337
338     private void checkIfDestroyed() throws ResourceException JavaDoc
339     {
340         if (destroyed)
341         {
342             throw new ResourceException JavaDoc(
343                 new Message(Messages.X_IS_DISPOSED, "MuleManagedConnection").toString());
344         }
345     }
346
347     /**
348      * Removes the associated connection handle from the connections set to the
349      * physical connection.
350      *
351      * @param connection the connection handle
352      */

353
354     public void removeConnection(MuleConnection connection)
355     {
356         connectionSet.remove(connection);
357     }
358
359     /**
360      * Checks validation of the physical connection.
361      *
362      * @return true if the connection has been destroyed; false otherwise
363      */

364
365     boolean isDestroyed()
366     {
367         return destroyed;
368     }
369
370     /**
371      * Returns the ManagedConnectionFactory that created this instance of
372      * ManagedConnection.
373      *
374      * @return the ManagedConnectionFactory for this connection
375      */

376
377     public MuleManagedConnectionFactory getManagedConnectionFactory()
378     {
379         return this.mcf;
380     }
381
382     void fireBeginEvent()
383     {
384         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(MuleManagedConnection.this,
385             ConnectionEvent.LOCAL_TRANSACTION_STARTED);
386         Iterator JavaDoc iterator = listeners.iterator();
387         while (iterator.hasNext())
388         {
389             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc)iterator.next();
390             l.localTransactionStarted(event);
391         }
392     }
393
394     void fireCommitEvent()
395     {
396         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(MuleManagedConnection.this,
397             ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
398         Iterator JavaDoc iterator = listeners.iterator();
399         while (iterator.hasNext())
400         {
401             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc)iterator.next();
402             l.localTransactionCommitted(event);
403         }
404     }
405
406     void fireRollbackEvent()
407     {
408         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(MuleManagedConnection.this,
409             ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
410         Iterator JavaDoc iterator = listeners.iterator();
411         while (iterator.hasNext())
412         {
413             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc)iterator.next();
414             l.localTransactionRolledback(event);
415         }
416     }
417
418     void fireCloseEvent(MuleConnection connection)
419     {
420         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(MuleManagedConnection.this,
421             ConnectionEvent.CONNECTION_CLOSED);
422         event.setConnectionHandle(connection);
423
424         Iterator JavaDoc iterator = listeners.iterator();
425         while (iterator.hasNext())
426         {
427             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc)iterator.next();
428             l.connectionClosed(event);
429         }
430     }
431
432     void fireErrorOccurredEvent(Exception JavaDoc error)
433     {
434         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(MuleManagedConnection.this,
435             ConnectionEvent.CONNECTION_ERROR_OCCURRED, error);
436         Iterator JavaDoc iterator = listeners.iterator();
437         while (iterator.hasNext())
438         {
439             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc)iterator.next();
440             l.connectionErrorOccurred(event);
441         }
442     }
443
444 }
445
Popular Tags