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 */ 017package org.apache.activemq.xbean; 018 019import java.beans.PropertyEditorManager; 020import java.net.URI; 021 022import org.apache.activemq.broker.BrokerService; 023import org.apache.xbean.spring.context.ResourceXmlApplicationContext; 024import org.apache.xbean.spring.context.impl.URIEditor; 025import org.springframework.beans.BeansException; 026import org.springframework.beans.factory.DisposableBean; 027import org.springframework.beans.factory.FactoryBean; 028import org.springframework.beans.factory.InitializingBean; 029import org.springframework.context.ApplicationContext; 030import org.springframework.context.ApplicationContextAware; 031import org.springframework.core.io.Resource; 032 033/** 034 * A Spring {@link FactoryBean} which creates an embedded broker inside a Spring 035 * XML using an external <a href="http://gbean.org/Custom+XML">XBean Spring XML 036 * configuration file</a> which provides a much neater and more concise XML 037 * format. 038 * 039 * 040 */ 041public class BrokerFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware { 042 043 static { 044 PropertyEditorManager.registerEditor(URI.class, URIEditor.class); 045 } 046 047 private Resource config; 048 private XBeanBrokerService broker; 049 private boolean start; 050 private ResourceXmlApplicationContext context; 051 private ApplicationContext parentContext; 052 053 private boolean systemExitOnShutdown; 054 private int systemExitOnShutdownExitCode; 055 056 public BrokerFactoryBean() { 057 } 058 059 public BrokerFactoryBean(Resource config) { 060 this.config = config; 061 } 062 063 public Object getObject() throws Exception { 064 return broker; 065 } 066 067 public Class getObjectType() { 068 return BrokerService.class; 069 } 070 071 public boolean isSingleton() { 072 return true; 073 } 074 075 public void setApplicationContext(ApplicationContext parentContext) throws BeansException { 076 this.parentContext = parentContext; 077 } 078 079 public void afterPropertiesSet() throws Exception { 080 if (config == null) { 081 throw new IllegalArgumentException("config property must be set"); 082 } 083 context = new ResourceXmlApplicationContext(config, parentContext); 084 085 try { 086 broker = (XBeanBrokerService)context.getBean("broker"); 087 } catch (BeansException e) { 088 // ignore... 089 // log.trace("No bean named broker available: " + e, e); 090 } 091 if (broker == null) { 092 // lets try find by type 093 String[] names = context.getBeanNamesForType(BrokerService.class); 094 for (int i = 0; i < names.length; i++) { 095 String name = names[i]; 096 broker = (XBeanBrokerService)context.getBean(name); 097 if (broker != null) { 098 break; 099 } 100 } 101 } 102 if (broker == null) { 103 throw new IllegalArgumentException("The configuration has no BrokerService instance for resource: " + config); 104 } 105 106 if( systemExitOnShutdown ) { 107 broker.addShutdownHook(new Runnable(){ 108 public void run() { 109 System.exit(systemExitOnShutdownExitCode); 110 } 111 }); 112 } 113 if (start) { 114 broker.start(); 115 } 116 } 117 118 public void destroy() throws Exception { 119 if (context != null) { 120 context.close(); 121 } 122 if (broker != null) { 123 broker.stop(); 124 } 125 } 126 127 public Resource getConfig() { 128 return config; 129 } 130 131 public void setConfig(Resource config) { 132 this.config = config; 133 } 134 135 public BrokerService getBroker() { 136 return broker; 137 } 138 139 public boolean isStart() { 140 return start; 141 } 142 143 public void setStart(boolean start) { 144 this.start = start; 145 } 146 147 public boolean isSystemExitOnStop() { 148 return systemExitOnShutdown; 149 } 150 151 public void setSystemExitOnStop(boolean systemExitOnStop) { 152 this.systemExitOnShutdown = systemExitOnStop; 153 } 154 155 public boolean isSystemExitOnShutdown() { 156 return systemExitOnShutdown; 157 } 158 159 public void setSystemExitOnShutdown(boolean systemExitOnShutdown) { 160 this.systemExitOnShutdown = systemExitOnShutdown; 161 } 162 163 public int getSystemExitOnShutdownExitCode() { 164 return systemExitOnShutdownExitCode; 165 } 166 167 public void setSystemExitOnShutdownExitCode(int systemExitOnShutdownExitCode) { 168 this.systemExitOnShutdownExitCode = systemExitOnShutdownExitCode; 169 } 170 171}