001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.irc;
019    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.RuntimeCamelException;
022    import org.apache.camel.impl.DefaultComponent;
023    import org.apache.camel.util.IntrospectionSupport;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.schwering.irc.lib.IRCConnection;
027    
028    import java.net.URI;
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    /**
033     * Defines the <a href="http://activemq.apache.org/camel/irc.html">IRC Component</a>
034     *
035     * @version $Revision:$
036     */
037    public class IrcComponent extends DefaultComponent<IrcExchange> {
038        private static final transient Log log = LogFactory.getLog(IrcComponent.class);
039        private IrcConfiguration configuration;
040        private final Map<String, IRCConnection> connectionCache = new HashMap<String, IRCConnection>();
041    
042        public static IrcComponent ircComponent() {
043            return new IrcComponent();
044        }
045    
046        public IrcComponent() {
047            configuration = new IrcConfiguration();
048        }
049    
050        public IrcComponent(IrcConfiguration configuration) {
051            this.configuration = configuration;
052        }
053    
054        public IrcComponent(CamelContext context) {
055            super(context);
056            configuration = new IrcConfiguration();
057        }
058    
059        protected IrcEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
060            IrcConfiguration config = getConfiguration().copy();
061            config.configure(new URI(uri));
062    
063            // lets make sure we copy the configuration as each endpoint can customize its own version
064            final IrcEndpoint endpoint = new IrcEndpoint(uri, this, config);
065    
066            IntrospectionSupport.setProperties(endpoint.getConfiguration(), parameters);
067            return endpoint;
068        }
069    
070        public IrcConfiguration getConfiguration() {
071            return configuration;
072        }
073    
074        public void setConfiguration(IrcConfiguration configuration) {
075            this.configuration = configuration;
076        }
077    
078        public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) {
079            final IRCConnection connection;
080            if (connectionCache.containsKey(configuration.getCacheKey())) {
081                if (log.isDebugEnabled()) {
082                    log.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget());
083                }
084                connection = connectionCache.get(configuration.getCacheKey());
085            }
086            else {
087                connection = createConnection(configuration);
088                connectionCache.put(configuration.getCacheKey(), connection);
089            }
090            return connection;
091        }
092    
093        protected IRCConnection createConnection(IrcConfiguration configuration) {
094            log.debug("Creating Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget()
095                    + " nick: " + configuration.getNickname() + " user: " + configuration.getUsername());
096    
097            final IRCConnection conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(), configuration.getNickname(), configuration.getUsername(), configuration.getRealname());
098            conn.setEncoding("UTF-8");
099    //        conn.setDaemon(true);
100            conn.setColors(configuration.isColors());
101            conn.setPong(true);
102    
103            try {
104                conn.connect();
105            }
106            catch (Exception e) {
107                log.error("Failed to connect: " + e, e);
108    
109                // TODO use checked exceptions?
110                throw new RuntimeCamelException(e);
111            }
112            return conn;
113        }
114    
115        public void closeConnection(String key, IRCConnection connection) {
116            try {
117                connection.doQuit();
118                connection.close();
119            }
120            catch (Exception e) {
121                e.printStackTrace();
122            }
123        }
124    
125        @Override
126        protected synchronized void doStop() throws Exception {
127            // lets use a copy so we can clear the connections eagerly in case of exceptions
128            Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache);
129            connectionCache.clear();
130            for (Map.Entry<String, IRCConnection> entry : map.entrySet()) {
131                closeConnection(entry.getKey(), entry.getValue());
132            }
133            super.doStop();
134        }
135    }