org.mule.runtime.config.internal.factories.ConstantFactoryBean Maven / Gradle / Ivy
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.runtime.config.internal.factories;
import static org.mule.runtime.api.util.Preconditions.checkArgument;
import org.mule.runtime.api.component.AbstractComponent;
import org.mule.runtime.api.component.Component;
import org.mule.runtime.api.lifecycle.Lifecycle;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.context.MuleContextAware;
import javax.inject.Inject;
import org.springframework.beans.factory.FactoryBean;
/**
* A {@link FactoryBean} which returns a fixed instanced obtained through the constructor. {@link #isSingleton()} always returns
* {@code true}.
*
* Invocations related to the {@link MuleContextAware} and {@link Lifecycle} interfaces are delegated into the {@link #value}
* object when applies.
*
* @param
* @since 3.7.0
*/
public class ConstantFactoryBean extends AbstractComponent implements FactoryBean {
@Inject
private MuleContext muleContext;
private final T value;
public ConstantFactoryBean(T value) {
checkArgument(value != null, "value cannot be null");
this.value = value;
}
@Override
public T getObject() throws Exception {
if (value instanceof Component) {
((Component) value).setAnnotations(getAnnotations());
}
muleContext.getInjector().inject(value);
return value;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public Class getObjectType() {
return value.getClass();
}
public void setMuleContext(MuleContext muleContext) {
this.muleContext = muleContext;
}
}