KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.activemq.broker.BrokerService;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.springframework.beans.BeansException;
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.FactoryBean;
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.context.ApplicationContextAware;
29 import org.springframework.core.io.Resource;
30 import org.apache.xbean.spring.context.ResourceXmlApplicationContext;
31 import org.apache.xbean.spring.context.impl.URIEditor;
32
33 import java.beans.PropertyEditorManager JavaDoc;
34 import java.net.URI JavaDoc;
35
36 /**
37  * A Spring {@link FactoryBean} which creates an embedded broker inside a Spring
38  * XML using an external <a HREF="http://gbean.org/Custom+XML">XBean Spring XML
39  * configuration file</a> which provides a much neater and more concise XML
40  * format.
41  *
42  * @version $Revision: 1.1 $
43  */

44 public class BrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware {
45     private static final Log log = LogFactory.getLog(BrokerFactoryBean.class);
46
47     static {
48         PropertyEditorManager.registerEditor(URI JavaDoc.class, URIEditor.class);
49     }
50
51     private Resource config;
52     private XBeanBrokerService broker;
53     private boolean start = false;
54     private ResourceXmlApplicationContext context;
55     private ApplicationContext parentContext;
56
57     public BrokerFactoryBean() {
58     }
59
60     public BrokerFactoryBean(Resource config) {
61         this.config = config;
62     }
63
64     public Object JavaDoc getObject() throws Exception JavaDoc {
65         return broker;
66     }
67
68     public Class JavaDoc getObjectType() {
69         return BrokerService.class;
70     }
71
72     public boolean isSingleton() {
73         return true;
74     }
75
76     public void setApplicationContext(ApplicationContext parentContext) throws BeansException {
77         this.parentContext = parentContext;
78     }
79     
80     public void afterPropertiesSet() throws Exception JavaDoc {
81         if (config == null) {
82             throw new IllegalArgumentException JavaDoc("config property must be set");
83         }
84         context = new ResourceXmlApplicationContext(config, parentContext);
85
86         try {
87             broker = (XBeanBrokerService) context.getBean("broker");
88         }
89         catch (BeansException e) {
90             // ignore...
91
//log.trace("No bean named broker available: " + e, e);
92
}
93         if (broker == null) {
94             // lets try find by type
95
String JavaDoc[] names = context.getBeanNamesForType(BrokerService.class);
96             for (int i = 0; i < names.length; i++) {
97                 String JavaDoc name = names[i];
98                 broker = (XBeanBrokerService) context.getBean(name);
99                 if (broker != null) {
100                     break;
101                 }
102             }
103         }
104         if (broker == null) {
105             throw new IllegalArgumentException JavaDoc("The configuration has no BrokerService instance for resource: " + config);
106         }
107         if (start) {
108             broker.start();
109         }
110     }
111
112     public void destroy() throws Exception JavaDoc {
113         if (context != null) {
114             context.close();
115         }
116         if (broker != null) {
117             broker.stop();
118         }
119     }
120
121     public Resource getConfig() {
122         return config;
123     }
124
125     public void setConfig(Resource config) {
126         this.config = config;
127     }
128
129     public BrokerService getBroker() {
130         return broker;
131     }
132
133     public boolean isStart() {
134         return start;
135     }
136
137     public void setStart(boolean start) {
138         this.start = start;
139     }
140
141
142 }
143
Popular Tags