Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.http

org.jcoderz.commons.connector.http.HttpManagedConnectionFactoryImpl

LineHitsNoteSource
1  /*
2   * $Id: HttpManagedConnectionFactoryImpl.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.http;
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.spi.ConnectionManager;
42  import javax.resource.spi.ConnectionRequestInfo;
43  import javax.resource.spi.ManagedConnection;
44  import javax.resource.spi.ManagedConnectionFactory;
45  import javax.security.auth.Subject;
46  
47  
48  /**
49   * Managed Connection Factory used by the Application Server to create
50   * a non-managed factory and connection for the application.
51   *
52   */
530(1)public final class HttpManagedConnectionFactoryImpl
54        implements ManagedConnectionFactory
55  {
56     /** The serial UID generated for revision 1.4 */
57     static final long serialVersionUID = 5601979166228177337L;
58  
59     /** Class name for use in logging. */
60     private static final transient
610       String CLASSNAME = HttpManagedConnectionFactoryImpl.class.getName();
62  
63     /** The logger in use. */
64     private static final transient
650       Logger logger = Logger.getLogger(CLASSNAME);
66  
67     /** The system specific line separator */
68     private static final String
690       LINE_FEED = System.getProperty("line.separator");
700    private transient HttpManagedConnectionImpl mManagedConnection = null;
710    private transient int mAmountOfMatchingConnections = 0;
720    private transient int mAmountOfOpenMatchingConnections = 0;
73  
74  
75    /** The URL to connect to. */
76     private String mUrl;
77  
78     /** The maximum amount of open connections. */
79     private Integer mMaxConnections;
80  
81  
82     /** {@inheritDoc} */
83     public Object createConnectionFactory (ConnectionManager cm)
84     {
850       return new HttpConnectionFactoryImpl(this, cm);
86     }
87  
88     /** {@inheritDoc} */
89     public Object createConnectionFactory ()
90     {
910       return new HttpConnectionFactoryImpl(this, null);
92     }
93  
94     /** {@inheritDoc} */
95     public ManagedConnection createManagedConnection (
96           Subject subject, ConnectionRequestInfo cri)
97     {
980       return new HttpManagedConnectionImpl(this, cri);
99     }
100  
101     /** {@inheritDoc} */
102     public ManagedConnection matchManagedConnections (
103           Set connectionSet,
104           Subject subject,
105           ConnectionRequestInfo cri)
106     {
1070       final String methodName = "matchManagedConnections";
108  
109        // trace message
1100       if (logger.isLoggable(Level.FINEST))
111        {
1120          logger.finest("Connections in pool=" + connectionSet.size());
1130          logger.finest("ConnectionRequestInfo: " + cri);
114        }
115        //
1160       mManagedConnection = null;
1170       mAmountOfOpenMatchingConnections = 0;
1180       mAmountOfMatchingConnections = 0;
119  
1200       final Iterator it = connectionSet.iterator();
1210       while (it.hasNext())
122        {
1230          final Object obj = it.next();
1240          if (obj instanceof ManagedConnection)
125           {
1260             final HttpManagedConnectionImpl mc
127                    = (HttpManagedConnectionImpl) obj;
128              // If there are no special requirements about the
129              // ManagedConnection, the first ManagedConnection in
130              // the pool is good enough
1310             if (cri == null)
132              {
1330                logger.finest("No ConnectionRequestInfo specified.");
1340                logger.finest("Found connection in pool: " + mc);
135  
1360                mManagedConnection = mc;
1370                break; // found managed connection - without cri
138              }
139               // otherwise try to find a managed connection with an appropriate
140              // CtsConnectionRequestInfo
1410             findMatchingConnection(mc, cri);
142           }
1430       }
1440        if (mManagedConnection == null)
145        {
1460          logger.finest("No matching connection found in pool.");
147        }
148        else
149        {
1500          if (logger.isLoggable(Level.FINEST))
151           {
1520             logger.log(Level.FINEST, LINE_FEED
153                 + "---------- Connection POOL -----------" + LINE_FEED
154                 + " Connection to: " + cri.toString() + LINE_FEED
155                 + " Total: " + mAmountOfMatchingConnections + LINE_FEED
156                 + " Open: " + mAmountOfOpenMatchingConnections + LINE_FEED
157                 + "--------------------------------------" + LINE_FEED
158                 + LINE_FEED);
159           }
1600          logger.exiting(CLASSNAME, methodName, mManagedConnection);
161        }
1620       return mManagedConnection;
163     }
164  
165     /**
166      * Tries to match the given connection with the used connection request
167      * info and calculates the pool amounts.
168      *
169      * @param mc
170    * the managed connection to match
171      * @param cri
172    * the connection request info to match with
173      */
174     private void findMatchingConnection (
175           HttpManagedConnectionImpl mc, ConnectionRequestInfo cri)
176     {
177        // otherwise try to find a managed connection with an appropriate
178        // CtsConnectionRequestInfo
1790       if (mc.getConnectionRequestInfo().equals(cri))
180        {
1810          if (mManagedConnection == null)
182           {
1830             logger.finest("Found matching connection in pool: " + mc);
1840             mManagedConnection = mc; // found managed connection - with cri
185           }
1860          mAmountOfMatchingConnections++;
1870          if (mc.isOpen())
188           {
1890             mAmountOfOpenMatchingConnections++;
190           }
191        }
1920    }
193  
194     /** {@inheritDoc} */
195     public void setLogWriter (PrintWriter arg0)
196     {
197        // not used
1980    }
199  
200     /** {@inheritDoc} */
201     public PrintWriter getLogWriter ()
202     {
203        // not used
2040       return null;
205     }
206  
207     /**
208      * Sets the configuration parameter "MaxConnections"
209      * defined in the deployment descriptor.
210      *
211      * @param connections max count of connections
212      */
213     public void setMaxConnections (Integer connections)
214     {
2150       final String methodName = "setMaxConnection";
2160       if (logger.isLoggable(Level.FINER))
217        {
2180          final Object[] args = {connections};
2190          logger.entering(CLASSNAME, methodName, args);
220        }
2210       mMaxConnections = connections;
2220       logger.exiting(CLASSNAME, methodName);
2230    }
224  
225     /**
226      * Gets the configuration parameter "MaxConnections".
227      *
228      * @return Long - the maximum number of connections
229      */
230     public Integer getMaxConnections ()
231     {
2320       return mMaxConnections;
233     }
234  
235     /**
236      * Set configuration parameter "Url" as defined in deployment descriptor.
237      *
238      * @param url the url connecting to
239      */
240     public void setUrl (String url)
241     {
2420       final String methodName = "setUrl";
2430       if (logger.isLoggable(Level.FINER))
244        {
2450          final Object[] args = {url};
2460          logger.entering(CLASSNAME, methodName, args);
247        }
248  
2490       mUrl = url;
2500       logger.exiting(CLASSNAME, methodName);
2510    }
252  
253     /**
254      * Gets the configuration parameter "Url".
255      *
256      * @return String - the url connection to
257      */
258     public String getUrl ()
259     {
2600       return mUrl;
261     }
262  }

Findings in this File

i (2) The field org.jcoderz.commons.connector.http.HttpManagedConnectionFactoryImpl.CLASSNAME is transient but isn't set by deserialization
c (1) 53 : 0 Type Javadoc comment is missing an @author tag.