View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.springframework.security.context;
17  
18  import org.springframework.security.Authentication;
19  
20  
21  /**
22   * Base implementation of {@link SecurityContext}.<p>Used by default by {@link SecurityContextHolder} and {@link
23   * HttpSessionContextIntegrationFilter}.</p>
24   *
25   * @author Ben Alex
26   * @version $Id: SecurityContextImpl.java 2217 2007-10-27 00:45:30Z luke_t $
27   */
28  public class SecurityContextImpl implements SecurityContext {
29      //~ Instance fields ================================================================================================
30  
31      private Authentication authentication;
32  
33      //~ Methods ========================================================================================================
34  
35      public boolean equals(Object obj) {
36          if (obj instanceof SecurityContextImpl) {
37              SecurityContextImpl test = (SecurityContextImpl) obj;
38  
39              if ((this.getAuthentication() == null) && (test.getAuthentication() == null)) {
40                  return true;
41              }
42  
43              if ((this.getAuthentication() != null) && (test.getAuthentication() != null)
44                  && this.getAuthentication().equals(test.getAuthentication())) {
45                  return true;
46              }
47          }
48  
49          return false;
50      }
51  
52      public Authentication getAuthentication() {
53          return authentication;
54      }
55  
56      public int hashCode() {
57          if (this.authentication == null) {
58              return -1;
59          } else {
60              return this.authentication.hashCode();
61          }
62      }
63  
64      public void setAuthentication(Authentication authentication) {
65          this.authentication = authentication;
66      }
67  
68      public String toString() {
69          StringBuffer sb = new StringBuffer();
70          sb.append(super.toString());
71  
72          if (this.authentication == null) {
73              sb.append(": Null authentication");
74          } else {
75              sb.append(": Authentication: ").append(this.authentication);
76          }
77  
78          return sb.toString();
79      }
80  }