1 17 package org.apache.servicemix.jsr181; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.jbi.management.DeploymentException; 25 26 import org.apache.servicemix.common.BaseComponent; 27 import org.apache.servicemix.common.BaseLifeCycle; 28 import org.apache.servicemix.common.Endpoint; 29 import org.apache.servicemix.common.xbean.AbstractXBeanDeployer; 30 import org.apache.servicemix.common.xbean.XBeanServiceUnit; 31 import org.springframework.beans.BeansException; 32 import org.springframework.beans.factory.BeanFactory; 33 import org.springframework.beans.factory.BeanNotOfRequiredTypeException; 34 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 35 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 36 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 37 38 public class Jsr181XBeanDeployer extends AbstractXBeanDeployer { 39 40 public Jsr181XBeanDeployer(BaseComponent component) { 41 super(component); 42 } 43 44 protected boolean validate(Endpoint endpoint) throws DeploymentException { 45 if (endpoint instanceof Jsr181Endpoint == false) { 46 throw failure("deploy", "Endpoint should be a Jsr181 endpoint", null); 47 } 48 Jsr181Endpoint ep = (Jsr181Endpoint) endpoint; 49 if (ep.getPojo() == null && ep.getPojoClass() == null) { 50 throw failure("deploy", "Endpoint must have a non-null pojo or a pojoClass", null); 51 } 52 ClassLoader old = Thread.currentThread().getContextClassLoader(); 53 try { 54 ClassLoader cl = ((XBeanServiceUnit)ep.getServiceUnit()).getConfigurationClassLoader(); 55 Thread.currentThread().setContextClassLoader(cl); 56 if (logger.isDebugEnabled()) { 57 logger.debug("Registering endpoint with context classloader " + cl); 58 } 59 ep.registerService(); 60 } catch (Exception e) { 61 throw failure("deploy", "Could not register endpoint", e); 62 } finally { 63 Thread.currentThread().setContextClassLoader(old); 64 } 65 return true; 66 } 67 68 protected List getBeanFactoryPostProcessors(String serviceUnitRootPath) { 69 List processors = new ArrayList (super.getBeanFactoryPostProcessors(serviceUnitRootPath)); 70 processors.add(new BeanFactoryPostProcessor() { 71 public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException { 72 Map beans = new HashMap (); 73 beans.put("context", new EndpointComponentContext(((BaseLifeCycle) component.getLifeCycle()).getContext())); 74 BeanFactory parent = new SimpleBeanFactory(beans); 75 factory.setParentBeanFactory(parent); 76 } 77 }); 78 return processors; 79 } 80 81 private static class SimpleBeanFactory implements BeanFactory { 82 private final Map beans; 83 public SimpleBeanFactory(Map beans) { 84 this.beans = beans; 85 } 86 public boolean containsBean(String name) { 87 return beans.containsKey(name); 88 } 89 public String [] getAliases(String name) throws NoSuchBeanDefinitionException { 90 Object bean = beans.get(name); 91 if (bean == null) { 92 throw new NoSuchBeanDefinitionException(name); 93 } 94 return new String [0]; 95 } 96 public Object getBean(String name) throws BeansException { 97 return getBean(name, null); 98 } 99 public Object getBean(String name, Class requiredType) throws BeansException { 100 Object bean = beans.get(name); 101 if (bean == null) { 102 throw new NoSuchBeanDefinitionException(name); 103 } 104 if (requiredType != null && !requiredType.isInstance(bean)) { 105 throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); 106 } 107 return bean; 108 } 109 public Class getType(String name) throws NoSuchBeanDefinitionException { 110 Object bean = beans.get(name); 111 if (bean == null) { 112 throw new NoSuchBeanDefinitionException(name); 113 } 114 return bean.getClass(); 115 } 116 public boolean isSingleton(String name) throws NoSuchBeanDefinitionException { 117 Object bean = beans.get(name); 118 if (bean == null) { 119 throw new NoSuchBeanDefinitionException(name); 120 } 121 return true; 122 } 123 } 124 125 } 126 | Popular Tags |