blob: aaf843674f74a4f337d5327531973c4434aa47d7 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package sun.management.snmp.jvminstr;
26
27import java.net.InetAddress;
28import java.net.UnknownHostException;
29
30/**
31 * Notification Target data.
32 */
33public class NotificationTargetImpl implements NotificationTarget {
34 private InetAddress address;
35 private int port;
36 private String community;
37
38 /**
39 * Target parameter is a <CODE>java.lang.String</CODE>
40 * target synctax is <host>:<port>:community. Eg: "localhost:163:private".
41 * <p>The <code><em>host</em></code> is a host name, an IPv4 numeric
42 * host address, or an IPv6 numeric address enclosed in square
43 * brackets.</p>
44 * @throws IllegalArgumentException In case the target is malformed
45 */
46 public NotificationTargetImpl(String target)
47 throws IllegalArgumentException, UnknownHostException {
48 parseTarget(target);
49 }
50
51 public NotificationTargetImpl(String address, int port,
52 String community)
53 throws UnknownHostException {
54 this(InetAddress.getByName(address),port,community);
55 }
56
57 public NotificationTargetImpl(InetAddress address, int port,
58 String community) {
59 this.address = address;
60 this.port = port;
61 this.community = community;
62 }
63
64 private void parseTarget(String target)
65 throws IllegalArgumentException, UnknownHostException {
66
67 if(target == null ||
68 target.length() == 0) throw new
69 IllegalArgumentException("Invalid target [" + target + "]");
70
71 String addrStr;
72 if (target.startsWith("[")) {
73 final int index = target.indexOf("]");
74 final int index2 = target.lastIndexOf(":");
75 if(index == -1)
76 throw new IllegalArgumentException("Host starts with [ but " +
77 "does not end with ]");
78 addrStr = target.substring(1, index);
79 port = Integer.parseInt(target.substring(index + 2,
80 index2));
81 if (!isNumericIPv6Address(addrStr)) {
82 throw new IllegalArgumentException("Address inside [...] must " +
83 "be numeric IPv6 address");
84 }
85 if (addrStr.startsWith("["))
86 throw new IllegalArgumentException("More than one [[...]]");
87 } else {
88 final int index = target.indexOf(":");
89 final int index2 = target.lastIndexOf(":");
90 if(index == -1) throw new
91 IllegalArgumentException("Missing port separator \":\"");
92 addrStr = target.substring(0, index);
93
94 port = Integer.parseInt(target.substring(index + 1,
95 index2));
96 }
97
98 address = InetAddress.getByName(addrStr);
99
100 //THE CHECK SHOULD BE STRONGER!!!
101 final int index = target.lastIndexOf(":");
102
103 community = target.substring(index + 1, target.length());
104
105 }
106
107 /* True if this string, assumed to be a valid argument to
108 * InetAddress.getByName, is a numeric IPv6 address.
109 */
110 private static boolean isNumericIPv6Address(String s) {
111 // address contains colon iff it's a numeric IPv6 address
112 return (s.indexOf(':') >= 0);
113 }
114
115 public String getCommunity() {
116 return community;
117 }
118
119 public InetAddress getAddress() {
120 return address;
121 }
122
123 public int getPort() {
124 return port;
125 }
126
127 public String toString() {
128 return "address : " + address + " port : " + port +
129 " community : " + community;
130 }
131}