View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/common/src/java/org/astrogrid/community/common/security/data/SecurityToken.java,v $</cvs:source>
3    * <cvs:author>$Author: dave $</cvs:author>
4    * <cvs:date>$Date: 2004/06/18 13:45:20 $</cvs:date>
5    * <cvs:version>$Revision: 1.5 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: SecurityToken.java,v $
9    *   Revision 1.5  2004/06/18 13:45:20  dave
10   *   Merged development branch, dave-dev-200406081614, into HEAD
11   *
12   *   Revision 1.4.36.1  2004/06/17 13:38:59  dave
13   *   Tidied up old CVS log entries
14   *
15   * </cvs:log>
16   *
17   */
18  package org.astrogrid.community.common.security.data ;
19  
20  public class SecurityToken
21      {
22      /***
23       * Status value for an invalid token.
24       *
25       */
26      public static final int INVALID_TOKEN = 0x00 ;
27  
28      /***
29       * Status value for a valid token.
30       *
31       */
32      public static final int VALID_TOKEN = 0x01 ;
33  
34      /***
35       * Delimiter for to String.
36       *
37       */
38      public static final String DELIMTER_STRING = "|" ;
39  
40      /***
41       * Delimiter for regexp split.
42       *
43       */
44      public static final String SPLIT_STRING = "//|" ;
45  
46      /***
47       * Public constructor.
48       *
49       */
50      public SecurityToken()
51          {
52          this(null, null) ;
53          }
54  
55      /***
56       * Public constructor from a String.
57       *
58       */
59      public SecurityToken(String value)
60          {
61          if (null != value)
62              {
63              //
64              // Split the string on delimiters.
65              String[] split = value.split(SPLIT_STRING) ;
66              //
67              // If we have a Token value.
68              if (split.length > 0)
69                  {
70                  this.setToken(split[0]) ;
71                  }
72              //
73              // If we have an Account ident.
74              if (split.length > 1)
75                  {
76                  this.setAccount(split[1]) ;
77                  }
78              }
79          }
80  
81      /***
82       * Public constructor.
83       *
84       */
85      public SecurityToken(String account, String token)
86          {
87          this.setAccount(account) ;
88          this.setToken(token) ;
89          this.setStatus(INVALID_TOKEN) ;
90          }
91  
92      /***
93       * The Account identifier that this token was issued to.
94       *
95       */
96      private String account ;
97  
98      /***
99       * Get our Account identifier.
100      *
101      */
102     public String getAccount()
103         {
104         return this.account ;
105         }
106 
107     /***
108      * Set our Account identifier.
109      * Fails if the identifier is already set.
110      *
111      */
112     public void setAccount(String value)
113         {
114         if (null == this.account)
115             {
116             this.account = value ;
117             }
118         }
119 
120     /***
121      * Our token value.
122      *
123      */
124     private String token ;
125 
126     /***
127      * Get the token value.
128      *
129      */
130     public String getToken()
131         {
132         return this.token ;
133         }
134 
135     /***
136      * Set our token value.
137      * Fails if the token is already set.
138      *
139      */
140     public void setToken(String value)
141         {
142         if (null == this.token)
143             {
144             this.token = value ;
145             }
146         }
147 
148     /***
149      * The token status.
150      *
151      */
152     private int status ;
153 
154     /***
155      * Get the token status.
156      *
157      */
158     public int getStatus()
159         {
160         return this.status ;
161         }
162 
163     /***
164      * Set the token status.
165      *
166      */
167     public void setStatus(int value)
168         {
169         this.status = value ;
170         }
171 
172     /***
173      * Chec the token status.
174      *
175      */
176     public boolean isValid()
177         {
178         return (this.status == VALID_TOKEN) ;
179         }
180 
181     /***
182      * Convert the token to a string.
183      *
184      */
185     public String toString()
186         {
187         return this.token + DELIMTER_STRING + this.account ;
188         }
189 
190     /*
191      * Compare this with another SecurityToken.
192      * This checks both the the Account identifier and value.
193      *
194      */
195     public synchronized boolean equals(Object object)
196         {
197         //
198         // If the object is null.
199         if (null == object)
200             {
201             return false ;
202             }
203         //
204         // If the object is not null.
205         else {
206             //
207             // If the object is a SecurityToken
208             if (object instanceof SecurityToken)
209                 {
210                 SecurityToken that = (SecurityToken) object ;
211                 //
212                 // If our account identifier is null
213                 if (null == this.getAccount())
214                     {
215                     //
216                     // Check that account identifier is null.
217                     return (null == that.getAccount()) ;
218                     }
219                 //
220                 // If our account identifier is not null.
221                 else {
222                     //
223                     // Check both the account identifier and token are the same.
224                     return (
225                         this.getAccount().equals(that.getAccount())
226                         &&
227                         this.getToken().equals(that.getToken())
228                         ) ;
229                     }
230                 }
231             //
232             // If object is not a SecurityToken
233             else {
234                 return false ;
235                 }
236             }
237         }
238 
239     /***
240      * Generate a hash code for comparison tests.
241      * This combines the hash code for both identifier and token value.
242      *
243      */
244     public synchronized int hashCode()
245         {
246         int result = 0 ;
247         //
248         // If we have a valid account.
249         if (null != this.getAccount())
250             {
251             result += this.getAccount().hashCode() ;
252             }
253         //
254         // If we have a valid token.
255         if (null != this.getToken())
256             {
257             result += this.getToken().hashCode() ;
258             }
259         return result ;
260         }
261     }