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

org.javabits.yar.guice.RegistrationBindingHandler Maven / Gradle / Ivy

There is a newer version: 3.0.1.RELEASE
Show newest version
/*
 * Copyright 2013 Romain Gilles
 *
 *    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 org.javabits.yar.guice;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import org.javabits.yar.Id;
import org.javabits.yar.Registration;
import org.javabits.yar.Registry;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
import java.util.logging.Logger;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;

/**
 * TODO comment
 * This class handles the register the providing supplier into the registry.
 * Date: 2/10/13
 *
 * @author Romain Gilles
 */
@Singleton
public class RegistrationBindingHandler implements RegistrationHandler {
    private static final Logger LOG = Logger.getLogger(RegistrationBindingHandler.class.getName());

    private final Injector injector;
    private final Registry registry;
    volatile private List registrations;

    @Inject
    public RegistrationBindingHandler(Injector injector, Registry registry) {
        this.injector = injector;
        this.registry = registry;
    }

    @Override
    public void init() {
        registrations = registerBindings();
    }

    private List registerBindings() {
        List registrationsBuilder = newArrayList();
        for (Pair idGuiceSupplierPair : getSuppliers()) {
            registrationsBuilder.add(putRegistrationToRegistry(idGuiceSupplierPair));
        }
        return registrationsBuilder;
    }

    //enforce load all providers before register them
    private List> getSuppliers() {
        ImmutableList.Builder> suppliersBuilder = ImmutableList.builder();
        for (Binding registrationBinding : injector.findBindingsByType(TypeLiteral.get(GuiceRegistration.class))) {
            Key key = registrationBinding.getProvider().get().key();
            suppliersBuilder.add(newPair(key));
        }
        return suppliersBuilder.build();
    }

    @SuppressWarnings("unchecked")
    private StrongPair newPair(Key key) {
        Id id = GuiceId.of(key);
        return new StrongPair(id, new GuiceSupplier(injector.getProvider(key)));
    }

    @SuppressWarnings("unchecked")
    private RegistrationHolder putRegistrationToRegistry(Pair idGuiceSupplierPair) {
        Registration future = registry.put(idGuiceSupplierPair.left(), idGuiceSupplierPair.right());
        return new RegistrationHolder(future, idGuiceSupplierPair.left());
    }

    @Override
    public List> registrations() {
        List registrations = this.registrations;
        return transform(registrations, new Function>() {
            @Nullable
            @Override
            public Id apply(@Nullable RegistrationHolder registrationHolder) {
                if (registrationHolder == null) {
                    throw new NullPointerException("registrationHolder");
                }
                return registrationHolder.id;
            }
        });
    }

    @Override
    public List> ids() {
        return registrations();
    }

    @Override
    public void clear() {
        List registrations = this.registrations;
        if (registrations == null) {
            return;
        }
        for (final RegistrationHolder registration : registrations) {
            registry.remove(registration.registration);
        }
        registrations.clear();
    }

    private class RegistrationHolder {
        private final Registration registration;
        private final Id id;

        private RegistrationHolder(Registration registration, Id id) {
            this.registration = registration;
            this.id = id;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy