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

com.mycila.guice.ext.closeable.MycilaCloseableInjector Maven / Gradle / Ivy

There is a newer version: 5.0
Show newest version
/**
 * Copyright (C) 2010 Mycila ([email protected])
 *
 * 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 com.mycila.guice.ext.closeable;

import com.google.inject.*;
import com.google.inject.spi.TypeConverterBinding;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Mathieu Carbou ([email protected])
 * date 2013-07-21
 */
final class MycilaCloseableInjector implements CloseableInjector {

    private volatile boolean closed;

    @Inject
    private volatile Injector injector;

    @Override
    public synchronized void close() {
        if (!closed && injector != null) {
            closed = true;
            Injector current = injector;
            injector = null;
            for (Binding binding : current.getAllBindings().values()) {
                if (Scopes.isSingleton(binding)) {
                    Object o = binding.getProvider().get();
                    if (o instanceof InjectorCloseListener) {
                        ((InjectorCloseListener) o).onInjectorClosing();
                    }
                }
            }
        }
    }

    private Injector injector() {
        if (closed || injector == null) throw new IllegalStateException("Injector closed !");
        return injector;
    }

    @Override
    public void injectMembers(Object instance) {
        injector().injectMembers(instance);
    }

    @Override
    public  MembersInjector getMembersInjector(TypeLiteral typeLiteral) {
        return injector().getMembersInjector(typeLiteral);
    }

    @Override
    public  MembersInjector getMembersInjector(Class type) {
        return injector().getMembersInjector(type);
    }

    @Override
    public Map, Binding> getBindings() {
        return injector().getBindings();
    }

    @Override
    public Map, Binding> getAllBindings() {
        return injector().getAllBindings();
    }

    @Override
    public  Binding getBinding(Key key) {
        return injector().getBinding(key);
    }

    @Override
    public  Binding getBinding(Class type) {
        return injector().getBinding(type);
    }

    @Override
    public  Binding getExistingBinding(Key key) {
        return injector().getExistingBinding(key);
    }

    @Override
    public  List> findBindingsByType(TypeLiteral type) {
        return injector().findBindingsByType(type);
    }

    @Override
    public  Provider getProvider(Key key) {
        return injector().getProvider(key);
    }

    @Override
    public  Provider getProvider(Class type) {
        return injector().getProvider(type);
    }

    @Override
    public  T getInstance(Key key) {
        return injector().getInstance(key);
    }

    @Override
    public  T getInstance(Class type) {
        return injector().getInstance(type);
    }

    @Override
    public Injector getParent() {
        return injector().getParent();
    }

    @Override
    public Injector createChildInjector(Iterable modules) {
        return injector().createChildInjector(modules);
    }

    @Override
    public Injector createChildInjector(Module... modules) {
        return injector().createChildInjector(modules);
    }

    @Override
    public Map, Scope> getScopeBindings() {
        return injector().getScopeBindings();
    }

    @Override
    public Set getTypeConverterBindings() {
        return injector().getTypeConverterBindings();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy