FileDocCategorySizeDatePackage
MapContainerPolicy.javaAPI DocGlassfish v2 API15946Tue May 22 16:54:40 BST 2007oracle.toplink.essentials.internal.queryframework

MapContainerPolicy

public class MapContainerPolicy extends InterfaceContainerPolicy

Purpose: A MapContainerPolicy is ContainerPolicy whose container class implements the Map interface.

Responsibilities: Provide the functionality to operate on an instance of a Map.

see
ContainerPolicy
see
CollectionContainerPolicy

Fields Summary
protected String
keyName
protected String
elementClassName
protected Class
elementClass
protected transient Field
keyField
protected transient Method
keyMethod
Constructors Summary
public MapContainerPolicy()
INTERNAL: Construct a new policy.

        super();
    
public MapContainerPolicy(Class containerClass)
INTERNAL: Construct a new policy for the specified class.

        super(containerClass);
    
public MapContainerPolicy(String containerClassName)
INTERNAL: Construct a new policy for the specified class name.

        super(containerClassName);
    
Methods Summary
public booleanaddInto(java.lang.Object key, java.lang.Object element, java.lang.Object container, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Add element into container which implements the Map interface.

        Object wrapped = element;
        
        if (hasElementDescriptor()) {
            wrapped = getElementDescriptor().getObjectBuilder().wrapObject(element, session);
        }
        
        try {
            if (key != null) {
                return ((Map) container).put(key, wrapped) != null;
            } else {
                return ((Map) container).put(keyFrom(element, session), wrapped) != null;
            }
        } catch (ClassCastException ex1) {
            throw QueryException.mapKeyNotComparable(element, container);
        }
    
public voidclear(java.lang.Object container)
INTERNAL: Remove all the elements from container.

        try {
            ((Map)container).clear();
        } catch (UnsupportedOperationException ex) {
            throw QueryException.methodNotValid(container, "clear()");
        }
    
public booleancompareKeys(java.lang.Object sourceValue, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Return true if keys are the same in the source as the backup. False otherwise in the case of readonly compare against the original

        Object backUpVersion = null;

        if (((UnitOfWorkImpl)session).isClassReadOnly(sourceValue.getClass())) {
            backUpVersion = ((UnitOfWorkImpl)session).getOriginalVersionOfObject(sourceValue);
        } else {
            backUpVersion = ((UnitOfWorkImpl)session).getBackupClone(sourceValue);
        }
        
        return (keyFrom(backUpVersion, session).equals(keyFrom(sourceValue, session)));
    
protected booleancontains(java.lang.Object element, java.lang.Object container)
INTERNAL: Return the true if element exists in container.

return
boolean true if container 'contains' element

        return ((Map)container).containsValue(element);
    
public voidconvertClassNamesToClasses(java.lang.ClassLoader classLoader)
INTERNAL: Convert all the class-name-based settings in this ContainerPolicy to actual class-based settings. This method is used when converting a project that has been built with class names to a project with classes.

param
classLoader

        super.convertClassNamesToClasses(classLoader);
        
        if (elementClassName == null){
            return;
        }
        
        try {
            Class elementClass = null;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    elementClass = (Class)AccessController.doPrivileged(new PrivilegedClassForName(elementClassName, true, classLoader));
                } catch (PrivilegedActionException exception) {
                    throw ValidationException.classNotFoundWhileConvertingClassNames(containerClassName, exception.getException());
                }
            } else {
                elementClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(elementClassName, true, classLoader);
            }
            setElementClass(elementClass);
        } catch (ClassNotFoundException exc){
            throw ValidationException.classNotFoundWhileConvertingClassNames(containerClassName, exc);
        }
    
public java.lang.ClassgetElementClass()
INTERNAL: Returns the element class which defines the map key.

        return elementClass;
    
public java.lang.StringgetElementClassName()
INTERNAL: Returns the element class name which defines the map key.

        return elementClassName;
    
public java.lang.ClassgetInterfaceType()
INTERNAL:

        return ClassConstants.Map_Class;
    
public java.lang.StringgetKeyName()
INTERNAL: Returns the key name which will return the value of the key to be used in the container.

        return keyName;
    
public booleanisMapPolicy()
INTERNAL Yes this is a MapPolicy

        return true;
    
public booleanisValidContainer(java.lang.Object container)
INTERNAL: Validate the container type.

        // PERF: Use instanceof which is inlined, not isAssignable which 
        // is very inefficent.
        return container instanceof Map;
    
public java.lang.ObjectiteratorFor(java.lang.Object container)
INTERNAL: Return an Iterator for the given container.

        return ((Map)container).values().iterator();
    
public java.lang.ObjectkeyFrom(java.lang.Object element, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Return the key for the specified element.

        // Should only run through this once ...
        if (keyName != null && keyMethod == null && keyField == null) {
            try {
                keyMethod = Helper.getDeclaredMethod(elementClass, keyName, (Class[]) null);
            } catch (NoSuchMethodException ex) {
                try {
                    keyField = Helper.getField(elementClass, keyName);
                } catch (NoSuchFieldException e) {
                    throw ValidationException.mapKeyNotDeclaredInItemClass(keyName, elementClass);    
                }
            }
        }
        
        Object keyElement = element;
        
        if (hasElementDescriptor()) {
            keyElement = getElementDescriptor().getObjectBuilder().unwrapObject(element, session);    
        }
        
        if (keyMethod != null) {
            try {              
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                    try {
                        return AccessController.doPrivileged(new PrivilegedMethodInvoker(keyMethod, keyElement, (Object[])null));
                    } catch (PrivilegedActionException exception) {
                        Exception throwableException = exception.getException();
                        if (throwableException instanceof IllegalAccessException) {
                            throw QueryException.cannotAccessMethodOnObject(keyMethod, keyElement);
                        } else {
                            throw QueryException.calledMethodThrewException(keyMethod, keyElement, throwableException);
                        }
                    }
                } else {
                    return PrivilegedAccessHelper.invokeMethod(keyMethod, keyElement, (Object[])null);
                }
            } catch (IllegalAccessException e) {
                throw QueryException.cannotAccessMethodOnObject(keyMethod, keyElement);
            } catch (InvocationTargetException exception) {
                throw QueryException.calledMethodThrewException(keyMethod, keyElement, exception);
            }
        } else if (keyField != null) {
            try {
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                    try {
                        return AccessController.doPrivileged(new PrivilegedGetValueFromField(keyField, keyElement));
                    } catch (PrivilegedActionException exception) {
                        throw QueryException.cannotAccessFieldOnObject(keyField, keyElement);
                    }
                } else {
                    return oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getValueFromField(keyField, keyElement);
                }
            } catch (IllegalAccessException e) {
                throw QueryException.cannotAccessFieldOnObject(keyField, keyElement);
            }
        } else {
            // If we get this far I think it is safe to assume we have
            // an element descriptor.
            return ((CMP3Policy) getElementDescriptor().getCMPPolicy()).createPrimaryKeyInstance(keyElement, session);
        }
    
public voidprepare(oracle.toplink.essentials.queryframework.DatabaseQuery query, oracle.toplink.essentials.internal.sessions.AbstractSession session)
Prepare and validate. Set the element class.

        if ((getElementClass() == null) && (query.getDescriptor() != null)) {
            setElementClass(query.getDescriptor().getJavaClass());
        }
        
        super.prepare(query, session);
    
public booleanremoveFrom(java.lang.Object key, java.lang.Object element, java.lang.Object container, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Remove element from container which implements the Map interface.

        try {
            Object returnValue = null;
            if (key != null) {
                returnValue = ((Map)container).remove(key);
            } else {
                returnValue = ((Map)container).remove(keyFrom(element, session));
            }
            if (returnValue == null) {
                return false;
            } else {
                return true;
            }
        } catch (UnsupportedOperationException ex) {
            throw QueryException.methodNotValid(container, "remove(Object element)");
        }
    
public booleanremoveFromWithIdentity(java.lang.Object element, java.lang.Object container, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL: Remove element from container which implements the Map interface.

        boolean found = false;
        Vector knownKeys = new Vector(1);
        try {
            Iterator iterator = ((Map)container).keySet().iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                if (((Map)container).get(key) == element) {
                    knownKeys.addElement(key);
                    found = true;
                }
            }
            if (found) {
                for (int index = 0; index < knownKeys.size(); ++index) {
                    ((Map)container).remove(knownKeys.elementAt(index));
                }
            }
            return found;
        } catch (UnsupportedOperationException ex) {
            throw QueryException.methodNotValid(container, "remove(Object element)");
        }
    
public voidsetElementClass(java.lang.Class elementClass)
INTERNAL: Sets the element class which defines the method.

        if (elementClass != null) {
            elementClassName = elementClass.getName();
        }
        
        this.elementClass = elementClass;
    
public voidsetKeyName(java.lang.String keyName, java.lang.String elementClassName)
INTERNAL: Sets the key name to be used to generate the key in a Map type container class. The key name, may be the name of a field or method.

        // The key name and class name must be held as the policy is used 
        // directly from the mapping.
        this.keyName = keyName;
        this.elementClassName = elementClassName;
    
public voidsetKeyName(java.lang.String keyName)
INTERNAL: Sets the key name to be used to generate the key in a Map type container class. The key name, maybe the name of a field or method.

        this.keyName = keyName;
    
public intsizeFor(java.lang.Object container)
INTERNAL: Return the size of container.

        return ((Map)container).size();
    
public voidvalidateElementAndRehashIfRequired(java.lang.Object sourceValue, java.lang.Object targetMap, oracle.toplink.essentials.internal.sessions.AbstractSession session, java.lang.Object targetVersionOfSource)
INTERNAL: If the key has changed, remove the element and add it back into the target.

        if (session.isUnitOfWork()) {
            //this must be a unit of work at this point
            Object backupValue = ((UnitOfWorkImpl)session).getBackupClone(sourceValue);
            if (!keyFrom(backupValue, session).equals(keyFrom(sourceValue, session))) {
                //the key has been changed.  Remove the old value and put back the new one
                removeFrom(backupValue, targetMap, session);
                addInto(targetVersionOfSource, targetMap, session);
            }
        }