Package org.jpox.jdo

Source Code of org.jpox.jdo.JDOSequenceImpl

/**********************************************************************
Copyright (c) 2006 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.jdo;

import java.util.Properties;

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

import org.jpox.ManagedConnection;
import org.jpox.ObjectManager;
import org.jpox.ObjectManagerFactoryImpl;
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.StoreManager;
import org.jpox.store.poid.PoidConnectionProvider;
import org.jpox.store.poid.PoidGenerator;
import org.jpox.store.poid.PoidManager;
import org.jpox.util.JPOXLogger;
import org.jpox.util.Localiser;

/**
* Basic generic implementation of a JDO2 datastore sequence for DB4O datastores.
* Utilises the "org.jpox.store_valuegenerator" extensions.
*/
public class JDOSequenceImpl implements Sequence, JPOXSequence
{
    /** Localisation of messages */
    protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.Localisation",
        ObjectManagerFactoryImpl.class.getClassLoader());

    /** Store Manager where we obtain our sequence. */
    protected final StoreManager 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, final StoreManager storeMgr, SequenceMetaData seqmd)
    {
        this.om = objectMgr;
        this.storeManager = storeMgr;
        this.seqMetaData = seqmd;

        // Allocate the PoidManager for this sequence
        String poidGeneratorName = "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());
            }
        }
        props.put("sequence-name", seqMetaData.getDatastoreSequence());

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

        PoidConnectionProvider connProvider = new PoidConnectionProvider()
        {
            ManagedConnection mconn;
            public ManagedConnection retrieveConnection()
            {
                mconn = storeMgr.getConnection(thisOM);
                return mconn;
            }

            public void releaseConnection()
            {
                this.mconn.release();
                this.mconn = null;
            }
        };

        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.jdo.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.