1 /* 2 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 27 package com.sun.jmx.snmp.IPAcl; 28 29 30 31 import java.net.InetAddress; 32 import java.net.UnknownHostException; 33 import java.io.Serializable; 34 35 36 /** 37 * Principal represents a host. 38 * 39 */ 40 41 class PrincipalImpl implements java.security.Principal, Serializable { 42 private static final long serialVersionUID = -7910027842878976761L; 43 44 private InetAddress[] add = null; 45 46 /** 47 * Constructs a principal with the local host. 48 */ 49 public PrincipalImpl () throws UnknownHostException { 50 add = new InetAddress[1]; 51 add[0] = java.net.InetAddress.getLocalHost(); 52 } 53 54 /** 55 * Construct a principal using the specified host. 56 * <P> 57 * The host can be either: 58 * <UL> 59 * <LI> a host name 60 * <LI> an IP address 61 * </UL> 62 * 63 * @param hostName the host used to make the principal. 64 */ 65 public PrincipalImpl(String hostName) throws UnknownHostException { 66 if ((hostName.equals("localhost")) || (hostName.equals("127.0.0.1"))) { 67 add = new InetAddress[1]; 68 add[0] = java.net.InetAddress.getByName(hostName); 69 } 70 else 71 add = java.net.InetAddress.getAllByName( hostName ); 72 } 73 74 /** 75 * Constructs a principal using an Internet Protocol (IP) address. 76 * 77 * @param address the Internet Protocol (IP) address. 78 */ 79 public PrincipalImpl(InetAddress address) { 80 add = new InetAddress[1]; 81 add[0] = address; 82 } 83 84 /** 85 * Returns the name of this principal. 86 * 87 * @return the name of this principal. 88 */ 89 public String getName() { 90 return add[0].toString(); 91 } 92 93 /** 94 * Compares this principal to the specified object. Returns true if the 95 * object passed in matches the principal 96 * represented by the implementation of this interface. 97 * 98 * @param a the principal to compare with. 99 * @return true if the principal passed in is the same as that encapsulated by this principal, false otherwise. 100 */ 101 public boolean equals(Object a) { 102 if (a instanceof PrincipalImpl){ 103 for(int i = 0; i < add.length; i++) { 104 if(add[i].equals (((PrincipalImpl) a).getAddress())) 105 return true; 106 } 107 return false; 108 } else { 109 return false; 110 } 111 } 112 113 /** 114 * Returns a hashcode for this principal. 115 * 116 * @return a hashcode for this principal. 117 */ 118 public int hashCode(){ 119 return add[0].hashCode(); 120 } 121 122 /** 123 * Returns a string representation of this principal. In case of multiple address, the first one is returned. 124 * 125 * @return a string representation of this principal. 126 */ 127 public String toString() { 128 return ("PrincipalImpl :"+add[0].toString()); 129 } 130 131 /** 132 * Returns the Internet Protocol (IP) address for this principal. In case of multiple address, the first one is returned. 133 * 134 * @return the Internet Protocol (IP) address for this principal. 135 */ 136 public InetAddress getAddress(){ 137 return add[0]; 138 } 139 140 /** 141 * Returns the Internet Protocol (IP) address for this principal. In case of multiple address, the first one is returned. 142 * 143 * @return the array of Internet Protocol (IP) addresses for this principal. 144 */ 145 public InetAddress[] getAddresses(){ 146 return add; 147 } 148 }