com.coditory.quark.context.BeanHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quark-context Show documentation
Show all versions of quark-context Show documentation
Coditory Quark Configuration Library
package com.coditory.quark.context;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;
import static com.coditory.quark.context.BeanFinalizer.closeBean;
import static com.coditory.quark.context.BeanInitializer.initializeBean;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toSet;
final class BeanHolder {
static BeanHolder holder(BeanDescriptor descriptor, BeanCreator creator, boolean eager) {
return new BeanHolder<>(descriptor, creator, eager);
}
static BeanHolder holder(BeanDescriptor descriptor, BeanCreator creator) {
return new BeanHolder<>(descriptor, creator, false);
}
private final Logger log = LoggerFactory.getLogger(this.getClass());
private final BeanCreator creator;
private final BeanDescriptor descriptor;
private final Set> classHierarchy;
private final boolean eager;
private T bean;
private boolean initialized = false;
private boolean closed = false;
private BeanHolder(BeanDescriptor descriptor, BeanCreator creator, boolean eager) {
this.creator = requireNonNull(creator);
this.descriptor = requireNonNull(descriptor);
this.classHierarchy = HierarchyIterator.getClassHierarchy(descriptor.getType());
this.eager = eager;
}
public boolean isEager() {
return eager;
}
public boolean isInitialized() {
return initialized;
}
public boolean isClosed() {
return closed;
}
Class getBeanType() {
return descriptor.getType();
}
String getBeanName() {
return descriptor.getName();
}
Set> getBeanClassHierarchy() {
return classHierarchy;
}
Set> getBeanClassHierarchyDescriptors() {
return classHierarchy.stream()
.map(descriptor::withType)
.collect(toSet());
}
public BeanDescriptor getDescriptor() {
return descriptor;
}
boolean isCached() {
return bean != null;
}
T getCached() {
return bean;
}
boolean isActive(ConditionContext context) {
Timer timer = Timer.start();
boolean result = creator.isActive(context);
if (timer.isOverThreshold()) {
log.warn("Detected long bean condition check. Bean: {}, Time: {}", descriptor.toShortString(), timer.measureAndFormat());
}
return result;
}
@Nullable
public T get(ResolutionContext context) {
if (bean == null) {
createBean(context);
}
return bean;
}
private void createBean(ResolutionContext context) {
Timer timer = Timer.start();
bean = creator.create(context);
if (bean == null) {
throw new ContextException("Expected non-null bean: " + descriptor);
}
log.debug("Created bean {} in {}", descriptor.toShortString(), timer.measureAndFormat());
if (timer.isOverThreshold()) {
log.warn("Detected long bean creation. Bean: {}, Time: {}", descriptor.toShortString(), timer.measureAndFormat());
}
if (!initialized) {
initializeBean(bean, descriptor, context);
initialized = true;
}
}
void close(ResolutionContext context) {
if (!closed) {
closeBean(bean, descriptor, context);
closed = true;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy