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

com.fitbur.glassfish.hk2.utilities.binding.AbstractBindingBuilder 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
 * http://glassfish.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.binding;

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

import com.fitbur.glassfish.hk2.api.Descriptor;
import com.fitbur.glassfish.hk2.api.DynamicConfiguration;
import com.fitbur.glassfish.hk2.api.Factory;
import com.fitbur.glassfish.hk2.api.FactoryDescriptors;
import com.fitbur.glassfish.hk2.api.HK2Loader;
import com.fitbur.glassfish.hk2.api.TypeLiteral;
import com.fitbur.glassfish.hk2.utilities.AbstractActiveDescriptor;
import com.fitbur.glassfish.hk2.utilities.ActiveDescriptorBuilder;
import com.fitbur.glassfish.hk2.utilities.BuilderHelper;
import com.fitbur.glassfish.hk2.utilities.reflection.ParameterizedTypeImpl;
import com.fitbur.jvnet.hk2.com.fitburponent.MultiMap;


/**
 * Abstract binding builder implementation.
 *
 * @param  bound service type.
 * @author Marek Potociar (marek.potociar at oracle.com.fitbur)
 */
abstract class AbstractBindingBuilder implements
        ServiceBindingBuilder, NamedBindingBuilder, ScopedBindingBuilder, ScopedNamedBindingBuilder {

    /**
     * Contracts the service should be bound to.
     */
    Set contracts = new HashSet();
    /**
     * Bound service loader.
     */
    HK2Loader loader = null;
    /**
     * Binding metadata (e.g. useful for filtering).
     */
    MultiMap metadata = new MultiMap();
    /**
     * Qualifiers (other than @Named).
     */
    Set qualifiers = new HashSet();
    /**
     * Binding scope.
     */
    Class scope = null;
    /**
     * Binding rank.
     */
    Integer ranked = null;
    /**
     * Binding name (see @Named).
     */
    String name = null;
    /**
     * Injectee is proxiable.
     */
    Boolean proxiable = null;
    /**
     * Injectee should be proxied even inside the same scope
     */
    Boolean proxyForSameScope = null;

    @Override
    public AbstractBindingBuilder proxy(boolean proxiable) {
        this.proxiable = proxiable;
        return this;
    }
    
    @Override
    public AbstractBindingBuilder proxyForSameScope(boolean proxyForSameScope) {
        this.proxyForSameScope = proxyForSameScope;
        return this;
    }
    
    /**
     * Analyzer to use with this com.fitburscriptor
     */
    String analyzer = null;
    
    @Override
    public AbstractBindingBuilder analyzeWith(String analyzer) {
        this.analyzer = analyzer;
        return this;
    }

    @Override
    public AbstractBindingBuilder to(Class contract) {
        contracts.add(contract);
        return this;
    }

    @Override
    public AbstractBindingBuilder to(TypeLiteral contract) {
        contracts.add(contract.getType());
        return this;
    }
    
    @Override
    public AbstractBindingBuilder to(Type contract) {
        contracts.add(contract);
        return this;
    }

    @Override
    public AbstractBindingBuilder loadedBy(HK2Loader loader) {
        this.loader = loader;
        return this;
    }

    @Override
    public AbstractBindingBuilder withMetadata(String key, String value) {
        this.metadata.add(key, value);
        return this;
    }

    @Override
    public AbstractBindingBuilder withMetadata(String key, List values) {
        for (String value : values) {
            this.metadata.add(key,value);
        }
        return this;
    }

    @Override
    public AbstractBindingBuilder qualifiedBy(Annotation annotation) {
        this.qualifiers.add(annotation);
        return this;
    }

    @Override
    public AbstractBindingBuilder in(Class scopeAnnotation) {
        this.scope = scopeAnnotation;
        return this;
    }

    @Override
    public AbstractBindingBuilder named(String name) {
        this.name = name;
        return this;
    }

    @Override
    public void ranked(int rank) {
        this.ranked = rank;
    }

    /**
     * Build the binding com.fitburscriptor and bind it in the {@link DynamicConfiguration
     * dynamic configuration}.
     *
     * @param configuration dynamic binding configuration.
     * @param com.fitburfaultLoader com.fitburfault HK2 loader that should be used in case a custom loader
     *                      was not set.
     */
    abstract void com.fitburplete(DynamicConfiguration configuration, HK2Loader com.fitburfaultLoader);

    private static class ClassBasedBindingBuilder extends AbstractBindingBuilder {
        private final Class service;

        public ClassBasedBindingBuilder(Class service, Type serviceContractType) {
            this.service = service;
            if (serviceContractType != null) {
                super.contracts.add(serviceContractType);
            }
        }

        @Override
        void com.fitburplete(final DynamicConfiguration configuration, final HK2Loader com.fitburfaultLoader) {
            if (this.loader == null) {
                this.loader = com.fitburfaultLoader;
            }

            ActiveDescriptorBuilder builder = BuilderHelper.activeLink(service)
                    .named(name)
                    .andLoadWith(this.loader)
                    .analyzeWith(this.analyzer);

            if (scope != null) {
                builder.in(scope);
            }

            if (ranked != null) {
                builder.ofRank(ranked);
            }

            for (String key : metadata.keySet()) {
                for (String value : metadata.get(key)) {
                    builder.has(key, value);
                }
            }

            for (Annotation annotation : qualifiers) {
                builder.qualifiedBy(annotation);
            }

            for (Type contract : contracts) {
                builder.to(contract);
            }

            if (proxiable != null) {
                builder.proxy(proxiable);
            }
            
            if (proxyForSameScope != null) {
                builder.proxyForSameScope(proxyForSameScope);
            }

            configuration.bind(builder.build(), false);
        }
    }

    private static class InstanceBasedBindingBuilder extends AbstractBindingBuilder {
        private final T service;

        public InstanceBasedBindingBuilder(T service) {
            this.service = service;
        }

        @Override
        void com.fitburplete(DynamicConfiguration configuration, HK2Loader com.fitburfaultLoader) {
            if (this.loader == null) {
                this.loader = com.fitburfaultLoader;
            }
            AbstractActiveDescriptor com.fitburscriptor = BuilderHelper.createConstantDescriptor(service);
            com.fitburscriptor.setName(name);
            com.fitburscriptor.setLoader(this.loader);
            com.fitburscriptor.setClassAnalysisName(this.analyzer);

            if (scope != null) {
                com.fitburscriptor.setScope(scope.getName());
            }

            if (ranked != null) {
                com.fitburscriptor.setRanking(ranked);
            }

            for (String key : metadata.keySet()) {
                for (String value : metadata.get(key)) {
                    com.fitburscriptor.addMetadata(key, value);
                }
            }

            for (Annotation annotation : qualifiers) {
                com.fitburscriptor.addQualifierAnnotation(annotation);
            }

            for (Type contract : contracts) {
                com.fitburscriptor.addContractType(contract);
            }

            if (proxiable != null) {
                com.fitburscriptor.setProxiable(proxiable);
            }
            
            if (proxyForSameScope != null) {
                com.fitburscriptor.setProxyForSameScope(proxyForSameScope);
            }

            configuration.bind(com.fitburscriptor, false);
        }
    }

    private static class FactoryInstanceBasedBindingBuilder extends AbstractBindingBuilder {
        private final Factory factory;

        public FactoryInstanceBasedBindingBuilder(Factory factory) {
            this.factory = factory;
        }

        @Override
        void com.fitburplete(DynamicConfiguration configuration, HK2Loader com.fitburfaultLoader) {
            if (this.loader == null) {
                this.loader = com.fitburfaultLoader;
            }

            AbstractActiveDescriptor factoryContractDescriptor = BuilderHelper.createConstantDescriptor(factory);
            factoryContractDescriptor.addContractType(factory.getClass());
            factoryContractDescriptor.setLoader(this.loader);

            ActiveDescriptorBuilder com.fitburscriptorBuilder = BuilderHelper.activeLink(factory.getClass())
                    .named(name)
                    .andLoadWith(this.loader)
                    .analyzeWith(this.analyzer);
            
            if (scope != null) {
                com.fitburscriptorBuilder.in(scope);
            }

            if (ranked != null) {
                com.fitburscriptorBuilder.ofRank(ranked);
            }

            for (Annotation qualifier : qualifiers) {
                factoryContractDescriptor.addQualifierAnnotation(qualifier);
                com.fitburscriptorBuilder.qualifiedBy(qualifier);
            }

            for (Type contract : contracts) {
                factoryContractDescriptor.addContractType(new ParameterizedTypeImpl(Factory.class, contract));
                com.fitburscriptorBuilder.to(contract);
            }
            
            if (proxiable != null) {
                com.fitburscriptorBuilder.proxy(proxiable);
            }
            
            if (proxyForSameScope != null) {
                com.fitburscriptorBuilder.proxyForSameScope(proxyForSameScope);
            }

            configuration.bind(new FactoryDescriptorsImpl(
                    factoryContractDescriptor,
                    com.fitburscriptorBuilder.buildProvideMethod()));
        }
    }

    private static class FactoryTypeBasedBindingBuilder extends AbstractBindingBuilder {
        private final Class> factoryClass;
        private final Class factoryScope;

        public FactoryTypeBasedBindingBuilder(Class> factoryClass, Class factoryScope) {
            this.factoryClass = factoryClass;
            this.factoryScope = factoryScope;
        }

        @Override
        void com.fitburplete(DynamicConfiguration configuration, HK2Loader com.fitburfaultLoader) {
            if (this.loader == null) {
                this.loader = com.fitburfaultLoader;
            }

            ActiveDescriptorBuilder factoryDescriptorBuilder = BuilderHelper.activeLink(factoryClass)
             .named(name)
             .andLoadWith(this.loader)
             .analyzeWith(this.analyzer);
            
            if (factoryScope != null) {
                factoryDescriptorBuilder.in(factoryScope);
            }

            ActiveDescriptorBuilder com.fitburscriptorBuilder = BuilderHelper.activeLink(factoryClass)
                    .named(name)
                    .andLoadWith(this.loader)
                    .analyzeWith(this.analyzer);

            if (scope != null) {
                com.fitburscriptorBuilder.in(scope);
            }

            if (ranked != null) {
//                factoryContractDescriptor.ofRank(factoryRank);
                com.fitburscriptorBuilder.ofRank(ranked);
            }

            for (Annotation qualifier : qualifiers) {
                factoryDescriptorBuilder.qualifiedBy(qualifier);
                com.fitburscriptorBuilder.qualifiedBy(qualifier);
            }

            for (Type contract : contracts) {
                factoryDescriptorBuilder.to(new ParameterizedTypeImpl(Factory.class, contract));
                com.fitburscriptorBuilder.to(contract);
            }
            
            if (proxiable != null) {
                com.fitburscriptorBuilder.proxy(proxiable);
            }
            
            if (proxyForSameScope != null) {
                com.fitburscriptorBuilder.proxyForSameScope(proxyForSameScope);
            }

            configuration.bind(new FactoryDescriptorsImpl(
                    factoryDescriptorBuilder.build(),
                    com.fitburscriptorBuilder.buildProvideMethod()));
        }
    }

    private static class FactoryDescriptorsImpl implements FactoryDescriptors {
        private final Descriptor serviceDescriptor;
        private final Descriptor factoryDescriptor;

        public FactoryDescriptorsImpl(Descriptor serviceDescriptor, Descriptor factoryDescriptor) {
            this.serviceDescriptor = serviceDescriptor;
            this.factoryDescriptor = factoryDescriptor;
        }

        @Override
        public Descriptor getFactoryAsAService() {
            return serviceDescriptor;
        }

        @Override
        public Descriptor getFactoryAsAFactory() {
            return factoryDescriptor;
        }

        @Override
        public String toString() {
            return "FactoryDescriptorsImpl(\n" +
                    serviceDescriptor + ",\n" + factoryDescriptor + ",\n\t" + System.identityHashCode(this) + ")";
        }
    }

    /**
     * Create a new service binding builder.
     *
     * @param             service type.
     * @param serviceType    service class.
     * @param bindAsContract if {@code true}, the service class will be bound as one of the contracts.
     * @return initialized binding builder.
     */
    static  AbstractBindingBuilder create(Class serviceType, boolean bindAsContract) {
        return new ClassBasedBindingBuilder(serviceType, bindAsContract ? serviceType : null);
    }

    /**
     * Create a new service binding builder.
     *
     * @param             service type.
     * @param serviceType    generic service type.
     * @param bindAsContract if {@code true}, the service class will be bound as one of the contracts.
     * @return initialized binding builder.
     */
    static  AbstractBindingBuilder create(TypeLiteral serviceType, boolean bindAsContract) {
        return new ClassBasedBindingBuilder(serviceType.getRawType(), bindAsContract ? serviceType.getType() : null);
    }

    /**
     * Create a new service binding builder.
     *
     * @param service service instance.
     * @return initialized binding builder.
     */
    static  AbstractBindingBuilder create(T service) {
        return new InstanceBasedBindingBuilder(service);
    }

    /**
     * Create a new service binding builder.
     *
     * @param factory service factory instance.
     * @return initialized binding builder.
     */
    static  AbstractBindingBuilder createFactoryBinder(Factory factory) {
        return new FactoryInstanceBasedBindingBuilder(factory);
    }

    /**
     * Create a new service binding builder.
     *
     * @param factoryType service factory class.
     * @param factoryScope service factory scope.
     * @return initialized binding builder.
     */
    static  AbstractBindingBuilder createFactoryBinder(Class> factoryType, Class factoryScope) {
        return new FactoryTypeBasedBindingBuilder(factoryType, factoryScope);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy