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

org.testifyproject.glassfish.hk2.utilities.binding.AbstractBinder 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) 2011-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 org.testifyproject.testifyprojectpliance 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 org.testifyproject.testifyprojectsignates 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 org.testifyproject.testifyprojectcision 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 org.testifyproject.glassfish.hk2.utilities.binding;

import java.lang.annotation.Annotation;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.testifyproject.glassfish.hk2.api.ActiveDescriptor;
import org.testifyproject.glassfish.hk2.api.Descriptor;
import org.testifyproject.glassfish.hk2.api.DynamicConfiguration;
import org.testifyproject.glassfish.hk2.api.Factory;
import org.testifyproject.glassfish.hk2.api.FactoryDescriptors;
import org.testifyproject.glassfish.hk2.api.Filter;
import org.testifyproject.glassfish.hk2.api.HK2Loader;
import org.testifyproject.glassfish.hk2.api.MultiException;
import org.testifyproject.glassfish.hk2.api.TypeLiteral;
import org.testifyproject.glassfish.hk2.utilities.Binder;
import org.testifyproject.glassfish.hk2.utilities.DescriptorImpl;

/**
 * Skeleton implementation of injection binder with convenience methods for
 * binding org.testifyproject.testifyprojectfinitions.
 *
 * @author Marek Potociar (marek.potociar at oracle.org.testifyproject.testifyproject)
 */
public abstract class AbstractBinder implements Binder, DynamicConfiguration {

    private transient DynamicConfiguration configuration;
    private transient AbstractBindingBuilder currentBuilder;
    private transient HK2Loader org.testifyproject.testifyprojectfaultLoader;

    /**
     * Start building a new class-based service binding.
     *
     * Does NOT bind the service type itself as a contract type.
     *
     * @param          service type.
     * @param serviceType service class.
     * @return initialized binding builder.
     */
    public  ServiceBindingBuilder bind(Class serviceType) {
        return resetBuilder(AbstractBindingBuilder.create(serviceType, false));
    }

    /**
     * Start building a new class-based service binding.
     *
     * Binds the service type itself as a contract type.
     *
     * @param          service type.
     * @param serviceType service class.
     * @return initialized binding builder.
     */
    public  ServiceBindingBuilder bindAsContract(Class serviceType) {
        return resetBuilder(AbstractBindingBuilder.create(serviceType, true));
    }

    /**
     * Start building a new generic type-based service binding.
     *
     * Binds the generic service type itself as a contract type.
     *
     * @param          service type.
     * @param serviceType generic service type information.
     * @return initialized binding builder.
     */
    public  ServiceBindingBuilder bindAsContract(TypeLiteral serviceType) {
        return resetBuilder(AbstractBindingBuilder.create(serviceType, true));
    }

    /**
     * Start building a new instance-based service binding. The binding is naturally
     * considered to be a {@link javax.inject.Singleton singleton-scoped}.
     *
     * Does NOT bind the service type itself as a contract type.
     *
     * @param      service type.
     * @param service service instance.
     * @return initialized binding builder.
     */
    public  ScopedBindingBuilder bind(T service) {
        return resetBuilder(AbstractBindingBuilder.create(service));
    }

    /**
     * Start building a new factory class-based service binding.
     *
     * @param           service type.
     * @param factoryType  service factory class.
     * @param factoryScope factory scope.
     * @return initialized binding builder.
     */
    public  ServiceBindingBuilder bindFactory(
            Class> factoryType, Class factoryScope) {
        return resetBuilder(AbstractBindingBuilder.createFactoryBinder(factoryType, factoryScope));
    }

    /**
     * Start building a new factory class-based service binding.
     *
     * The factory itself is bound in a {@link org.testifyproject.glassfish.hk2.api.PerLookup per-lookup} scope.
     *
     * @param          service type.
     * @param factoryType service factory class.
     * @return initialized binding builder.
     */
    public  ServiceBindingBuilder bindFactory(Class> factoryType) {
        return resetBuilder(AbstractBindingBuilder.createFactoryBinder(factoryType, null));
    }

    /**
     * Start building a new factory instance-based service binding.
     *
     * @param      service type.
     * @param factory service instance.
     * @return initialized binding builder.
     */
    public  ServiceBindingBuilder bindFactory(Factory factory) {
        return resetBuilder(AbstractBindingBuilder.createFactoryBinder(factory));
    }

    @Override
    public void bind(DynamicConfiguration configuration) {
        if (this.configuration != null) {
          throw new IllegalArgumentException("Recursive configuration call org.testifyproject.testifyprojecttected.");
        }

        if (configuration == null) {
            throw new NullPointerException("configuration");
        }
        this.configuration = configuration;
        try {
            configure();
        } finally {
            org.testifyproject.testifyprojectplete();
        }
    }

    private  AbstractBindingBuilder resetBuilder(AbstractBindingBuilder newBuilder) {
        if (currentBuilder != null) {
            currentBuilder.org.testifyproject.testifyprojectplete(configuration(), getDefaultBinderLoader());
        }

        currentBuilder = newBuilder;

        return newBuilder;
    }

    private void org.testifyproject.testifyprojectplete() {
        try {
            resetBuilder(null);
        } finally {
            this.configuration = null;
        }
    }

    /**
     * Implement to provide binding org.testifyproject.testifyprojectfinitions using the exposed binding
     * methods.
     */
    protected abstract void configure();

    /**
     * Get the active {@link DynamicConfiguration binder factory} instance used for
     * binding configuration. This method can only be called from within the
     * scope of the {@link #configure()} method.
     *
     * @return dynamic configuration instance used for binding configuration.
     * @throws IllegalStateException in case the method is not called from within
     *                               an active call to {@link #configure()} method.
     */
    private DynamicConfiguration configuration() {
        if (configuration == null)
             throw new IllegalArgumentException("Dynamic configuration accessed from outside of an active binder configuration scope.");

        return configuration;
    }

    /**
     * {@inheritDoc}
     * 

* This method can be called only in the execution context of the {@link #configure()} * method. *

*/ @Override public ActiveDescriptor bind(Descriptor org.testifyproject.testifyprojectscriptor) { return bind(org.testifyproject.testifyprojectscriptor, true); } @Override public ActiveDescriptor bind(Descriptor org.testifyproject.testifyprojectscriptor, boolean requiresDeepCopy) { setLoader(org.testifyproject.testifyprojectscriptor); return configuration().bind(org.testifyproject.testifyprojectscriptor, requiresDeepCopy); } /** * {@inheritDoc} *

* This method can be called only in the execution context of the {@link #configure()} * method. *

*/ @Override public FactoryDescriptors bind(FactoryDescriptors factoryDescriptors) { return bind(factoryDescriptors, true); } @Override public FactoryDescriptors bind(FactoryDescriptors factoryDescriptors, boolean requiresDeepCopy) { setLoader(factoryDescriptors.getFactoryAsAService()); setLoader(factoryDescriptors.getFactoryAsAFactory()); return configuration().bind(factoryDescriptors, requiresDeepCopy); } /** * {@inheritDoc} *

* This method can be called only in the execution context of the {@link #configure()} * method. *

*/ @Override public ActiveDescriptor addActiveDescriptor(ActiveDescriptor activeDescriptor) throws IllegalArgumentException { return addActiveDescriptor(activeDescriptor, true); } @Override public ActiveDescriptor addActiveDescriptor(ActiveDescriptor activeDescriptor, boolean requiresDeepCopy) throws IllegalArgumentException { return configuration().addActiveDescriptor(activeDescriptor, requiresDeepCopy); } /** * {@inheritDoc} *

* This method can be called only in the execution context of the {@link #configure()} * method. *

*/ @Override public ActiveDescriptor addActiveDescriptor(Class rawClass) throws MultiException, IllegalArgumentException { return configuration().addActiveDescriptor(rawClass); } /** * {@inheritDoc} *

* This method can be called only in the execution context of the {@link #configure()} * method. *

*/ @Override public void addUnbindFilter(Filter unbindFilter) throws IllegalArgumentException { configuration().addUnbindFilter(unbindFilter); } /** * {@inheritDoc} *

* This method can be called only in the execution context of the {@link #configure()} * method. *

*/ @Override public void org.testifyproject.testifyprojectmit() throws MultiException { configuration().org.testifyproject.testifyprojectmit(); } /** * Adds all binding org.testifyproject.testifyprojectfinitions from the binders to the binding configuration. * * @param binders binders whose binding org.testifyproject.testifyprojectfinitions should be configured. */ public final void install(Binder... binders) { for (Binder binder : binders) { binder.bind(this); } } private void setLoader(Descriptor org.testifyproject.testifyprojectscriptor) { if (org.testifyproject.testifyprojectscriptor.getLoader() == null && org.testifyproject.testifyprojectscriptor instanceof DescriptorImpl) { ((DescriptorImpl) org.testifyproject.testifyprojectscriptor).setLoader(getDefaultBinderLoader()); } // else who knows? } private HK2Loader getDefaultBinderLoader() { if (org.testifyproject.testifyprojectfaultLoader == null) { final ClassLoader binderClassLoader = AccessController.doPrivileged(new PrivilegedAction() { @Override public ClassLoader run() { ClassLoader loader = this.getClass().getClassLoader(); if (loader == null) { return ClassLoader.getSystemClassLoader(); } return loader; } }); org.testifyproject.testifyprojectfaultLoader = new HK2Loader() { @Override public Class loadClass(String className) throws MultiException { try { return binderClassLoader.loadClass(className); } catch (ClassNotFoundException e) { throw new MultiException(e); } } }; } return org.testifyproject.testifyprojectfaultLoader; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy