001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.activemq.xbean; 018 019 import java.beans.PropertyEditorManager; 020 import java.io.File; 021 import java.net.MalformedURLException; 022 import java.net.URI; 023 import java.util.Map; 024 025 import org.apache.activemq.broker.BrokerFactoryHandler; 026 import org.apache.activemq.broker.BrokerService; 027 import org.apache.activemq.spring.Utils; 028 import org.apache.activemq.util.IntrospectionSupport; 029 import org.apache.activemq.util.URISupport; 030 import org.slf4j.Logger; 031 import org.slf4j.LoggerFactory; 032 import org.apache.xbean.spring.context.ResourceXmlApplicationContext; 033 import org.apache.xbean.spring.context.impl.URIEditor; 034 import org.springframework.beans.BeansException; 035 import org.springframework.beans.FatalBeanException; 036 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 037 import org.springframework.context.ApplicationContext; 038 import org.springframework.context.ApplicationContextAware; 039 import org.springframework.core.io.ClassPathResource; 040 import org.springframework.core.io.FileSystemResource; 041 import org.springframework.core.io.Resource; 042 import org.springframework.core.io.UrlResource; 043 import org.springframework.util.ResourceUtils; 044 045 /** 046 * 047 */ 048 public class XBeanBrokerFactory implements BrokerFactoryHandler { 049 private static final transient Logger LOG = LoggerFactory.getLogger(XBeanBrokerFactory.class); 050 051 static { 052 PropertyEditorManager.registerEditor(URI.class, URIEditor.class); 053 } 054 055 private boolean validate = true; 056 public boolean isValidate() { 057 return validate; 058 } 059 060 public void setValidate(boolean validate) { 061 this.validate = validate; 062 } 063 064 public BrokerService createBroker(URI config) throws Exception { 065 066 String uri = config.getSchemeSpecificPart(); 067 if (uri.lastIndexOf('?') != -1) { 068 IntrospectionSupport.setProperties(this, URISupport.parseQuery(uri)); 069 uri = uri.substring(0, uri.lastIndexOf('?')); 070 } 071 072 ApplicationContext context = createApplicationContext(uri); 073 074 BrokerService broker = null; 075 try { 076 broker = (BrokerService)context.getBean("broker"); 077 } catch (BeansException e) { 078 } 079 080 if (broker == null) { 081 // lets try find by type 082 String[] names = context.getBeanNamesForType(BrokerService.class); 083 for (int i = 0; i < names.length; i++) { 084 String name = names[i]; 085 broker = (BrokerService)context.getBean(name); 086 if (broker != null) { 087 break; 088 } 089 } 090 } 091 if (broker == null) { 092 throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config); 093 } 094 095 if (broker instanceof ApplicationContextAware) { 096 ((ApplicationContextAware)broker).setApplicationContext(context); 097 } 098 099 // TODO warning resources from the context may not be closed down! 100 101 return broker; 102 } 103 104 protected ApplicationContext createApplicationContext(String uri) throws MalformedURLException { 105 Resource resource = Utils.resourceFromString(uri); 106 LOG.debug("Using " + resource + " from " + uri); 107 try { 108 return new ResourceXmlApplicationContext(resource) { 109 @Override 110 protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) { 111 reader.setValidating(isValidate()); 112 } 113 }; 114 } catch (FatalBeanException errorToLog) { 115 LOG.error("Failed to load: " + resource + ", reason: " + errorToLog.getLocalizedMessage(), errorToLog); 116 throw errorToLog; 117 } 118 } 119 }