Search in sources :

Example 1 with NucleusSequence

use of org.datanucleus.store.NucleusSequence in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method getSequence.

// ------------------------------------- Sequence Management --------------------------------------
/**
 * Method to retrieve a sequence by name. As per JDO2 spec section 12.14.
 * If the named sequence is not known, throws a JDOUserException.
 * @param sequenceName Fully qualified name of the sequence
 * @return The sequence
 */
public Sequence getSequence(String sequenceName) {
    assertIsOpen();
    SequenceMetaData seqmd = ec.getMetaDataManager().getMetaDataForSequence(ec.getClassLoaderResolver(), sequenceName);
    if (seqmd == null) {
        throw new JDOUserException(Localiser.msg("017000", sequenceName));
    }
    Sequence seq = null;
    if (seqmd.getFactoryClass() != null) {
        // User has specified a factory class
        seq = pmf.getSequenceForFactoryClass(seqmd.getFactoryClass());
        if (seq == null) {
            // Create a new instance of the factory class and obtain the Sequence
            Class factory = ec.getClassLoaderResolver().classForName(seqmd.getFactoryClass());
            if (factory == null) {
                throw new JDOUserException(Localiser.msg("017001", sequenceName, seqmd.getFactoryClass()));
            }
            Class[] argTypes = null;
            Object[] arguments = null;
            if (seqmd.getStrategy() != null) {
                argTypes = new Class[2];
                argTypes[0] = String.class;
                argTypes[1] = String.class;
                arguments = new Object[2];
                arguments[0] = seqmd.getName();
                arguments[1] = seqmd.getStrategy().toString();
            } else {
                argTypes = new Class[1];
                argTypes[0] = String.class;
                arguments = new Object[1];
                arguments[0] = seqmd.getName();
            }
            Method newInstanceMethod;
            try {
                // Obtain the sequence from the static "newInstance(...)" method
                newInstanceMethod = factory.getMethod("newInstance", argTypes);
                seq = (Sequence) newInstanceMethod.invoke(null, arguments);
            } catch (Exception e) {
                throw new JDOUserException(Localiser.msg("017002", seqmd.getFactoryClass(), e.getMessage()));
            }
            // Register the sequence with the PMF
            pmf.addSequenceForFactoryClass(seqmd.getFactoryClass(), seq);
        }
    } else {
        NucleusSequence nucSeq = ec.getStoreManager().getNucleusSequence(ec, seqmd);
        seq = new JDOSequence(nucSeq);
    }
    return seq;
}
Also used : Sequence(javax.jdo.datastore.Sequence) NucleusSequence(org.datanucleus.store.NucleusSequence) Method(java.lang.reflect.Method) NucleusSequence(org.datanucleus.store.NucleusSequence) JDOUserException(javax.jdo.JDOUserException) SequenceMetaData(org.datanucleus.metadata.SequenceMetaData) JDONullIdentityException(javax.jdo.JDONullIdentityException) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) NucleusException(org.datanucleus.exceptions.NucleusException) TransactionActiveOnCloseException(org.datanucleus.exceptions.TransactionActiveOnCloseException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusOptimisticException(org.datanucleus.exceptions.NucleusOptimisticException)

Aggregations

Method (java.lang.reflect.Method)1 JDOException (javax.jdo.JDOException)1 JDOFatalUserException (javax.jdo.JDOFatalUserException)1 JDONullIdentityException (javax.jdo.JDONullIdentityException)1 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)1 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)1 JDOUserException (javax.jdo.JDOUserException)1 Sequence (javax.jdo.datastore.Sequence)1 TransactionNotActiveException (org.datanucleus.api.jdo.exceptions.TransactionNotActiveException)1 TransactionNotWritableException (org.datanucleus.api.jdo.exceptions.TransactionNotWritableException)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusOptimisticException (org.datanucleus.exceptions.NucleusOptimisticException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)1 SequenceMetaData (org.datanucleus.metadata.SequenceMetaData)1 NucleusSequence (org.datanucleus.store.NucleusSequence)1