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.irc;
018
019 import java.net.URI;
020 import java.util.HashMap;
021 import java.util.Map;
022
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.RuntimeCamelException;
025 import org.apache.camel.impl.DefaultComponent;
026 import org.apache.commons.logging.Log;
027 import org.apache.commons.logging.LogFactory;
028 import org.schwering.irc.lib.IRCConnection;
029
030 /**
031 * Defines the <a href="http://camel.apache.org/irc.html">IRC Component</a>
032 *
033 * @version $Revision: 748183 $
034 */
035 public class IrcComponent extends DefaultComponent {
036 private static final transient Log LOG = LogFactory.getLog(IrcComponent.class);
037 private IrcConfiguration configuration;
038 private final Map<String, IRCConnection> connectionCache = new HashMap<String, IRCConnection>();
039
040 public IrcComponent() {
041 configuration = new IrcConfiguration();
042 }
043
044 public IrcComponent(IrcConfiguration configuration) {
045 this.configuration = configuration;
046 }
047
048 public IrcComponent(CamelContext context) {
049 super(context);
050 configuration = new IrcConfiguration();
051 }
052
053 protected IrcEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
054 // lets make sure we copy the configuration as each endpoint can customize its own version
055 IrcConfiguration config = getConfiguration().copy();
056 config.configure(new URI(uri));
057
058 IrcEndpoint endpoint = new IrcEndpoint(uri, this, config);
059 setProperties(endpoint.getConfiguration(), parameters);
060 return endpoint;
061 }
062
063 public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) {
064 final IRCConnection connection;
065 if (connectionCache.containsKey(configuration.getCacheKey())) {
066 if (LOG.isDebugEnabled()) {
067 LOG.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget());
068 }
069 connection = connectionCache.get(configuration.getCacheKey());
070 } else {
071 connection = createConnection(configuration);
072 connectionCache.put(configuration.getCacheKey(), connection);
073 }
074 return connection;
075 }
076
077 protected IRCConnection createConnection(IrcConfiguration configuration) {
078 if (LOG.isDebugEnabled()) {
079 LOG.debug("Creating Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget()
080 + " nick: " + configuration.getNickname() + " user: " + configuration.getUsername());
081 }
082
083 final IRCConnection conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(),
084 configuration.getNickname(), configuration.getUsername(), configuration.getRealname());
085 conn.setEncoding("UTF-8");
086 conn.setColors(configuration.isColors());
087 conn.setPong(true);
088
089 try {
090 conn.connect();
091 } catch (Exception e) {
092 throw new RuntimeCamelException(e);
093 }
094 return conn;
095 }
096
097 public void closeConnection(String key, IRCConnection connection) {
098 try {
099 connection.doQuit();
100 connection.close();
101 } catch (Exception e) {
102 LOG.warn("Error during closing connection.", e);
103 }
104 }
105
106 @Override
107 protected synchronized void doStop() throws Exception {
108 // lets use a copy so we can clear the connections eagerly in case of exceptions
109 Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache);
110 connectionCache.clear();
111 for (Map.Entry<String, IRCConnection> entry : map.entrySet()) {
112 closeConnection(entry.getKey(), entry.getValue());
113 }
114 super.doStop();
115 }
116
117 public IrcConfiguration getConfiguration() {
118 return configuration;
119 }
120
121 public void setConfiguration(IrcConfiguration configuration) {
122 this.configuration = configuration;
123 }
124
125 }