Package de.danet.an.util.ra

Source Code of de.danet.an.util.ra.ManagedConnectionSupport

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: ManagedConnectionSupport.java 2372 2007-05-17 21:53:00Z mlipp $
*
* $Log$
*/
package de.danet.an.util.ra;

import java.io.PrintWriter;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.resource.NotSupportedException;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ConnectionEventListener;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.LocalTransaction;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionMetaData;
import javax.security.auth.Subject;
import javax.transaction.xa.XAResource;

/**
* This class provides a base for implementing managed connections.
*
* @author mnl
*
*/
public abstract class ManagedConnectionSupport implements ManagedConnection {

    private Set connections = Collections.synchronizedSet(new HashSet());
    private List listeners = Collections.synchronizedList(new ArrayList ());
    private PrintWriter logWriter;
    private ManagedConnectionMetaData metaData;
   
    /**
     * Create a new instance with all attributes initialized
     * to defaults or the given values.
     * @param the managed connection factory
     * @param subject security context as JAAS subject
     *
     */
    public ManagedConnectionSupport
        (ManagedConnectionFactorySupport mcf, Subject subject) {
        try {
            this.logWriter = mcf.getLogWriter();
        } catch (ResourceException e) {
            throw (IllegalArgumentException)
                (new IllegalArgumentException
                 ("Cannot get log writer: " + e.getMessage())).initCause(e);
        }
        String userName = null;
        if (subject != null && subject.getPrincipals() != null
            && subject.getPrincipals().size() > 0) {
            userName = ((Principal)subject.getPrincipals().iterator().next())
                .getName();
        }
        metaData = mcf.createMetaData(userName);
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#destroy()
     */
    public final void destroy() throws ResourceException {
        cleanup ();
        connections = null;
        listeners = null;
        doDestroy ();
    }

    /**
     * Destroy any resources allocated by the derived class. This is
     * invoked after the cleanup done by <code>destroy</code> for this
     * class.
     */
    protected abstract void doDestroy ();
   
    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#getConnection
     */
    public Object getConnection
        (Subject subject, ConnectionRequestInfo cxRequestInfo)
        throws ResourceException {
        ConnectionSupport connection
            = createConnection (subject, cxRequestInfo);
        connection.setManagedConnection(this);
        connections.add (connection);
        return connection;
    }

    /**
     * Create a connection. This method must be implemented by derived
     * classes.
     *
     * @param subject security context as JAAS subject
     * @param cxRequestInfo ConnectionRequestInfo instance
     * @return the connection implementation object
     * @see javax.resource.spi.ManagedConnection#getConnection
     */
    protected abstract ConnectionSupport createConnection
        (Subject subject, ConnectionRequestInfo cxRequestInfo);
   
    /**
     * Close the managed connection.
     */
    public void close (ConnectionSupport connection) {
        ConnectionEvent event = new ConnectionEvent
            (this, ConnectionEvent.CONNECTION_CLOSED);
        event.setConnectionHandle(connection);
        for (Iterator i = listeners.iterator(); i.hasNext();) {
            ((ConnectionEventListener)i.next()).connectionClosed(event);
        }
        connection.setManagedConnection(null);
        connections.remove(connection);
    }
   
    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#associateConnection(java.lang.Object)
     */
    public void associateConnection(Object newConnection)
        throws ResourceException {
        ((ConnectionSupport)newConnection).setManagedConnection (this);
        connections.add (newConnection);
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#cleanup()
     */
    public void cleanup() throws ResourceException {
        for (Iterator i = connections.iterator(); i.hasNext();) {
            ConnectionSupport con = (ConnectionSupport)i.next();
            con.setManagedConnection(null);
        }
        connections.clear();
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#getLocalTransaction()
     */
    public LocalTransaction getLocalTransaction() throws ResourceException {
        throw new NotSupportedException ("Not supported.");
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#getXAResource()
     */
    public XAResource getXAResource() throws ResourceException {
        throw new NotSupportedException ("Not supported.");
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#setLogWriter(java.io.PrintWriter)
     */
    public void setLogWriter(PrintWriter logWriter) throws ResourceException {
        this.logWriter = logWriter;
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#getLogWriter()
     */
    public PrintWriter getLogWriter() throws ResourceException {
        return logWriter;
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#getMetaData()
     */
    public ManagedConnectionMetaData getMetaData() throws ResourceException {
        return metaData;
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#addConnectionEventListener(javax.resource.spi.ConnectionEventListener)
     */
    public void addConnectionEventListener(ConnectionEventListener listener) {
        listeners.add(listener);
    }

    /* (non-Javadoc)
     * @see javax.resource.spi.ManagedConnection#removeConnectionEventListener(javax.resource.spi.ConnectionEventListener)
     */
    public void removeConnectionEventListener(ConnectionEventListener listener) {
        listeners.remove(listener);
    }

}
TOP

Related Classes of de.danet.an.util.ra.ManagedConnectionSupport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.