1 18 package org.apache.activemq.xbean; 19 20 import org.apache.activemq.broker.BrokerFactoryHandler; 21 import org.apache.activemq.broker.BrokerService; 22 import org.apache.xbean.spring.context.ResourceXmlApplicationContext; 23 import org.apache.xbean.spring.context.impl.URIEditor; 24 import org.springframework.beans.BeansException; 25 import org.springframework.context.ApplicationContext; 26 import org.springframework.core.io.ClassPathResource; 27 import org.springframework.core.io.FileSystemResource; 28 import org.springframework.core.io.Resource; 29 import org.springframework.core.io.UrlResource; 30 import org.springframework.util.ResourceUtils; 31 32 import java.beans.PropertyEditorManager ; 33 import java.io.File ; 34 import java.net.URI ; 35 import java.net.MalformedURLException ; 36 37 40 public class XBeanBrokerFactory implements BrokerFactoryHandler { 41 42 static { 43 PropertyEditorManager.registerEditor(URI .class, URIEditor.class); 44 } 45 46 public BrokerService createBroker(URI config) throws Exception { 47 48 String uri = config.getSchemeSpecificPart(); 49 ApplicationContext context = createApplicationContext(uri); 50 51 BrokerService broker = null; 52 try { 53 broker = (BrokerService) context.getBean("broker"); 54 } 55 catch (BeansException e) { 56 } 57 58 if (broker == null) { 59 String [] names = context.getBeanNamesForType(BrokerService.class); 61 for (int i = 0; i < names.length; i++) { 62 String name = names[i]; 63 broker = (BrokerService) context.getBean(name); 64 if (broker != null) { 65 break; 66 } 67 } 68 } 69 if (broker == null) { 70 throw new IllegalArgumentException ("The configuration has no BrokerService instance for resource: " + config); 71 } 72 73 75 return broker; 76 } 77 78 protected ApplicationContext createApplicationContext(String uri) throws MalformedURLException { 79 System.out.println("Now attempting to figure out the type of resource: " + uri); 80 Resource resource; 81 File file = new File (uri); 82 if (file.exists()) { 83 resource = new FileSystemResource(uri); 84 } 85 else if (ResourceUtils.isUrl(uri)) { 86 resource = new UrlResource(uri); 87 } 88 else { 89 resource = new ClassPathResource(uri); 90 } 91 return new ResourceXmlApplicationContext(resource); 92 } 93 } 94 | Popular Tags |