KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > IPAcl > PrincipalImpl


1 /*
2  * @(#)file PrincipalImpl.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 4.15
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12
13 package com.sun.jmx.snmp.IPAcl;
14
15
16
17 import java.net.InetAddress JavaDoc;
18 import java.net.UnknownHostException JavaDoc;
19 import java.io.Serializable JavaDoc;
20
21
22 /**
23  * Principal represents a host.
24  *
25  * @version 4.15 12/19/03
26  * @author Sun Microsystems, Inc
27  */

28
29 class PrincipalImpl implements java.security.Principal JavaDoc, Serializable JavaDoc {
30     private InetAddress JavaDoc[] add = null;
31   
32     /**
33      * Constructs a principal with the local host.
34      */

35     public PrincipalImpl () throws UnknownHostException JavaDoc {
36     add = new InetAddress JavaDoc[1];
37         add[0] = java.net.InetAddress.getLocalHost();
38     }
39   
40     /**
41      * Construct a principal using the specified host.
42      * <P>
43      * The host can be either:
44      * <UL>
45      * <LI> a host name
46      * <LI> an IP address
47      * </UL>
48      *
49      * @param hostName the host used to make the principal.
50      */

51     public PrincipalImpl(String JavaDoc hostName) throws UnknownHostException JavaDoc {
52         if ((hostName.equals("localhost")) || (hostName.equals("127.0.0.1"))) {
53         add = new InetAddress JavaDoc[1];
54             add[0] = java.net.InetAddress.getByName(hostName);
55     }
56         else
57             add = java.net.InetAddress.getAllByName( hostName );
58     }
59     
60     /**
61      * Constructs a principal using an Internet Protocol (IP) address.
62      *
63      * @param address the Internet Protocol (IP) address.
64      */

65     public PrincipalImpl(InetAddress JavaDoc address) {
66         add = new InetAddress JavaDoc[1];
67     add[0] = address;
68     }
69   
70     /**
71      * Returns the name of this principal.
72      *
73      * @return the name of this principal.
74      */

75     public String JavaDoc getName() {
76         return add[0].toString();
77     }
78   
79     /**
80      * Compares this principal to the specified object. Returns true if the
81      * object passed in matches the principal
82      * represented by the implementation of this interface.
83      *
84      * @param a the principal to compare with.
85      * @return true if the principal passed in is the same as that encapsulated by this principal, false otherwise.
86      */

87     public boolean equals(Object JavaDoc a) {
88         if (a instanceof PrincipalImpl){
89         for(int i = 0; i < add.length; i++) {
90         if(add[i].equals ((InetAddress JavaDoc)((PrincipalImpl) a).getAddress()))
91             return true;
92         }
93         return false;
94         } else {
95             return false;
96         }
97     }
98     
99     /**
100      * Returns a hashcode for this principal.
101      *
102      * @return a hashcode for this principal.
103      */

104     public int hashCode(){
105         return add[0].hashCode();
106     }
107   
108     /**
109      * Returns a string representation of this principal. In case of multiple address, the first one is returned.
110      *
111      * @return a string representation of this principal.
112      */

113     public String JavaDoc toString() {
114         return ("PrincipalImpl :"+add[0].toString());
115     }
116   
117     /**
118      * Returns the Internet Protocol (IP) address for this principal. In case of multiple address, the first one is returned.
119      *
120      * @return the Internet Protocol (IP) address for this principal.
121      */

122     public InetAddress JavaDoc getAddress(){
123         return add[0];
124     }
125
126     /**
127      * Returns the Internet Protocol (IP) address for this principal. In case of multiple address, the first one is returned.
128      *
129      * @return the array of Internet Protocol (IP) addresses for this principal.
130      */

131     public InetAddress JavaDoc[] getAddresses(){
132         return add;
133     }
134 }
135
136
Popular Tags