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.transport.discovery;
018    
019    import java.io.IOException;
020    import java.net.URI;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import org.apache.activemq.util.FactoryFinder;
024    import org.apache.activemq.util.IOExceptionSupport;
025    
026    public abstract class DiscoveryAgentFactory {
027    
028        private static final FactoryFinder DISCOVERY_AGENT_FINDER = new FactoryFinder("META-INF/services/org/apache/activemq/transport/discoveryagent/");
029        private static final ConcurrentHashMap<String, DiscoveryAgentFactory> DISCOVERY_AGENT_FACTORYS = new ConcurrentHashMap<String, DiscoveryAgentFactory>();
030    
031        /**
032         * @param uri
033         * @return
034         * @throws IOException
035         */
036        private static DiscoveryAgentFactory findDiscoveryAgentFactory(URI uri) throws IOException {
037            String scheme = uri.getScheme();
038            if (scheme == null) {
039                throw new IOException("DiscoveryAgent scheme not specified: [" + uri + "]");
040            }
041            DiscoveryAgentFactory daf = DISCOVERY_AGENT_FACTORYS.get(scheme);
042            if (daf == null) {
043                // Try to load if from a META-INF property.
044                try {
045                    daf = (DiscoveryAgentFactory)DISCOVERY_AGENT_FINDER.newInstance(scheme);
046                    DISCOVERY_AGENT_FACTORYS.put(scheme, daf);
047                } catch (Throwable e) {
048                    throw IOExceptionSupport.create("DiscoveryAgent scheme NOT recognized: [" + scheme + "]", e);
049                }
050            }
051            return daf;
052        }
053    
054        public static DiscoveryAgent createDiscoveryAgent(URI uri) throws IOException {
055            DiscoveryAgentFactory tf = findDiscoveryAgentFactory(uri);
056            return tf.doCreateDiscoveryAgent(uri);
057    
058        }
059    
060        protected abstract DiscoveryAgent doCreateDiscoveryAgent(URI uri) throws IOException;
061        // {
062        // try {
063        // String type = ( uri.getScheme() == null ) ? uri.getPath() :
064        // uri.getScheme();
065        // DiscoveryAgent rc = (DiscoveryAgent)
066        // discoveryAgentFinder.newInstance(type);
067        // Map options = URISupport.parseParamters(uri);
068        // IntrospectionSupport.setProperties(rc, options);
069        // if( rc.getClass() == SimpleDiscoveryAgent.class ) {
070        // CompositeData data = URISupport.parseComposite(uri);
071        // ((SimpleDiscoveryAgent)rc).setServices(data.getComponents());
072        // }
073        // return rc;
074        // } catch (Throwable e) {
075        // throw IOExceptionSupport.create("Could not create discovery agent: "+uri,
076        // e);
077        // }
078        // }
079    }