1 18 package org.apache.activemq.xbean; 19 20 import org.apache.activemq.broker.BrokerService; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.springframework.beans.BeansException; 24 import org.springframework.beans.factory.DisposableBean; 25 import org.springframework.beans.factory.FactoryBean; 26 import org.springframework.beans.factory.InitializingBean; 27 import org.springframework.context.ApplicationContext; 28 import org.springframework.context.ApplicationContextAware; 29 import org.springframework.core.io.Resource; 30 import org.apache.xbean.spring.context.ResourceXmlApplicationContext; 31 import org.apache.xbean.spring.context.impl.URIEditor; 32 33 import java.beans.PropertyEditorManager ; 34 import java.net.URI ; 35 36 44 public class BrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware { 45 private static final Log log = LogFactory.getLog(BrokerFactoryBean.class); 46 47 static { 48 PropertyEditorManager.registerEditor(URI .class, URIEditor.class); 49 } 50 51 private Resource config; 52 private XBeanBrokerService broker; 53 private boolean start = false; 54 private ResourceXmlApplicationContext context; 55 private ApplicationContext parentContext; 56 57 public BrokerFactoryBean() { 58 } 59 60 public BrokerFactoryBean(Resource config) { 61 this.config = config; 62 } 63 64 public Object getObject() throws Exception { 65 return broker; 66 } 67 68 public Class getObjectType() { 69 return BrokerService.class; 70 } 71 72 public boolean isSingleton() { 73 return true; 74 } 75 76 public void setApplicationContext(ApplicationContext parentContext) throws BeansException { 77 this.parentContext = parentContext; 78 } 79 80 public void afterPropertiesSet() throws Exception { 81 if (config == null) { 82 throw new IllegalArgumentException ("config property must be set"); 83 } 84 context = new ResourceXmlApplicationContext(config, parentContext); 85 86 try { 87 broker = (XBeanBrokerService) context.getBean("broker"); 88 } 89 catch (BeansException e) { 90 } 93 if (broker == null) { 94 String [] names = context.getBeanNamesForType(BrokerService.class); 96 for (int i = 0; i < names.length; i++) { 97 String name = names[i]; 98 broker = (XBeanBrokerService) context.getBean(name); 99 if (broker != null) { 100 break; 101 } 102 } 103 } 104 if (broker == null) { 105 throw new IllegalArgumentException ("The configuration has no BrokerService instance for resource: " + config); 106 } 107 if (start) { 108 broker.start(); 109 } 110 } 111 112 public void destroy() throws Exception { 113 if (context != null) { 114 context.close(); 115 } 116 if (broker != null) { 117 broker.stop(); 118 } 119 } 120 121 public Resource getConfig() { 122 return config; 123 } 124 125 public void setConfig(Resource config) { 126 this.config = config; 127 } 128 129 public BrokerService getBroker() { 130 return broker; 131 } 132 133 public boolean isStart() { 134 return start; 135 } 136 137 public void setStart(boolean start) { 138 this.start = start; 139 } 140 141 142 } 143 | Popular Tags |