001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.ac;
020:
021: import java.io.Serializable;
022: import java.net.InetAddress;
023: import java.net.UnknownHostException;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: /**
028: * A machine (representing an IP address).
029: * @version $Id: Machine.java 473861 2006-11-12 03:51:14Z gregor $
030: */
031: public class Machine implements Identifiable, Serializable {
032:
033: /**
034: *
035: */
036: private static final long serialVersionUID = 1L;
037:
038: /**
039: * Creates a new machine object. This method accepts
040: * numeric IPv4 addresses like <code>"129.168.0.32"</code>,
041: * numeric IPv6 addresses like <code>"1080::8:800:200C:417A"</code>
042: * as well as hostnames (if DNS resolution is available) like
043: * <code>"localhost"</code> or <code>"www.apache.com"</code>.
044: *
045: * @param ip a <code>String</code> like <code>"192.168.0.32"</code>,
046: * <code>"::1"</code>, ...
047: * .
048: * @throws AccessControlException when the conversion of the
049: * <code>String</code> to an <code>InetAddress</code> failed
050: */
051: public Machine(String ip) throws AccessControlException {
052: try {
053: setAddress(InetAddress.getByName(ip));
054: } catch (UnknownHostException uhe) {
055: throw new AccessControlException(
056: "Failed to convert address [" + ip + "]: ", uhe);
057: }
058: }
059:
060: private InetAddress address;
061:
062: /**
063: * @see java.lang.Object#equals(Object)
064: */
065: public boolean equals(Object otherObject) {
066: boolean equals = false;
067:
068: if (otherObject instanceof Machine) {
069: Machine otherMachine = (Machine) otherObject;
070: equals = getAddress().equals(otherMachine.getAddress());
071: }
072:
073: return equals;
074: }
075:
076: /**
077: * @see java.lang.Object#hashCode()
078: */
079: public int hashCode() {
080: return getAddress().hashCode();
081: }
082:
083: /**
084: * @see org.apache.lenya.ac.Accreditable#getAccreditables()
085: */
086: public Accreditable[] getAccreditables() {
087: Accreditable[] ranges = getIPRanges();
088: Accreditable[] accreditables = new Accreditable[ranges.length + 1];
089: accreditables[0] = this ;
090: for (int i = 0; i < ranges.length; i++) {
091: accreditables[i + 1] = ranges[i];
092: }
093: return accreditables;
094: }
095:
096: /**
097: * Returns the IP address.
098: * @return The IP address.
099: */
100: public String getIp() {
101: return getAddress().getHostAddress();
102: }
103:
104: /**
105: * Converts a string to an IP addres.
106: * @param string The IP address, represented by a string.
107: * @return An InetAddress object.
108: * @throws AccessControlException when something went wrong.
109: * @deprecated This method is unnecessary and does not work for IPv6.
110: * Use <code>InetAddress.getByName(string)</code> instead!
111: */
112: public static InetAddress getAddress(String string)
113: throws AccessControlException {
114: String[] strings = string.split("\\.");
115:
116: InetAddress address;
117: try {
118: byte[] numbers = new byte[strings.length];
119: for (int i = 0; i < strings.length; i++) {
120: int number = Integer.parseInt(strings[i]);
121: if (number > 127) {
122: number = number - 256;
123: }
124: numbers[i] = (byte) number;
125: }
126:
127: address = InetAddress.getByAddress(numbers);
128: } catch (final NumberFormatException e1) {
129: throw new AccessControlException(
130: "Failed to convert address [" + string + "]: ", e1);
131: } catch (final UnknownHostException e1) {
132: throw new AccessControlException(
133: "Failed to convert address [" + string + "]: ", e1);
134: }
135: return address;
136: }
137:
138: /**
139: * @see java.lang.Object#toString()
140: */
141: public String toString() {
142: return getIp();
143: }
144:
145: /**
146: * Returns the IP address.
147: * @return An IP address.
148: */
149: public InetAddress getAddress() {
150: return this .address;
151: }
152:
153: /**
154: * Sets the IP address.
155: * @param _address An IP address.
156: */
157: public void setAddress(InetAddress _address) {
158: this .address = _address;
159: }
160:
161: private List ipRanges = new ArrayList();
162:
163: /**
164: * Adds an IP range to this machine.
165: * @param range An IP range this machine belongs to.
166: */
167: public void addIPRange(IPRange range) {
168: assert range != null;
169: assert !this .ipRanges.contains(range);
170: this .ipRanges.add(range);
171: }
172:
173: /**
174: * Returns the IP ranges this machine belongs to.
175: * @return An array of IP ranges.
176: */
177: public IPRange[] getIPRanges() {
178: return (IPRange[]) this .ipRanges
179: .toArray(new IPRange[this.ipRanges.size()]);
180: }
181:
182: }
|