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    
024    import org.apache.activemq.broker.BrokerFactoryHandler;
025    import org.apache.activemq.broker.BrokerService;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
029    import org.apache.xbean.spring.context.impl.URIEditor;
030    import org.springframework.beans.BeansException;
031    import org.springframework.context.ApplicationContext;
032    import org.springframework.context.ApplicationContextAware;
033    import org.springframework.core.io.ClassPathResource;
034    import org.springframework.core.io.FileSystemResource;
035    import org.springframework.core.io.Resource;
036    import org.springframework.core.io.UrlResource;
037    import org.springframework.util.ResourceUtils;
038    
039    /**
040     * @version $Revision$
041     */
042    public class XBeanBrokerFactory implements BrokerFactoryHandler {
043        private static final transient Log LOG = LogFactory.getLog(XBeanBrokerFactory.class);
044    
045        static {
046            PropertyEditorManager.registerEditor(URI.class, URIEditor.class);
047        }
048    
049        public BrokerService createBroker(URI config) throws Exception {
050    
051            String uri = config.getSchemeSpecificPart();
052            ApplicationContext context = createApplicationContext(uri);
053    
054            BrokerService broker = null;
055            try {
056                broker = (BrokerService)context.getBean("broker");
057            } catch (BeansException e) {
058            }
059    
060            if (broker == null) {
061                // lets try find by type
062                String[] names = context.getBeanNamesForType(BrokerService.class);
063                for (int i = 0; i < names.length; i++) {
064                    String name = names[i];
065                    broker = (BrokerService)context.getBean(name);
066                    if (broker != null) {
067                        break;
068                    }
069                }
070            }
071            if (broker == null) {
072                throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config);
073            }
074            
075            if (broker instanceof ApplicationContextAware) {
076                    ((ApplicationContextAware)broker).setApplicationContext(context);
077            }
078            
079            // TODO warning resources from the context may not be closed down!
080    
081            return broker;
082        }
083    
084        protected ApplicationContext createApplicationContext(String uri) throws MalformedURLException {
085            LOG.debug("Now attempting to figure out the type of resource: " + uri);
086    
087            Resource resource;
088            File file = new File(uri);
089            if (file.exists()) {
090                resource = new FileSystemResource(uri);
091            } else if (ResourceUtils.isUrl(uri)) {
092                resource = new UrlResource(uri);
093            } else {
094                resource = new ClassPathResource(uri);
095            }
096            return new ResourceXmlApplicationContext(resource);
097        }
098    }