1 18 package org.apache.activemq.broker; 19 20 import org.apache.activemq.util.IntrospectionSupport; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URI ; 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 38 public class PropertiesBrokerFactory implements BrokerFactoryHandler { 39 40 public BrokerService createBroker(URI brokerURI) throws Exception { 41 42 Map properties = loadProperties(brokerURI); 43 BrokerService brokerService = createBrokerService(brokerURI, properties); 44 45 IntrospectionSupport.setProperties(brokerService, properties); 46 return brokerService; 47 } 48 49 52 protected Map loadProperties(URI brokerURI) throws IOException { 53 String remaining = brokerURI.getSchemeSpecificPart(); 55 Properties properties = new Properties (); 56 File file = new File (remaining); 57 58 InputStream inputStream = null; 59 if (file.exists()) { 60 inputStream = new FileInputStream (file); 61 } 62 else { 63 URL url = null; 64 try { 65 url = new URL (remaining); 66 } 67 catch (MalformedURLException e) { 68 inputStream = findResourceOnClassPath(remaining); 70 if (inputStream == null) { 71 throw new IOException ("File does not exist: " + remaining + ", could not be found on the classpath and is not a valid URL: " + e); 72 } 73 } 74 if (inputStream == null) { 75 inputStream = url.openStream(); 76 } 77 } 78 properties.load(inputStream); 79 80 try { 82 Properties systemProperties = System.getProperties(); 83 properties.putAll(systemProperties); 84 } 85 catch (Exception e) { 86 } 88 return properties; 89 } 90 91 protected InputStream findResourceOnClassPath(String remaining) { 92 InputStream answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(remaining); 93 if (answer == null) { 94 answer = getClass().getClassLoader().getResourceAsStream(remaining); 95 } 96 return answer; 97 } 98 99 protected BrokerService createBrokerService(URI brokerURI, Map properties) { 100 return new BrokerService(); 101 } 102 } 103 | Popular Tags |