All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.identity.IdentityManagerImpl Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.9
Show newest version
/**********************************************************************
Copyright (c) 2014 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.datanucleus.identity;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.datanucleus.ClassConstants;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.PersistenceNucleusContext;
import org.datanucleus.PropertyNames;
import org.datanucleus.enhancement.Persistable;
import org.datanucleus.enhancer.EnhancementHelper;
import org.datanucleus.exceptions.ClassNotResolvedException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.util.ClassUtils;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;

/**
 * Manager for identity operations.
 */
public class IdentityManagerImpl implements IdentityManager
{
    private static final Class[] CTR_CLASS_OBJECT_ARG_TYPES = new Class[] {Class.class, Object.class};
    private static final Class[] CTR_CLASS_STRING_ARG_TYPES = new Class[] {Class.class, ClassConstants.JAVA_LANG_STRING};
    private static final Class[] CTR_STRING_OBJECT_ARG_TYPES = new Class[] {ClassConstants.JAVA_LANG_STRING, Object.class};
    private static final Class[] CTR_STRING_ARG_TYPES = new Class[] {ClassConstants.JAVA_LANG_STRING};
    private static final Class[] CTR_LONG_ARG_TYPES = new Class[] {ClassConstants.JAVA_LANG_LONG};

    /** Default DatastoreId implementation used by DataNucleus. */
    protected Class datastoreIdClass = null;

    /** Identity string translator (if any). */
    protected IdentityStringTranslator idStringTranslator = null;

    /** Identity key translator (if any). */
    protected IdentityKeyTranslator idKeyTranslator = null;

    /** Cache of id class Constructor, keyed by string of the type+args. */
    private Map> constructorCache = new ConcurrentHashMap<>();

    public IdentityManagerImpl(PersistenceNucleusContext nucCtx)
    {
        // Datastore Identity type
        String dsidName = nucCtx.getConfiguration().getStringProperty(PropertyNames.PROPERTY_DATASTORE_IDENTITY_TYPE);
        String dsidNameLower = dsidName.toLowerCase();
        if ("datanucleus".equals(dsidNameLower))
        {
            datastoreIdClass = DatastoreIdImpl.class;
        }
        else if ("kodo".equals(dsidNameLower))
        {
            datastoreIdClass = DatastoreIdImplKodo.class;
        }
        else if ("xcalia".equals(dsidNameLower))
        {
            datastoreIdClass = DatastoreIdImplXcalia.class;
        }
        else if ("unique".equals(dsidNameLower))
        {
            datastoreIdClass = DatastoreUniqueLongId.class;
        }
        else
        {
            // Fallback to plugin mechanism
            String datastoreIdentityClassName = nucCtx.getPluginManager().getAttributeValueForExtension("org.datanucleus.store_datastoreidentity", "name", dsidName, "class-name");
            if (datastoreIdentityClassName == null)
            {
                // User has specified a datastore_identity plugin that has not registered
                throw new NucleusUserException(Localiser.msg("002001", dsidName)).setFatal();
            }
            ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
            try
            {
                datastoreIdClass = clr.classForName(datastoreIdentityClassName, org.datanucleus.ClassConstants.NUCLEUS_CONTEXT_LOADER);
            }
            catch (ClassNotResolvedException cnre)
            {
                throw new NucleusUserException(Localiser.msg("002002", dsidName, datastoreIdentityClassName)).setFatal();
            }
        }

        // Identity key translation
        String keyTranslatorType = nucCtx.getConfiguration().getStringProperty(PropertyNames.PROPERTY_IDENTITY_KEY_TRANSLATOR_TYPE);
        if (keyTranslatorType != null)
        {
            try
            {
                idKeyTranslator = (IdentityKeyTranslator)nucCtx.getPluginManager().createExecutableExtension(
                    "org.datanucleus.identity_key_translator", "name", keyTranslatorType, "class-name", null, null);
            }
            catch (Exception e)
            {
                // User has specified a identity key translator plugin that has not registered
                throw new NucleusUserException(Localiser.msg("002001", keyTranslatorType)).setFatal();
            }
        }

        // Identity string translation
        String stringTranslatorType = nucCtx.getConfiguration().getStringProperty(PropertyNames.PROPERTY_IDENTITY_STRING_TRANSLATOR_TYPE);
        if (stringTranslatorType != null)
        {
            if ("xcalia".equals(stringTranslatorType.toLowerCase()))
            {
                idStringTranslator = new XcaliaIdentityStringTranslator();
            }
            else
            {
                // Fallback to the plugin mechanism
                try
                {
                    idStringTranslator = (IdentityStringTranslator)nucCtx.getPluginManager().createExecutableExtension(
                        "org.datanucleus.identity_string_translator", "name", stringTranslatorType, "class-name", null, null);
                }
                catch (Exception e)
                {
                    // User has specified a string identity translator plugin that has not registered
                    throw new NucleusUserException(Localiser.msg("002001", stringTranslatorType)).setFatal();
                }
            }
        }
    }

    protected String getConstructorNameForCache(Class type, Class[] ctrArgTypes)
    {
        StringBuilder name = new StringBuilder(type.getName());
        for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy