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 if (LOG.isDebugEnabled()) {
042 LOG.debug("Using cached endpoint for URI " + uri);
043 }
044 return endpointCache.get(uri);
045 }
046
047 if (LOG.isDebugEnabled()) {
048 LOG.debug("Creating new endpoint for URI " + uri);
049 }
050 XmppEndpoint endpoint = new XmppEndpoint(uri, this);
051
052 URI u = new URI(uri);
053 endpoint.setHost(u.getHost());
054 endpoint.setPort(u.getPort());
055 if (u.getUserInfo() != null) {
056 endpoint.setUser(u.getUserInfo());
057 }
058 String remainingPath = u.getPath();
059 if (remainingPath != null) {
060 if (remainingPath.startsWith("/")) {
061 remainingPath = remainingPath.substring(1);
062 }
063
064 // assume its a participant
065 if (remainingPath.length() > 0) {
066 endpoint.setParticipant(remainingPath);
067 }
068 }
069
070 endpointCache.put(uri, endpoint);
071
072 return endpoint;
073 }
074
075 @Override
076 protected synchronized void doStop() throws Exception {
077 for (Map.Entry<String, XmppEndpoint> entry : endpointCache.entrySet()) {
078 entry.getValue().destroy();
079 }
080 endpointCache.clear();
081 }
082 }