1 17 18 package org.apache.lenya.ac; 19 20 import java.io.Serializable ; 21 import java.net.InetAddress ; 22 import java.net.UnknownHostException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 30 public class Machine implements Identifiable, Serializable { 31 32 45 public Machine(String ip) throws AccessControlException { 46 try { 47 setAddress(InetAddress.getByName(ip)); 48 } catch(UnknownHostException uhe) { 49 throw new AccessControlException 50 ("Failed to convert address [" + ip + "]: ", uhe); 51 } 52 } 53 54 private InetAddress address; 55 56 59 public boolean equals(Object otherObject) { 60 boolean equals = false; 61 62 if (otherObject instanceof Machine) { 63 Machine otherMachine = (Machine) otherObject; 64 equals = getAddress().equals(otherMachine.getAddress()); 65 } 66 67 return equals; 68 } 69 70 73 public int hashCode() { 74 return getAddress().hashCode(); 75 } 76 77 80 public Accreditable[] getAccreditables() { 81 Accreditable[] ranges = getIPRanges(); 82 Accreditable[] accreditables = new Accreditable[ranges.length + 1]; 83 accreditables[0] = this; 84 for (int i = 0; i < ranges.length; i++) { 85 accreditables[i+1] = ranges[i]; 86 } 87 return accreditables; 88 } 89 90 94 public String getIp() { 95 return getAddress().getHostAddress(); 96 } 97 98 107 public static InetAddress getAddress(String string) 108 throws AccessControlException { 109 String [] strings = string.split("\\."); 110 111 InetAddress address; 112 try { 113 byte[] numbers = new byte[strings.length]; 114 for (int i = 0; i < strings.length; i++) { 115 int number = Integer.parseInt(strings[i]); 116 if (number > 127) { 117 number = number - 256; 118 } 119 numbers[i] = (byte) number; 120 } 121 122 address = InetAddress.getByAddress(numbers); 123 } catch (Exception e) { 124 throw new AccessControlException( 125 "Failed to convert address [" + string + "]: ", 126 e); 127 } 128 return address; 129 } 130 131 134 public String toString() { 135 return getIp(); 136 } 137 138 142 public InetAddress getAddress() { 143 return address; 144 } 145 146 150 public void setAddress(InetAddress address) { 151 this.address = address; 152 } 153 154 private List ipRanges = new ArrayList (); 155 156 160 public void addIPRange(IPRange range) { 161 assert range != null; 162 assert !ipRanges.contains(range); 163 ipRanges.add(range); 164 } 165 166 170 public IPRange[] getIPRanges() { 171 return (IPRange[]) ipRanges.toArray(new IPRange[ipRanges.size()]); 172 } 173 174 } 175 | Popular Tags |