| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 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 | */ |
|---|
| 53 | 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 |
|---|
| 61 | String CLASSNAME = HttpManagedConnectionFactoryImpl.class.getName(); |
|---|
| 62 | |
|---|
| 63 | /** The logger in use. */ |
|---|
| 64 | private static final transient |
|---|
| 65 | Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 66 | |
|---|
| 67 | /** The system specific line separator */ |
|---|
| 68 | private static final String |
|---|
| 69 | LINE_FEED = System.getProperty("line.separator"); |
|---|
| 70 | private transient HttpManagedConnectionImpl mManagedConnection = null; |
|---|
| 71 | private transient int mAmountOfMatchingConnections = 0; |
|---|
| 72 | 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 | { |
|---|
| 85 | return new HttpConnectionFactoryImpl(this, cm); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** {@inheritDoc} */ |
|---|
| 89 | public Object createConnectionFactory () |
|---|
| 90 | { |
|---|
| 91 | return new HttpConnectionFactoryImpl(this, null); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** {@inheritDoc} */ |
|---|
| 95 | public ManagedConnection createManagedConnection ( |
|---|
| 96 | Subject subject, ConnectionRequestInfo cri) |
|---|
| 97 | { |
|---|
| 98 | return new HttpManagedConnectionImpl(this, cri); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** {@inheritDoc} */ |
|---|
| 102 | public ManagedConnection matchManagedConnections ( |
|---|
| 103 | Set connectionSet, |
|---|
| 104 | Subject subject, |
|---|
| 105 | ConnectionRequestInfo cri) |
|---|
| 106 | { |
|---|
| 107 | final String methodName = "matchManagedConnections"; |
|---|
| 108 | |
|---|
| 109 | // trace message |
|---|
| 110 | if (logger.isLoggable(Level.FINEST)) |
|---|
| 111 | { |
|---|
| 112 | logger.finest("Connections in pool=" + connectionSet.size()); |
|---|
| 113 | logger.finest("ConnectionRequestInfo: " + cri); |
|---|
| 114 | } |
|---|
| 115 | // |
|---|
| 116 | mManagedConnection = null; |
|---|
| 117 | mAmountOfOpenMatchingConnections = 0; |
|---|
| 118 | mAmountOfMatchingConnections = 0; |
|---|
| 119 | |
|---|
| 120 | final Iterator it = connectionSet.iterator(); |
|---|
| 121 | while (it.hasNext()) |
|---|
| 122 | { |
|---|
| 123 | final Object obj = it.next(); |
|---|
| 124 | if (obj instanceof ManagedConnection) |
|---|
| 125 | { |
|---|
| 126 | 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 |
|---|
| 131 | if (cri == null) |
|---|
| 132 | { |
|---|
| 133 | logger.finest("No ConnectionRequestInfo specified."); |
|---|
| 134 | logger.finest("Found connection in pool: " + mc); |
|---|
| 135 | |
|---|
| 136 | mManagedConnection = mc; |
|---|
| 137 | break; // found managed connection - without cri |
|---|
| 138 | } |
|---|
| 139 | // otherwise try to find a managed connection with an appropriate |
|---|
| 140 | // CtsConnectionRequestInfo |
|---|
| 141 | findMatchingConnection(mc, cri); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | if (mManagedConnection == null) |
|---|
| 145 | { |
|---|
| 146 | logger.finest("No matching connection found in pool."); |
|---|
| 147 | } |
|---|
| 148 | else |
|---|
| 149 | { |
|---|
| 150 | if (logger.isLoggable(Level.FINEST)) |
|---|
| 151 | { |
|---|
| 152 | 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 | } |
|---|
| 160 | logger.exiting(CLASSNAME, methodName, mManagedConnection); |
|---|
| 161 | } |
|---|
| 162 | 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 |
|---|
| 179 | if (mc.getConnectionRequestInfo().equals(cri)) |
|---|
| 180 | { |
|---|
| 181 | if (mManagedConnection == null) |
|---|
| 182 | { |
|---|
| 183 | logger.finest("Found matching connection in pool: " + mc); |
|---|
| 184 | mManagedConnection = mc; // found managed connection - with cri |
|---|
| 185 | } |
|---|
| 186 | mAmountOfMatchingConnections++; |
|---|
| 187 | if (mc.isOpen()) |
|---|
| 188 | { |
|---|
| 189 | mAmountOfOpenMatchingConnections++; |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /** {@inheritDoc} */ |
|---|
| 195 | public void setLogWriter (PrintWriter arg0) |
|---|
| 196 | { |
|---|
| 197 | // not used |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** {@inheritDoc} */ |
|---|
| 201 | public PrintWriter getLogWriter () |
|---|
| 202 | { |
|---|
| 203 | // not used |
|---|
| 204 | 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 | { |
|---|
| 215 | final String methodName = "setMaxConnection"; |
|---|
| 216 | if (logger.isLoggable(Level.FINER)) |
|---|
| 217 | { |
|---|
| 218 | final Object[] args = {connections}; |
|---|
| 219 | logger.entering(CLASSNAME, methodName, args); |
|---|
| 220 | } |
|---|
| 221 | mMaxConnections = connections; |
|---|
| 222 | logger.exiting(CLASSNAME, methodName); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | /** |
|---|
| 226 | * Gets the configuration parameter "MaxConnections". |
|---|
| 227 | * |
|---|
| 228 | * @return Long - the maximum number of connections |
|---|
| 229 | */ |
|---|
| 230 | public Integer getMaxConnections () |
|---|
| 231 | { |
|---|
| 232 | 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 | { |
|---|
| 242 | final String methodName = "setUrl"; |
|---|
| 243 | if (logger.isLoggable(Level.FINER)) |
|---|
| 244 | { |
|---|
| 245 | final Object[] args = {url}; |
|---|
| 246 | logger.entering(CLASSNAME, methodName, args); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | mUrl = url; |
|---|
| 250 | logger.exiting(CLASSNAME, methodName); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | /** |
|---|
| 254 | * Gets the configuration parameter "Url". |
|---|
| 255 | * |
|---|
| 256 | * @return String - the url connection to |
|---|
| 257 | */ |
|---|
| 258 | public String getUrl () |
|---|
| 259 | { |
|---|
| 260 | return mUrl; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|