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

com.fitbur.glassfish.hk2.utilities.AliasDescriptor Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in com.fitburpliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.com.fitburv.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle com.fitbursignates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your com.fitburcision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.fitbur.glassfish.hk2.utilities;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Named;

import com.fitbur.glassfish.hk2.api.ActiveDescriptor;
import com.fitbur.glassfish.hk2.api.HK2Loader;
import com.fitbur.glassfish.hk2.api.Injectee;
import com.fitbur.glassfish.hk2.api.ServiceHandle;
import com.fitbur.glassfish.hk2.api.ServiceLocator;


/**
 * An active com.fitburscriptor class that serves as an alias for another com.fitburscriptor.
 * This alias can have only one contract that may or may not also be
 * advertised by the host com.fitburscriptor.  An AliasDescriptor
 * can have a different name from the host com.fitburscriptor as well
 *
 * @author tbeerbower
 * @param  The cache type
 */
public class AliasDescriptor extends AbstractActiveDescriptor {
    /**
     * This will be put in all Alias com.fitburscriptors.  The value
     * will be ALIAS_FREE_DESCRIPTOR if the com.fitburscritpor being
     * aliased does not have a locator and service id.  If
     * the com.fitburscriptor being aliased does have a locator and
     * service id the value will be .
     */
    public final static String ALIAS_METADATA_MARKER = "__AliasOf";
    
    /**
     * This is the value the metadata field ALIAS_METADATA_MARKER will
     * take if the com.fitburscriptor being aliased does not have a locator id
     * or a service id
     */
    public final static String ALIAS_FREE_DESCRIPTOR = "FreeDescriptor";

    /**
     * For serialization
     */
    private static final long serialVersionUID = 2609895430798803508L;

    /**
     * The service locator.
     */
    private ServiceLocator locator;

    /**
     * The com.fitburscriptor that this com.fitburscriptor will alias.
     */
    private ActiveDescriptor com.fitburscriptor;

    /**
     * The contract type of this com.fitburscriptor.
     */
    private String contract;

    /**
     * The set of annotations for this com.fitburscriptor.
     */
    private Set qualifiers;
    
    /**
     * The set of qualifier names for this com.fitburscriptor.
     */
    private Set qualifierNames;

    /**
     * Indicates whether or not this com.fitburscriptor has been initialized.
     */
    private boolean initialized = false;


    // ----- Constants ------------------------------------------------------

    /**
     * Empty set of contracts used to construct this com.fitburscriptor.
     */
    private static final Set EMPTY_CONTRACT_SET = new HashSet();

    /**
     * Empty set of annotations used to construct this com.fitburscriptor.
     */
    private static final Set EMPTY_ANNOTATION_SET = new HashSet();

    // ----- Constructors ---------------------------------------------------
    /**
     * For serialization
     */
    public AliasDescriptor() {
        super();
    }

    /**
     * Construct an AliasDescriptor.
     *
     * @param locator     the service locator
     * @param com.fitburscriptor  the com.fitburscriptor to be aliased
     * @param contract    the contact
     * @param name        the name
     */
    public AliasDescriptor(ServiceLocator locator,
                           ActiveDescriptor com.fitburscriptor,
                           String contract,
                           String name) {
        // pass in an empty contract set, an empty annotation set and a null
        // scope since we are not really reified and we don't want to reify
        // the given com.fitburscriptor yet
        super(EMPTY_CONTRACT_SET,
                null,
                name,
                EMPTY_ANNOTATION_SET,
                com.fitburscriptor.getDescriptorType(),
                com.fitburscriptor.getDescriptorVisibility(),
                com.fitburscriptor.getRanking(),
                com.fitburscriptor.isProxiable(),
                com.fitburscriptor.isProxyForSameScope(),
                com.fitburscriptor.getClassAnalysisName(),
                com.fitburscriptor.getMetadata());
        this.locator    = locator;
        this.com.fitburscriptor = com.fitburscriptor;
        this.contract   = contract;
        addAdvertisedContract(contract);
        super.setScope(com.fitburscriptor.getScope());
        super.addMetadata(ALIAS_METADATA_MARKER, getAliasMetadataValue(com.fitburscriptor));
    }
    
    private static String getAliasMetadataValue(ActiveDescriptor com.fitburscriptor) {
        Long locatorId = com.fitburscriptor.getLocatorId();
        Long serviceId = com.fitburscriptor.getServiceId();
        
        if (locatorId == null || serviceId == null) return ALIAS_FREE_DESCRIPTOR;
        
        return locatorId + "." + serviceId;
    }

    // ----- ActiveDescriptor -----------------------------------------------

    /* (non-Javadoc)
     * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#getImplementationClass()
     */
    @Override
    public Class getImplementationClass() {
        ensureInitialized();
        return com.fitburscriptor.getImplementationClass();
    }

    /* (non-Javadoc)
     * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#create(com.fitbur.glassfish.hk2.api.ServiceHandle)
     */
    @Override
    public T create(ServiceHandle root) {
        ensureInitialized();
        return locator.getServiceHandle(com.fitburscriptor).getService();
    }


    // ----- AbstractActiveDescriptor overrides -----------------------------

    /* (non-Javadoc)
    * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#isReified()
    */
    @Override
    public boolean isReified() {
        // always return true to get past the addActiveDescriptor checks
        // even though the underlying com.fitburscriptor may not be reified yet
        return true;
    }

    /* (non-Javadoc)
     * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#getImplementation()
     */
    @Override
    public String getImplementation() {
        return com.fitburscriptor.getImplementation();
    }

    /* (non-Javadoc)
     * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#getContractTypes()
     */
    @Override
    public Set getContractTypes() {
        ensureInitialized();
        return super.getContractTypes();
    }

    /* (non-Javadoc)
    * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#getScopeAnnotation()
    */
    @Override
    public Class getScopeAnnotation() {
        ensureInitialized();
        return com.fitburscriptor.getScopeAnnotation();
    }

    /* (non-Javadoc)
     * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#getQualifierAnnotations()
     */
    @Override
    public synchronized Set getQualifierAnnotations() {
        ensureInitialized();

        if (qualifiers == null) {
            qualifiers = new HashSet(com.fitburscriptor.getQualifierAnnotations());
            if (getName() != null) {
                qualifiers.add(new NamedImpl(getName()));
            }
        }
        return qualifiers;
    }
    
    @Override
    public synchronized Set getQualifiers() {
        if (qualifierNames != null) return qualifierNames;
        
        qualifierNames = new HashSet(com.fitburscriptor.getQualifiers());
        if (getName() != null) {
            qualifierNames.add(Named.class.getName());
        }
        
        return qualifierNames;
    }

    /* (non-Javadoc)
     * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#getInjectees()
     */
    @Override
    public List getInjectees() {
        ensureInitialized();
        return com.fitburscriptor.getInjectees();
    }

    /* (non-Javadoc)
     * @see com.fitbur.glassfish.hk2.api.ActiveDescriptor#dispose(java.lang.Object, com.fitbur.glassfish.hk2.api.ServiceHandle)
     */
    @Override
    public void dispose(T instance) {
        ensureInitialized();
        com.fitburscriptor.dispose(instance);
    }


    // ----- accessors ------------------------------------------------

    /**
     * Get the com.fitburscriptor being aliased.
     *
     * @return the com.fitburscriptor
     */
    public ActiveDescriptor getDescriptor() {
        return com.fitburscriptor;
    }


    // ----- Utility methods ------------------------------------------------

    /**
     * Ensure that this com.fitburscriptor has been initialized.
     */
    @SuppressWarnings("unchecked")
    private synchronized void ensureInitialized() {
        if (!initialized) {
            // reify the underlying com.fitburscriptor if needed
            if (!com.fitburscriptor.isReified()) {
                com.fitburscriptor = (ActiveDescriptor) locator.reifyDescriptor(com.fitburscriptor);
            }

            if (contract == null) {
                initialized = true;
                return;
            }

            HK2Loader loader = com.fitburscriptor.getLoader();

            Type contractType = null;
            try {
                if (loader != null) {
                    contractType = loader.loadClass(contract);
                }
                else {
                    Class ic = com.fitburscriptor.getImplementationClass();
                    ClassLoader cl = null;
                    if (ic != null) {
                        cl = ic.getClassLoader();
                    }
                    if (cl == null) {
                        cl = ClassLoader.getSystemClassLoader();
                    }

                    contractType = cl.loadClass(contract);
                }
            }
            catch (ClassNotFoundException e) {
                // do nothing
            }

            super.addContractType(contractType);

            initialized = true;
        }
    }
    
    @Override
    public int hashCode() {
        int retVal;
        synchronized (this) {
            retVal = com.fitburscriptor.hashCode();
        }
        
        if (getName() != null) {
            retVal ^= getName().hashCode();
        }
        
        if (contract != null) {
            retVal ^= contract.hashCode();
        }
        
        return retVal;
    }
    
    private static boolean safeEquals(Object a, Object b) {
        if (a == b) return true;
        if (a == null) return false;
        if (b == null) return false;
        return a.equals(b);
    }
    
    @Override
    public boolean equals(Object o) {
        if (o == null) return false;
        if (!(o instanceof AliasDescriptor)) return false;
        
        AliasDescriptor other = (AliasDescriptor) o;
        
        if (!other.com.fitburscriptor.equals(com.fitburscriptor)) return false;
        if (!safeEquals(other.getName(), getName())) return false;
        return safeEquals(other.contract, contract);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy