| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | {@link } |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | @see |
| 55 | | | |
| 56 | | | |
| 57 | 100 | (1) | public abstract class ManagedConnectionFactoryBase |
| 58 | | | implements ManagedConnectionFactory |
| 59 | | | { |
| 60 | | | |
| 61 | | | private static final transient String CREATECONNECTIONFACTORY |
| 62 | | | = "createConnectionFactory"; |
| 63 | | | |
| 64 | | | |
| 65 | 75 | | private static final transient String CLASSNAME |
| 66 | | | = ManagedConnectionFactoryBase.class.getName(); |
| 67 | | | |
| 68 | | | |
| 69 | 100 | | private static final transient Logger logger = Logger.getLogger(CLASSNAME); |
| 70 | | | |
| 71 | | | <code></code> |
| 72 | | | private static final long serialVersionUID = 1L; |
| 73 | | | |
| 74 | | | |
| 75 | 100 | | private final DefaultConnectionManager mDCM = new DefaultConnectionManager(); |
| 76 | | | |
| 77 | | | |
| 78 | | | private PrintWriter mPrintWriter; |
| 79 | | | |
| 80 | | | |
| 81 | 100 | | private final Object mPwSemaphore = new Object(); |
| 82 | | | |
| 83 | | | |
| 84 | | | private String mUserName; |
| 85 | | | |
| 86 | | | private String mPassword; |
| 87 | | | |
| 88 | | | |
| 89 | | | <code></code> |
| 90 | | | |
| 91 | | | |
| 92 | | | @param |
| 93 | | | @param |
| 94 | | | @param |
| 95 | | | |
| 96 | | | @return<code></code> |
| 97 | | | |
| 98 | | | |
| 99 | | | @throws |
| 100 | | | |
| 101 | | | protected abstract boolean isMatchingManagedConnection (ManagedConnection mc, |
| 102 | | | UserPassword up, ConnectionRequestInfo cri) |
| 103 | | | throws ResourceException; |
| 104 | | | |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | |
| 109 | | | |
| 110 | | | @param |
| 111 | | | @return |
| 112 | | | @throws |
| 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 | 100 | | logger.entering(CLASSNAME, "createManagedConnection", |
| 126 | | | new Object [] {subject, cri}); |
| 127 | | | |
| 128 | 100 | | UserPassword up = SecurityUtil.getUserPassword(subject, this, cri); |
| 129 | 100 | | final UserPassword propPw = getUserPassword(); |
| 130 | | | |
| 131 | 100 | | if (UserPassword.isEmpty(up) && !UserPassword.isEmpty(propPw)) |
| 132 | | | { |
| 133 | 0 | | up = propPw; |
| 134 | | | } |
| 135 | | | |
| 136 | 100 | | final ManagedConnection result = createManagedConnectionImpl(up, cri); |
| 137 | | | |
| 138 | 100 | | logger.exiting(CLASSNAME, "createManagedConnection", result); |
| 139 | | | |
| 140 | 100 | | return result; |
| 141 | | | } |
| 142 | | | |
| 143 | | | {@inheritDoc} |
| 144 | | | public Object createConnectionFactory (ConnectionManager cm) |
| 145 | | | throws ResourceException |
| 146 | | | { |
| 147 | 100 | | logger.entering(CLASSNAME, CREATECONNECTIONFACTORY, cm); |
| 148 | | | |
| 149 | 100 | | final Object result = createConnectionFactoryImpl(cm); |
| 150 | | | |
| 151 | 100 | | logger.exiting(CLASSNAME, CREATECONNECTIONFACTORY, result); |
| 152 | | | |
| 153 | 100 | | return result; |
| 154 | | | } |
| 155 | | | |
| 156 | | | {@inheritDoc} |
| 157 | | | public Object createConnectionFactory () |
| 158 | | | throws ResourceException |
| 159 | | | { |
| 160 | 100 | | logger.entering(CLASSNAME, CREATECONNECTIONFACTORY); |
| 161 | | | |
| 162 | 100 | | final Object result = createConnectionFactory(mDCM); |
| 163 | | | |
| 164 | 100 | | logger.exiting(CLASSNAME, CREATECONNECTIONFACTORY, result); |
| 165 | | | |
| 166 | 100 | | return result; |
| 167 | | | } |
| 168 | | | |
| 169 | | | |
| 170 | | | {@inheritDoc} |
| 171 | | | public ManagedConnection matchManagedConnections (Set set, Subject subject, |
| 172 | | | ConnectionRequestInfo cri) |
| 173 | | | throws ResourceException |
| 174 | | | { |
| 175 | 100 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 176 | | | |
| 177 | 100 | | if (finer) |
| 178 | | | { |
| 179 | 100 | | logger.entering(CLASSNAME, "matchManagedConnections", new Object [] { |
| 180 | | | set, subject, cri}); |
| 181 | | | } |
| 182 | | | |
| 183 | 100 | | final UserPassword up = SecurityUtil.getUserPassword(subject, this, cri); |
| 184 | 100 | | ManagedConnection result = null; |
| 185 | 100 | | if (set != null) |
| 186 | | | { |
| 187 | 100 | | final Iterator itr = set.iterator(); |
| 188 | 100 | | while (itr.hasNext()) |
| 189 | | | { |
| 190 | 100 | | final Object obj = itr.next(); |
| 191 | 100 | | if (obj instanceof ManagedConnection) |
| 192 | | | { |
| 193 | 100 | | final ManagedConnection mc = (ManagedConnection) obj; |
| 194 | 100 | | if (isMatchingManagedConnection(mc, up, cri)) |
| 195 | | | { |
| 196 | 100 | | result = mc; |
| 197 | 100 | | break; |
| 198 | | | } |
| 199 | | | } |
| 200 | 0 | | } |
| 201 | | | } |
| 202 | | | |
| 203 | 100 | | if (finer) |
| 204 | | | { |
| 205 | 100 | | logger.exiting(CLASSNAME, "matchManagedConnections", result); |
| 206 | | | } |
| 207 | | | |
| 208 | 100 | | return result; |
| 209 | | | } |
| 210 | | | |
| 211 | | | {@inheritDoc} |
| 212 | | | public void setLogWriter (PrintWriter pw) |
| 213 | | | throws ResourceException |
| 214 | | | { |
| 215 | 0 | | synchronized (mPwSemaphore) |
| 216 | | | { |
| 217 | 0 | | mPrintWriter = pw; |
| 218 | 0 | | } |
| 219 | 0 | | } |
| 220 | | | |
| 221 | | | {@inheritDoc} |
| 222 | | | public PrintWriter getLogWriter () |
| 223 | | | throws ResourceException |
| 224 | | | { |
| 225 | 0 | | synchronized (mPwSemaphore) |
| 226 | | | { |
| 227 | 0 | | return mPrintWriter; |
| 228 | 0 | | } |
| 229 | | | } |
| 230 | | | |
| 231 | | | |
| 232 | | | |
| 233 | | | |
| 234 | | | |
| 235 | | | @param |
| 236 | | | |
| 237 | | | public void setUserName (String userName) |
| 238 | | | { |
| 239 | 0 | | logger.entering(CLASSNAME, "setUserName", userName); |
| 240 | 0 | | mUserName = userName; |
| 241 | 0 | | logger.exiting(CLASSNAME, "setUserName"); |
| 242 | 0 | | } |
| 243 | | | |
| 244 | | | |
| 245 | | | |
| 246 | | | |
| 247 | | | |
| 248 | | | @param |
| 249 | | | |
| 250 | | | public void setPassword (String password) |
| 251 | | | { |
| 252 | 0 | | logger.entering(CLASSNAME, "setPassword", "xxx"); |
| 253 | 0 | | mPassword = password; |
| 254 | 0 | | logger.exiting(CLASSNAME, "setPassword"); |
| 255 | 0 | | } |
| 256 | | | |
| 257 | | | |
| 258 | | | |
| 259 | | | |
| 260 | | | |
| 261 | | | @return |
| 262 | | | |
| 263 | | | public UserPassword getUserPassword () |
| 264 | | | { |
| 265 | 100 | | return UserPassword.fromUserPassword(mUserName, mPassword); |
| 266 | | | } |
| 267 | | | |
| 268 | | | {@inheritDoc} |
| 269 | | (2) | public int hashCode () |
| 270 | | | { |
| 271 | | (3) | |
| 272 | 100 | | return super.hashCode(); |
| 273 | | | } |
| 274 | | | |
| 275 | | | |
| 276 | | | <code></code> |
| 277 | | | <code></code> |
| 278 | | | @param |
| 279 | | | @return<code></code> |
| 280 | | | <code></code> |
| 281 | | | |
| 282 | | | public boolean equals (Object o) |
| 283 | | | { |
| 284 | 0 | | if (this == o) |
| 285 | | | { |
| 286 | 0 | | return true; |
| 287 | | | } |
| 288 | 0 | | if (o == null) |
| 289 | | | { |
| 290 | 0 | | return false; |
| 291 | | | } |
| 292 | 0 | | if (o.getClass() != getClass()) |
| 293 | | | { |
| 294 | 0 | | return false; |
| 295 | | | } |
| 296 | 0 | | if (! (o instanceof ManagedConnectionFactoryBase)) |
| 297 | | | { |
| 298 | 0 | | return false; |
| 299 | | | } |
| 300 | | | |
| 301 | 0 | | return true; |
| 302 | | | } |
| 303 | | | } |