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.broker; 018 019import java.io.File; 020import java.io.FileInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023import java.net.MalformedURLException; 024import java.net.URI; 025import java.net.URL; 026import java.util.Map; 027import java.util.Properties; 028 029import org.apache.activemq.util.IntrospectionSupport; 030 031/** 032 * A {@link BrokerFactoryHandler} which uses a properties file to configure the 033 * broker's various policies. 034 * 035 * 036 */ 037public class PropertiesBrokerFactory implements BrokerFactoryHandler { 038 039 public BrokerService createBroker(URI brokerURI) throws Exception { 040 041 Map<Object, Object> properties = loadProperties(brokerURI); 042 BrokerService brokerService = createBrokerService(brokerURI, properties); 043 044 IntrospectionSupport.setProperties(brokerService, properties); 045 return brokerService; 046 } 047 048 /** 049 * Lets load the properties from some external URL or a relative file 050 */ 051 protected Map<Object, Object> loadProperties(URI brokerURI) throws IOException { 052 // lets load a URI 053 String remaining = brokerURI.getSchemeSpecificPart(); 054 Properties properties = new Properties(); 055 File file = new File(remaining); 056 057 try (InputStream inputStream = loadStream(file, remaining)) { 058 if (inputStream != null) { 059 properties.load(inputStream); 060 } 061 } 062 063 // should we append any system properties? 064 try { 065 Properties systemProperties = System.getProperties(); 066 properties.putAll(systemProperties); 067 } catch (Exception e) { 068 // ignore security exception 069 } 070 return properties; 071 } 072 073 protected InputStream loadStream(File file, String remaining) throws IOException { 074 InputStream inputStream = null; 075 if (file != null && file.exists()) { 076 inputStream = new FileInputStream(file); 077 } else { 078 URL url = null; 079 try { 080 url = new URL(remaining); 081 } catch (MalformedURLException e) { 082 // lets now see if we can find the name on the classpath 083 inputStream = findResourceOnClassPath(remaining); 084 if (inputStream == null) { 085 throw new IOException("File does not exist: " + remaining + ", could not be found on the classpath and is not a valid URL: " + e); 086 } 087 } 088 if (inputStream == null && url != null) { 089 inputStream = url.openStream(); 090 } 091 } 092 return inputStream; 093 } 094 095 protected InputStream findResourceOnClassPath(String remaining) { 096 InputStream answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(remaining); 097 if (answer == null) { 098 answer = getClass().getClassLoader().getResourceAsStream(remaining); 099 } 100 return answer; 101 } 102 103 protected BrokerService createBrokerService(URI brokerURI, Map<Object, Object> properties) { 104 return new BrokerService(); 105 } 106}