Coverage Report - org.apache.camel.component.irc.IrcComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
IrcComponent
0% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.irc;
 19  
 
 20  
 import org.apache.camel.CamelContext;
 21  
 import org.apache.camel.RuntimeCamelException;
 22  
 import org.apache.camel.impl.DefaultComponent;
 23  
 import org.apache.camel.util.IntrospectionSupport;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.schwering.irc.lib.IRCConnection;
 27  
 
 28  
 import java.net.URI;
 29  
 import java.util.HashMap;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * Defines the <a href="http://activemq.apache.org/camel/irc.html">IRC Component</a>
 34  
  *
 35  
  * @version $Revision:$
 36  
  */
 37  0
 public class IrcComponent extends DefaultComponent<IrcExchange> {
 38  0
     private static final transient Log log = LogFactory.getLog(IrcComponent.class);
 39  
     private IrcConfiguration configuration;
 40  0
     private final Map<String, IRCConnection> connectionCache = new HashMap<String, IRCConnection>();
 41  
 
 42  
     public static IrcComponent ircComponent() {
 43  0
         return new IrcComponent();
 44  
     }
 45  
 
 46  0
     public IrcComponent() {
 47  0
         configuration = new IrcConfiguration();
 48  0
     }
 49  
 
 50  0
     public IrcComponent(IrcConfiguration configuration) {
 51  0
         this.configuration = configuration;
 52  0
     }
 53  
 
 54  
     public IrcComponent(CamelContext context) {
 55  0
         super(context);
 56  0
         configuration = new IrcConfiguration();
 57  0
     }
 58  
 
 59  
     protected IrcEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
 60  0
         IrcConfiguration config = getConfiguration().copy();
 61  0
         config.configure(new URI(uri));
 62  
 
 63  
         // lets make sure we copy the configuration as each endpoint can customize its own version
 64  0
         final IrcEndpoint endpoint = new IrcEndpoint(uri, this, config);
 65  
 
 66  0
         IntrospectionSupport.setProperties(endpoint.getConfiguration(), parameters);
 67  0
         return endpoint;
 68  
     }
 69  
 
 70  
     public IrcConfiguration getConfiguration() {
 71  0
         return configuration;
 72  
     }
 73  
 
 74  
     public void setConfiguration(IrcConfiguration configuration) {
 75  0
         this.configuration = configuration;
 76  0
     }
 77  
 
 78  
     public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) {
 79  
         final IRCConnection connection;
 80  0
         if (connectionCache.containsKey(configuration.getCacheKey())) {
 81  0
             if (log.isDebugEnabled()) {
 82  0
                 log.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget());
 83  
             }
 84  0
             connection = connectionCache.get(configuration.getCacheKey());
 85  0
         }
 86  
         else {
 87  0
             connection = createConnection(configuration);
 88  0
             connectionCache.put(configuration.getCacheKey(), connection);
 89  
         }
 90  0
         return connection;
 91  
     }
 92  
 
 93  
     protected IRCConnection createConnection(IrcConfiguration configuration) {
 94  0
         log.debug("Creating Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget()
 95  
                 + " nick: " + configuration.getNickname() + " user: " + configuration.getUsername());
 96  
 
 97  0
         final IRCConnection conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(), configuration.getNickname(), configuration.getUsername(), configuration.getRealname());
 98  0
         conn.setEncoding("UTF-8");
 99  
 //        conn.setDaemon(true);
 100  0
         conn.setColors(configuration.isColors());
 101  0
         conn.setPong(true);
 102  
 
 103  
         try {
 104  0
             conn.connect();
 105  
         }
 106  0
         catch (Exception e) {
 107  0
             log.error("Failed to connect: " + e, e);
 108  
 
 109  
             // TODO use checked exceptions?
 110  0
             throw new RuntimeCamelException(e);
 111  0
         }
 112  0
         return conn;
 113  
     }
 114  
 
 115  
     public void closeConnection(String key, IRCConnection connection) {
 116  
         try {
 117  0
             connection.doQuit();
 118  0
             connection.close();
 119  
         }
 120  0
         catch (Exception e) {
 121  0
             e.printStackTrace();
 122  0
         }
 123  0
     }
 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  0
         Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache);
 129  0
         connectionCache.clear();
 130  0
         for (Map.Entry<String, IRCConnection> entry : map.entrySet()) {
 131  0
             closeConnection(entry.getKey(), entry.getValue());
 132  0
         }
 133  0
         super.doStop();
 134  0
     }
 135  
 }