com.lithium.flow.ioc.PicoLocator Maven / Gradle / Ivy
/*
* Copyright 2015 Lithium Technologies, Inc.
*
* 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.lithium.flow.ioc;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.picocontainer.Characteristics.CACHE;
import com.lithium.flow.util.Mutex;
import com.lithium.flow.util.Mutexes;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
import org.picocontainer.adapters.AbstractAdapter;
import com.google.common.io.Closer;
/**
* @author Matt Ayres
*/
public class PicoLocator implements Locator {
private final MutablePicoContainer pico;
private final Mutexes> mutexes;
private final Set> types = Collections.synchronizedSet(new HashSet<>());
public PicoLocator() {
this(null);
}
public PicoLocator(@Nullable PicoLocator parentLocator) {
if (parentLocator == null) {
pico = new DefaultPicoContainer().as(CACHE);
mutexes = new Mutexes<>();
} else {
pico = new DefaultPicoContainer(parentLocator.pico).as(CACHE);
mutexes = parentLocator.mutexes;
}
}
@Override
public void addType(@Nonnull Class type) {
checkNotNull(type);
pico.addComponent(type);
types.add(type);
}
@Override
public void addProvider(@Nonnull Class type, @Nonnull Provider provider) {
checkNotNull(type);
checkNotNull(provider);
if (types.stream().noneMatch(type::isAssignableFrom)) {
pico.addAdapter(new Adapter(type, provider));
types.add(type);
}
}
@Override
public void addInstance(@Nonnull Object object) {
checkNotNull(object);
pico.addComponent(object);
types.add(object.getClass());
}
@Override
public void addInstance(@Nonnull Object key, @Nonnull Object object) {
checkNotNull(key);
checkNotNull(object);
pico.addComponent(key, object);
types.add(object.getClass());
}
@Override
@Nonnull
public T getInstance(@Nonnull Class type) {
checkNotNull(type);
try (Mutex ignored = mutexes.getMutex(type)) {
T instance = pico.getComponent(type);
if (instance == null) {
throw new RuntimeException("no instance for type: " + type);
}
return instance;
}
}
@Override
@Nonnull
public List