Package org.datanucleus.api

Examples of org.datanucleus.api.ApiAdapter


        }

        // Generate a list of metadata for the roots of inheritance tree(s) that this identity can represent
        // Really ought to be for a single inheritance tree (hence one element in the List) but we allow for
        // a user reusing their PK class in multiple trees
        ApiAdapter api = getApiAdapter();
        List<AbstractClassMetaData> rootCmds = new ArrayList<AbstractClassMetaData>();
        if (id instanceof OID)
        {
            // Datastore Identity, so identity is an OID, and the object is of the target class or a subclass
            OID oid = (OID) id;
            AbstractClassMetaData cmd = getMetaDataManager().getMetaDataForClass(oid.getPcClass(), clr);
            rootCmds.add(cmd);
            if (cmd.getIdentityType() != IdentityType.DATASTORE)
            {
                throw new NucleusUserException(LOCALISER_RDBMS.msg("050022", id, cmd.getFullClassName()));
            }
        }
        else if (api.isSingleFieldIdentity(id))
        {
            // Using SingleFieldIdentity so can assume that object is of the target class or a subclass
            String className = api.getTargetClassNameForSingleFieldIdentity(id);
            AbstractClassMetaData cmd = getMetaDataManager().getMetaDataForClass(className, clr);
            rootCmds.add(cmd);
            if (cmd.getIdentityType() != IdentityType.APPLICATION || !cmd.getObjectidClass().equals(id.getClass().getName()))
            {
                throw new NucleusUserException(LOCALISER_RDBMS.msg("050022", id, cmd.getFullClassName()));
View Full Code Here


            return new NullLiteral(stmt, null, null, null);
        }
        if (expr instanceof SQLLiteral)
        {
            RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
            ApiAdapter api = storeMgr.getApiAdapter();
            Object id = api.getIdForObject(((SQLLiteral)expr).getValue());
            if (id == null)
            {
                return new NullLiteral(stmt, null, null, null);
            }
            else
View Full Code Here

            throw new NucleusUserException("Cannot invoke JDOHelper.getVersion on null expression");
        }
        if (expr instanceof SQLLiteral)
        {
            RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
            ApiAdapter api = storeMgr.getApiAdapter();
            Object obj = ((SQLLiteral)expr).getValue();
            if (obj == null || !api.isPersistable(obj))
            {
                return new NullLiteral(stmt, null, null, null);
            }
            else
            {
View Full Code Here

                // Make sure lifecycle changes take place to all "enlisted" objects
                List failures = null;
                try
                {
                    // "commit" all enlisted StateManagers
                    ApiAdapter api = getApiAdapter();
                    StateManager[] sms = enlistedSMCache.values().toArray(new StateManager[enlistedSMCache.size()]);
                    for (int i = 0; i < sms.length; ++i)
                    {
                        try
                        {
                            // Run through "postCommit" to migrate the lifecycle state
                            if (sms[i] != null && sms[i].getObject() != null &&
                                (api.isPersistent(sms[i].getObject()) && api.isTransactional(sms[i].getObject())))
                            {
                                sms[i].postCommit(getTransaction());
                            }
                        }
                        catch (RuntimeException e)
View Full Code Here

        {
            return null;
        }

        // TODO Support embeddedOwner/objectType, so we can add StateManager for embedded objects here
        ApiAdapter api = getApiAdapter();
        Object id = null; // Id of the object that was persisted during this process (if any)
        try
        {
            clr.setPrimary(obj.getClass().getClassLoader());
            assertClassPersistable(obj.getClass());
            ExecutionContext ec = api.getExecutionContext(obj);
            if (ec != null && ec != this)
            {
                // Object managed by a different manager
                throw new NucleusUserException(LOCALISER.msg("010007", obj));
            }

            Object persistedPc = obj; // Persisted object is the passed in pc (unless being attached as a copy)
            if (api.isDetached(obj))
            {
                // Detached : attach it
                assertDetachable(obj);
                if (getCopyOnAttach())
                {
                    // Attach a copy and return the copy
                    persistedPc = attachObjectCopy(ownerOP, obj, api.getIdForObject(obj) == null);
                }
                else
                {
                    // Attach the object
                    attachObject(ownerOP, obj, api.getIdForObject(obj) == null);
                    persistedPc = obj;
                }
            }
            else if (api.isTransactional(obj) && !api.isPersistent(obj))
            {
                // TransientTransactional : persist it
                if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                {
                    NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010015", StringUtils.toJVMIDString(obj)));
                }
                StateManager sm = findStateManager(obj);
                if (sm == null)
                {
                    throw new NucleusUserException(LOCALISER.msg("010007", getApiAdapter().getIdForObject(obj)));
                }
                sm.makePersistentTransactionalTransient();
            }
            else if (!api.isPersistent(obj))
            {
                // Transient : persist it
                if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                {
                    NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010015", StringUtils.toJVMIDString(obj)));
                }
                boolean merged = false;
                ThreadContextInfo threadInfo = acquireThreadContextInfo();
                try
                {
                    if (threadInfo.merging)
                    {
                        AbstractClassMetaData cmd = getMetaDataManager().getMetaDataForClass(obj.getClass(), clr);
                        if (cmd.getIdentityType() == IdentityType.APPLICATION)
                        {
                            Object transientId = api.getNewApplicationIdentityObjectId(obj, cmd);
                            Object existingObj = findObject(transientId, true, true, cmd.getFullClassName());
                            ObjectProvider existingOP = findObjectProvider(existingObj);
                            ((StateManager)existingOP).attach(obj);
                            id = transientId;
                            merged = true;
                            persistedPc = existingObj;
                        }
                    }
                }
                catch (NucleusObjectNotFoundException onfe)
                {
                    // Object with this id doesn't exist, so just persist the transient (below)
                }
                finally
                {
                    releaseThreadContextInfo();
                }

                if (!merged)
                {
                    StateManager sm = findStateManager(obj);
                    if (sm == null)
                    {
                        if ((objectType == ObjectProvider.EMBEDDED_COLLECTION_ELEMENT_PC ||
                                objectType == ObjectProvider.EMBEDDED_MAP_KEY_PC ||
                                objectType == ObjectProvider.EMBEDDED_MAP_VALUE_PC ||
                                objectType == ObjectProvider.EMBEDDED_PC) && ownerOP != null)
                        {
                            // SCO object
                            sm = (StateManager) ObjectProviderFactory.newForEmbedded(this, obj, false, ownerOP, ownerFieldNum);
                            sm.setPcObjectType((short) objectType);
                            sm.makePersistent();
                            id = sm.getInternalObjectId();
                        }
                        else
                        {
                            // FCO object
                            sm = (StateManager) ObjectProviderFactory.newForPersistentNew(this, obj, preInsertChanges);
                            sm.makePersistent();
                            id = sm.getInternalObjectId();
                        }
                    }
                    else
                    {
                        if (sm.getReferencedPC() == null)
                        {
                            // Persist it
                            sm.makePersistent();
                            id = sm.getInternalObjectId();
                        }
                        else
                        {
                            // Being attached, so use the attached object
                            persistedPc = sm.getReferencedPC();
                        }
                    }
                }
            }
            else if (api.isPersistent(obj) && api.getIdForObject(obj) == null)
            {
                // Embedded/Serialised : have SM but no identity, allow persist in own right
                // Should we be making a copy of the object here ?
                if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                {
                    NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010015", StringUtils.toJVMIDString(obj)));
                }
                StateManager sm = findStateManager(obj);
                sm.makePersistent();
                id = sm.getInternalObjectId();
            }
            else if (api.isDeleted(obj))
            {
                // Deleted : (re)-persist it (permitted in JPA, but not JDO - see StateManager)
                if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                {
                    NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010015", StringUtils.toJVMIDString(obj)));
                }
                StateManager sm = findStateManager(obj);
                sm.makePersistent();
                id = sm.getInternalObjectId();
            }
            else
            {
                if (api.isPersistent(obj) && api.isTransactional(obj) && api.isDirty(obj) &&
                    isDelayDatastoreOperationsEnabled())
                {
                    // Object provisionally persistent (but not in datastore) so re-run reachability maybe
                    if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                    {
View Full Code Here

        if (attachedOwnerByObject != null)
        {
            attachedOwnerByObject.put(pc, ownerOP);
        }

        ApiAdapter api = getApiAdapter();
        Object id = api.getIdForObject(pc);
        if (id != null && isInserting(pc))
        {
            // Object is being inserted in this transaction so just return
            return;
        }
        else if (id == null && !sco)
        {
            // Transient object so needs persisting
            persistObjectInternal(pc, null, null, -1, ObjectProvider.PC);
            return;
        }

        if (api.isDetached(pc))
        {
            // Detached, so migrate to attached
            if (cache != null)
            {
                StateManager l1CachedSM = (StateManager)cache.get(id);
                if (l1CachedSM != null && l1CachedSM.getObject() != pc)
                {
                    // attached object with the same id already present in the L1 cache so cannot attach in-situ
                    throw new NucleusUserException(LOCALISER.msg("010017",
                        StringUtils.toJVMIDString(pc)));
                }
            }

            if (NucleusLogger.PERSISTENCE.isDebugEnabled())
            {
                NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("010016", StringUtils.toJVMIDString(pc)));
            }
            StateManager sm =
                (StateManager) ObjectProviderFactory.newForDetached(this, pc, id, api.getVersionForObject(pc));
            sm.attach(sco);
        }
        else
        {
            // Not detached so can't attach it. Just return
View Full Code Here

        if (attachedOwnerByObject != null)
        {
            attachedOwnerByObject.put(pc, ownerOP);
        }

        ApiAdapter api = getApiAdapter();
        Object id = api.getIdForObject(pc);
        if (id != null && isInserting(pc))
        {
            // Object is being inserted in this transaction
            return pc;
        }
        else if (id == null && !sco)
        {
            // Object was not persisted before so persist it
            return persistObjectInternal(pc, null, null, -1, ObjectProvider.PC);
        }
        else if (api.isPersistent(pc))
        {
            // Already persistent hence can't be attached
            return pc;
        }
View Full Code Here

                sm.loadFieldValues(fv); // Load the values retrieved by the query
                return pc;
            }
            if (checkInheritance)
            {
                ApiAdapter api = getApiAdapter();
                if (oid instanceof OID || api.isSingleFieldIdentity(oid))
                {
                    // Check if this id for any known subclasses is in the cache to save searching
                    String[] subclasses = getMetaDataManager().getSubclassesForClass(pcClass.getName(), true);
                    if (subclasses != null)
                    {
                        for (int i=0;i<subclasses.length;i++)
                        {
                            if (api.isDatastoreIdentity(oid))
                            {
                                oid = OIDFactory.getInstance(getNucleusContext(), subclasses[i], ((OID)oid).getKeyValue());
                            }
                            else if (api.isSingleFieldIdentity(oid))
                            {
                                oid = api.getNewSingleFieldIdentity(oid.getClass(), clr.classForName(subclasses[i]),
                                    api.getTargetKeyForSingleFieldIdentity(oid));
                            }
                            pc = getObjectFromCache(oid);
                            if (pc != null)
                            {
                                sm = findStateManager(pc);
View Full Code Here

     */
    public Object[] findObjects(Object[] identities, boolean validate)
    {
        assertIsOpen();

        ApiAdapter api = getApiAdapter();
        Object[] objs = new Object[identities.length];
        Object[] ids = new Object[identities.length];
        List idsToFind = new ArrayList();
        for (int i=0;i<identities.length;i++)
        {
            if (identities[i] == null)
            {
                throw new NucleusUserException(LOCALISER.msg("010044"));
            }

            // Translate the identity if required
            if (getNucleusContext().getIdentityStringTranslator() != null && identities[i] instanceof String)
            {
                // DataNucleus extension to translate input identities into valid persistent identities.
                IdentityStringTranslator translator = getNucleusContext().getIdentityStringTranslator();
                ids[i] = translator.getIdentity(this, (String)identities[i]);
            }
            else
            {
                ids[i] = identities[i];
            }

            // Find the object in the cache if possible
            Object pc = getObjectFromCache(ids[i]);
            if (pc != null)
            {
                objs[i] = pc;
                if (ids[i] instanceof SCOID)
                {
                    if (api.isPersistent(pc) && !api.isNew(pc) && !api.isDeleted(pc) && !api.isTransactional(pc))
                    {
                        // JDO2 [5.4.4] Can't return HOLLOW nondurable objects
                        throw new NucleusUserException(LOCALISER.msg("010005"));
                    }
                }
            }
            else
            {
                idsToFind.add(ids[i]);
            }
        }

        // Try to find missing objects direct from the datastore if supported by the StoreManager
        Object[] foundPcs = null;
        foundPcs = getStoreManager().getPersistenceHandler().findObjects(this, idsToFind.toArray());

        int foundPcIdx = 0;

        for (int i=0;i<ids.length;i++)
        {
            Object id = ids[i];
            Object pc = objs[i];
            boolean fromCache = true;
            if (pc == null)
            {
                // Try the "findObjects" results in case supported by StoreManager
                pc = foundPcs[foundPcIdx];
                foundPcIdx++;
            }

            StateManager sm = null;
            if (pc == null)
            {
                // Object not found yet, so work out class name
                String className = null;
                String originalClassName = null;
                boolean checkedClassName = false;
                if (id instanceof SCOID)
                {
                    throw new NucleusUserException(LOCALISER.msg("010006"));
                }
                else if (id instanceof DatastoreUniqueOID)
                {
                    // Should have been found using "persistenceHandler.findObject()"
                    throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                }
                else if (api.isDatastoreIdentity(id) || api.isSingleFieldIdentity(id))
                {
                    // OID or SingleFieldIdentity, so check that the implied class is managed
                    originalClassName = getStoreManager().manageClassForIdentity(id, clr);
                }
                else
                {
                    // We dont know the object class so try to deduce it from what is known by the StoreManager
                    originalClassName = getStoreManager().getClassNameForObjectID(id, clr, this);
                    checkedClassName = true;
                }

                if (validate)
                {
                    // Validate the inheritance level
                    className =
                        (checkedClassName ? originalClassName : getStoreManager().getClassNameForObjectID(id, clr, this));
                    if (className == null)
                    {
                        throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                    }

                    if (originalClassName != null && !originalClassName.equals(className))
                    {
                        // Inheritance check implies different inheritance level, so retry
                        if (api.isDatastoreIdentity(id))
                        {
                            // Create new OID using correct target class, and recheck cache
                            id = OIDFactory.getInstance(getNucleusContext(), className, ((OID)id).getKeyValue());
                            pc = getObjectFromCache(id);
                        }
                        else if (api.isSingleFieldIdentity(id))
                        {
                            // Create new SingleFieldIdentity using correct targetClass, and recheck cache
                            id = api.getNewSingleFieldIdentity(id.getClass(), clr.classForName(className),
                                api.getTargetKeyForSingleFieldIdentity(id));
                            pc = getObjectFromCache(id);
                        }
                    }
                }
                else
                {
                    className = originalClassName;
                }

                if (pc == null)
                {
                    // Still not found so create a Hollow instance with the supplied field values
                    try
                    {
                        Class pcClass = clr.classForName(className, (id instanceof OID) ? null : id.getClass().getClassLoader());
                        sm = (StateManager) ObjectProviderFactory.newForHollow(this, pcClass, id);
                        pc = sm.getObject();
                        fromCache = false;
                    }
                    catch (ClassNotResolvedException e)
                    {
                        NucleusLogger.PERSISTENCE.warn(LOCALISER.msg("010027", getIdentityAsString(id)));
                        throw new NucleusUserException(LOCALISER.msg("010027", getIdentityAsString(id)), e);
                    }
                }
            }

            // TODO Perform locate of uncached objects in single operation using persistenceHandler.locateObjects
            boolean performValidationWhenCached =
                (context.getPersistenceConfiguration().getBooleanProperty("datanucleus.findObject.validateWhenCached"));
            if (validate && (!fromCache || performValidationWhenCached))
            {
                if (fromCache && pc != null && api.isTransactional(pc))
                {
                    // JDO2 [12.6.5] Already an object with the same id and it's transactional, so use it
                    objs[i] = pc;
                    continue;
                }
View Full Code Here

        }

        // try to find object in cache(s)
        Object pc = getObjectFromCache(id);
        boolean fromCache = true;
        ApiAdapter api = getApiAdapter();
        if (id instanceof SCOID && pc != null)
        {
            if (api.isPersistent(pc) && !api.isNew(pc) && !api.isDeleted(pc) && !api.isTransactional(pc))
            {
                // JDO2 [5.4.4] Cant return HOLLOW nondurable objects
                throw new NucleusUserException(LOCALISER.msg("010005"));
            }
        }

        if (pc != null && api.isTransactional(pc))
        {
            // JDO2 [12.6.5] If there's already an object with the same id and it's transactional, return it
            return pc;
        }

        StateManager sm = null;
        if (pc == null)
        {
            // Find it direct from the store if the store supports that
            pc = getStoreManager().getPersistenceHandler().findObject(this, id);

            if (pc == null)
            {
                // Object not found in cache(s) with this identity
                String className = null;
                String originalClassName = null;
                boolean checkedClassName = false;
                if (id instanceof SCOID)
                {
                    throw new NucleusUserException(LOCALISER.msg("010006"));
                }
                else if (id instanceof DatastoreUniqueOID)
                {
                    throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                }
                else if (api.isDatastoreIdentity(id) || api.isSingleFieldIdentity(id))
                {
                    // DatastoreIdentity or SingleFieldIdentity, so check that the implied class is managed
                    originalClassName = getStoreManager().manageClassForIdentity(id, clr);
                }
                else if (objectClassName != null)
                {
                    // Object class name specified so use that directly
                    originalClassName = objectClassName;
                }
                else
                {
                    // We dont know the object class so try to deduce it from what is known by the StoreManager
                    originalClassName = getStoreManager().getClassNameForObjectID(id, clr, this);
                    checkedClassName = true;
                }

                if (checkInheritance)
                {
                    // Verify if correct class inheritance level is set
                    if (!checkedClassName)
                    {
                        className = getStoreManager().getClassNameForObjectID(id, clr, this);
                    }
                    else
                    {
                        // We just checked the name of the class in the section above so just use that
                        className = originalClassName;
                    }

                    if (className == null)
                    {
                        throw new NucleusObjectNotFoundException(LOCALISER.msg("010026"), id);
                    }

                    if (originalClassName != null && !originalClassName.equals(className))
                    {
                        // Inheritance checking has found a different inherited
                        // object with this identity so create new id
                        if (api.isDatastoreIdentity(id))
                        {
                            // Create new OID using correct target class and recheck the cache
                            id = OIDFactory.getInstance(getNucleusContext(), className, ((OID)id).getKeyValue());
                            pc = getObjectFromCache(id);
                        }
                        else if (api.isSingleFieldIdentity(id))
                        {
                            // Create new SingleFieldIdentity using correct targetClass and recheck the cache
                            id = api.getNewSingleFieldIdentity(id.getClass(), clr.classForName(className),
                                    api.getTargetKeyForSingleFieldIdentity(id));
                            pc = getObjectFromCache(id);
                        }
                    }
                }
                else
View Full Code Here

TOP

Related Classes of org.datanucleus.api.ApiAdapter

Copyright © 2018 www.massapicom. 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.