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://activemq.apache.org/camel/irc.html">IRC Component</a>
032     *
033     * @version $Revision: 695516 $
034     */
035    public class IrcComponent extends DefaultComponent<IrcExchange> {
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        public static IrcComponent ircComponent() {
054            return new IrcComponent();
055        }
056    
057        protected IrcEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
058            IrcConfiguration config = getConfiguration().copy();
059            config.configure(new URI(uri));
060    
061            // lets make sure we copy the configuration as each endpoint can
062            // customize its own version
063            final IrcEndpoint endpoint = new IrcEndpoint(uri, this, config);
064    
065            setProperties(endpoint.getConfiguration(), parameters);
066            return endpoint;
067        }
068    
069        public IrcConfiguration getConfiguration() {
070            return configuration;
071        }
072    
073        public void setConfiguration(IrcConfiguration configuration) {
074            this.configuration = configuration;
075        }
076    
077        public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) {
078            final IRCConnection connection;
079            if (connectionCache.containsKey(configuration.getCacheKey())) {
080                if (LOG.isDebugEnabled()) {
081                    LOG.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget());
082                }
083                connection = connectionCache.get(configuration.getCacheKey());
084            } else {
085                connection = createConnection(configuration);
086                connectionCache.put(configuration.getCacheKey(), connection);
087            }
088            return connection;
089        }
090    
091        protected IRCConnection createConnection(IrcConfiguration configuration) {
092            LOG.debug("Creating Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget() + " nick: " + configuration.getNickname() + " user: "
093                      + configuration.getUsername());
094    
095            final IRCConnection conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(), configuration.getNickname(), configuration.getUsername(),
096                                                         configuration.getRealname());
097            conn.setEncoding("UTF-8");
098            // conn.setDaemon(true);
099            conn.setColors(configuration.isColors());
100            conn.setPong(true);
101    
102            try {
103                conn.connect();
104            } catch (Exception e) {
105                LOG.error("Failed to connect: " + e, e);
106                throw new RuntimeCamelException(e);
107            }
108            return conn;
109        }
110    
111        public void closeConnection(String key, IRCConnection connection) {
112            try {
113                connection.doQuit();
114                connection.close();
115            } catch (Exception e) {
116                LOG.warn("Error closing connection.", e);
117            }
118        }
119    
120        @Override
121        protected synchronized void doStop() throws Exception {
122            // lets use a copy so we can clear the connections eagerly in case of
123            // exceptions
124            Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache);
125            connectionCache.clear();
126            for (Map.Entry<String, IRCConnection> entry : map.entrySet()) {
127                closeConnection(entry.getKey(), entry.getValue());
128            }
129            super.doStop();
130        }
131    }