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

org.eclipse.persistence.internal.jpa.metamodel.proxy.AttributeProxyImpl Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*******************************************************************************
 * Copyright (c)  2013 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     tware - initial implementation
 ******************************************************************************/
package org.eclipse.persistence.internal.jpa.metamodel.proxy;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.lang.reflect.Member;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;

import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl;

/**
 * A proxy class that allows EclipseLink to trigger the deployment of a persistence unit
 * as an Attribute is accessed in the metamodel.
 * @author tware
 *
 * @param 
 * @param 
 */
public class AttributeProxyImpl implements Attribute, Serializable {

    protected Attribute attribute = null;
    protected Set> factories = new HashSet>();
    
    public synchronized Attribute getAttribute(){
        if (attribute == null){
            for (WeakReference factoryRef: factories){
                EntityManagerFactoryImpl factory = factoryRef.get();
                if (factory != null){
                    factory.getDatabaseSession();
                } else {
                    factories.remove(factoryRef);
                }
            }
        }
        return attribute;
    }
    
    public synchronized Attribute getAttributeInternal(){
        return attribute;
    }
    
    public synchronized void setAttribute(Attribute attribute){
        this.attribute = attribute;
    }
    
    public synchronized void addFactory(EntityManagerFactoryImpl factory){
        for (WeakReference factoryRef: factories){
            
            if (factoryRef.get() != null && factoryRef.get().equals(factory)){
                return;
            }
        }
        factories.add(new WeakReference(factory));
    }
    
    @Override
    public String getName() {
        return getAttribute().getName();
    }

    @Override
    public javax.persistence.metamodel.Attribute.PersistentAttributeType getPersistentAttributeType() {
        return getAttribute().getPersistentAttributeType();
    }

    @Override
    public ManagedType getDeclaringType() {
        return getAttribute().getDeclaringType();
    }

    @Override
    public Class getJavaType() {
        return getAttribute().getJavaType();
    }

    @Override
    public Member getJavaMember() {
        return getAttribute().getJavaMember();
    }

    @Override
    public boolean isAssociation() {
        return getAttribute().isAssociation();
    }

    @Override
    public boolean isCollection() {
        return getAttribute().isCollection();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy