package org.servicemix.jbi.container;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.DisposableBean;
import javax.jbi.JBIException;
import java.util.Iterator;
import java.util.List;
/**
* Used to hold a Server Unit configuration. The components
* are registered into the JBI container using the Service Unit
* Manager life cycle methods.
*
* @version $Revision$
*/
public class SpringServiceUnitContainer {
private List activationSpecs;
private BeanFactory beanFactory;
private String[] componentNames;
private String[] deployArchives;
public void start(JBIContainer container) throws Exception {
// lets iterate through all the component names and register them
if (componentNames != null) {
for (int i = 0; i < componentNames.length; i++) {
String componentName = componentNames[i];
container.activateComponent(new ActivationSpec(componentName, lookupBean(componentName)));
}
}
if (activationSpecs != null) {
for (Iterator iter = activationSpecs.iterator(); iter.hasNext();) {
ActivationSpec activationSpec = (ActivationSpec) iter.next();
container.activateComponent(activationSpec);
}
}
if (deployArchives != null) {
for (int i = 0; i < deployArchives.length; i++) {
String archive = deployArchives[i];
container.installArchive(archive);
}
}
}
public void stop(JBIContainer container) throws JBIException {
if (beanFactory instanceof DisposableBean) {
DisposableBean disposable = (DisposableBean) beanFactory;
try {
disposable.destroy();
}
catch (Exception e) {
throw new JBIException("Failed to dispose of the Spring BeanFactory due to: " + e, e);
}
}
}
// /**
// * Returns the component or POJO registered with the given component ID.
// *
// * @param id
// * @return the Component
// */
// public Object getBean(String id) {
// Object bean = getComponent(id);
// if (bean instanceof ComponentAdaptor) {
// ComponentAdaptor adaptor = (ComponentAdaptor) bean;
// return adaptor.getLifeCycle();
// }
// return bean;
// }
// Properties
//-------------------------------------------------------------------------
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public String[] getComponentNames() {
return componentNames;
}
public void setComponentNames(String[] componentNames) {
this.componentNames = componentNames;
}
public List getActivationSpecs() {
return activationSpecs;
}
public void setActivationSpecs(List activationSpecs) throws JBIException {
this.activationSpecs = activationSpecs;
}
public String[] getDeployArchives() {
return deployArchives;
}
public void setDeployArchives(String[] deployArchives) {
this.deployArchives = deployArchives;
}
// Implementation methods
//-------------------------------------------------------------------------
protected Object lookupBean(String componentName) {
Object bean = beanFactory.getBean(componentName);
if (bean == null) {
throw new IllegalArgumentException("Component name: " + componentName
+ " is not found in the Spring BeanFactory");
}
return bean;
}
}