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