Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.file

org.jcoderz.commons.connector.file.FsManagedConnectionImpl

LineHitsNoteSource
1  /*
2   * $Id: FsManagedConnectionImpl.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.connector.file;
34  
35  import java.io.PrintWriter;
36  import java.util.HashSet;
37  import java.util.Iterator;
38  import java.util.Properties;
39  import java.util.Random;
40  import java.util.Set;
41  import java.util.logging.Level;
42  import java.util.logging.Logger;
43  
44  import javax.resource.NotSupportedException;
45  import javax.resource.ResourceException;
46  import javax.resource.spi.ConnectionEvent;
47  import javax.resource.spi.ConnectionEventListener;
48  import javax.resource.spi.ConnectionRequestInfo;
49  import javax.resource.spi.LocalTransaction;
50  import javax.resource.spi.ManagedConnection;
51  import javax.resource.spi.ManagedConnectionFactory;
52  import javax.resource.spi.ManagedConnectionMetaData;
53  import javax.security.auth.Subject;
54  import javax.transaction.xa.XAResource;
55  
56  import org.jcoderz.commons.connector.ConnectionHandle;
57  import org.jcoderz.commons.connector.ConnectionNotificationListener;
58  import org.jcoderz.commons.connector.SecurityUtil;
59  import org.jcoderz.commons.connector.UserPassword;
60  
61  
62  /**
63   * This Managed Connection is the factory for a File System Connction.
64   *
65   * The File System Connector is not transactional, so the methods
66   * {@linkplain #getLocalTransaction()} and {@linkplain #getXAResource()} always
67   * throw the {@link javax.resource.NotSupportedException}.
68   *
69   */
70 (1)public class FsManagedConnectionImpl
71        implements ManagedConnection, ConnectionNotificationListener
72  {
73     /** The full qualified name of this class. */
7475    private static final String CLASSNAME
75             = FsManagedConnectionImpl.class.getName();
76  
77     /** The logger to use. */
78100    private static final Logger logger = Logger.getLogger(CLASSNAME);
79100    private static final Random RANDOM = new Random();
80  
81     private final FsManagedConnectionMetaData mMetaData;
82  
83     /** Registered ConnectionEventListeners. */
84100    private final Set mConnectionEventListeners = new HashSet();
85  
86     /** All active connections, created by this Factory. */
87100    private final Set mConnections = new HashSet();
88  
89     /** The PrintWriter instance to use. */
90     private PrintWriter mPrintWriter;
91  
92     private final UserPassword mUp;
93  
94     private final ConnectionRequestInfo mCri;
95     private final ManagedConnectionFactory mMcf;
96  
97     private final int mIndex;
98     private final String mStringified;
99  
100     /**
101      * Constructor.
102      * @param mcf The underlying ManagedConnectionFactory
103      * @param up The UserPassword to be used.
104      * @param cri Connection Request Info
105      */
106     public FsManagedConnectionImpl (ManagedConnectionFactory mcf,
107           UserPassword up, ConnectionRequestInfo cri)
108100    {
109100       synchronized (RANDOM)
110        {
111100          mIndex = RANDOM.nextInt(Integer.MAX_VALUE);
11250       }
113100       mStringified = "FsManagedConnectionImpl[" + mIndex + "]";
114100       mMcf = mcf;
115100       mUp = UserPassword.fromUserPassword(up);
116100(2)(3)      mCri = cri;
117100       mMetaData = new FsManagedConnectionMetaData(mUp.getUserName());
118  
119100       logger.fine("Created FsManagedConnectionImpl[" + mIndex + "]");
120100    }
121  
122     /** {@inheritDoc} */
123     public Object getConnection (Subject subject, ConnectionRequestInfo cri)
124           throws ResourceException
125     {
126100       final String method = "getConnection";
127100       final boolean finer = logger.isLoggable(Level.FINER);
128100       if (finer)
129        {
130100          logger.entering(CLASSNAME, method, new Object [] {subject, cri});
131        }
132  
133100       final UserPassword up = SecurityUtil.getUserPassword(subject, mMcf, cri);
134  
135100       if (!mUp.equals(up))
136        {
137           // JCA 1.0 8.2.7
138           // If a resource adapter does not support re-authentication, the
139           // getConnection method should throw
140           // javax.resource.spi. SecurityException if the passed Subject in the
141           // getConnection method is different from the security context
142           // associated with the ManagedConnection instance.
1430          final javax.resource.spi.SecurityException rse
144                 = new javax.resource.spi.SecurityException(
145                       "Re-authentication is not supported.");
1460          if (finer)
147           {
1480             logger.throwing(CLASSNAME, method, rse);
149           }
1500          throw rse;
151        }
152  
153        final Properties props;
154100       if (cri instanceof FsConnectionRequestInfo)
155        {
156100          props = ((FsConnectionRequestInfo) cri).getProperties();
157        }
158        else
159        {
1600          props = null;
161        }
162100       final Object result = new FsConnectionImpl(this, props);
163100       registerHandle(result, finer);
164  
165100       if (finer)
166        {
167100          logger.exiting(CLASSNAME, method, result);
168        }
169  
170100       return result;
171     }
172  
173     /** {@inheritDoc} */
174     public void destroy ()
175           throws ResourceException
176     {
177100       final boolean finer = logger.isLoggable(Level.FINER);
178  
179100       if (finer)
180        {
181100          logger.entering(CLASSNAME, "destroy");
182        }
183  
184100       cleanupConnection();
185  
186100       if (finer)
187        {
188100          logger.exiting(CLASSNAME, "destroy");
189        }
190100    }
191  
192     /** {@inheritDoc} */
193     public void cleanup ()
194           throws ResourceException
195     {
196100       final boolean finer = logger.isLoggable(Level.FINER);
197100       if (finer)
198        {
199100          logger.entering(CLASSNAME, "cleanup");
200        }
201  
202100       cleanupConnection();
203  
204100       if (finer)
205        {
206100          logger.exiting(CLASSNAME, "cleanup");
207        }
208100    }
209  
210     /** {@inheritDoc} */
211     public void associateConnection (Object connection)
212           throws ResourceException
213     {
2140       final boolean finer = logger.isLoggable(Level.FINER);
215  
2160       if (finer)
217        {
2180          logger.entering(CLASSNAME, "associateConnection", connection);
219        }
220  
2210       if (!(connection instanceof ConnectionHandle))
222        {
2230          final ResourceException re = new ResourceException("Can not associate "
224                 + "the new connection handle: '" + connection + "'.");
2250          logger.throwing(CLASSNAME, "associateConnection", re);
2260          throw re;
227        }
228  
2290       final ConnectionHandle cb = (ConnectionHandle) connection;
2300       cb.changeAssociation(this);
231  
2320       registerHandle(cb, finer);
233  
2340       if (finer)
235        {
2360          logger.exiting(CLASSNAME, "associateConnection");
237        }
2380    }
239  
240     /** {@inheritDoc} */
241     public void addConnectionEventListener (ConnectionEventListener cel)
242     {
2430       logger.entering(CLASSNAME, "addConnectionEventListener", cel);
244  
2450       synchronized (mConnectionEventListeners)
246        {
2470          mConnectionEventListeners.add(cel);
2480       }
249  
2500       logger.exiting(CLASSNAME, "addConnectionEventListener");
2510    }
252  
253     /** {@inheritDoc} */
254     public void removeConnectionEventListener (ConnectionEventListener cel)
255     {
2560       logger.entering(CLASSNAME, "removeConnectionEventListener", cel);
257  
2580       synchronized (mConnectionEventListeners)
259        {
2600          mConnectionEventListeners.remove(cel);
2610       }
262  
2630       logger.exiting(CLASSNAME, "removeConnectionEventListener", cel);
2640    }
265  
266     /**
267      * Always throws the {@link NotSupportedException}.
268      * The File System Connector does not support any XA Transaction.
269      *
270      * @return never return.
271      *
272      * @throws ResourceException Always thrown.
273      *
274      * @see javax.resource.spi.ManagedConnection#getXAResource()
275      */
276     public XAResource getXAResource ()
277           throws ResourceException
278     {
2790       throw new NotSupportedException("Resource Adapter does not support "
280              + "XA Transaction.");
281     }
282  
283     /**
284      * Always throws the {@link NotSupportedException}.
285      * The File System Connector does not support any Local Transaction.
286      *
287      * @return never return.
288      *
289      * @throws ResourceException Always thrown.
290      *
291      * @see javax.resource.spi.ManagedConnection#getLocalTransaction()
292      */
293     public LocalTransaction getLocalTransaction ()
294           throws ResourceException
295     {
2960       throw new NotSupportedException("Resource Adapter does not support "
297              + "Local Transaction.");
298     }
299  
300     /** {@inheritDoc} */
301     public ManagedConnectionMetaData getMetaData ()
302           throws ResourceException
303     {
3040       return mMetaData;
305     }
306  
307     /** {@inheritDoc} */
308     public void setLogWriter (PrintWriter pw)
309           throws ResourceException
310     {
3110       logger.entering(CLASSNAME, "setLogWriter", pw);
3120       mPrintWriter = pw;
3130       logger.exiting(CLASSNAME, "setLogWriter");
3140    }
315  
316     /** {@inheritDoc} */
317     public PrintWriter getLogWriter ()
318           throws ResourceException
319     {
3200       if (logger.isLoggable(Level.FINER))
321        {
3220          logger.entering(CLASSNAME, "getLogWriter");
3230          logger.exiting(CLASSNAME, "getLogWriter", mPrintWriter);
324        }
325  
3260       return mPrintWriter;
327     }
328  
329     /** {@inheritDoc} */
330     public void notifyConnectionClosed (Object source)
331     {
332100       final boolean finer = logger.isLoggable(Level.FINER);
333  
334100       if (finer)
335        {
336100          logger.entering(CLASSNAME, "notifyConnectionClosed", source);
337        }
338  
339        // This connection has been closed
340100       deregisterHandle(source, finer);
341  
342        // Connection Closed Event.
343100       final ConnectionEvent closedEvent = new ConnectionEvent(this,
344              ConnectionEvent.CONNECTION_CLOSED);
345  
346        // Set connection handle
347100       closedEvent.setConnectionHandle(source);
348  
349        // Notify all connection listeners
350100       notifyEvent(closedEvent);
351  
352100       if (finer)
353        {
354100          logger.exiting(CLASSNAME, "notifyConnectionClosed");
355        }
356100    }
357  
358     /** {@inheritDoc} */
359     public void notifyConnectionErrorOccurred (Object source, Exception e)
360     {
3610       logger.entering(CLASSNAME, "notifyConnectionErrorOccurred",
362              new Object [] {source, e});
363  
364        // Connection Closed Event.
3650       final ConnectionEvent closedEvent = new ConnectionEvent(this,
366              ConnectionEvent.CONNECTION_ERROR_OCCURRED, e);
367  
368        // Set connection handle
3690       closedEvent.setConnectionHandle(source);
370  
371        // Notify all connection listeners
3720       notifyEvent(closedEvent);
373  
3740       logger.exiting(CLASSNAME, "notifyConnectionErrorOccurred");
3750    }
376  
377     /** {@inheritDoc} */
378     public void notifyConnectionDissociated (Object source)
379     {
3800       final boolean finer = logger.isLoggable(Level.FINER);
3810       if (finer)
382        {
3830          logger.entering(CLASSNAME, "notifyConnectionDissociated", source);
384        }
385  
3860       deregisterHandle(source, finer);
387  
3880       if (finer)
389        {
3900          logger.exiting(CLASSNAME, "notifyConnectionDissociated");
391        }
3920    }
393  
394  
395     /**
396      * @return UserPassword set for this managed connection.
397      */
398     UserPassword getUserPassword ()
399     {
400100       return mUp;
401     }
402  
403     /** {@inheritDoc} */
404     public String toString ()
405     {
406100       return mStringified;
407     }
408  
409     /** {@inheritDoc} */
410     public int hashCode ()
411     {
412100       return mIndex;
413     }
414  
415     /**
416      * Indicates whether some other object is "equal to" this one.
417      *
418      * @param obj the object to compare to.
419      * @return true if this object is the same as the obj argument; false
420      *         otherwise.
421      */
422     public boolean equals (Object obj)
423     {
4240       return (obj instanceof FsManagedConnectionImpl
425              && ((FsManagedConnectionImpl) obj).mIndex == this.mIndex);
426     }
427  
428     private void registerHandle (final Object result, boolean loggable)
429     {
430100       mConnections.add(result);
431  
432100       if (loggable)
433        {
434100          logger.finer(toString() + " added " + result
435                 + ", new connection count " + mConnections.size());
436        }
437100    }
438  
439     private void deregisterHandle (Object source, boolean loggable)
440     {
441100       mConnections.remove(source);
442  
443100       if (loggable)
444        {
445100          logger.finer(toString() + " removed " + source
446                 + ", new connection count " + mConnections.size());
447        }
448100    }
449  
450     /**
451      * Cleans up all handles created by this factory and frees all allocaded
452      * resources. Removes all registered notification listeners.
453      */
454     private void cleanupConnection ()
455     {
456100       final boolean finer = logger.isLoggable(Level.FINER);
457  
458100       if (finer)
459        {
460100          logger.entering(CLASSNAME, "cleanupConnection");
461        }
462  
463100       final Iterator itr = mConnections.iterator();
464  
465100       ConnectionHandle current = null;
466100       while (itr.hasNext())
467        {
468100          current = (ConnectionHandle) itr.next();
469           try
470           {
471100             current.cleanUp();
472           }
4730          catch (ResourceException re)
474           {
4750             logger.log(Level.WARNING, "Could not clean up the connection '"
476                    + current + "'.", re);
47750          }
478        }
479100       mConnections.clear();
480100       if (finer)
481        {
482100          logger.exiting(CLASSNAME, "cleanupConnection");
483        }
484100    }
485  
486     private void notifyEvent (final ConnectionEvent event)
487     {
488        final Object [] a;
489100       synchronized (mConnectionEventListeners)
490        {
491           // to avoid locking on the mConnectionEventListeners object
492100          a = mConnectionEventListeners.toArray();
49350       }
494  
495        // Notify all listeners
49666       for (int i = 0; i < a.length; i++)
497        {
4980          ((ConnectionEventListener) a[i]).connectionClosed(event);
499        }
500100    }
501  }

Findings in this File

c (4) Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag 'ResourceException'.
c (1) 70 : 0 Type Javadoc comment is missing an @author tag.
w (2) 116 : 0 class org.jcoderz.commons.connector.file.FsManagedConnectionImpl defines fields that are used only as locals
i (3) 116 : 0 Unread field: org.jcoderz.commons.connector.file.FsManagedConnectionImpl.mCri