| 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; |
|---|
| 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 | */ |
|---|
| 57 | 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. */ |
|---|
| 65 | private static final transient String CLASSNAME |
|---|
| 66 | = ManagedConnectionFactoryBase.class.getName(); |
|---|
| 67 | |
|---|
| 68 | /** The logger to use. */ |
|---|
| 69 | 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. */ |
|---|
| 75 | 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. */ |
|---|
| 81 | 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 | { |
|---|
| 125 | logger.entering(CLASSNAME, "createManagedConnection", |
|---|
| 126 | new Object [] {subject, cri}); |
|---|
| 127 | |
|---|
| 128 | UserPassword up = SecurityUtil.getUserPassword(subject, this, cri); |
|---|
| 129 | final UserPassword propPw = getUserPassword(); |
|---|
| 130 | |
|---|
| 131 | if (UserPassword.isEmpty(up) && !UserPassword.isEmpty(propPw)) |
|---|
| 132 | { |
|---|
| 133 | up = propPw; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | final ManagedConnection result = createManagedConnectionImpl(up, cri); |
|---|
| 137 | |
|---|
| 138 | logger.exiting(CLASSNAME, "createManagedConnection", result); |
|---|
| 139 | |
|---|
| 140 | return result; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** {@inheritDoc} */ |
|---|
| 144 | public Object createConnectionFactory (ConnectionManager cm) |
|---|
| 145 | throws ResourceException |
|---|
| 146 | { |
|---|
| 147 | logger.entering(CLASSNAME, CREATECONNECTIONFACTORY, cm); |
|---|
| 148 | |
|---|
| 149 | final Object result = createConnectionFactoryImpl(cm); |
|---|
| 150 | |
|---|
| 151 | logger.exiting(CLASSNAME, CREATECONNECTIONFACTORY, result); |
|---|
| 152 | |
|---|
| 153 | return result; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** {@inheritDoc} */ |
|---|
| 157 | public Object createConnectionFactory () |
|---|
| 158 | throws ResourceException |
|---|
| 159 | { |
|---|
| 160 | logger.entering(CLASSNAME, CREATECONNECTIONFACTORY); |
|---|
| 161 | |
|---|
| 162 | final Object result = createConnectionFactory(mDCM); |
|---|
| 163 | |
|---|
| 164 | logger.exiting(CLASSNAME, CREATECONNECTIONFACTORY, result); |
|---|
| 165 | |
|---|
| 166 | return result; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | /** {@inheritDoc} */ |
|---|
| 171 | public ManagedConnection matchManagedConnections (Set set, Subject subject, |
|---|
| 172 | ConnectionRequestInfo cri) |
|---|
| 173 | throws ResourceException |
|---|
| 174 | { |
|---|
| 175 | final boolean finer = logger.isLoggable(Level.FINER); |
|---|
| 176 | |
|---|
| 177 | if (finer) |
|---|
| 178 | { |
|---|
| 179 | logger.entering(CLASSNAME, "matchManagedConnections", new Object [] { |
|---|
| 180 | set, subject, cri}); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | final UserPassword up = SecurityUtil.getUserPassword(subject, this, cri); |
|---|
| 184 | ManagedConnection result = null; |
|---|
| 185 | if (set != null) |
|---|
| 186 | { |
|---|
| 187 | final Iterator itr = set.iterator(); |
|---|
| 188 | while (itr.hasNext()) |
|---|
| 189 | { |
|---|
| 190 | final Object obj = itr.next(); |
|---|
| 191 | if (obj instanceof ManagedConnection) |
|---|
| 192 | { |
|---|
| 193 | final ManagedConnection mc = (ManagedConnection) obj; |
|---|
| 194 | if (isMatchingManagedConnection(mc, up, cri)) |
|---|
| 195 | { |
|---|
| 196 | result = mc; |
|---|
| 197 | break; |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | if (finer) |
|---|
| 204 | { |
|---|
| 205 | logger.exiting(CLASSNAME, "matchManagedConnections", result); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | return result; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | /** {@inheritDoc} */ |
|---|
| 212 | public void setLogWriter (PrintWriter pw) |
|---|
| 213 | throws ResourceException |
|---|
| 214 | { |
|---|
| 215 | synchronized (mPwSemaphore) |
|---|
| 216 | { |
|---|
| 217 | mPrintWriter = pw; |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** {@inheritDoc} */ |
|---|
| 222 | public PrintWriter getLogWriter () |
|---|
| 223 | throws ResourceException |
|---|
| 224 | { |
|---|
| 225 | synchronized (mPwSemaphore) |
|---|
| 226 | { |
|---|
| 227 | return mPrintWriter; |
|---|
| 228 | } |
|---|
| 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 | { |
|---|
| 239 | logger.entering(CLASSNAME, "setUserName", userName); |
|---|
| 240 | mUserName = userName; |
|---|
| 241 | logger.exiting(CLASSNAME, "setUserName"); |
|---|
| 242 | } |
|---|
| 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 | { |
|---|
| 252 | logger.entering(CLASSNAME, "setPassword", "xxx"); |
|---|
| 253 | mPassword = password; |
|---|
| 254 | logger.exiting(CLASSNAME, "setPassword"); |
|---|
| 255 | } |
|---|
| 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 | { |
|---|
| 265 | return UserPassword.fromUserPassword(mUserName, mPassword); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | /** {@inheritDoc} */ |
|---|
| 269 | public int hashCode () |
|---|
| 270 | { |
|---|
| 271 | // FIXME Implement this method |
|---|
| 272 | 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 | { |
|---|
| 284 | if (this == o) |
|---|
| 285 | { |
|---|
| 286 | return true; |
|---|
| 287 | } |
|---|
| 288 | if (o == null) |
|---|
| 289 | { |
|---|
| 290 | return false; |
|---|
| 291 | } |
|---|
| 292 | if (o.getClass() != getClass()) |
|---|
| 293 | { |
|---|
| 294 | return false; |
|---|
| 295 | } |
|---|
| 296 | if (! (o instanceof ManagedConnectionFactoryBase)) |
|---|
| 297 | { |
|---|
| 298 | return false; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | return true; |
|---|
| 302 | } |
|---|
| 303 | } |
|---|