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.camel.util.ServiceHelper;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 /**
030 * @version
031 */
032 public class XmppComponent extends DefaultComponent {
033 private static final transient Logger LOG = LoggerFactory.getLogger(XmppComponent.class);
034
035 //keep a cache of endpoints so they can be properly cleaned up
036 private final Map<String, XmppEndpoint> endpointCache = new HashMap<String, XmppEndpoint>();
037
038 @Override
039 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
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 void doStop() throws Exception {
073 ServiceHelper.stopServices(endpointCache.values());
074 endpointCache.clear();
075 }
076
077 }