001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 **/
018 package org.activemq.spring;
019
020 import org.springframework.beans.factory.FactoryBean;
021 import org.springframework.beans.factory.InitializingBean;
022 import org.springframework.beans.factory.DisposableBean;
023 import org.springframework.core.io.Resource;
024 import org.activemq.broker.BrokerContainer;
025
026 /**
027 * A Spring {@link FactoryBean} to make it easy to create an embedded broker
028 * inside Spring.
029 *
030 * @version $Revision: 1.1 $
031 */
032 public class BrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
033 private Resource config;
034 private BrokerContainer broker;
035
036 public Object getObject() throws Exception {
037 return broker;
038 }
039
040 public Class getObjectType() {
041 return BrokerContainer.class;
042 }
043
044 public boolean isSingleton() {
045 return true;
046 }
047
048 public void afterPropertiesSet() throws Exception {
049 if (config == null) {
050 throw new IllegalArgumentException("configu property must be set");
051 }
052 broker = SpringBrokerContainerFactory.newInstance(config);
053 broker.start();
054 }
055
056 public void destroy() throws Exception {
057 if (broker != null) {
058 broker.stop();
059 }
060 }
061
062 public Resource getConfig() {
063 return config;
064 }
065
066 public void setConfig(Resource config) {
067 this.config = config;
068 }
069 }