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

juzu.impl.inject.spi.InjectionContext Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright 2013 eXo Platform SAS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package juzu.impl.inject.spi;

import java.io.Closeable;
import java.lang.reflect.InvocationTargetException;

/** @author Julien Viet */
public abstract class InjectionContext implements Closeable {

  /**
   * Returns the injector provider.
   *
   * @return the injector provider
   */
  public abstract InjectorProvider getProvider();

  public abstract ClassLoader getClassLoader();

  public abstract B resolveBean(Class type);

  public abstract B resolveBean(String name);

  public abstract Iterable resolveBeans(Class type);

  /**
   * Create a bean instance for the specified bean.
   *
   * @param bean the bean
   * @return the bean instance
   * @throws InvocationTargetException wrap any exception throws,by the bean class during its creation.
   */
  public abstract I create(B bean) throws InvocationTargetException;

  /**
   * Get the bean object associated the bean instance.
   *
   * @param bean     the bean
   * @param instance the bean instance
   * @return the bean instance
   * @throws InvocationTargetException wrap any exception throws,by the bean class during its creation.
   */
  public abstract Object get(B bean, I instance) throws InvocationTargetException;

  public abstract void release(B bean, I instance);

  /**
   * Close the manager. The implementation should care bout shutting down the existing bean in particular the
   * singleton beans that are managed outside of an explicit scope.
   */
  public abstract void close();

  private static class BeanLifeCycleImpl implements BeanLifeCycle {

    final Class type;
    final InjectionContext manager;
    final B a;
    private I instance;
    private T o;

    private BeanLifeCycleImpl(Class type, InjectionContext manager, B a) {
      this.type = type;
      this.manager = manager;
      this.a = a;
    }

    public T get() throws InvocationTargetException {
      if (o == null) {
        instance = manager.create(a);
        o = type.cast(manager.get(a, instance));
      }
      return o;
    }

    public T peek() {
      return o;
    }

    public void close() {
      if (instance != null) {
        manager.release(a, instance);
      }
    }
  }

  public final  BeanLifeCycle get(Class type) {
    final B a = resolveBean(type);
    if (a == null) {
      return null;
    } else {
      return new BeanLifeCycleImpl(type, this, a);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy