KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > Machine


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.lenya.ac;
19
20 import java.io.Serializable JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * A machine (representing an IP address).
28  * @version $Id: Machine.java 169164 2005-05-08 21:12:53Z gregor $
29  */

30 public class Machine implements Identifiable, Serializable JavaDoc {
31
32     /**
33      * Creates a new machine object. This method accepts
34      * numeric IPv4 addresses like <code>"129.168.0.32"</code>,
35      * numeric IPv6 addresses like <code>"1080::8:800:200C:417A"</code>
36      * as well as hostnames (if DNS resolution is available) like
37      * <code>"localhost"</code> or <code>"www.apache.com"</code>.
38      *
39      * @param ip a <code>String</code> like <code>"192.168.0.32"</code>,
40      * <code>"::1"</code>, ...
41      * .
42      * @throws AccessControlException when the conversion of the
43      * <code>String</code> to an <code>InetAddress</code> failed
44      */

45     public Machine(String JavaDoc ip) throws AccessControlException {
46         try {
47             setAddress(InetAddress.getByName(ip));
48         } catch(UnknownHostException JavaDoc uhe) {
49             throw new AccessControlException
50                 ("Failed to convert address [" + ip + "]: ", uhe);
51         }
52     }
53
54     private InetAddress JavaDoc address;
55
56     /**
57      * @see java.lang.Object#equals(Object)
58      */

59     public boolean equals(Object JavaDoc 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     /**
71      * @see java.lang.Object#hashCode()
72      */

73     public int hashCode() {
74         return getAddress().hashCode();
75     }
76
77     /**
78      * @see org.apache.lenya.ac.Accreditable#getAccreditables()
79      */

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     /**
91      * Returns the IP address.
92      * @return The IP address.
93      */

94     public String JavaDoc getIp() {
95         return getAddress().getHostAddress();
96     }
97
98     /**
99      * Converts a string to an IP addres.
100      * @param string The IP address, represented by a string.
101      * @return An InetAddress object.
102      * @throws AccessControlException when something went wrong.
103      *
104      * @deprecated This method is unnecessary and does not work for IPv6.
105      * Use <code>InetAddress.getByName(string)</code> instead!
106      */

107     public static InetAddress JavaDoc getAddress(String JavaDoc string)
108         throws AccessControlException {
109         String JavaDoc[] strings = string.split("\\.");
110
111         InetAddress JavaDoc 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 JavaDoc e) {
124             throw new AccessControlException(
125                 "Failed to convert address [" + string + "]: ",
126                 e);
127         }
128         return address;
129     }
130
131     /**
132      * @see java.lang.Object#toString()
133      */

134     public String JavaDoc toString() {
135         return getIp();
136     }
137
138     /**
139      * Returns the IP address.
140      * @return An IP address.
141      */

142     public InetAddress JavaDoc getAddress() {
143         return address;
144     }
145
146     /**
147      * Sets the IP address.
148      * @param address An IP address.
149      */

150     public void setAddress(InetAddress JavaDoc address) {
151         this.address = address;
152     }
153
154     private List JavaDoc ipRanges = new ArrayList JavaDoc();
155     
156     /**
157      * Adds an IP range to this machine.
158      * @param range An IP range this machine belongs to.
159      */

160     public void addIPRange(IPRange range) {
161         assert range != null;
162         assert !ipRanges.contains(range);
163         ipRanges.add(range);
164     }
165     
166     /**
167      * Returns the IP ranges this machine belongs to.
168      * @return An array of IP ranges.
169      */

170     public IPRange[] getIPRanges() {
171         return (IPRange[]) ipRanges.toArray(new IPRange[ipRanges.size()]);
172     }
173     
174 }
175
Popular Tags