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.osgi;
018    
019    import org.apache.activemq.broker.BrokerService;
020    import org.apache.activemq.spring.Utils;
021    import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
022    import org.osgi.framework.BundleContext;
023    import org.osgi.service.cm.ConfigurationException;
024    import org.osgi.service.cm.ManagedServiceFactory;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
028    import org.springframework.context.ConfigurableApplicationContext;
029    import org.springframework.context.support.ClassPathXmlApplicationContext;
030    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
031    import org.springframework.core.io.Resource;
032    
033    import java.util.*;
034    
035    public class ActiveMQServiceFactory implements ManagedServiceFactory {
036    
037        private static final Logger LOG = LoggerFactory.getLogger(ActiveMQServiceFactory.class);
038    
039        BundleContext bundleContext;
040        HashMap<String, BrokerService> brokers = new HashMap<String, BrokerService>();
041    
042        @Override
043        public String getName() {
044            return "ActiveMQ Server Controller";
045        }
046    
047        @Override
048        synchronized public void updated(String pid, Dictionary properties) throws ConfigurationException {
049    
050            // First stop currently running broker (if any)
051            deleted(pid);
052    
053            String config = (String)properties.get("config");
054            if (config == null) {
055                throw new ConfigurationException("config", "Property must be set");
056            }
057            String name = (String)properties.get("broker-name");
058            if (name == null) {
059                throw new ConfigurationException("broker-name", "Property must be set");
060            }
061    
062            LOG.info("Starting broker " + name);
063    
064            try {
065                Thread.currentThread().setContextClassLoader(BrokerService.class.getClassLoader());
066                Resource resource = Utils.resourceFromString(config);
067    
068                ResourceXmlApplicationContext ctx = new ResourceXmlApplicationContext(resource, Collections.EMPTY_LIST, null, Collections.EMPTY_LIST, false) {
069                    protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
070                        reader.setValidating(false);
071                    }
072                };
073    
074                // Handle properties in configuration
075                PropertySourcesPlaceholderConfigurer configurator =
076                            new PropertySourcesPlaceholderConfigurer();
077    
078                //convert dictionary to properties. Is there a better way?
079                Properties props = new Properties();
080                Enumeration elements = properties.keys();
081                while (elements.hasMoreElements()) {
082                    Object key = elements.nextElement();
083                    props.put(key, properties.get(key));
084                }
085    
086                configurator.setProperties(props);
087                configurator.setIgnoreUnresolvablePlaceholders(true);
088    
089                ctx.addBeanFactoryPostProcessor(configurator);
090    
091                ctx.refresh();
092    
093                // Start the broker
094                BrokerService broker = ctx.getBean(BrokerService.class);
095                if (broker == null) {
096                    throw new ConfigurationException(null, "Broker not defined");
097                }
098                //TODO deal with multiple brokers
099    
100    
101                broker.start();
102                broker.waitUntilStarted();
103                brokers.put(pid, broker);
104    
105    
106            } catch (Exception e) {
107                throw new ConfigurationException(null, "Cannot start the broker", e);
108            }
109        }
110    
111        @Override
112        synchronized public void deleted(String pid) {
113            BrokerService broker = brokers.get(pid);
114            if (broker == null) {
115                return;
116            }
117            try {
118                LOG.info("Stopping broker " + pid);
119                broker.stop();
120                broker.waitUntilStopped();
121            } catch (Exception e) {
122                LOG.error("Exception on stopping broker", e);
123            }
124        }
125    
126        synchronized public void destroy() {
127            for (String broker: brokers.keySet()) {
128                deleted(broker);
129            }
130        }
131    
132        public BundleContext getBundleContext() {
133            return bundleContext;
134        }
135    
136        public void setBundleContext(BundleContext bundleContext) {
137            this.bundleContext = bundleContext;
138        }
139    }