1 20 package org.jboss.jmx.adaptor.snmp.trapd; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 25 import org.jboss.system.ServiceMBeanSupport; 26 import org.jboss.system.server.ServerConfig; 27 import org.opennms.protocols.snmp.SnmpTrapSession; 28 29 43 public class TrapdService 44 extends ServiceMBeanSupport 45 implements TrapdServiceMBean 46 { 47 48 private int port; 49 50 51 private InetAddress bindAddress; 52 53 54 protected SnmpTrapSession trapSession; 55 56 59 public TrapdService() 60 { 61 } 63 64 71 public void setPort(int port) 72 { 73 this.port = port; 74 } 75 76 83 public int getPort() 84 { 85 return this.port; 86 } 87 88 95 public void setBindAddress(String host) 96 throws UnknownHostException 97 { 98 this.bindAddress = toInetAddress(host); 99 } 100 101 108 public String getBindAddress() 109 { 110 String address = null; 111 112 if (this.bindAddress != null) 113 address = this.bindAddress.getHostAddress(); 114 115 return address; 116 } 117 118 121 protected void startService() 122 throws Exception 123 { 124 try { 127 InetAddress address = this.bindAddress != null ? this.bindAddress : 129 toInetAddress(System.getProperty(ServerConfig.SERVER_BIND_ADDRESS)); 130 131 this.trapSession = 132 new SnmpTrapSession(new TrapReceiver(this.log), this.port, address); 133 } 134 catch (Exception e) { 135 log.error("Cannot instantiate trap session"); 136 137 throw e; } 139 } 140 141 144 protected void stopService() 145 throws Exception 146 { 147 this.trapSession.close(); 148 this.trapSession = null; } 150 151 154 private InetAddress toInetAddress(String host) 155 throws UnknownHostException 156 { 157 if (host == null || host.length() == 0) 158 return null; 159 else 160 return InetAddress.getByName(host); 161 } 162 163 } | Popular Tags |