| 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.util.HashSet; |
|---|
| 36 | import java.util.Iterator; |
|---|
| 37 | import java.util.Set; |
|---|
| 38 | import java.util.logging.Logger; |
|---|
| 39 | |
|---|
| 40 | import javax.resource.ResourceException; |
|---|
| 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 | |
|---|
| 46 | /** |
|---|
| 47 | * The default Connection Manager. This Manager is intended to be used in a |
|---|
| 48 | * <b>two tier scenario</b> (Non-managed Environment, see Connection |
|---|
| 49 | * Architecture 1.0 Chapter 5.9). |
|---|
| 50 | * |
|---|
| 51 | */ |
|---|
| 52 | public class DefaultConnectionManager |
|---|
| 53 | implements ConnectionManager |
|---|
| 54 | { |
|---|
| 55 | /** The full qualified name of this class. */ |
|---|
| 56 | private static final transient String CLASSNAME |
|---|
| 57 | = DefaultConnectionManager.class.getName(); |
|---|
| 58 | |
|---|
| 59 | /** The logger to use. */ |
|---|
| 60 | private static final transient Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * The Default <code>serialVersionUID</code> |
|---|
| 64 | */ |
|---|
| 65 | private static final long serialVersionUID = 1L; |
|---|
| 66 | |
|---|
| 67 | private final Set mMc = new HashSet(); |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Creates a new physical connection to the underlying EIS instance. |
|---|
| 71 | * |
|---|
| 72 | * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory, javax.resource.spi.ConnectionRequestInfo) |
|---|
| 73 | */ |
|---|
| 74 | public Object allocateConnection (ManagedConnectionFactory mcf, |
|---|
| 75 | ConnectionRequestInfo cri) |
|---|
| 76 | throws ResourceException |
|---|
| 77 | { |
|---|
| 78 | logger.entering(CLASSNAME, "allocateConnection", |
|---|
| 79 | new Object [] {mcf, cri}); |
|---|
| 80 | |
|---|
| 81 | ManagedConnection mc = mcf.matchManagedConnections(mMc, null, cri); |
|---|
| 82 | |
|---|
| 83 | if (mc == null) |
|---|
| 84 | { |
|---|
| 85 | mc = mcf.createManagedConnection(null, cri); |
|---|
| 86 | synchronized (mMc) |
|---|
| 87 | { |
|---|
| 88 | mMc.add(mc); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | final Object result = mc.getConnection(null, cri); |
|---|
| 93 | |
|---|
| 94 | logger.exiting(CLASSNAME, "allocateConnection", result); |
|---|
| 95 | |
|---|
| 96 | return result; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Cleans up all managed connections. |
|---|
| 101 | */ |
|---|
| 102 | public void cleanUp () |
|---|
| 103 | { |
|---|
| 104 | synchronized (mMc) |
|---|
| 105 | { |
|---|
| 106 | final Iterator itr = mMc.iterator(); |
|---|
| 107 | while (itr.hasNext()) |
|---|
| 108 | { |
|---|
| 109 | try |
|---|
| 110 | { |
|---|
| 111 | ((ManagedConnection) itr.next()).cleanup(); |
|---|
| 112 | } |
|---|
| 113 | catch (ResourceException re) |
|---|
| 114 | { |
|---|
| 115 | // nothing to do |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Destroys all managed connections. |
|---|
| 123 | */ |
|---|
| 124 | public void destroy () |
|---|
| 125 | { |
|---|
| 126 | synchronized (mMc) |
|---|
| 127 | { |
|---|
| 128 | final Iterator itr = mMc.iterator(); |
|---|
| 129 | while (itr.hasNext()) |
|---|
| 130 | { |
|---|
| 131 | try |
|---|
| 132 | { |
|---|
| 133 | ((ManagedConnection) itr.next()).destroy(); |
|---|
| 134 | } |
|---|
| 135 | catch (ResourceException re) |
|---|
| 136 | { |
|---|
| 137 | // nothing to do |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | mMc.clear(); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|