View Javadoc

1   /*
2    * GridUser.java
3    *
4    * Created on August 31, 2004, 4:00 PM
5    */
6   
7   package gov.bnl.gums;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  /** 
13   * Represent a GRID Identity in GUMS, which is a certificate with its DN and FQAN.
14   *
15   * @author  Gabriele Carcassi, Jay Packard
16   */
17  public class GridUser {
18      private Log log = LogFactory.getLog(GridUser.class);
19      private String certificateDN;
20      private FQAN voFQAN;
21      
22      /**
23       * Creates a GRID credentail with no DN and FQAN.
24       */
25      public GridUser() {
26      }
27      
28      /**
29       * Creates a new object representing a Grid credential.
30       * 
31       * @param userDN the DN of the user certificate (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
32       * @param fqan The Fully Qualified Attribute name String representation (i.e. "/atlas/production/Role=Leader")
33       */
34      public GridUser(String userDN, String fqan) {
35          setCertificateDN( userDN );
36          if (fqan!=null && fqan.length()>0)
37              setVoFQAN(new FQAN(fqan));
38      }
39      
40      /**
41       * @param user
42       * @return true if user DN element matches
43       */
44      public int compareDn(GridUser user) {
45  		if (this.certificateDN == null || user.certificateDN == null)
46  			return (this.certificateDN==user.certificateDN ? 0 : (user.certificateDN==null ? -1: 1));
47      	return this.compareDn( user.getCertificateDN() );
48      }
49      
50      /**
51       * @param userDn
52       * @return true if user DN element matches
53       */
54      public int compareDn(String userDn) {
55  		if (this.certificateDN == null || userDn == null)
56  			return (this.certificateDN==userDn ? 0 : (userDn==null ? -1: 1));
57      	return this.certificateDN.compareTo( userDn );//compareToIgnoreCase( userDn );
58      }
59      
60      /**
61       * A GridUser will be equal only to another GridUser with the same DN and FQAN.
62       * 
63       * @param obj another object
64       * @return true if the object was a GridUser with equivalent credentials
65       */
66      public boolean equals(Object obj) {
67          GridUser user = (GridUser) obj;
68          if ((user.getCertificateDN() == null) ? certificateDN != null : (user.compareDn(certificateDN)!=0)) {
69              if (log.isTraceEnabled()) {
70                  log.trace(this + " !equals " + obj + " for different DN");
71              }
72              return false;
73          }
74          if ((user.voFQAN == null) ? voFQAN != null : (!user.voFQAN.equals(voFQAN))) {
75              if (log.isTraceEnabled()) {
76                  log.trace(this + " !equals " + obj + " for different FQAN");
77              }
78              return false;
79          }
80          if (log.isTraceEnabled()) {
81              log.trace(this + " equals " + obj);
82          }
83          return true;
84      }
85      
86      /**
87       * Retrieve the certificate DN of the user.
88       * 
89       * @return The certificate DN (i.e. "/DC=org/DC=doegrids/OU=People/CN=John Smith")
90       */
91      public String getCertificateDN() {
92          return this.certificateDN;
93      }
94      
95      /**
96       * Retrieve the VOMS Fully Qualified Attribute name.
97       * 
98       * @return The VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
99       */
100     public FQAN getVoFQAN() {
101         return this.voFQAN;
102     }
103     
104     /**
105      * Changed to reflect the change in equals, as in Object contract.
106      * 
107      * @return A hash created from the DN and FQAN.
108      */
109     public int hashCode() {
110         if (certificateDN != null) {
111             return certificateDN.hashCode();
112         }
113         if (voFQAN != null) {
114             return voFQAN.getFqan().hashCode();
115         }
116         return 0;
117     }
118     
119     /**
120      * Changes the certificate DN for the Grid credential.
121      * 
122      * @param certificateDN A GRID certificate DN (i.e. "/DC=org/DC=doegrids/OU=People/CN=Gabriele Carcassi")
123      */
124     public void setCertificateDN(String certificateDN) {
125         this.certificateDN = certificateDN;//removeSpaces(certificateDN);
126     }
127     
128     /**
129      * Sets the VOMS Fully Qualified Attribute name for the credential.
130      * 
131      * @param voFQAN The VOMS FQAN selected with voms-proxy-init (i.e. "/atlas/production/Role=Leader")
132      */
133     public void setVoFQAN(FQAN voFQAN) {
134         this.voFQAN = voFQAN;
135     }
136     
137     /**
138      * Returns a legible String representation for the credentail.
139      * 
140      * @return String reprentation of the credential (i.e. "GridID[/DC=org/DC=doegrids/OU=People/CN=Gabriele Carcassi]")
141      */
142     public String toString() {
143         if (voFQAN == null) {
144             return "GridID[" + certificateDN + "]";
145         }
146         return "GridID[" + certificateDN + ", " + voFQAN + "]";
147     }
148     
149     /**
150      * Trim and remove two or more consecutive strings
151      * 
152      * @param str
153      * @return new string
154      */
155     private String removeSpaces(String str) {
156     	if (str!=null) {
157     		str = str.trim();
158 	    	String tempStr;
159 	    	while ( !(tempStr=str.replaceAll("\\s\\s", " ")).equals(str) )
160 	    		str = tempStr;	   	    	
161     	}
162    		return str;
163     }
164     
165 }