001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.activemq.transport;
019    
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    import java.util.Map;
023    
024    import javax.jms.JMSException;
025    
026    import org.activemq.io.WireFormat;
027    import org.activemq.transport.composite.CompositeTransportChannelFactory;
028    import org.activemq.util.BeanUtils;
029    import org.activemq.util.JMSExceptionHelper;
030    import org.activemq.util.URIHelper;
031    
032    /**
033     * @version $Revision: 1.1.1.1 $
034     */
035    public abstract class DiscoveryTransportChannelFactorySupport extends CompositeTransportChannelFactory {
036        private DiscoveryAgent discoveryAgent;
037    
038        public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
039            if (discoveryAgent == null) {
040                try {
041                    discoveryAgent = createDiscoveryAgent(remoteLocation);
042                }
043                catch (URISyntaxException e) {
044                    throw JMSExceptionHelper.newJMSException("Could not parse URI: " + remoteLocation + ". Reason: " + e, e);
045                }
046    
047                // populate any properties on the discovery agent
048                discoveryAgent = populateAgentProperties(discoveryAgent, remoteLocation);
049            }
050            return new DiscoveryTransportChannel(wireFormat, discoveryAgent);
051        }
052    
053        public DiscoveryAgent getDiscoveryAgent() {
054            return discoveryAgent;
055        }
056    
057        public void setDiscoveryAgent(DiscoveryAgent discoveryAgent) {
058            this.discoveryAgent = discoveryAgent;
059        }
060    
061        protected abstract DiscoveryAgent createDiscoveryAgent(URI remoteLocation) throws JMSException, URISyntaxException;
062    
063        protected DiscoveryAgent populateAgentProperties(DiscoveryAgent agent, URI uri) throws JMSException {
064            Map properties = URIHelper.parseQuery(uri);
065            if (!properties.isEmpty()) {
066                BeanUtils.populate(agent, properties);
067            }
068            return agent;
069        }
070    
071    
072    }