Coverage Report - net.sf.snmpadaptor4j.core.trap.SnmpManagers
 
Classes in this File Line Coverage Branch Coverage Complexity
SnmpManagers
100 %
55/55
100 %
20/20
2,714
SnmpManagers$ClosingTask
100 %
3/3
N/A
2,714
 
 1  
 package net.sf.snmpadaptor4j.core.trap;
 2  
 
 3  
 import java.net.InetAddress;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 import java.util.Timer;
 7  
 import java.util.TimerTask;
 8  
 import org.apache.log4j.Logger;
 9  
 import net.sf.snmpadaptor4j.SnmpManagerConfiguration;
 10  
 import net.sf.snmpadaptor4j.api.SnmpApiFactory;
 11  
 import net.sf.snmpadaptor4j.api.SnmpTrapSender;
 12  
 import net.sf.snmpadaptor4j.object.SnmpTrap;
 13  
 
 14  
 /**
 15  
  * Object representing all SNMP managers on the network to which SNMP traps should be sent.
 16  
  * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
 17  
  */
 18  
 public class SnmpManagers {
 19  
 
 20  
         /**
 21  
          * CONSTANT: Connection duration after each sending of SNMP traps (in milliseconds).
 22  
          */
 23  
         private static final long CONNECTION_DURATION = 30000;
 24  
 
 25  
         /**
 26  
          * {@link TimerTask} for close connections to all SNMP managers after a period of inactivity.
 27  
          */
 28  2
         final class ClosingTask
 29  
                         extends TimerTask {
 30  
 
 31  
                 /*
 32  
                  * {@inheritDoc}
 33  
                  * @see java.util.TimerTask#run()
 34  
                  */
 35  
                 @Override
 36  
                 public void run () {
 37  4
                         SnmpManagers.this.close();
 38  4
                 }
 39  
 
 40  
         }
 41  
 
 42  
         /**
 43  
          * Logger.
 44  
          */
 45  14
         protected final Logger logger = Logger.getLogger(SnmpManagers.class);
 46  
 
 47  
         /**
 48  
          * Connection duration after each sending of SNMP traps (in milliseconds).
 49  
          */
 50  
         private final long connectionDuration;
 51  
 
 52  
         /**
 53  
          * List of SNMP trap sender.
 54  
          */
 55  14
         protected final List<SnmpTrapSender> senderList = new ArrayList<SnmpTrapSender>();
 56  
 
 57  
         /**
 58  
          * Factory used for instantiate senders of SNMP traps to SNMP managers.
 59  
          */
 60  
         private final SnmpApiFactory senderFactory;
 61  
 
 62  
         /**
 63  
          * Timer used to close connections to all SNMP managers.
 64  
          */
 65  14
         protected Timer timer = null;
 66  
 
 67  
         /**
 68  
          * Closing time of connections to all SNMP managers (in milliseconds).
 69  
          */
 70  14
         protected long closingTime = Long.MAX_VALUE;
 71  
 
 72  
         /**
 73  
          * Constructor.
 74  
          * @param senderFactory Factory used for instantiate senders of SNMP traps to SNMP managers (must not be <code>NULL</code>).
 75  
          */
 76  
         public SnmpManagers (final SnmpApiFactory senderFactory) {
 77  11
                 this(senderFactory, SnmpManagers.CONNECTION_DURATION);
 78  11
         }
 79  
 
 80  
         /**
 81  
          * Constructor (only for tests).
 82  
          * @param senderFactory Factory used for instantiate senders of SNMP traps to SNMP managers (must not be <code>NULL</code>).
 83  
          * @param connectionDuration Connection duration after each sending of SNMP traps (in milliseconds).
 84  
          */
 85  
         protected SnmpManagers (final SnmpApiFactory senderFactory, final long connectionDuration) {
 86  14
                 super();
 87  14
                 this.senderFactory = senderFactory;
 88  14
                 this.connectionDuration = connectionDuration;
 89  14
         }
 90  
 
 91  
         /**
 92  
          * Initializes the configuration for SNMP traps sending to SNMP managers.
 93  
          * @param managerList Connection parameters to all SNMP managers (must not be <code>NULL</code>).
 94  
          * @throws Exception Exception if an error has occurred.
 95  
          */
 96  
         public final void initialize (final List<SnmpManagerConfiguration> managerList) throws Exception {
 97  4
                 synchronized (this.senderList) {
 98  4
                         this.senderList.clear();
 99  4
                         final InetAddress agentAddress = InetAddress.getLocalHost();
 100  4
                         this.logger.info("Local IP address (SNMP agent) = " + agentAddress);
 101  4
                         for (final SnmpManagerConfiguration manager : managerList) {
 102  8
                                 this.logger.info("SNMP manager at " + manager.getAddress() + ":" + manager.getPort() + " (v" + manager.getVersion() + " - community "
 103  
                                                 + manager.getCommunity() + ")");
 104  8
                                 this.senderList.add(this.senderFactory.newSnmpTrapSender(agentAddress, manager.getAddress(), manager.getPort(), manager.getVersion(),
 105  
                                                 manager.getCommunity()));
 106  
                         }
 107  4
                 }
 108  4
         }
 109  
 
 110  
         /**
 111  
          * Sends a SNMP trap to all managers.
 112  
          * @param trap Object representing a specific or generic SNMP trap (must not be <code>NULL</code>).
 113  
          */
 114  
         public void send (final SnmpTrap trap) {
 115  5
                 synchronized (this.senderList) {
 116  5
                         if (this.logger.isDebugEnabled()) {
 117  3
                                 this.logger.debug("New " + trap + " to send");
 118  
                         }
 119  5
                         if (this.logger.isTraceEnabled()) {
 120  3
                                 trap.traceTo(this.logger);
 121  
                         }
 122  5
                         for (final SnmpTrapSender sender : this.senderList) {
 123  
                                 try {
 124  10
                                         if (!sender.isConnected()) {
 125  4
                                                 sender.open();
 126  
                                         }
 127  10
                                         sender.send(trap);
 128  
                                 }
 129  2
                                 catch (final Throwable e) {
 130  2
                                         this.logger.error("Unable to send a trap to the SNMP manager at " + sender.getName(), e);
 131  18
                                 }
 132  
                         }
 133  5
                         this.closingTime = System.currentTimeMillis() + this.connectionDuration;
 134  5
                         if (this.timer == null) {
 135  2
                                 this.timer = new Timer();
 136  2
                                 this.timer.schedule(new ClosingTask(), this.connectionDuration, this.connectionDuration);
 137  
                         }
 138  5
                 }
 139  5
         }
 140  
 
 141  
         /**
 142  
          * Closes connections to all SNMP managers.
 143  
          */
 144  
         final void close () {
 145  6
                 this.logger.trace("Checking Connections to the SNMP managers...");
 146  6
                 if (this.closingTime < System.currentTimeMillis()) {
 147  4
                         synchronized (this.senderList) {
 148  4
                                 if (this.closingTime < System.currentTimeMillis()) {
 149  3
                                         this.logger.trace("Closing connections to the SNMP managers...");
 150  3
                                         this.timer.cancel();
 151  3
                                         this.timer = null;
 152  3
                                         this.closingTime = Long.MAX_VALUE;
 153  3
                                         for (final SnmpTrapSender sender : this.senderList) {
 154  
                                                 try {
 155  7
                                                         if (sender.isConnected()) {
 156  6
                                                                 sender.close();
 157  
                                                         }
 158  
                                                 }
 159  2
                                                 catch (final Throwable e) {
 160  2
                                                         this.logger.error("Unable to close the connection to the SNMP manager at " + sender.getName(), e);
 161  12
                                                 }
 162  
                                         }
 163  3
                                         this.logger.debug("Connections closed to the SNMP managers");
 164  
                                 }
 165  4
                         }
 166  
                 }
 167  6
         }
 168  
 
 169  
         /*
 170  
          * {@inheritDoc}
 171  
          * @see java.lang.Object#toString()
 172  
          */
 173  
         @Override
 174  
         public final String toString () {
 175  1
                 return "SnmpManagers";
 176  
         }
 177  
 
 178  
 }