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.util.HashMap;
020
021 import org.apache.activemq.broker.BrokerService;
022 import org.springframework.beans.factory.DisposableBean;
023 import org.springframework.beans.factory.FactoryBean;
024 import org.springframework.beans.factory.InitializingBean;
025 import org.springframework.core.io.Resource;
026
027 /**
028 * Used to share a single broker even if you have multiple broker bean
029 * definitions. A use case is where you have multiple web applications that want
030 * to start an embedded broker but only the first one to deploy should actually
031 * start it.
032 *
033 * @version $Revision$
034 */
035 public class PooledBrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
036
037 static final HashMap<String, SharedBroker> SHARED_BROKER_MAP = new HashMap<String, SharedBroker>();
038
039 private boolean start;
040 private Resource config;
041
042 static class SharedBroker {
043 BrokerFactoryBean factory;
044 int refCount;
045 }
046
047 public void afterPropertiesSet() throws Exception {
048 synchronized (SHARED_BROKER_MAP) {
049 SharedBroker sharedBroker = SHARED_BROKER_MAP.get(config.getFilename());
050 if (sharedBroker == null) {
051 sharedBroker = new SharedBroker();
052 sharedBroker.factory = new BrokerFactoryBean();
053 sharedBroker.factory.setConfig(config);
054 sharedBroker.factory.setStart(start);
055 sharedBroker.factory.afterPropertiesSet();
056 SHARED_BROKER_MAP.put(config.getFilename(), sharedBroker);
057 }
058 sharedBroker.refCount++;
059 }
060 }
061
062 public void destroy() throws Exception {
063 synchronized (SHARED_BROKER_MAP) {
064 SharedBroker sharedBroker = SHARED_BROKER_MAP.get(config.getFilename());
065 if (sharedBroker != null) {
066 sharedBroker.refCount--;
067 if (sharedBroker.refCount == 0) {
068 sharedBroker.factory.destroy();
069 SHARED_BROKER_MAP.remove(config.getFilename());
070 }
071 }
072 }
073 }
074
075 public Resource getConfig() {
076 return config;
077 }
078
079 public Object getObject() throws Exception {
080 synchronized (SHARED_BROKER_MAP) {
081 SharedBroker sharedBroker = SHARED_BROKER_MAP.get(config.getFilename());
082 if (sharedBroker != null) {
083 return sharedBroker.factory.getObject();
084 }
085 }
086 return null;
087 }
088
089 public Class getObjectType() {
090 return BrokerService.class;
091 }
092
093 public boolean isSingleton() {
094 return true;
095 }
096
097 public boolean isStart() {
098 return start;
099 }
100
101 public void setConfig(Resource config) {
102 this.config = config;
103 }
104
105 public void setStart(boolean start) {
106 this.start = start;
107 }
108
109 }