KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > xbean > PooledBrokerFactoryBean


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.xbean;
19
20 import java.util.HashMap JavaDoc;
21
22 import org.apache.activemq.broker.BrokerService;
23 import org.springframework.beans.factory.DisposableBean;
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.core.io.Resource;
27
28 /**
29  * Used to share a single broker even if you have multiple broker bean definitions.
30  *
31  * A use case is where you have multiple web applications that want to start an embedded broker
32  * but only the first one to deploy should actually start it.
33  *
34  * @version $Revision$
35  */

36 public class PooledBrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
37
38     static final HashMap JavaDoc sharedBrokerMap = new HashMap JavaDoc();
39     
40     private boolean start;
41     private Resource config;
42     
43     static class SharedBroker {
44         BrokerFactoryBean factory;
45         int refCount;
46     }
47     
48     public void afterPropertiesSet() throws Exception JavaDoc {
49         synchronized( sharedBrokerMap ) {
50             SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config.getFilename());
51             if( sharedBroker == null ) {
52                 sharedBroker = new SharedBroker();
53                 sharedBroker.factory = new BrokerFactoryBean();
54                 sharedBroker.factory.setConfig(config);
55                 sharedBroker.factory.setStart(start);
56                 sharedBroker.factory.afterPropertiesSet();
57                 sharedBrokerMap.put(config.getFilename(), sharedBroker);
58             }
59             sharedBroker.refCount++;
60         }
61     }
62
63     public void destroy() throws Exception JavaDoc {
64         synchronized( sharedBrokerMap ) {
65             SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config.getFilename());
66             if( sharedBroker != null ) {
67                 sharedBroker.refCount--;
68                 if( sharedBroker.refCount==0 ) {
69                     sharedBroker.factory.destroy();
70                     sharedBrokerMap.remove(config.getFilename());
71                 }
72             }
73         }
74     }
75
76     public Resource getConfig() {
77         return config;
78     }
79
80     public Object JavaDoc getObject() throws Exception JavaDoc {
81         synchronized( sharedBrokerMap ) {
82             SharedBroker sharedBroker = (SharedBroker) sharedBrokerMap.get(config.getFilename());
83             if( sharedBroker != null ) {
84                 return sharedBroker.factory.getObject();
85             }
86         }
87         return null;
88     }
89
90     public Class JavaDoc getObjectType() {
91         return BrokerService.class;
92     }
93
94     public boolean isSingleton() {
95         return true;
96     }
97
98     public boolean isStart() {
99         return start;
100     }
101
102     public void setConfig(Resource config) {
103         this.config = config;
104     }
105
106     public void setStart(boolean start) {
107         this.start=start;
108     }
109     
110 }
111
Popular Tags