1 /*
2 * Copyright (c) 2003, Intracom S.A. - www.intracom.com
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * This package and its source code is available at www.jboss.org
19 **/
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 /**
30 * MBean wrapper class that acts as an SNMP trap receiver/logger.
31 * It logs traps as INFO messages - change log4j configuration to
32 * redirect logging output. To reconfigure the listening port
33 * the MBean needs to be stopped and re-started.
34 *
35 * @jmx:mbean
36 * extends="org.jboss.system.ServiceMBean"
37 *
38 * @version $Revision: 23955 $
39 *
40 * @author <a href="mailto:spol@intracom.gr">Spyros Pollatos</a>
41 * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
42 **/
43 public class TrapdService
44 extends ServiceMBeanSupport
45 implements TrapdServiceMBean
46 {
47 /** The listening port */
48 private int port;
49
50 /** The interface to bind, useful for multi-homed hosts */
51 private InetAddress bindAddress;
52
53 /** The snmp session used to receive the traps*/
54 protected SnmpTrapSession trapSession;
55
56 /**
57 * Empty CTOR
58 **/
59 public TrapdService()
60 {
61 // empty
62 }
63
64 /**
65 * Sets the port that will be used to receive traps
66 *
67 * @param port the port to listen for traps
68 *
69 * @jmx:managed-attribute
70 **/
71 public void setPort(int port)
72 {
73 this.port = port;
74 }
75
76 /**
77 * Gets the port that will be used to receive traps
78 *
79 * @return the port to listen for traps
80 *
81 * @jmx:managed-attribute
82 **/
83 public int getPort()
84 {
85 return this.port;
86 }
87
88 /**
89 * Sets the interface that will be bound
90 *
91 * @param host the interface to bind
92 *
93 * @jmx:managed-attribute
94 **/
95 public void setBindAddress(String host)
96 throws UnknownHostException
97 {
98 this.bindAddress = toInetAddress(host);
99 }
100
101 /**
102 * Gets the interface that will be bound
103 *
104 * @return the interface to bind
105 *
106 * @jmx:managed-attribute
107 **/
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 /**
119 * Performs service start-up by instantiating an SnmpTrapSession
120 **/
121 protected void startService()
122 throws Exception
123 {
124 // Create the SNMP trap receiving session with the logging handler,
125 // using Logger inherited from ServiceMBeanSupport
126 try {
127 // cater for possible global -b option, if no override has been specified
128 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; // ServiceMBeanSupport will log this
138 }
139 }
140
141 /**
142 * Performs service shutdown by stopping SnmpTrapSession
143 **/
144 protected void stopService()
145 throws Exception
146 {
147 this.trapSession.close();
148 this.trapSession = null; // gc
149 }
150
151 /**
152 * Safely convert a host string to InetAddress or null
153 */
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 } // class TrapdService