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     */
017    package org.apache.activemq.broker;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.net.MalformedURLException;
024    import java.net.URI;
025    import java.net.URL;
026    import java.util.Map;
027    import java.util.Properties;
028    
029    import 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     */
037    public class PropertiesBrokerFactory implements BrokerFactoryHandler {
038    
039        public BrokerService createBroker(URI brokerURI) throws Exception {
040    
041            Map 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 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            InputStream inputStream = null;
058            if (file.exists()) {
059                inputStream = new FileInputStream(file);
060            } else {
061                URL url = null;
062                try {
063                    url = new URL(remaining);
064                } catch (MalformedURLException e) {
065                    // lets now see if we can find the name on the classpath
066                    inputStream = findResourceOnClassPath(remaining);
067                    if (inputStream == null) {
068                        throw new IOException("File does not exist: " + remaining + ", could not be found on the classpath and is not a valid URL: " + e);
069                    }
070                }
071                if (inputStream == null && url != null) {
072                    inputStream = url.openStream();
073                }
074            }
075            if (inputStream != null) {
076                properties.load(inputStream);
077                inputStream.close();
078            }
079    
080            // should we append any system properties?
081            try {
082                Properties systemProperties = System.getProperties();
083                properties.putAll(systemProperties);
084            } catch (Exception e) {
085                // ignore security exception
086            }
087            return properties;
088        }
089    
090        protected InputStream findResourceOnClassPath(String remaining) {
091            InputStream answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(remaining);
092            if (answer == null) {
093                answer = getClass().getClassLoader().getResourceAsStream(remaining);
094            }
095            return answer;
096        }
097    
098        protected BrokerService createBrokerService(URI brokerURI, Map properties) {
099            return new BrokerService();
100        }
101    }