Coverage Report - org.apache.camel.component.irc.IrcProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
IrcProducer
0% 
0% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.irc;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.RuntimeCamelException;
 21  
 import org.apache.camel.impl.DefaultProducer;
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.schwering.irc.lib.IRCConnection;
 25  
 import org.schwering.irc.lib.IRCEventListener;
 26  
 
 27  
 public class IrcProducer extends DefaultProducer<IrcExchange> {
 28  
 
 29  0
     public static final String[] COMMANDS = new String[] {"AWAY", "INVITE", "ISON", "JOIN", "KICK", "LIST", "NAMES", "PRIVMSG", "MODE", "NICK", "NOTICE", "PART", "PONG", "QUIT", "TOPIC", "WHO",
 30  
                                                           "WHOIS", "WHOWAS", "USERHOST"};
 31  0
     private static final transient Log LOG = LogFactory.getLog(IrcProducer.class);
 32  
 
 33  
     private IRCConnection connection;
 34  
     private IrcEndpoint endpoint;
 35  
     private IRCEventListener ircErrorLogger;
 36  
 
 37  
     public IrcProducer(IrcEndpoint endpoint, IRCConnection connection) {
 38  0
         super(endpoint);
 39  0
         this.endpoint = endpoint;
 40  0
         this.connection = connection;
 41  0
     }
 42  
 
 43  
     public void process(Exchange exchange) throws Exception {
 44  
         try {
 45  0
             final String msg = exchange.getIn().getBody(String.class);
 46  0
             if (isMessageACommand(msg)) {
 47  0
                 connection.send(msg);
 48  0
             } else {
 49  0
                 final String target = endpoint.getConfiguration().getTarget();
 50  
 
 51  0
                 if (LOG.isDebugEnabled()) {
 52  0
                     LOG.debug("sending to: " + target + " message: " + msg);
 53  
                 }
 54  
 
 55  0
                 connection.doPrivmsg(target, msg);
 56  
             }
 57  0
         } catch (Exception e) {
 58  0
             throw new RuntimeCamelException(e);
 59  0
         }
 60  0
     }
 61  
 
 62  
     @Override
 63  
     protected void doStart() throws Exception {
 64  0
         super.doStart();
 65  
 
 66  0
         ircErrorLogger = createIrcErrorLogger();
 67  0
         connection.addIRCEventListener(ircErrorLogger);
 68  
 
 69  0
         final String target = endpoint.getConfiguration().getTarget();
 70  
 
 71  0
         LOG.debug("joining: " + target);
 72  0
         connection.doJoin(target);
 73  0
     }
 74  
 
 75  
     @Override
 76  
     protected void doStop() throws Exception {
 77  0
         super.doStop();
 78  0
         if (connection != null) {
 79  0
             connection.removeIRCEventListener(ircErrorLogger);
 80  
         }
 81  0
     }
 82  
 
 83  
     protected boolean isMessageACommand(String msg) {
 84  0
         for (String command : COMMANDS) {
 85  0
             if (msg.startsWith(command)) {
 86  0
                 return true;
 87  
             }
 88  
         }
 89  0
         return false;
 90  
     }
 91  
 
 92  
     protected IRCEventListener createIrcErrorLogger() {
 93  0
         return new IrcErrorLogger(LOG);
 94  
     }
 95  
 
 96  
 }