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