blob: e6572776f011f87cbc39dd4261260e9dfb5a4f8c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2007 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 */
25
26
27package com.sun.jmx.snmp.IPAcl;
28
29
30
31// java import
32//
33import java.io.Serializable;
34import java.net.InetAddress;
35import java.net.UnknownHostException;
36import java.util.Hashtable;
37import java.util.logging.Level;
38import java.util.Vector;
39import java.security.acl.NotOwnerException;
40
41import static com.sun.jmx.defaults.JmxProperties.SNMP_LOGGER;
42
43/**
44 * The class defines an abstract representation of a host.
45 *
46 */
47abstract class Host extends SimpleNode implements Serializable {
48
49 public Host(int id) {
50 super(id);
51 }
52
53 public Host(Parser p, int id) {
54 super(p, id);
55 }
56
57 protected abstract PrincipalImpl createAssociatedPrincipal()
58 throws UnknownHostException;
59
60 protected abstract String getHname();
61
62 public void buildAclEntries(PrincipalImpl owner, AclImpl acl) {
63 // Create a principal
64 //
65 PrincipalImpl p=null;
66 try {
67 p = createAssociatedPrincipal();
68 } catch(UnknownHostException e) {
69 if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
70 SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
71 "buildAclEntries",
72 "Cannot create ACL entry; got exception", e);
73 }
74 throw new IllegalArgumentException("Cannot create ACL entry for " + e.getMessage());
75 }
76
77 // Create an AclEntry
78 //
79 AclEntryImpl entry= null;
80 try {
81 entry = new AclEntryImpl(p);
82 // Add permission
83 //
84 registerPermission(entry);
85 acl.addEntry(owner, entry);
86 } catch(UnknownHostException e) {
87 if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
88 SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
89 "buildAclEntries",
90 "Cannot create ACL entry; got exception", e);
91 }
92 return;
93 } catch(NotOwnerException a) {
94 if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
95 SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
96 "buildAclEntries",
97 "Cannot create ACL entry; got exception", a);
98 }
99 return;
100 }
101 }
102
103 private void registerPermission(AclEntryImpl entry) {
104 JDMHost host= (JDMHost) jjtGetParent();
105 JDMManagers manager= (JDMManagers) host.jjtGetParent();
106 JDMAclItem acl= (JDMAclItem) manager.jjtGetParent();
107 JDMAccess access= acl.getAccess();
108 access.putPermission(entry);
109 JDMCommunities comm= acl.getCommunities();
110 comm.buildCommunities(entry);
111 }
112
113 public void buildTrapEntries(Hashtable<InetAddress, Vector<String>> dest) {
114
115 JDMHostTrap host= (JDMHostTrap) jjtGetParent();
116 JDMTrapInterestedHost hosts= (JDMTrapInterestedHost) host.jjtGetParent();
117 JDMTrapItem trap = (JDMTrapItem) hosts.jjtGetParent();
118 JDMTrapCommunity community = trap.getCommunity();
119 String comm = community.getCommunity();
120
121 InetAddress add = null;
122 try {
123 add = java.net.InetAddress.getByName(getHname());
124 } catch(UnknownHostException e) {
125 if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
126 SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
127 "buildTrapEntries",
128 "Cannot create TRAP entry; got exception", e);
129 }
130 return;
131 }
132
133 Vector<String> list = null;
134 if (dest.containsKey(add)){
135 list = dest.get(add);
136 if (!list.contains(comm)){
137 list.addElement(comm);
138 }
139 } else {
140 list = new Vector<String>();
141 list.addElement(comm);
142 dest.put(add,list);
143 }
144 }
145
146 public void buildInformEntries(Hashtable<InetAddress, Vector<String>> dest) {
147
148 JDMHostInform host= (JDMHostInform) jjtGetParent();
149 JDMInformInterestedHost hosts= (JDMInformInterestedHost) host.jjtGetParent();
150 JDMInformItem inform = (JDMInformItem) hosts.jjtGetParent();
151 JDMInformCommunity community = inform.getCommunity();
152 String comm = community.getCommunity();
153
154 InetAddress add = null;
155 try {
156 add = java.net.InetAddress.getByName(getHname());
157 } catch(UnknownHostException e) {
158 if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
159 SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
160 "buildTrapEntries",
161 "Cannot create INFORM entry; got exception", e);
162 }
163 return;
164 }
165
166 Vector<String> list = null;
167 if (dest.containsKey(add)){
168 list = dest.get(add);
169 if (!list.contains(comm)){
170 list.addElement(comm);
171 }
172 } else {
173 list = new Vector<String>();
174 list.addElement(comm);
175 dest.put(add,list);
176 }
177 }
178
179
180
181}