1. /*
  2. * @(#)file PrincipalImpl.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.15
  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. import java.net.InetAddress;
  13. import java.net.UnknownHostException;
  14. import java.io.Serializable;
  15. /**
  16. * Principal represents a host.
  17. *
  18. * @version 4.15 12/19/03
  19. * @author Sun Microsystems, Inc
  20. */
  21. class PrincipalImpl implements java.security.Principal, Serializable {
  22. private InetAddress[] add = null;
  23. /**
  24. * Constructs a principal with the local host.
  25. */
  26. public PrincipalImpl () throws UnknownHostException {
  27. add = new InetAddress[1];
  28. add[0] = java.net.InetAddress.getLocalHost();
  29. }
  30. /**
  31. * Construct a principal using the specified host.
  32. * <P>
  33. * The host can be either:
  34. * <UL>
  35. * <LI> a host name
  36. * <LI> an IP address
  37. * </UL>
  38. *
  39. * @param hostName the host used to make the principal.
  40. */
  41. public PrincipalImpl(String hostName) throws UnknownHostException {
  42. if ((hostName.equals("localhost")) || (hostName.equals("127.0.0.1"))) {
  43. add = new InetAddress[1];
  44. add[0] = java.net.InetAddress.getByName(hostName);
  45. }
  46. else
  47. add = java.net.InetAddress.getAllByName( hostName );
  48. }
  49. /**
  50. * Constructs a principal using an Internet Protocol (IP) address.
  51. *
  52. * @param address the Internet Protocol (IP) address.
  53. */
  54. public PrincipalImpl(InetAddress address) {
  55. add = new InetAddress[1];
  56. add[0] = address;
  57. }
  58. /**
  59. * Returns the name of this principal.
  60. *
  61. * @return the name of this principal.
  62. */
  63. public String getName() {
  64. return add[0].toString();
  65. }
  66. /**
  67. * Compares this principal to the specified object. Returns true if the
  68. * object passed in matches the principal
  69. * represented by the implementation of this interface.
  70. *
  71. * @param a the principal to compare with.
  72. * @return true if the principal passed in is the same as that encapsulated by this principal, false otherwise.
  73. */
  74. public boolean equals(Object a) {
  75. if (a instanceof PrincipalImpl){
  76. for(int i = 0; i < add.length; i++) {
  77. if(add[i].equals ((InetAddress)((PrincipalImpl) a).getAddress()))
  78. return true;
  79. }
  80. return false;
  81. } else {
  82. return false;
  83. }
  84. }
  85. /**
  86. * Returns a hashcode for this principal.
  87. *
  88. * @return a hashcode for this principal.
  89. */
  90. public int hashCode(){
  91. return add[0].hashCode();
  92. }
  93. /**
  94. * Returns a string representation of this principal. In case of multiple address, the first one is returned.
  95. *
  96. * @return a string representation of this principal.
  97. */
  98. public String toString() {
  99. return ("PrincipalImpl :"+add[0].toString());
  100. }
  101. /**
  102. * Returns the Internet Protocol (IP) address for this principal. In case of multiple address, the first one is returned.
  103. *
  104. * @return the Internet Protocol (IP) address for this principal.
  105. */
  106. public InetAddress getAddress(){
  107. return add[0];
  108. }
  109. /**
  110. * Returns the Internet Protocol (IP) address for this principal. In case of multiple address, the first one is returned.
  111. *
  112. * @return the array of Internet Protocol (IP) addresses for this principal.
  113. */
  114. public InetAddress[] getAddresses(){
  115. return add;
  116. }
  117. }