| 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.file; |
| 34 | | | |
| 35 | | | import java.io.PrintWriter; |
| 36 | | | import java.util.HashSet; |
| 37 | | | import java.util.Iterator; |
| 38 | | | import java.util.Properties; |
| 39 | | | import java.util.Random; |
| 40 | | | import java.util.Set; |
| 41 | | | import java.util.logging.Level; |
| 42 | | | import java.util.logging.Logger; |
| 43 | | | |
| 44 | | | import javax.resource.NotSupportedException; |
| 45 | | | import javax.resource.ResourceException; |
| 46 | | | import javax.resource.spi.ConnectionEvent; |
| 47 | | | import javax.resource.spi.ConnectionEventListener; |
| 48 | | | import javax.resource.spi.ConnectionRequestInfo; |
| 49 | | | import javax.resource.spi.LocalTransaction; |
| 50 | | | import javax.resource.spi.ManagedConnection; |
| 51 | | | import javax.resource.spi.ManagedConnectionFactory; |
| 52 | | | import javax.resource.spi.ManagedConnectionMetaData; |
| 53 | | | import javax.security.auth.Subject; |
| 54 | | | import javax.transaction.xa.XAResource; |
| 55 | | | |
| 56 | | | import org.jcoderz.commons.connector.ConnectionHandle; |
| 57 | | | import org.jcoderz.commons.connector.ConnectionNotificationListener; |
| 58 | | | import org.jcoderz.commons.connector.SecurityUtil; |
| 59 | | | import org.jcoderz.commons.connector.UserPassword; |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | {@linkplain }{@linkplain } |
| 67 | | | {@link } |
| 68 | | | |
| 69 | | | |
| 70 | | (1) | public class FsManagedConnectionImpl |
| 71 | | | implements ManagedConnection, ConnectionNotificationListener |
| 72 | | | { |
| 73 | | | |
| 74 | 75 | | private static final String CLASSNAME |
| 75 | | | = FsManagedConnectionImpl.class.getName(); |
| 76 | | | |
| 77 | | | |
| 78 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 79 | 100 | | private static final Random RANDOM = new Random(); |
| 80 | | | |
| 81 | | | private final FsManagedConnectionMetaData mMetaData; |
| 82 | | | |
| 83 | | | |
| 84 | 100 | | private final Set mConnectionEventListeners = new HashSet(); |
| 85 | | | |
| 86 | | | |
| 87 | 100 | | private final Set mConnections = new HashSet(); |
| 88 | | | |
| 89 | | | |
| 90 | | | private PrintWriter mPrintWriter; |
| 91 | | | |
| 92 | | | private final UserPassword mUp; |
| 93 | | | |
| 94 | | | private final ConnectionRequestInfo mCri; |
| 95 | | | private final ManagedConnectionFactory mMcf; |
| 96 | | | |
| 97 | | | private final int mIndex; |
| 98 | | | private final String mStringified; |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | @param |
| 103 | | | @param |
| 104 | | | @param |
| 105 | | | |
| 106 | | | public FsManagedConnectionImpl (ManagedConnectionFactory mcf, |
| 107 | | | UserPassword up, ConnectionRequestInfo cri) |
| 108 | 100 | | { |
| 109 | 100 | | synchronized (RANDOM) |
| 110 | | | { |
| 111 | 100 | | mIndex = RANDOM.nextInt(Integer.MAX_VALUE); |
| 112 | 50 | | } |
| 113 | 100 | | mStringified = "FsManagedConnectionImpl[" + mIndex + "]"; |
| 114 | 100 | | mMcf = mcf; |
| 115 | 100 | | mUp = UserPassword.fromUserPassword(up); |
| 116 | 100 | (2)(3) | mCri = cri; |
| 117 | 100 | | mMetaData = new FsManagedConnectionMetaData(mUp.getUserName()); |
| 118 | | | |
| 119 | 100 | | logger.fine("Created FsManagedConnectionImpl[" + mIndex + "]"); |
| 120 | 100 | | } |
| 121 | | | |
| 122 | | | {@inheritDoc} |
| 123 | | | public Object getConnection (Subject subject, ConnectionRequestInfo cri) |
| 124 | | | throws ResourceException |
| 125 | | | { |
| 126 | 100 | | final String method = "getConnection"; |
| 127 | 100 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 128 | 100 | | if (finer) |
| 129 | | | { |
| 130 | 100 | | logger.entering(CLASSNAME, method, new Object [] {subject, cri}); |
| 131 | | | } |
| 132 | | | |
| 133 | 100 | | final UserPassword up = SecurityUtil.getUserPassword(subject, mMcf, cri); |
| 134 | | | |
| 135 | 100 | | if (!mUp.equals(up)) |
| 136 | | | { |
| 137 | | | |
| 138 | | | |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | |
| 143 | 0 | | final javax.resource.spi.SecurityException rse |
| 144 | | | = new javax.resource.spi.SecurityException( |
| 145 | | | "Re-authentication is not supported."); |
| 146 | 0 | | if (finer) |
| 147 | | | { |
| 148 | 0 | | logger.throwing(CLASSNAME, method, rse); |
| 149 | | | } |
| 150 | 0 | | throw rse; |
| 151 | | | } |
| 152 | | | |
| 153 | | | final Properties props; |
| 154 | 100 | | if (cri instanceof FsConnectionRequestInfo) |
| 155 | | | { |
| 156 | 100 | | props = ((FsConnectionRequestInfo) cri).getProperties(); |
| 157 | | | } |
| 158 | | | else |
| 159 | | | { |
| 160 | 0 | | props = null; |
| 161 | | | } |
| 162 | 100 | | final Object result = new FsConnectionImpl(this, props); |
| 163 | 100 | | registerHandle(result, finer); |
| 164 | | | |
| 165 | 100 | | if (finer) |
| 166 | | | { |
| 167 | 100 | | logger.exiting(CLASSNAME, method, result); |
| 168 | | | } |
| 169 | | | |
| 170 | 100 | | return result; |
| 171 | | | } |
| 172 | | | |
| 173 | | | {@inheritDoc} |
| 174 | | | public void destroy () |
| 175 | | | throws ResourceException |
| 176 | | | { |
| 177 | 100 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 178 | | | |
| 179 | 100 | | if (finer) |
| 180 | | | { |
| 181 | 100 | | logger.entering(CLASSNAME, "destroy"); |
| 182 | | | } |
| 183 | | | |
| 184 | 100 | | cleanupConnection(); |
| 185 | | | |
| 186 | 100 | | if (finer) |
| 187 | | | { |
| 188 | 100 | | logger.exiting(CLASSNAME, "destroy"); |
| 189 | | | } |
| 190 | 100 | | } |
| 191 | | | |
| 192 | | | {@inheritDoc} |
| 193 | | | public void cleanup () |
| 194 | | | throws ResourceException |
| 195 | | | { |
| 196 | 100 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 197 | 100 | | if (finer) |
| 198 | | | { |
| 199 | 100 | | logger.entering(CLASSNAME, "cleanup"); |
| 200 | | | } |
| 201 | | | |
| 202 | 100 | | cleanupConnection(); |
| 203 | | | |
| 204 | 100 | | if (finer) |
| 205 | | | { |
| 206 | 100 | | logger.exiting(CLASSNAME, "cleanup"); |
| 207 | | | } |
| 208 | 100 | | } |
| 209 | | | |
| 210 | | | {@inheritDoc} |
| 211 | | | public void associateConnection (Object connection) |
| 212 | | | throws ResourceException |
| 213 | | | { |
| 214 | 0 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 215 | | | |
| 216 | 0 | | if (finer) |
| 217 | | | { |
| 218 | 0 | | logger.entering(CLASSNAME, "associateConnection", connection); |
| 219 | | | } |
| 220 | | | |
| 221 | 0 | | if (!(connection instanceof ConnectionHandle)) |
| 222 | | | { |
| 223 | 0 | | final ResourceException re = new ResourceException("Can not associate " |
| 224 | | | + "the new connection handle: '" + connection + "'."); |
| 225 | 0 | | logger.throwing(CLASSNAME, "associateConnection", re); |
| 226 | 0 | | throw re; |
| 227 | | | } |
| 228 | | | |
| 229 | 0 | | final ConnectionHandle cb = (ConnectionHandle) connection; |
| 230 | 0 | | cb.changeAssociation(this); |
| 231 | | | |
| 232 | 0 | | registerHandle(cb, finer); |
| 233 | | | |
| 234 | 0 | | if (finer) |
| 235 | | | { |
| 236 | 0 | | logger.exiting(CLASSNAME, "associateConnection"); |
| 237 | | | } |
| 238 | 0 | | } |
| 239 | | | |
| 240 | | | {@inheritDoc} |
| 241 | | | public void addConnectionEventListener (ConnectionEventListener cel) |
| 242 | | | { |
| 243 | 0 | | logger.entering(CLASSNAME, "addConnectionEventListener", cel); |
| 244 | | | |
| 245 | 0 | | synchronized (mConnectionEventListeners) |
| 246 | | | { |
| 247 | 0 | | mConnectionEventListeners.add(cel); |
| 248 | 0 | | } |
| 249 | | | |
| 250 | 0 | | logger.exiting(CLASSNAME, "addConnectionEventListener"); |
| 251 | 0 | | } |
| 252 | | | |
| 253 | | | {@inheritDoc} |
| 254 | | | public void removeConnectionEventListener (ConnectionEventListener cel) |
| 255 | | | { |
| 256 | 0 | | logger.entering(CLASSNAME, "removeConnectionEventListener", cel); |
| 257 | | | |
| 258 | 0 | | synchronized (mConnectionEventListeners) |
| 259 | | | { |
| 260 | 0 | | mConnectionEventListeners.remove(cel); |
| 261 | 0 | | } |
| 262 | | | |
| 263 | 0 | | logger.exiting(CLASSNAME, "removeConnectionEventListener", cel); |
| 264 | 0 | | } |
| 265 | | | |
| 266 | | | |
| 267 | | | {@link } |
| 268 | | | |
| 269 | | | |
| 270 | | | @return |
| 271 | | | |
| 272 | | | @throws |
| 273 | | | |
| 274 | | | @see |
| 275 | | | |
| 276 | | | public XAResource getXAResource () |
| 277 | | | throws ResourceException |
| 278 | | | { |
| 279 | 0 | | throw new NotSupportedException("Resource Adapter does not support " |
| 280 | | | + "XA Transaction."); |
| 281 | | | } |
| 282 | | | |
| 283 | | | |
| 284 | | | {@link } |
| 285 | | | |
| 286 | | | |
| 287 | | | @return |
| 288 | | | |
| 289 | | | @throws |
| 290 | | | |
| 291 | | | @see |
| 292 | | | |
| 293 | | | public LocalTransaction getLocalTransaction () |
| 294 | | | throws ResourceException |
| 295 | | | { |
| 296 | 0 | | throw new NotSupportedException("Resource Adapter does not support " |
| 297 | | | + "Local Transaction."); |
| 298 | | | } |
| 299 | | | |
| 300 | | | {@inheritDoc} |
| 301 | | | public ManagedConnectionMetaData getMetaData () |
| 302 | | | throws ResourceException |
| 303 | | | { |
| 304 | 0 | | return mMetaData; |
| 305 | | | } |
| 306 | | | |
| 307 | | | {@inheritDoc} |
| 308 | | | public void setLogWriter (PrintWriter pw) |
| 309 | | | throws ResourceException |
| 310 | | | { |
| 311 | 0 | | logger.entering(CLASSNAME, "setLogWriter", pw); |
| 312 | 0 | | mPrintWriter = pw; |
| 313 | 0 | | logger.exiting(CLASSNAME, "setLogWriter"); |
| 314 | 0 | | } |
| 315 | | | |
| 316 | | | {@inheritDoc} |
| 317 | | | public PrintWriter getLogWriter () |
| 318 | | | throws ResourceException |
| 319 | | | { |
| 320 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 321 | | | { |
| 322 | 0 | | logger.entering(CLASSNAME, "getLogWriter"); |
| 323 | 0 | | logger.exiting(CLASSNAME, "getLogWriter", mPrintWriter); |
| 324 | | | } |
| 325 | | | |
| 326 | 0 | | return mPrintWriter; |
| 327 | | | } |
| 328 | | | |
| 329 | | | {@inheritDoc} |
| 330 | | | public void notifyConnectionClosed (Object source) |
| 331 | | | { |
| 332 | 100 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 333 | | | |
| 334 | 100 | | if (finer) |
| 335 | | | { |
| 336 | 100 | | logger.entering(CLASSNAME, "notifyConnectionClosed", source); |
| 337 | | | } |
| 338 | | | |
| 339 | | | |
| 340 | 100 | | deregisterHandle(source, finer); |
| 341 | | | |
| 342 | | | |
| 343 | 100 | | final ConnectionEvent closedEvent = new ConnectionEvent(this, |
| 344 | | | ConnectionEvent.CONNECTION_CLOSED); |
| 345 | | | |
| 346 | | | |
| 347 | 100 | | closedEvent.setConnectionHandle(source); |
| 348 | | | |
| 349 | | | |
| 350 | 100 | | notifyEvent(closedEvent); |
| 351 | | | |
| 352 | 100 | | if (finer) |
| 353 | | | { |
| 354 | 100 | | logger.exiting(CLASSNAME, "notifyConnectionClosed"); |
| 355 | | | } |
| 356 | 100 | | } |
| 357 | | | |
| 358 | | | {@inheritDoc} |
| 359 | | | public void notifyConnectionErrorOccurred (Object source, Exception e) |
| 360 | | | { |
| 361 | 0 | | logger.entering(CLASSNAME, "notifyConnectionErrorOccurred", |
| 362 | | | new Object [] {source, e}); |
| 363 | | | |
| 364 | | | |
| 365 | 0 | | final ConnectionEvent closedEvent = new ConnectionEvent(this, |
| 366 | | | ConnectionEvent.CONNECTION_ERROR_OCCURRED, e); |
| 367 | | | |
| 368 | | | |
| 369 | 0 | | closedEvent.setConnectionHandle(source); |
| 370 | | | |
| 371 | | | |
| 372 | 0 | | notifyEvent(closedEvent); |
| 373 | | | |
| 374 | 0 | | logger.exiting(CLASSNAME, "notifyConnectionErrorOccurred"); |
| 375 | 0 | | } |
| 376 | | | |
| 377 | | | {@inheritDoc} |
| 378 | | | public void notifyConnectionDissociated (Object source) |
| 379 | | | { |
| 380 | 0 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 381 | 0 | | if (finer) |
| 382 | | | { |
| 383 | 0 | | logger.entering(CLASSNAME, "notifyConnectionDissociated", source); |
| 384 | | | } |
| 385 | | | |
| 386 | 0 | | deregisterHandle(source, finer); |
| 387 | | | |
| 388 | 0 | | if (finer) |
| 389 | | | { |
| 390 | 0 | | logger.exiting(CLASSNAME, "notifyConnectionDissociated"); |
| 391 | | | } |
| 392 | 0 | | } |
| 393 | | | |
| 394 | | | |
| 395 | | | |
| 396 | | | @return |
| 397 | | | |
| 398 | | | UserPassword getUserPassword () |
| 399 | | | { |
| 400 | 100 | | return mUp; |
| 401 | | | } |
| 402 | | | |
| 403 | | | {@inheritDoc} |
| 404 | | | public String toString () |
| 405 | | | { |
| 406 | 100 | | return mStringified; |
| 407 | | | } |
| 408 | | | |
| 409 | | | {@inheritDoc} |
| 410 | | | public int hashCode () |
| 411 | | | { |
| 412 | 100 | | return mIndex; |
| 413 | | | } |
| 414 | | | |
| 415 | | | |
| 416 | | | |
| 417 | | | |
| 418 | | | @param |
| 419 | | | @return |
| 420 | | | |
| 421 | | | |
| 422 | | | public boolean equals (Object obj) |
| 423 | | | { |
| 424 | 0 | | return (obj instanceof FsManagedConnectionImpl |
| 425 | | | && ((FsManagedConnectionImpl) obj).mIndex == this.mIndex); |
| 426 | | | } |
| 427 | | | |
| 428 | | | private void registerHandle (final Object result, boolean loggable) |
| 429 | | | { |
| 430 | 100 | | mConnections.add(result); |
| 431 | | | |
| 432 | 100 | | if (loggable) |
| 433 | | | { |
| 434 | 100 | | logger.finer(toString() + " added " + result |
| 435 | | | + ", new connection count " + mConnections.size()); |
| 436 | | | } |
| 437 | 100 | | } |
| 438 | | | |
| 439 | | | private void deregisterHandle (Object source, boolean loggable) |
| 440 | | | { |
| 441 | 100 | | mConnections.remove(source); |
| 442 | | | |
| 443 | 100 | | if (loggable) |
| 444 | | | { |
| 445 | 100 | | logger.finer(toString() + " removed " + source |
| 446 | | | + ", new connection count " + mConnections.size()); |
| 447 | | | } |
| 448 | 100 | | } |
| 449 | | | |
| 450 | | | |
| 451 | | | |
| 452 | | | |
| 453 | | | |
| 454 | | | private void cleanupConnection () |
| 455 | | | { |
| 456 | 100 | | final boolean finer = logger.isLoggable(Level.FINER); |
| 457 | | | |
| 458 | 100 | | if (finer) |
| 459 | | | { |
| 460 | 100 | | logger.entering(CLASSNAME, "cleanupConnection"); |
| 461 | | | } |
| 462 | | | |
| 463 | 100 | | final Iterator itr = mConnections.iterator(); |
| 464 | | | |
| 465 | 100 | | ConnectionHandle current = null; |
| 466 | 100 | | while (itr.hasNext()) |
| 467 | | | { |
| 468 | 100 | | current = (ConnectionHandle) itr.next(); |
| 469 | | | try |
| 470 | | | { |
| 471 | 100 | | current.cleanUp(); |
| 472 | | | } |
| 473 | 0 | | catch (ResourceException re) |
| 474 | | | { |
| 475 | 0 | | logger.log(Level.WARNING, "Could not clean up the connection '" |
| 476 | | | + current + "'.", re); |
| 477 | 50 | | } |
| 478 | | | } |
| 479 | 100 | | mConnections.clear(); |
| 480 | 100 | | if (finer) |
| 481 | | | { |
| 482 | 100 | | logger.exiting(CLASSNAME, "cleanupConnection"); |
| 483 | | | } |
| 484 | 100 | | } |
| 485 | | | |
| 486 | | | private void notifyEvent (final ConnectionEvent event) |
| 487 | | | { |
| 488 | | | final Object [] a; |
| 489 | 100 | | synchronized (mConnectionEventListeners) |
| 490 | | | { |
| 491 | | | |
| 492 | 100 | | a = mConnectionEventListeners.toArray(); |
| 493 | 50 | | } |
| 494 | | | |
| 495 | | | |
| 496 | 66 | | for (int i = 0; i < a.length; i++) |
| 497 | | | { |
| 498 | 0 | | ((ConnectionEventListener) a[i]).connectionClosed(event); |
| 499 | | | } |
| 500 | 100 | | } |
| 501 | | | } |