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: 835186 $
035     */
036    public class IrcComponent extends DefaultComponent {
037        private static final transient Log LOG = LogFactory.getLog(IrcComponent.class);
038        private IrcConfiguration configuration;
039        private final Map<String, IRCConnection> connectionCache = new HashMap<String, IRCConnection>();
040        private IRCEventListener ircLogger;
041    
042        public IrcComponent() {
043            configuration = new IrcConfiguration();
044        }
045    
046        public IrcComponent(IrcConfiguration configuration) {
047            this.configuration = configuration;
048        }
049    
050        public IrcComponent(CamelContext context) {
051            super(context);
052            configuration = new IrcConfiguration();
053        }
054    
055        protected IrcEndpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
056            // lets make sure we copy the configuration as each endpoint can customize its own version
057            IrcConfiguration config = getConfiguration().copy();
058            config.configure(uri);
059    
060            IrcEndpoint endpoint = new IrcEndpoint(uri, this, config);
061            setProperties(endpoint.getConfiguration(), parameters);
062            return endpoint;
063        }
064    
065        public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) {
066            final IRCConnection connection;
067            if (connectionCache.containsKey(configuration.getCacheKey())) {
068                if (LOG.isDebugEnabled()) {
069                    LOG.debug("Returning Cached Connection to " + configuration.getHostname() + ":" + configuration.getNickname());
070                }
071                connection = connectionCache.get(configuration.getCacheKey());
072            } else {
073                connection = createConnection(configuration);
074                connectionCache.put(configuration.getCacheKey(), connection);
075            }
076            return connection;
077        }
078    
079        protected IRCConnection createConnection(IrcConfiguration configuration) {
080            IRCConnection conn = null;
081    
082            if (configuration.getUsingSSL()) {
083                if (LOG.isDebugEnabled()) {
084                    LOG.debug("Creating SSL Connection to " + configuration.getHostname() + " destination(s): " + configuration.getListOfChannels()
085                            + " nick: " + configuration.getNickname() + " user: " + configuration.getUsername());
086                }
087                SSLIRCConnection sconn = new SSLIRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(),
088                                                             configuration.getNickname(), configuration.getUsername(), configuration.getRealname());
089    
090                sconn.addTrustManager(configuration.getTrustManager());
091                conn = sconn;
092    
093            } else {
094                if (LOG.isDebugEnabled()) {
095                    LOG.debug("Creating Connection to " + configuration.getHostname() + " destination(s): " + configuration.getListOfChannels()
096                            + " nick: " + configuration.getNickname() + " user: " + configuration.getUsername());
097                }
098    
099                conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(),
100                                                             configuration.getNickname(), configuration.getUsername(), configuration.getRealname());
101            }
102            conn.setEncoding("UTF-8");
103            conn.setColors(configuration.isColors());
104            conn.setPong(true);
105    
106            if (LOG.isDebugEnabled()) {
107                LOG.debug("Adding IRC event logging listener");
108                ircLogger = createIrcLogger(configuration.getHostname());
109                conn.addIRCEventListener(ircLogger);
110            }
111    
112            try {
113                conn.connect();
114            } catch (Exception e) {
115                throw new RuntimeCamelException(e);
116            }
117            return conn;
118        }
119    
120        public void closeConnection(String key, IRCConnection connection) {
121            try {
122                connection.doQuit();
123                connection.close();
124            } catch (Exception e) {
125                LOG.warn("Error during closing connection.", e);
126            }
127        }
128    
129        @Override
130        protected synchronized void doStop() throws Exception {
131            // lets use a copy so we can clear the connections eagerly in case of exceptions
132            Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache);
133            connectionCache.clear();
134            for (Map.Entry<String, IRCConnection> entry : map.entrySet()) {
135                closeConnection(entry.getKey(), entry.getValue());
136            }
137            super.doStop();
138        }
139    
140        protected IRCEventListener createIrcLogger(String hostname) {
141            return new IrcLogger(LOG, hostname);
142        }
143    
144        public IrcConfiguration getConfiguration() {
145            return configuration;
146        }
147    
148        public void setConfiguration(IrcConfiguration configuration) {
149            this.configuration = configuration;
150        }
151    
152    }