Search in sources :

Example 96 with JDOUserException

use of javax.jdo.JDOUserException 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)

Example 97 with JDOUserException

use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManager method deletePersistentAll.

/**
 * JDO method to delete a collection of objects from the datastore.
 * Throws a JDOUserException if objects could not be deleted.
 * @param pcs The objects
 * @throws JDOUserException Thrown if one (or more) object cannot be deleted
 */
public void deletePersistentAll(Collection pcs) {
    assertIsOpen();
    assertWritable();
    try {
        ec.deleteObjects(pcs.toArray());
    } catch (NucleusUserException nue) {
        Throwable[] failures = nue.getNestedExceptions();
        throw new JDOUserException(Localiser.msg("010040"), failures);
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JDOUserException(javax.jdo.JDOUserException)

Example 98 with JDOUserException

use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManagerFactory method processLifecycleListenersFromProperties.

/**
 * Convenience method to extract lifecycle listeners that are specified by way of persistence properties.
 * @param props Persistence props.
 */
protected void processLifecycleListenersFromProperties(Map props) {
    if (props != null) {
        // Process any lifecycle listeners defined in persistent properties
        Iterator<Map.Entry> propsIter = props.entrySet().iterator();
        while (propsIter.hasNext()) {
            Map.Entry entry = propsIter.next();
            String key = (String) entry.getKey();
            if (key.startsWith(Constants.PROPERTY_INSTANCE_LIFECYCLE_LISTENER)) {
                String listenerClsName = key.substring(45);
                String listenerClasses = (String) entry.getValue();
                ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
                Class listenerCls = null;
                try {
                    listenerCls = clr.classForName(listenerClsName);
                } catch (ClassNotResolvedException cnre) {
                    throw new JDOUserException(Localiser.msg("012022", listenerClsName));
                }
                InstanceLifecycleListener listener = null;
                // Find method getInstance()
                Method method = ClassUtils.getMethodForClass(listenerCls, "getInstance", null);
                if (method != null) {
                    // Create instance via getInstance()
                    try {
                        listener = (InstanceLifecycleListener) method.invoke(null);
                    } catch (Exception e) {
                        throw new JDOUserException(Localiser.msg("012021", listenerClsName), e);
                    }
                } else {
                    // Try default constructor
                    try {
                        listener = (InstanceLifecycleListener) listenerCls.newInstance();
                    } catch (Exception e) {
                        throw new JDOUserException(Localiser.msg("012020", listenerClsName), e);
                    }
                }
                Class[] classes = null;
                if (!StringUtils.isWhitespace(listenerClasses)) {
                    String[] classNames = StringUtils.split(listenerClasses, ",");
                    classes = new Class[classNames.length];
                    for (int i = 0; i < classNames.length; i++) {
                        classes[i] = clr.classForName(classNames[i]);
                    }
                }
                addInstanceLifecycleListener(listener, classes);
            }
        }
    }
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Method(java.lang.reflect.Method) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) InvalidObjectException(java.io.InvalidObjectException) JDOFatalUserException(javax.jdo.JDOFatalUserException) NucleusException(org.datanucleus.exceptions.NucleusException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TransactionIsolationNotSupportedException(org.datanucleus.exceptions.TransactionIsolationNotSupportedException) TransactionActiveOnCloseException(org.datanucleus.exceptions.TransactionActiveOnCloseException) IOException(java.io.IOException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) InstanceLifecycleListener(javax.jdo.listener.InstanceLifecycleListener) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 99 with JDOUserException

use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.

the class JDOPersistenceManagerFactory method setServerTimeZoneID.

/**
 * Mutator for the timezone id of the datastore server.
 * If not set assumes that it is running in the same timezone as this JVM.
 * @param id Timezone Id to use
 */
public void setServerTimeZoneID(String id) {
    assertConfigurable();
    boolean validated = new CorePropertyValidator().validate(PropertyNames.PROPERTY_SERVER_TIMEZONE_ID, id);
    if (validated) {
        getConfiguration().setProperty(PropertyNames.PROPERTY_SERVER_TIMEZONE_ID, id);
    } else {
        throw new JDOUserException("Invalid TimeZone ID specified");
    }
}
Also used : CorePropertyValidator(org.datanucleus.properties.CorePropertyValidator) JDOUserException(javax.jdo.JDOUserException)

Example 100 with JDOUserException

use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.

the class JDOReplicationManager method replicate.

/**
 * Method to perform the replication for all objects of the specified class names.
 * @param classNames Classes to replicate
 */
public void replicate(String... classNames) {
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(classNames)));
    }
    // Check if classes are detachable
    NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext();
    MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager();
    ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null);
    for (int i = 0; i < classNames.length; i++) {
        AbstractClassMetaData cmd = mmgr.getMetaDataForClass(classNames[i], clr);
        if (!cmd.isDetachable()) {
            throw new JDOUserException("Class " + classNames[i] + " is not detachable so cannot replicate");
        }
    }
    Object[] detachedObjects = null;
    // Detach from datastore 1
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("012053"));
    }
    PersistenceManager pm1 = pmfSource.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    if (getBooleanProperty("datanucleus.replicateObjectGraph")) {
        pm1.getFetchPlan().setGroup(javax.jdo.FetchPlan.ALL);
        pm1.getFetchPlan().setMaxFetchDepth(-1);
    }
    try {
        tx1.begin();
        clr = ((JDOPersistenceManager) pm1).getExecutionContext().getClassLoaderResolver();
        ArrayList objects = new ArrayList();
        for (int i = 0; i < classNames.length; i++) {
            Class cls = clr.classForName(classNames[i]);
            AbstractClassMetaData cmd = mmgr.getMetaDataForClass(cls, clr);
            if (!cmd.isEmbeddedOnly()) {
                Extent ex = pm1.getExtent(cls);
                Iterator iter = ex.iterator();
                while (iter.hasNext()) {
                    objects.add(iter.next());
                }
            }
        }
        Collection detachedColl = pm1.detachCopyAll(objects);
        detachedObjects = detachedColl.toArray();
        tx1.commit();
    } finally {
        if (tx1.isActive()) {
            tx1.rollback();
        }
        pm1.close();
    }
    replicateInTarget(detachedObjects);
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JDOUserException(javax.jdo.JDOUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection)

Aggregations

JDOUserException (javax.jdo.JDOUserException)191 PersistenceManager (javax.jdo.PersistenceManager)163 Transaction (javax.jdo.Transaction)161 Query (javax.jdo.Query)94 Iterator (java.util.Iterator)54 Collection (java.util.Collection)45 Employee (org.jpox.samples.models.company.Employee)35 List (java.util.List)33 HashSet (java.util.HashSet)30 ArrayList (java.util.ArrayList)22 Extent (javax.jdo.Extent)22 JDOException (javax.jdo.JDOException)15 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)15 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)15 Map (java.util.Map)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 JDOFatalUserException (javax.jdo.JDOFatalUserException)12 NucleusException (org.datanucleus.exceptions.NucleusException)10 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)10 Person (org.jpox.samples.models.company.Person)10