Project Report: fawkez

Packagesummary org.jcoderz.commons.connector

org.jcoderz.commons.connector.ManagedConnectionFactoryBase

LineHitsNoteSource
1  /*
2   * $Id: ManagedConnectionFactoryBase.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;
34  
35  import java.io.PrintWriter;
36  import java.util.Iterator;
37  import java.util.Set;
38  import java.util.logging.Level;
39  import java.util.logging.Logger;
40  
41  import javax.resource.ResourceException;
42  import javax.resource.spi.ConnectionManager;
43  import javax.resource.spi.ConnectionRequestInfo;
44  import javax.resource.spi.ManagedConnection;
45  import javax.resource.spi.ManagedConnectionFactory;
46  import javax.security.auth.Subject;
47  
48  
49  /**
50   * This class implements the {@link javax.resource.spi.ManagedConnectionFactory}
51   * interface and provides a set of abstract methods which have to be overrided
52   * by a derived class to provide the connector specific features.
53   *
54   * @see javax.resource.spi.ManagedConnection
55   *
56   */
57100(1)public abstract class ManagedConnectionFactoryBase
58        implements ManagedConnectionFactory
59  {
60     /** Used in logger exntering/exiting calls. */
61     private static final transient String CREATECONNECTIONFACTORY
62           = "createConnectionFactory";
63  
64     /** The full qualified name of this class. */
6575    private static final transient String CLASSNAME
66           = ManagedConnectionFactoryBase.class.getName();
67  
68     /** The logger to use. */
69100    private static final transient Logger logger = Logger.getLogger(CLASSNAME);
70  
71     /** Default <code>serialVersionUID</code>. */
72     private static final long serialVersionUID = 1L;
73  
74     /** The default ConnectionManager. */
75100    private final DefaultConnectionManager mDCM = new DefaultConnectionManager();
76  
77     /** The PrintWriter instance to use. */
78     private PrintWriter mPrintWriter;
79  
80     /** The semaphore object used for mPrintWriter's access. */
81100    private final Object mPwSemaphore = new Object();
82  
83     /** User name. */
84     private String mUserName;
85     /** Password. */
86     private String mPassword;
87  
88     /**
89      * Tests whether the given managed connection <code>mc</code> matchs for
90      * handling the connection allocation request.
91      *
92      * @param mc The managed connection to be tested.
93      * @param up The caller's security information.
94      * @param cri A resource adapter specific connection request information.
95      *
96      * @return True if the given managed connection <code>mc</code> matchs for
97      *    handling the connection allocation request. Otherwise false.
98      *
99      * @throws ResourceException thrown in error cases.
100      */
101     protected abstract boolean isMatchingManagedConnection (ManagedConnection mc,
102        UserPassword up, ConnectionRequestInfo cri)
103           throws ResourceException;
104  
105     /**
106      * Creates a new ConnectionFactory. A derived class may provide a CCI
107      * connection factory interface as recomended in the JCA Specification or
108      * a non-CCI interface that provides a resource adapter specific interface.
109      *
110      * @param cm The ConnectorManager to use.
111      * @return A new instance of connection factory (CCI or non-CCI).
112      * @throws ResourceException thrown in error cases.
113      */
114     protected abstract Object createConnectionFactoryImpl (ConnectionManager cm)
115           throws ResourceException;
116  
117     protected abstract ManagedConnection createManagedConnectionImpl (
118           UserPassword up, ConnectionRequestInfo cri);
119  
120     /** {@inheritDoc} */
121     public ManagedConnection createManagedConnection (Subject subject,
122           ConnectionRequestInfo cri)
123           throws ResourceException
124     {
125100       logger.entering(CLASSNAME, "createManagedConnection",
126              new Object [] {subject, cri});
127  
128100       UserPassword up = SecurityUtil.getUserPassword(subject, this, cri);
129100       final UserPassword propPw = getUserPassword();
130  
131100       if (UserPassword.isEmpty(up) && !UserPassword.isEmpty(propPw))
132        {
1330          up = propPw;
134        }
135  
136100       final ManagedConnection result = createManagedConnectionImpl(up, cri);
137  
138100       logger.exiting(CLASSNAME, "createManagedConnection", result);
139  
140100       return result;
141     }
142  
143     /** {@inheritDoc} */
144     public Object createConnectionFactory (ConnectionManager cm)
145           throws ResourceException
146     {
147100       logger.entering(CLASSNAME, CREATECONNECTIONFACTORY, cm);
148  
149100       final Object result = createConnectionFactoryImpl(cm);
150  
151100       logger.exiting(CLASSNAME, CREATECONNECTIONFACTORY, result);
152  
153100       return result;
154     }
155  
156     /** {@inheritDoc} */
157     public Object createConnectionFactory ()
158           throws ResourceException
159     {
160100       logger.entering(CLASSNAME, CREATECONNECTIONFACTORY);
161  
162100       final Object result = createConnectionFactory(mDCM);
163  
164100       logger.exiting(CLASSNAME, CREATECONNECTIONFACTORY, result);
165  
166100       return result;
167     }
168  
169  
170     /** {@inheritDoc} */
171     public ManagedConnection matchManagedConnections (Set set, Subject subject,
172           ConnectionRequestInfo cri)
173           throws ResourceException
174     {
175100       final boolean finer = logger.isLoggable(Level.FINER);
176  
177100       if (finer)
178        {
179100          logger.entering(CLASSNAME, "matchManagedConnections", new Object [] {
180                 set, subject, cri});
181        }
182  
183100       final UserPassword up = SecurityUtil.getUserPassword(subject, this, cri);
184100       ManagedConnection result = null;
185100       if (set != null)
186        {
187100          final Iterator itr = set.iterator();
188100          while (itr.hasNext())
189           {
190100             final Object obj = itr.next();
191100             if (obj instanceof ManagedConnection)
192              {
193100                final ManagedConnection mc = (ManagedConnection) obj;
194100                if (isMatchingManagedConnection(mc, up, cri))
195                 {
196100                   result = mc;
197100                   break;
198                 }
199              }
2000          }
201        }
202  
203100       if (finer)
204        {
205100          logger.exiting(CLASSNAME, "matchManagedConnections", result);
206        }
207  
208100       return result;
209     }
210  
211     /** {@inheritDoc} */
212     public void setLogWriter (PrintWriter pw)
213           throws ResourceException
214     {
2150       synchronized (mPwSemaphore)
216        {
2170          mPrintWriter = pw;
2180       }
2190    }
220  
221     /** {@inheritDoc} */
222     public PrintWriter getLogWriter ()
223           throws ResourceException
224     {
2250       synchronized (mPwSemaphore)
226        {
2270          return mPrintWriter;
2280       }
229     }
230  
231     /**
232      * Sets the user name for this factory. This method should be called by
233      *    deployment code.
234      *
235      * @param userName the user name to be set.
236      */
237     public void setUserName (String userName)
238     {
2390       logger.entering(CLASSNAME, "setUserName", userName);
2400       mUserName = userName;
2410       logger.exiting(CLASSNAME, "setUserName");
2420    }
243  
244     /**
245      * Sets the password for this factory. This method should be called by
246      *    deployment code.
247      *
248      * @param password the passwor to be set.
249      */
250     public void setPassword (String password)
251     {
2520       logger.entering(CLASSNAME, "setPassword", "xxx");
2530       mPassword = password;
2540       logger.exiting(CLASSNAME, "setPassword");
2550    }
256  
257     /**
258      * Returns the UserPassword instance, containing the user name and password,
259      *    set be deployment code.
260      *
261      * @return the UserPassword instance.
262      */
263     public UserPassword getUserPassword ()
264     {
265100       return UserPassword.fromUserPassword(mUserName, mPassword);
266     }
267  
268     /** {@inheritDoc} */
269 (2)   public int hashCode ()
270     {
271 (3)      // FIXME Implement this method
272100       return super.hashCode();
273     }
274  
275     /**
276      * Returns <code>true</code> if this
277      * <code>ManagedConnectionFactoryImpl</code> is the same as the o argument.
278      * @param o Object to be compared with this.
279      * @return <code>true</code> if this
280      * <code>ManagedConnectionFactoryImpl</code> is the same as the o argument.
281      */
282     public boolean equals (Object o)
283     {
2840       if (this == o)
285        {
2860          return true;
287        }
2880       if (o == null)
289        {
2900          return false;
291        }
2920       if (o.getClass() != getClass())
293        {
2940          return false;
295        }
2960       if (! (o instanceof ManagedConnectionFactoryBase))
297        {
2980          return false;
299        }
300  
3010       return true;
302     }
303  }

Findings in this File

c (1) 57 : 0 Type Javadoc comment is missing an @author tag.
d (2) 269 : 11 Overriding method merely calls super
i (3) 271 : 0 Comment matches to-do format '(TODO|FIXME|CHECKME)'.