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.camel.component.xmpp;
018    
019    import java.net.URI;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.impl.DefaultComponent;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    /**
029     * @version $Revision:520964 $
030     */
031    public class XmppComponent extends DefaultComponent {
032        private static final transient Log LOG = LogFactory.getLog(XmppComponent.class);
033    
034        //keep a cache of endpoints so they can be properly cleaned up
035        Map<String, XmppEndpoint> endpointCache = new HashMap<String, XmppEndpoint>();
036    
037        @Override
038        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
039    
040            if (endpointCache.containsKey(uri)) {
041                LOG.debug("Using cached endpoint for URI " + uri);
042                return endpointCache.get(uri);
043            }
044    
045            LOG.debug("Creating new endpoint for URI " + uri);
046            XmppEndpoint endpoint = new XmppEndpoint(uri, this);
047    
048            URI u = new URI(uri);
049            endpoint.setHost(u.getHost());
050            endpoint.setPort(u.getPort());
051            if (u.getUserInfo() != null) {
052                endpoint.setUser(u.getUserInfo());
053            }
054            String remainingPath = u.getPath();
055            if (remainingPath != null) {
056                if (remainingPath.startsWith("/")) {
057                    remainingPath = remainingPath.substring(1);
058                }
059    
060                // assume its a participant
061                if (remainingPath.length() > 0) {
062                    endpoint.setParticipant(remainingPath);
063                }
064            }
065    
066            endpointCache.put(uri, endpoint);
067            
068            return endpoint;
069        }
070    
071        @Override
072        protected synchronized void doStop() throws Exception {
073            for (Map.Entry<String, XmppEndpoint> entry : endpointCache.entrySet()) {
074                entry.getValue().destroy();
075            }
076        }
077    }