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 */
017package org.apache.activemq.xbean;
018
019import java.beans.PropertyEditorManager;
020import java.net.URI;
021
022import org.apache.activemq.broker.BrokerService;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
026import org.apache.xbean.spring.context.impl.URIEditor;
027import org.springframework.beans.BeansException;
028import org.springframework.beans.factory.DisposableBean;
029import org.springframework.beans.factory.FactoryBean;
030import org.springframework.beans.factory.InitializingBean;
031import org.springframework.context.ApplicationContext;
032import org.springframework.context.ApplicationContextAware;
033import org.springframework.core.io.Resource;
034
035/**
036 * A Spring {@link FactoryBean} which creates an embedded broker inside a Spring
037 * XML using an external <a href="http://gbean.org/Custom+XML">XBean Spring XML
038 * configuration file</a> which provides a much neater and more concise XML
039 * format.
040 * 
041 * 
042 */
043public class BrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware {
044
045    static {
046        PropertyEditorManager.registerEditor(URI.class, URIEditor.class);
047    }
048
049    private Resource config;
050    private XBeanBrokerService broker;
051    private boolean start;
052    private ResourceXmlApplicationContext context;
053    private ApplicationContext parentContext;
054    
055    private boolean systemExitOnShutdown;
056    private int systemExitOnShutdownExitCode;
057
058    public BrokerFactoryBean() {
059    }
060
061    public BrokerFactoryBean(Resource config) {
062        this.config = config;
063    }
064
065    public Object getObject() throws Exception {
066        return broker;
067    }
068
069    public Class getObjectType() {
070        return BrokerService.class;
071    }
072
073    public boolean isSingleton() {
074        return true;
075    }
076
077    public void setApplicationContext(ApplicationContext parentContext) throws BeansException {
078        this.parentContext = parentContext;
079    }
080
081    public void afterPropertiesSet() throws Exception {
082        if (config == null) {
083            throw new IllegalArgumentException("config property must be set");
084        }
085        context = new ResourceXmlApplicationContext(config, parentContext);
086
087        try {
088            broker = (XBeanBrokerService)context.getBean("broker");
089        } catch (BeansException e) {
090            // ignore...
091            // log.trace("No bean named broker available: " + e, e);
092        }
093        if (broker == null) {
094            // lets try find by type
095            String[] names = context.getBeanNamesForType(BrokerService.class);
096            for (int i = 0; i < names.length; i++) {
097                String name = names[i];
098                broker = (XBeanBrokerService)context.getBean(name);
099                if (broker != null) {
100                    break;
101                }
102            }
103        }
104        if (broker == null) {
105            throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config);
106        }
107        
108        if( systemExitOnShutdown ) {
109            broker.addShutdownHook(new Runnable(){
110                public void run() {
111                    System.exit(systemExitOnShutdownExitCode);
112                }
113            });
114        }
115        if (start) {
116            broker.start();
117        }
118    }
119
120    public void destroy() throws Exception {
121        if (context != null) {
122            context.close();
123        }
124        if (broker != null) {
125            broker.stop();
126        }
127    }
128
129    public Resource getConfig() {
130        return config;
131    }
132
133    public void setConfig(Resource config) {
134        this.config = config;
135    }
136
137    public BrokerService getBroker() {
138        return broker;
139    }
140
141    public boolean isStart() {
142        return start;
143    }
144
145    public void setStart(boolean start) {
146        this.start = start;
147    }
148
149    public boolean isSystemExitOnStop() {
150        return systemExitOnShutdown;
151    }
152
153    public void setSystemExitOnStop(boolean systemExitOnStop) {
154        this.systemExitOnShutdown = systemExitOnStop;
155    }
156
157    public boolean isSystemExitOnShutdown() {
158        return systemExitOnShutdown;
159    }
160
161    public void setSystemExitOnShutdown(boolean systemExitOnShutdown) {
162        this.systemExitOnShutdown = systemExitOnShutdown;
163    }
164
165    public int getSystemExitOnShutdownExitCode() {
166        return systemExitOnShutdownExitCode;
167    }
168
169    public void setSystemExitOnShutdownExitCode(int systemExitOnShutdownExitCode) {
170        this.systemExitOnShutdownExitCode = systemExitOnShutdownExitCode;
171    }
172
173}