1. /*
  2. * @(#)file Host.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.14
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp.IPAcl;
  12. // java import
  13. //
  14. import java.io.Serializable;
  15. import java.net.InetAddress;
  16. import java.net.UnknownHostException;
  17. import java.util.Hashtable;
  18. import java.util.Vector;
  19. import java.security.acl.NotOwnerException;
  20. // SNMP Runtime import
  21. //
  22. import com.sun.jmx.trace.Trace;
  23. /**
  24. * The class defines an abstract representation of a host.
  25. *
  26. * @version 4.14 12/19/03
  27. * @author Sun Microsystems, Inc.
  28. */
  29. abstract class Host extends SimpleNode implements Serializable {
  30. public Host(int id) {
  31. super(id);
  32. }
  33. public Host(Parser p, int id) {
  34. super(p, id);
  35. }
  36. protected abstract PrincipalImpl createAssociatedPrincipal()
  37. throws UnknownHostException;
  38. protected abstract String getHname();
  39. public void buildAclEntries(PrincipalImpl owner, AclImpl acl) {
  40. // Create a principal
  41. //
  42. PrincipalImpl p=null;
  43. try {
  44. p = createAssociatedPrincipal();
  45. } catch(UnknownHostException e) {
  46. if (isDebugOn()) {
  47. debug("buildAclEntries", "Cannot create ACL entry for " + e.getMessage());
  48. }
  49. throw new IllegalArgumentException("Cannot create ACL entry for " + e.getMessage());
  50. }
  51. // Create an AclEntry
  52. //
  53. AclEntryImpl entry= null;
  54. try {
  55. entry = new AclEntryImpl(p);
  56. // Add permission
  57. //
  58. registerPermission(entry);
  59. acl.addEntry(owner, entry);
  60. } catch(UnknownHostException e) {
  61. if (isDebugOn()) {
  62. debug("buildAclEntries", "Cannot create ACL entry for " + e.getMessage());
  63. }
  64. return;
  65. } catch(NotOwnerException a) {
  66. if (isDebugOn()) {
  67. debug("buildAclEntries", "Not owner of ACL " + a.getMessage());
  68. }
  69. return;
  70. }
  71. }
  72. private void registerPermission(AclEntryImpl entry) {
  73. JDMHost host= (JDMHost) jjtGetParent();
  74. JDMManagers manager= (JDMManagers) host.jjtGetParent();
  75. JDMAclItem acl= (JDMAclItem) manager.jjtGetParent();
  76. JDMAccess access= (JDMAccess) acl.getAccess();
  77. access.putPermission(entry);
  78. JDMCommunities comm= (JDMCommunities) acl.getCommunities();
  79. comm.buildCommunities(entry);
  80. }
  81. public void buildTrapEntries(Hashtable dest) {
  82. JDMHostTrap host= (JDMHostTrap) jjtGetParent();
  83. JDMTrapInterestedHost hosts= (JDMTrapInterestedHost) host.jjtGetParent();
  84. JDMTrapItem trap = (JDMTrapItem) hosts.jjtGetParent();
  85. JDMTrapCommunity community = (JDMTrapCommunity) trap.getCommunity();
  86. String comm = community.getCommunity();
  87. InetAddress add = null;
  88. try {
  89. add = java.net.InetAddress.getByName(getHname());
  90. } catch(UnknownHostException e) {
  91. if (isDebugOn()) {
  92. debug("buildTrapEntries", "Cannot create TRAP entry for " + e.getMessage());
  93. }
  94. return;
  95. }
  96. Vector list = null;
  97. if (dest.containsKey(add)){
  98. list = (Vector) dest.get(add);
  99. if (!list.contains(comm)){
  100. list.addElement(comm);
  101. }
  102. } else {
  103. list = new Vector();
  104. list.addElement(comm);
  105. dest.put(add,list);
  106. }
  107. }
  108. public void buildInformEntries(Hashtable dest) {
  109. JDMHostInform host= (JDMHostInform) jjtGetParent();
  110. JDMInformInterestedHost hosts= (JDMInformInterestedHost) host.jjtGetParent();
  111. JDMInformItem inform = (JDMInformItem) hosts.jjtGetParent();
  112. JDMInformCommunity community = (JDMInformCommunity) inform.getCommunity();
  113. String comm = community.getCommunity();
  114. InetAddress add = null;
  115. try {
  116. add = java.net.InetAddress.getByName(getHname());
  117. } catch(UnknownHostException e) {
  118. if (isDebugOn()) {
  119. debug("buildInformEntries", "Cannot create INFORM entry for " + e.getMessage());
  120. }
  121. return;
  122. }
  123. Vector list = null;
  124. if (dest.containsKey(add)){
  125. list = (Vector) dest.get(add);
  126. if (!list.contains(comm)){
  127. list.addElement(comm);
  128. }
  129. } else {
  130. list = new Vector();
  131. list.addElement(comm);
  132. dest.put(add,list);
  133. }
  134. }
  135. // TRACES & DEBUG
  136. //---------------
  137. boolean isTraceOn() {
  138. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP);
  139. }
  140. void trace(String clz, String func, String info) {
  141. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info);
  142. }
  143. void trace(String func, String info) {
  144. trace(dbgTag, func, info);
  145. }
  146. boolean isDebugOn() {
  147. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP);
  148. }
  149. void debug(String clz, String func, String info) {
  150. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info);
  151. }
  152. void debug(String func, String info) {
  153. debug(dbgTag, func, info);
  154. }
  155. String dbgTag = "Host";
  156. }