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.io.IOException;
020
021import javax.annotation.PostConstruct;
022import javax.annotation.PreDestroy;
023
024import org.apache.activemq.broker.BrokerFactory;
025import org.apache.activemq.broker.BrokerService;
026import org.apache.activemq.usage.SystemUsage;
027import org.springframework.beans.CachedIntrospectionResults;
028
029/**
030 * An ActiveMQ Message Broker. It consists of a number of transport
031 * connectors, network connectors and a bunch of properties which can be used to
032 * configure the broker as its lazily created.
033 *
034 * @org.apache.xbean.XBean element="broker" rootElement="true"
035 * @org.apache.xbean.Defaults {code:xml}
036 * <broker test="foo.bar">
037 *   lets.
038 *   see what it includes.
039 * </broker>
040 * {code}
041 *
042 */
043public class XBeanBrokerService extends BrokerService {
044
045    private boolean start;
046
047    public XBeanBrokerService() {
048        start = BrokerFactory.getStartDefault();
049    }
050
051    /**
052     * JSR-250 callback wrapper; converts checked exceptions to runtime exceptions
053     *
054     * delegates to afterPropertiesSet, done to prevent backwards incompatible signature change.
055     */
056    @PostConstruct
057    private void postConstruct() {
058        try {
059            afterPropertiesSet();
060        } catch (Exception ex) {
061            throw new RuntimeException(ex);
062        }
063    }
064
065    /**
066     *
067     * @throws Exception
068     * @org.apache.xbean.InitMethod
069     */
070    public void afterPropertiesSet() throws Exception {
071        ensureSystemUsageHasStore();
072        if (shouldAutostart()) {
073            start();
074        }
075    }
076
077    @Override
078    protected boolean shouldAutostart() {
079        return start;
080    }
081
082    private void ensureSystemUsageHasStore() throws IOException {
083        SystemUsage usage = getSystemUsage();
084        if (usage.getStoreUsage().getStore() == null) {
085            usage.getStoreUsage().setStore(getPersistenceAdapter());
086        }
087        if (usage.getTempUsage().getStore() == null) {
088            usage.getTempUsage().setStore(getTempDataStore());
089        }
090        if (usage.getJobSchedulerUsage().getStore() == null) {
091            usage.getJobSchedulerUsage().setStore(getJobSchedulerStore());
092        }
093    }
094
095    /**
096     * JSR-250 callback wrapper; converts checked exceptions to runtime exceptions
097     *
098     * delegates to destroy, done to prevent backwards incompatible signature change.
099     */
100    @PreDestroy
101    private void preDestroy() {
102        try {
103            destroy();
104        } catch (Exception ex) {
105            throw new RuntimeException(ex);
106        }
107    }
108
109    /**
110     *
111     * @throws Exception
112     * @org.apache.xbean.DestroyMethod
113     */
114    public void destroy() throws Exception {
115        stop();
116    }
117
118    @Override
119    public void stop() throws Exception {
120        // must clear this Spring cache to avoid any memory leaks
121        CachedIntrospectionResults.clearClassLoader(getClass().getClassLoader());
122        super.stop();
123    }
124
125    /**
126     * Sets whether or not the broker is started along with the ApplicationContext it is defined within.
127     * Normally you would want the broker to start up along with the ApplicationContext but sometimes when working
128     * with JUnit tests you may wish to start and stop the broker explicitly yourself.
129     */
130    public void setStart(boolean start) {
131        this.start = start;
132    }
133}