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.apache.commons.logging.Log; 021 import org.apache.commons.logging.LogFactory; 022 import org.activemq.broker.BrokerContainer; 023 import org.activemq.broker.BrokerContainerFactory; 024 import org.activemq.broker.BrokerContext; 025 import org.activemq.util.IdGenerator; 026 import org.springframework.core.io.Resource; 027 import org.springframework.core.io.ResourceEditor; 028 029 /** 030 * A Spring implementatation of {@link BrokerContainerFactory} which uses an XML 031 * deployment configuration file to load and configure a {@link BrokerContainer} 032 * 033 * @version $Revision$ 034 */ 035 public class SpringBrokerContainerFactory implements BrokerContainerFactory { 036 private static final Log log = LogFactory.getLog(SpringBrokerContainerFactory.class); 037 038 private Resource resource; 039 040 041 /** 042 * A static factory method that can be used in Spring config files using a factory method 043 * mechanism to create a broker container easily. 044 */ 045 public static BrokerContainer newInstance(Resource resource) { 046 IdGenerator idgen = new IdGenerator(); 047 return newInstance(resource, idgen.generateId()); 048 } 049 050 /** 051 * A static factory method that can be used in Spring config files using a factory method 052 * mechanism to create a broker container easily. 053 */ 054 public static BrokerContainer newInstance(Resource resource, String brokerName) { 055 SpringBrokerContainerFactory factory = new SpringBrokerContainerFactory(resource); 056 return factory.createBrokerContainer(brokerName, BrokerContext.getInstance()); 057 } 058 059 /** 060 * A helper method, invoked via reflection, to create a new factory from a given configuration 061 * file String which if it starts with classpath: is a classpath URI otherwise a URL is assumed. 062 * 063 * @param resourceName 064 * @return 065 */ 066 public static SpringBrokerContainerFactory newFactory(String resourceName) { 067 ResourceEditor editor = new ResourceEditor(); 068 editor.setAsText(resourceName); 069 Resource resource = (Resource) editor.getValue(); 070 if (resource == null) { 071 throw new IllegalArgumentException("Could not convert '" + resourceName + "' into a Spring Resource"); 072 } 073 return new SpringBrokerContainerFactory(resource); 074 } 075 076 public SpringBrokerContainerFactory() { 077 } 078 079 public SpringBrokerContainerFactory(Resource resource) { 080 this.resource = resource; 081 } 082 083 public BrokerContainer createBrokerContainer(String brokerName, BrokerContext context) { 084 log.info("Loading ActiveMQ broker from configuration: " + resource); 085 086 ActiveMQBeanFactory beanFactory = new ActiveMQBeanFactory(brokerName, resource); 087 return (BrokerContainer) beanFactory.getBean("broker"); 088 } 089 090 // Properties 091 //------------------------------------------------------------------------- 092 public Resource getResource() { 093 return resource; 094 } 095 096 public void setResource(Resource resource) { 097 this.resource = resource; 098 } 099 100 }