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.util.IntrospectionSupport;
028 import org.apache.activemq.util.URISupport;
029 import org.slf4j.Logger;
030 import org.slf4j.LoggerFactory;
031 import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
032 import org.apache.xbean.spring.context.impl.URIEditor;
033 import org.springframework.beans.BeansException;
034 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
035 import org.springframework.context.ApplicationContext;
036 import org.springframework.context.ApplicationContextAware;
037 import org.springframework.core.io.ClassPathResource;
038 import org.springframework.core.io.FileSystemResource;
039 import org.springframework.core.io.Resource;
040 import org.springframework.core.io.UrlResource;
041 import org.springframework.util.ResourceUtils;
042
043 /**
044 *
045 */
046 public class XBeanBrokerFactory implements BrokerFactoryHandler {
047 private static final transient Logger LOG = LoggerFactory.getLogger(XBeanBrokerFactory.class);
048
049 static {
050 PropertyEditorManager.registerEditor(URI.class, URIEditor.class);
051 }
052
053 private boolean validate = true;
054 public boolean isValidate() {
055 return validate;
056 }
057
058 public void setValidate(boolean validate) {
059 this.validate = validate;
060 }
061
062 public BrokerService createBroker(URI config) throws Exception {
063
064 String uri = config.getSchemeSpecificPart();
065 if (uri.lastIndexOf('?') != -1) {
066 IntrospectionSupport.setProperties(this, URISupport.parseQuery(uri));
067 uri = uri.substring(0, uri.lastIndexOf('?'));
068 }
069
070 ApplicationContext context = createApplicationContext(uri);
071
072 BrokerService broker = null;
073 try {
074 broker = (BrokerService)context.getBean("broker");
075 } catch (BeansException e) {
076 }
077
078 if (broker == null) {
079 // lets try find by type
080 String[] names = context.getBeanNamesForType(BrokerService.class);
081 for (int i = 0; i < names.length; i++) {
082 String name = names[i];
083 broker = (BrokerService)context.getBean(name);
084 if (broker != null) {
085 break;
086 }
087 }
088 }
089 if (broker == null) {
090 throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config);
091 }
092
093 if (broker instanceof ApplicationContextAware) {
094 ((ApplicationContextAware)broker).setApplicationContext(context);
095 }
096
097 // TODO warning resources from the context may not be closed down!
098
099 return broker;
100 }
101
102 protected ApplicationContext createApplicationContext(String uri) throws MalformedURLException {
103 LOG.debug("Now attempting to figure out the type of resource: " + uri);
104
105 Resource resource;
106 File file = new File(uri);
107 if (file.exists()) {
108 resource = new FileSystemResource(uri);
109 } else if (ResourceUtils.isUrl(uri)) {
110 resource = new UrlResource(uri);
111 } else {
112 resource = new ClassPathResource(uri);
113 }
114 return new ResourceXmlApplicationContext(resource) {
115 @Override
116 protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
117 reader.setValidating(isValidate());
118 }
119 };
120 }
121 }