Package org.jpox.store.rdbms

Source Code of org.jpox.store.rdbms.JDOSequenceImpl

/**********************************************************************
Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Contributors:
    ...
**********************************************************************/
package org.jpox.store.rdbms;

import java.sql.SQLException;
import java.util.Properties;

import javax.jdo.JDODataStoreException;
import javax.jdo.datastore.Sequence;

import org.jpox.ManagedConnection;
import org.jpox.ObjectManager;
import org.jpox.PersistenceConfiguration;
import org.jpox.exceptions.JPOXDataStoreException;
import org.jpox.exceptions.JPOXException;
import org.jpox.metadata.ExtensionMetaData;
import org.jpox.metadata.SequenceMetaData;
import org.jpox.plugin.ConfigurationElement;
import org.jpox.store.JPOXSequence;
import org.jpox.store.mapped.MappedStoreManager;
import org.jpox.store.poid.PoidConnectionProvider;
import org.jpox.store.poid.PoidGenerator;
import org.jpox.store.poid.PoidManager;
import org.jpox.transaction.TransactionUtils;
import org.jpox.util.JPOXLogger;
import org.jpox.util.Localiser;

/**
* Basic implementation of a JDO 2 datastore sequence for RDBMS datastores.
* Utilises the <B>org.jpox.store.poid</B> classes to generate
* sequence values.
* @version $Revision: 1.10 $
*/
public class JDOSequenceImpl implements Sequence, JPOXSequence
{
    /** Localisation of messages */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.Localisation",
        ObjectManager.class.getClassLoader());

    /** Store Manager where we obtain our sequence. */
    protected final MappedStoreManager storeManager;

    /** Name of the sequence. */
    protected final SequenceMetaData seqMetaData;

    /** The generator for the sequence. */
    protected final PoidGenerator generator;

    /** The controlling Object Manager. */
    protected final ObjectManager om;

    /**
     * Constructor.
     * @param objectMgr The Object Manager managing the sequence
     * @param storeMgr Manager of the store where we obtain the sequence
     * @param seqmd MetaData defining the sequence
     */
    public JDOSequenceImpl(ObjectManager objectMgr, MappedStoreManager storeMgr, SequenceMetaData seqmd)
    {
        this.om = objectMgr;
        this.storeManager = storeMgr;
        this.seqMetaData = seqmd;

        // Allocate the PoidManager for this sequence
        String poidGeneratorName = null;
        if (storeMgr.getDatastoreAdapter().supportsSequences())
        {
            poidGeneratorName = "sequence";
        }
        else
        {
            poidGeneratorName = "table-sequence";
        }

        // Create the controlling properties for this sequence
        Properties props = new Properties();
        ExtensionMetaData[] seqExtensions = seqmd.getExtensions();
        if (seqExtensions != null && seqExtensions.length > 0)
        {
            // Add all MetaData extension properties provided
            for (int i=0;i<seqExtensions.length;i++)
            {
                props.put(seqExtensions[i].getKey(), seqExtensions[i].getValue());
            }
        }
        if (poidGeneratorName.equals("sequence"))
        {
            // Use the provided name as the sequence name (what if it is null ?)
            props.put("sequence-name", seqMetaData.getDatastoreSequence());
        }
        else if (poidGeneratorName.equals("table-sequence"))
        {
            // Use the provided name as the table name (what if it is null ?)
            props.put("sequence-name", seqMetaData.getDatastoreSequence());
        }

        // Get a PoidManager to create the generator
        PoidManager mgr = storeMgr.getPoidManager();

        PoidConnectionProvider connProvider = new PoidConnectionProvider()
            {
                ManagedConnection mconn;

                public ManagedConnection retrieveConnection()
                {
                    try
                    {
                        // Obtain a new connection
                        // Note : it may be worthwhile to use the PM's connection here however where a Sequence doesnt yet
                        // exist the connection would then be effectively dead until the end of the tx
                        // The way around this would be to find a way of checking for existence of the sequence
                        PersistenceConfiguration conf = om.getOMFContext().getPersistenceConfiguration();
                        int isolationLevel = TransactionUtils.getTransactionIsolationLevelForName(conf.getStringProperty("org.jpox.poid.transactionIsolation"));
                        this.mconn = ((RDBMSManager)storeManager).getConnection(isolationLevel);
                    }
                    catch (SQLException e)
                    {
                        String msg = LOCALISER.msg("017006", e);
                        JPOXLogger.JDO.error(msg);
                        throw new JDODataStoreException(msg,e);
                    }
                    return mconn;
                }

                public void releaseConnection()
                {
                    try
                    {
                        // Release the connection
                        mconn.close();
                    }
                    catch (JPOXException e)
                    {
                        String msg = LOCALISER.msg("017007", e);
                        JPOXLogger.JDO.error(msg);
                        throw new JDODataStoreException(msg, e);
                    }
                }
            };
        Class cls = null;
        ConfigurationElement elem =
            objectMgr.getOMFContext().getPluginManager().getConfigurationElementForExtension("org.jpox.store_valuegenerator",
                new String[]{"name", "datastore"}, new String[] {poidGeneratorName, storeManager.getStoreManagerKey()});
        if (elem != null)
        {
            cls = objectMgr.getOMFContext().getPluginManager().loadClass(elem.getExtension().getPlugin().getSymbolicName(), elem.getAttribute("class-name"));
        }
        if( cls == null )
        {
            throw new JPOXException("Cannot create Poid Generator for strategy "+poidGeneratorName);
        }
        generator = mgr.createPoidGenerator(seqMetaData.getName(), cls, props, storeManager, connProvider);

        if (JPOXLogger.JDO.isDebugEnabled())
        {
            JPOXLogger.JDO.debug(LOCALISER.msg("017003", seqMetaData.getName(), poidGeneratorName));
        }
    }

    /**
     * Accessor for the sequence name.
     * @return The sequence name
     */
    public String getName()
    {
        return seqMetaData.getName();
    }

    /**
     * Method to allocate a set of elements.
     * @param additional The number of additional elements to allocate
     */
    public void allocate(int additional)
    {
        try
        {
            generator.allocate(additional);
        }
        catch (JPOXException de)
        {
        }
    }

    /**
     * Accessor for the next element in the sequence.
     * @return The next element
     */
    public Object next()
    {
        try
        {
            return generator.next();
        }
        catch (JPOXDataStoreException dse)
        {
            if (dse.getFailedObject() != null)
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getFailedObject());
            }
            else
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getNestedExceptions());
            }
        }
    }

    /**
     * Accessor for the next element in the sequence as a long.
     * @return The next element
     * @throws JDODataStoreException Thrown if not numeric
     */
    public long nextValue()
    {
        try
        {
            return generator.nextValue();
        }
        catch (JPOXDataStoreException dse)
        {
            if (dse.getFailedObject() != null)
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getFailedObject());
            }
            else
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getNestedExceptions());
            }
        }
    }

    /**
     * Accessor for the current element.
     * @return The current element.
     */
    public Object current()
    {
        try
        {
            return generator.current();
        }
        catch (JPOXDataStoreException dse)
        {
            if (dse.getFailedObject() != null)
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getFailedObject());
            }
            else
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getNestedExceptions());
            }
        }
    }

    /**
     * Accessor for the current element in the sequence as a long.
     * @return The current element
     * @throws JDODataStoreException Thrown if not numeric
     */
    public long currentValue()
    {
        try
        {
            return generator.currentValue();
        }
        catch (JPOXDataStoreException dse)
        {
            if (dse.getFailedObject() != null)
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getFailedObject());
            }
            else
            {
                throw new JDODataStoreException(dse.getMessage(), dse.getNestedExceptions());
            }
        }
    }
}
TOP

Related Classes of org.jpox.store.rdbms.JDOSequenceImpl

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.