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.BrokerService;
025    import org.apache.activemq.usage.SystemUsage;
026    import org.springframework.beans.CachedIntrospectionResults;
027    
028    /**
029     * An ActiveMQ Message Broker. It consists of a number of transport
030     * connectors, network connectors and a bunch of properties which can be used to
031     * configure the broker as its lazily created.
032     *
033     * @org.apache.xbean.XBean element="broker" rootElement="true"
034     * @org.apache.xbean.Defaults {code:xml}
035     * <broker test="foo.bar">
036     *   lets.
037     *   see what it includes.
038     * </broker>
039     * {code}
040     *
041     */
042    public class XBeanBrokerService extends BrokerService {
043    
044        private boolean start = true;
045    
046        public XBeanBrokerService() {
047        }
048    
049        /**
050         *
051         * @throws Exception
052         * @org.apache.xbean.InitMethod
053         */
054        @PostConstruct
055        public void afterPropertiesSet() throws Exception {
056            ensureSystemUsageHasStore();
057            if (shouldAutostart()) {
058                start();
059            }
060        }
061    
062        @Override
063        protected boolean shouldAutostart() {
064            return start;
065        }
066    
067        private void ensureSystemUsageHasStore() throws IOException {
068            SystemUsage usage = getSystemUsage();
069            if (usage.getStoreUsage().getStore() == null) {
070                usage.getStoreUsage().setStore(getPersistenceAdapter());
071            }
072            if (usage.getTempUsage().getStore() == null) {
073                usage.getTempUsage().setStore(getTempDataStore());
074            }
075        }
076    
077        /**
078         *
079         * @throws Exception
080         * @org.apache.xbean.DestroyMethod
081         */
082        @PreDestroy
083        public void destroy() throws Exception {
084            stop();
085        }
086    
087        @Override
088        public void stop() throws Exception {
089            // must clear this Spring cache to avoid any memory leaks
090            CachedIntrospectionResults.clearClassLoader(getClass().getClassLoader());
091            super.stop();
092        }
093    
094        /**
095         * Sets whether or not the broker is started along with the ApplicationContext it is defined within.
096         * Normally you would want the broker to start up along with the ApplicationContext but sometimes when working
097         * with JUnit tests you may wish to start and stop the broker explicitly yourself.
098         */
099        public void setStart(boolean start) {
100            this.start = start;
101        }
102    }