Package org.objectweb.speedo.jca.jdo

Source Code of org.objectweb.speedo.jca.jdo.JDOConnectionFactory

/**
* perseus/connector: this is an implementation of some JCA-related technologies
* (resource adapters and managers) for the ObjectWeb consortium.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Contact: speedo@objectweb.org
*
*/

package org.objectweb.speedo.jca.jdo;

import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.speedo.api.Debug;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.jca.SpeedoConnectionFactory;
import org.objectweb.speedo.jca.SpeedoConnectionSpec;
import org.objectweb.speedo.jca.SpeedoManagedConnectionFactory;
import org.objectweb.speedo.pm.jdo.api.JDOPOManagerFactoryItf;

import javax.naming.NamingException;
import javax.naming.Reference;
import javax.resource.NotSupportedException;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.ConnectionSpec;
import javax.resource.cci.RecordFactory;
import javax.resource.cci.ResourceAdapterMetaData;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ConnectionRequestInfo;
import javax.jdo.datastore.DataStoreCache;
import javax.jdo.listener.InstanceLifecycleListener;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.PersistenceManager;
import javax.jdo.JDOException;
import java.util.Properties;
import java.util.Collection;

/**
* @author  P. Dechamboux
*/
public class JDOConnectionFactory extends SpeedoConnectionFactory
        implements ConnectionFactory, ResourceAdapterMetaData,
        PersistenceManagerFactory {
    /**
     * The JDO Adapter Version.
     */
    private final static String JDOADAPTERVERSION = "1.00";
    /**
     * The JDO Adapter Vendor Name (i.e. ObjectWeb !!).
     */
    private final static String JDOADAPTERVENDORNAME = "The ObjectWeb Consortium";
    /**
     * The JDO Adapter Name.
     */
    private final static String JDOADAPTERNAME = "OW Java Data Object (JDO) Adapter";
    /**
     * A short description of the JDO Adapter.
     */
    private final static String JDOADAPTERSHORTDESCRIPTION = "It provides a JCA wrapper to any JDO compliant implementation.";
    /**
     * The JDO Spec Version (none yet).
     */
    private final static String JDOSPECVERSION = "1.00";

    /**
     * The JNDI Reference associated to this adapter.
     */
    private Reference jndiReference = null;
    /**
     * The ManagedConnectionFactory associated to this ConnectionFactory.
     */
    private SpeedoManagedConnectionFactory mcf;

    /**
     * The transaction mode within a j2ee context.
     */
    private byte transactionMode = SpeedoProperties.TRANSACTION_BMODE_NORMAL;
   
    /**
     * Constructs a JDOConnectionFactory. Associates a ConnectionManager with
     * it, or creates a default one (if null).
     * @param fmcf  The ManagedConnectionFactory to be associated with this
     *         ConnectionFactory.
     * @param cm  The ConnectionManager to be associated with this
     *         ConnectionFactory. It may be null.
     * @param transactionMode the mode of transaction chosen for the application.
     */
    JDOConnectionFactory(Logger logger,
             SpeedoManagedConnectionFactory fmcf,
             ConnectionManager cm,
             byte transactionMode) throws ResourceException {
        this.logger = logger;
        if (Debug.ON)
            logger.log(BasicLevel.DEBUG,
                       "Constructs a new JDOConnectionFactory.");
        mcf = fmcf;
        setConnectionManager(cm);
        this.transactionMode = transactionMode;
    }

    /**
     * Creates a new JDOConnectionImpl.
     */
    public Object createConnection() throws ResourceException {
        JDOConnectionImpl res = new JDOConnectionImpl(logger, this);
        if (Debug.ON)
            logger.log(BasicLevel.DEBUG, "New JDOConnectionImpl created: " + res);
        return res;
    }

    private void checkMCFStarted() {
    if (!mcf.started) {
      try {
        mcf.start();
      } catch (ResourceException e) {
        throw new JDOException("JDO Connector: cannot initialize the ManagedConnectionFactory.", e);
      }
    }
    }

    // IMPLEMENTATION OF METHODS FROM THE (cci)ConnectionFactory INTERFACE

    /**
     * Connection allocation is delegated to the ConnectionManager assigned to
     * this factory.
     */
    public Connection getConnection() throws ResourceException {
        if (!mcf.started) {
            mcf.start();
        }
    PersistenceManager pm = (PersistenceManager)
        connectionManager.allocateConnection(mcf, null);
    pm.currentTransaction();
    return (Connection) pm;
    }

    /**
     * Connection allocation is delegated to the ConnectionManager assigned to
     * this factory.
     * @param spec  The connection is not taken into account within JDO.
     */
    public Connection getConnection(ConnectionSpec spec) throws ResourceException {
    if (!mcf.started) {
      mcf.start();
    }
    ConnectionRequestInfo cri = null;
    if (spec instanceof ConnectionRequestInfo) {
      cri = (ConnectionRequestInfo) spec;
    }
    PersistenceManager pm = (PersistenceManager)
        connectionManager.allocateConnection(mcf, cri);
    pm.currentTransaction();
    return (Connection) pm;
    }

    /**
     * Assigns a JNDI Reference to the adapter.
     * @param reference  The JNDI Reference to be assigned.
     */
    public void setReference(Reference reference) {
        jndiReference = reference;
    }

    /**
     * Returns the JNDI Reference.
     * @return  The JNDI Reference.
     */
    public Reference getReference() throws NamingException {
        return jndiReference;
    }

    /**
     * No support for record.
     */
    public RecordFactory getRecordFactory() throws ResourceException {
        throw new NotSupportedException("JDO Connector: no support for record.");
    }

    /**
     * The JDOConnectionFactory manages its metadata on its own.
     */
    public ResourceAdapterMetaData getMetaData() throws ResourceException {
        return this;
    }

    // IMPLEMENTATION OF METHODS FROM THE (cci)ResourceAdapaterMetaData INTERFACE

    /**
     * Gives access to the adapter version metadata for the JDO.
     * @return  The JDO adapter version.
     */
    public String getAdapterVersion() {
        return JDOADAPTERVERSION;
    }

    /**
     * Gives access to the adapter vendor name metadata for the JDO.
     * @return  The JDO adapter vendor name.
     */
    public String getAdapterVendorName() {
        return JDOADAPTERVENDORNAME;
    }

    /**
     * Gives access to the adapter name metadata for the JDO.
     * @return  The JDO adapter name.
     */
    public String getAdapterName() {
        return JDOADAPTERNAME;
    }

    /**
     * Gives access to a short description of the JDO adapter.
     * @return  The JDO short description.
     */
    public String getAdapterShortDescription() {
        return JDOADAPTERSHORTDESCRIPTION;
    }

    /**
     * Gives access to the specification version of the JDO.
     * @return  The JDO specification version.
     */
    public String getSpecVersion() {
        return JDOSPECVERSION;
    }

    /**
     * No interaction is supported by the JDO adapter.
     */
    public String[] getInteractionSpecsSupported() {
        return new String[0];
    }

    /**
     * No support for record.
     */
    public boolean supportsExecuteWithInputAndOutputRecord() {
        return false;
    }

    /**
     * No support for record.
     */
    public boolean supportsExecuteWithInputRecordOnly() {
        return false;
    }

    /**
     * Yes, it does support local transaction demarcation.
     */
    public boolean supportsLocalTransactionDemarcation() {
        return true;
    }

    // IMPLEMENTATION OF METHODS FROM THE PersistenceManagerFactory INTERFACE

    public PersistenceManager getPersistenceManager() {
        try {
            return (PersistenceManager) getConnection();
        } catch (ResourceException e) {
            throw new JDOException("JDO Connector: problem while getting a PersistenceManager.", e);
        }
    }

    public PersistenceManager getPersistenceManager(String s, String s1) {
        try {
      PersistenceManager pm = (PersistenceManager)
          getConnection(new SpeedoConnectionSpec(s, s1));
      pm.currentTransaction();
            return pm;
        } catch (ResourceException e) {
            throw new JDOException("JDO Connector: problem while getting a PersistenceManager.", e);
        }
    }

    public void setConnectionUserName(String s) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionUserName(s);
    }

    public String getConnectionUserName() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getConnectionUserName();
    }

    public void setConnectionPassword(String s) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionPassword(s);
    }

    public void setConnectionURL(String s) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionURL(s);
    }

    public String getConnectionURL() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getConnectionURL();
    }

    public void setConnectionDriverName(String s) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionDriverName(s);
    }

    public String getConnectionDriverName() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getConnectionDriverName();
    }

    public void setConnectionFactoryName(String s) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionFactoryName(s);
    }

    public String getConnectionFactoryName() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getConnectionFactoryName();
    }

    public void setConnectionFactory(Object o) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionFactory(o);
    }

    public Object getConnectionFactory() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getConnectionFactory();
    }

    public void setConnectionFactory2Name(String s) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionFactory2Name(s);
    }

    public String getConnectionFactory2Name() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getConnectionFactory2Name();
    }

    public void setConnectionFactory2(Object o) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setConnectionFactory2(o);
    }

    public Object getConnectionFactory2() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getConnectionFactory2();
    }

    public void setMultithreaded(boolean b) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setMultithreaded(b);
    }

    public boolean getMultithreaded() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getMultithreaded();
    }

    public void setOptimistic(boolean b) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setOptimistic(b);
    }

    public boolean getOptimistic() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getOptimistic();
    }

    public void setRetainValues(boolean b) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setRetainValues(b);
    }

    public boolean getRetainValues() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getRetainValues();
    }

    public void setRestoreValues(boolean b) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setRestoreValues(b);
    }

    public boolean getRestoreValues() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getRestoreValues();
    }

    public void setNontransactionalRead(boolean b) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setNontransactionalRead(b);
    }

    public boolean getNontransactionalRead() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getNontransactionalRead();
    }

    public void setNontransactionalWrite(boolean b) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setNontransactionalWrite(b);
    }

    public boolean getNontransactionalWrite() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getNontransactionalWrite();
    }

    public void setIgnoreCache(boolean b) {
        checkMCFStarted();
        ((JDOPOManagerFactoryItf) mcf.pmf).setIgnoreCache(b);
    }

    public boolean getIgnoreCache() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getIgnoreCache();
    }

    public Properties getProperties() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).getProperties();
    }

    public Collection supportedOptions() {
        checkMCFStarted();
        return ((JDOPOManagerFactoryItf) mcf.pmf).supportedOptions();
    }

  public void close() {
    checkMCFStarted();
    ((JDOPOManagerFactoryItf) mcf.pmf).close();
  }
 
    public DataStoreCache getDataStoreCache() {
    checkMCFStarted();
    return ((JDOPOManagerFactoryItf) mcf.pmf).getDataStoreCache();
    }
    public String getMapping() {
    checkMCFStarted();
    return ((JDOPOManagerFactoryItf) mcf.pmf).getMapping();
    }
    public boolean isClosed() {
    checkMCFStarted();
    return ((JDOPOManagerFactoryItf) mcf.pmf).isClosed();
    }
    public void setMapping(String arg0) {
    checkMCFStarted();
    ((JDOPOManagerFactoryItf) mcf.pmf).setMapping(arg0);
    }
    public void addInstanceLifecycleListener(InstanceLifecycleListener arg0,
            Class[] arg1) {
    checkMCFStarted();
    ((JDOPOManagerFactoryItf) mcf.pmf).addInstanceLifecycleListener(arg0, arg1);
    }
    public void removeInstanceLifecycleListener(InstanceLifecycleListener arg0) {
    checkMCFStarted();
    ((JDOPOManagerFactoryItf) mcf.pmf).removeInstanceLifecycleListener(arg0);
    }
   
  public byte getTransactionMode() {
    return transactionMode;
  }

  /* (non-Javadoc)
   * @see javax.jdo.PersistenceManagerFactory#getDetachAllOnCommit()
   */
  public boolean getDetachAllOnCommit() {
    // TODO Auto-generated method stub
    return false;
  }

  /* (non-Javadoc)
   * @see javax.jdo.PersistenceManagerFactory#setDetachAllOnCommit(boolean)
   */
  public void setDetachAllOnCommit(boolean arg0) {
    // TODO Auto-generated method stub
   
  }
}
TOP

Related Classes of org.objectweb.speedo.jca.jdo.JDOConnectionFactory

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.