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.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
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        private static final transient Logger LOG = LoggerFactory.getLogger(XBeanBrokerService.class);
045        
046        private boolean start = true;
047        
048        public XBeanBrokerService() {
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        }
078    
079        /**
080         *
081         * @throws Exception
082         * @org.apache.xbean.DestroyMethod
083         */
084        @PreDestroy
085        public void destroy() throws Exception {
086            stop();
087        }
088        
089    
090        /**
091         * Sets whether or not the broker is started along with the ApplicationContext it is defined within.
092         * Normally you would want the broker to start up along with the ApplicationContext but sometimes when working
093         * with JUnit tests you may wish to start and stop the broker explicitly yourself.
094         */
095        public void setStart(boolean start) {
096            this.start = start;
097        }
098    
099        /**
100         * Sets whether the broker should shutdown the ApplicationContext when the broker jvm is shutdown.
101         * The broker can be stopped because the underlying JDBC store is unavailable for example.
102         */
103        @Deprecated
104        public void setDestroyApplicationContextOnShutdown(boolean destroy) {
105            LOG.warn("destroyApplicationContextOnShutdown parameter is deprecated, please use shutdown hooks instead");
106        }
107        
108        /**
109         * Sets whether the broker should shutdown the ApplicationContext when the broker is stopped.
110         * The broker can be stopped because the underlying JDBC store is unavailable for example.
111         */
112        @Deprecated
113        public void setDestroyApplicationContextOnStop(boolean destroy) {
114            LOG.warn("destroyApplicationContextOnStop parameter is deprecated, please use shutdown hooks instead");
115        }
116        
117    }