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.io.IOException;
020    
021    import javax.annotation.PostConstruct;
022    import javax.annotation.PreDestroy;
023    
024    import org.apache.activemq.broker.BrokerFactory;
025    import org.apache.activemq.broker.BrokerService;
026    import org.apache.activemq.usage.SystemUsage;
027    import 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     */
043    public class XBeanBrokerService extends BrokerService {
044    
045        private boolean start;
046    
047        public XBeanBrokerService() {
048            start = BrokerFactory.getStartDefault();
049        }
050    
051        /**
052         *
053         * @throws Exception
054         * @org.apache.xbean.InitMethod
055         */
056        @PostConstruct
057        public void afterPropertiesSet() throws Exception {
058            ensureSystemUsageHasStore();
059            if (shouldAutostart()) {
060                start();
061            }
062        }
063    
064        @Override
065        protected boolean shouldAutostart() {
066            return start;
067        }
068    
069        private void ensureSystemUsageHasStore() throws IOException {
070            SystemUsage usage = getSystemUsage();
071            if (usage.getStoreUsage().getStore() == null) {
072                usage.getStoreUsage().setStore(getPersistenceAdapter());
073            }
074            if (usage.getTempUsage().getStore() == null) {
075                usage.getTempUsage().setStore(getTempDataStore());
076            }
077            if (usage.getJobSchedulerUsage().getStore() == null) {
078                usage.getJobSchedulerUsage().setStore(getJobSchedulerStore());
079            }
080        }
081    
082        /**
083         *
084         * @throws Exception
085         * @org.apache.xbean.DestroyMethod
086         */
087        @PreDestroy
088        public void destroy() throws Exception {
089            stop();
090        }
091    
092        @Override
093        public void stop() throws Exception {
094            // must clear this Spring cache to avoid any memory leaks
095            CachedIntrospectionResults.clearClassLoader(getClass().getClassLoader());
096            super.stop();
097        }
098    
099        /**
100         * Sets whether or not the broker is started along with the ApplicationContext it is defined within.
101         * Normally you would want the broker to start up along with the ApplicationContext but sometimes when working
102         * with JUnit tests you may wish to start and stop the broker explicitly yourself.
103         */
104        public void setStart(boolean start) {
105            this.start = start;
106        }
107    }