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.net.URI;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.apache.activemq.util.IntrospectionSupport;
024    import org.apache.activemq.util.URISupport;
025    import org.apache.activemq.util.URISupport.CompositeData;
026    
027    /**
028     * Simple BrokerFactorySPI which using the brokerURI to extract the
029     * configuration parameters for the broker service. This directly configures the
030     * pojo model so there is no dependency on spring for configuration.
031     *
032     *
033     */
034    public class DefaultBrokerFactory implements BrokerFactoryHandler {
035    
036        public BrokerService createBroker(URI brokerURI) throws Exception {
037    
038            CompositeData compositeData = URISupport.parseComposite(brokerURI);
039            Map<String, String> params = new HashMap<String, String>(compositeData.getParameters());
040    
041            BrokerService brokerService = new BrokerService();
042            IntrospectionSupport.setProperties(brokerService, params);
043            if (!params.isEmpty()) {
044                String msg = "There are " + params.size()
045                    + " Broker options that couldn't be set on the BrokerService."
046                    + " Check the options are spelled correctly."
047                    + " Unknown parameters=[" + params + "]."
048                    + " This BrokerService cannot be started.";
049                throw new IllegalArgumentException(msg);
050            }
051    
052            if (compositeData.getPath() != null) {
053                brokerService.setBrokerName(compositeData.getPath());
054            }
055    
056            URI[] components = compositeData.getComponents();
057            for (int i = 0; i < components.length; i++) {
058                if ("network".equals(components[i].getScheme())) {
059                    brokerService.addNetworkConnector(components[i].getSchemeSpecificPart());
060                } else if ("proxy".equals(components[i].getScheme())) {
061                    brokerService.addProxyConnector(components[i].getSchemeSpecificPart());
062                } else {
063                    brokerService.addConnector(components[i]);
064                }
065            }
066            return brokerService;
067        }
068    
069    }