added support for using a properties file to configure the broker to avoid the dependency on Spring and xbean; for AMQ-1206

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@518590 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2007-03-15 11:39:04 +00:00
parent 489f5aa204
commit a640190fc4
11 changed files with 224 additions and 25 deletions

View File

@ -34,10 +34,6 @@ public class BrokerFactory {
static final private FactoryFinder brokerFactoryHandlerFinder = new FactoryFinder("META-INF/services/org/apache/activemq/broker/");
public interface BrokerFactoryHandler {
public BrokerService createBroker(URI brokerURI) throws Exception;
}
public static BrokerFactoryHandler createBrokerFactoryHandler(String type) throws IOException {
try {
return (BrokerFactoryHandler)brokerFactoryHandlerFinder.newInstance(type);
@ -48,22 +44,35 @@ public class BrokerFactory {
/**
* Creates a broker from a URI configuration
* @param brokerURI
* @throws Exception
* @param brokerURI the URI scheme to configure the broker
* @throws Exception
*/
public static BrokerService createBroker(URI brokerURI) throws Exception {
return createBroker(brokerURI, false);
}
/**
* Creates a broker from a URI configuration
* @param brokerURI the URI scheme to configure the broker
* @param startBroker whether or not the broker should have its {@link BrokerService#start()} method called after construction
* @throws Exception
*/
public static BrokerService createBroker(URI brokerURI, boolean startBroker) throws Exception {
if( brokerURI.getScheme() == null )
throw new IllegalArgumentException("Invalid broker URI, no scheme specified: "+brokerURI);
BrokerFactoryHandler handler = createBrokerFactoryHandler(brokerURI.getScheme());
BrokerService broker = handler.createBroker(brokerURI);
if (startBroker) {
broker.start();
}
return broker;
}
/**
* Creates a broker from a URI configuration
* @param brokerURI
* @param brokerURI the URI scheme to configure the broker
* @throws Exception
*/
public static BrokerService createBroker(String brokerURI) throws Exception {
@ -71,4 +80,15 @@ public class BrokerFactory {
}
/**
* Creates a broker from a URI configuration
* @param brokerURI the URI scheme to configure the broker
* @param startBroker whether or not the broker should have its {@link BrokerService#start()} method called after construction
* @throws Exception
*/
public static BrokerService createBroker(String brokerURI, boolean startBroker) throws Exception {
return createBroker(new URI(brokerURI), startBroker);
}
}

View File

@ -0,0 +1,29 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker;
import java.net.URI;
/**
* Represents the interface used to create a broker from a URI scheme.
*
* @version $Revision$
*/
public interface BrokerFactoryHandler {
public BrokerService createBroker(URI brokerURI) throws Exception;
}

View File

@ -155,7 +155,22 @@ public class BrokerService implements Service, Serializable {
private int persistenceThreadPriority = Thread.MAX_PRIORITY;
private boolean useLocalHostBrokerName = false;
private CountDownLatch stoppedLatch = new CountDownLatch(1);
static{
String localHostName = "localhost";
try{
localHostName=java.net.InetAddress.getLocalHost().getHostName();
}catch(UnknownHostException e){
log.error("Failed to resolve localhost");
}
LOCAL_HOST_NAME = localHostName;
}
@Override
public String toString() {
return "BrokerService[" + getBrokerName() + "]";
}
/**
* Adds a new transport connector for the given bind address
*
@ -1654,16 +1669,6 @@ public class BrokerService implements Service, Serializable {
}
}
}
static{
String localHostName = "localhost";
try{
localHostName=java.net.InetAddress.getLocalHost().getHostName();
}catch(UnknownHostException e){
log.error("Failed to resolve localhost");
}
LOCAL_HOST_NAME = localHostName;
}

View File

@ -17,7 +17,7 @@
*/
package org.apache.activemq.broker;
import org.apache.activemq.broker.BrokerFactory.BrokerFactoryHandler;
import org.apache.activemq.broker.BrokerFactoryHandler;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.URISupport;
import org.apache.activemq.util.URISupport.CompositeData;
@ -55,9 +55,6 @@ public class DefaultBrokerFactory implements BrokerFactoryHandler {
brokerService.addConnector(components[i]);
}
}
// TODO we want folks to be able to add other connectors and start the broker afterwards
//brokerService.start();
return brokerService;
}

View File

@ -0,0 +1,102 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker;
import org.apache.activemq.util.IntrospectionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Map;
import java.util.Properties;
/**
* A {@link BrokerFactoryHandler} which uses a properties file to
* configure the broker's various policies.
*
* @version $Revision$
*/
public class PropertiesBrokerFactory implements BrokerFactoryHandler {
public BrokerService createBroker(URI brokerURI) throws Exception {
Map properties = loadProperties(brokerURI);
BrokerService brokerService = createBrokerService(brokerURI, properties);
IntrospectionSupport.setProperties(brokerService, properties);
return brokerService;
}
/**
* Lets load the properties from some external URL or a relative file
*/
protected Map loadProperties(URI brokerURI) throws IOException {
// lets load a URI
String remaining = brokerURI.getSchemeSpecificPart();
Properties properties = new Properties();
File file = new File(remaining);
InputStream inputStream = null;
if (file.exists()) {
inputStream = new FileInputStream(file);
}
else {
URL url = null;
try {
url = new URL(remaining);
}
catch (MalformedURLException e) {
// lets now see if we can find the name on the classpath
inputStream = findResourceOnClassPath(remaining);
if (inputStream == null) {
throw new IOException("File does not exist: " + remaining + ", could not be found on the classpath and is not a valid URL: " + e);
}
}
if (inputStream == null) {
inputStream = url.openStream();
}
}
properties.load(inputStream);
// should we append any system properties?
try {
Properties systemProperties = System.getProperties();
properties.putAll(systemProperties);
}
catch (Exception e) {
// ignore security exception
}
return properties;
}
protected InputStream findResourceOnClassPath(String remaining) {
InputStream answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(remaining);
if (answer == null) {
answer = getClass().getClassLoader().getResourceAsStream(remaining);
}
return answer;
}
protected BrokerService createBrokerService(URI brokerURI, Map properties) {
return new BrokerService();
}
}

View File

@ -25,7 +25,7 @@ import java.util.Map;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.broker.BrokerFactory.BrokerFactoryHandler;
import org.apache.activemq.broker.BrokerFactoryHandler;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportFactory;
import org.apache.activemq.transport.TransportServer;

View File

@ -26,7 +26,7 @@ import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerRegistry;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.broker.BrokerFactory.BrokerFactoryHandler;
import org.apache.activemq.broker.BrokerFactoryHandler;
import org.apache.activemq.transport.MarshallingTransportFilter;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportFactory;

View File

@ -21,7 +21,7 @@ import java.beans.PropertyEditorManager;
import java.net.URI;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.BrokerFactory.BrokerFactoryHandler;
import org.apache.activemq.broker.BrokerFactoryHandler;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;

View File

@ -0,0 +1 @@
class=org.apache.activemq.broker.PropertiesBrokerFactory

View File

@ -0,0 +1,42 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.config;
import junit.framework.TestCase;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @version $Revision$
*/
public class BrokerPropertiesTest extends TestCase {
private static final transient Log log = LogFactory.getLog(BrokerPropertiesTest.class);
public void testPropertiesFile() throws Exception {
BrokerService broker = BrokerFactory.createBroker("properties:org/apache/activemq/config/broker.properties");
log.info("Created broker: " + broker);
assertNotNull(broker);
assertEquals("isUseJmx()", false, broker.isUseJmx());
assertEquals("isPersistent()", false, broker.isPersistent());
assertEquals("getBrokerName()", "Cheese", broker.getBrokerName());
}
}

View File

@ -0,0 +1,3 @@
useJmx = false
persistent = false
brokerName = Cheese