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