View Javadoc

1   /* Copyright 2004, 2005 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  package org.acegisecurity.context;
16  
17  import org.acegisecurity.Authentication;
18  
19  
20  /***
21   * Base implementation of {@link SecurityContext}.
22   *
23   * <p>
24   * Used by default by {@link SecurityContextHolder} and {@link HttpSessionContextIntegrationFilter}.
25   * </p>
26   *
27   * @author Ben Alex
28   * @version $Id: SecurityContextImpl.java,v 1.4 2005/11/17 00:55:49 benalex Exp $
29   */
30  public class SecurityContextImpl implements SecurityContext {
31      private Authentication authentication;
32  
33      public void setAuthentication(Authentication authentication) {
34          this.authentication = authentication;
35      }
36  
37      public Authentication getAuthentication() {
38          return authentication;
39      }
40  
41      public boolean equals(Object obj) {
42          if (obj instanceof SecurityContextImpl) {
43              SecurityContextImpl test = (SecurityContextImpl) obj;
44  
45              if ((this.getAuthentication() == null) &&
46                      (test.getAuthentication() == null)) {
47                  return true;
48              }
49  
50              if ((this.getAuthentication() != null) &&
51                      (test.getAuthentication() != null) &&
52                      this.getAuthentication().equals(test.getAuthentication())) {
53                  return true;
54              }
55          }
56  
57          return false;
58      }
59  
60      public String toString() {
61          StringBuffer sb = new StringBuffer();
62          sb.append(super.toString());
63  
64          if (this.authentication == null) {
65              sb.append(": Null authentication");
66          } else {
67              sb.append(": Authentication: " + this.authentication);
68          }
69  
70          return sb.toString();
71      }
72  
73      public int hashCode() {
74          if (this.authentication == null) {
75              return -1;
76          } else {
77              return this.authentication.hashCode();
78          }
79      }
80  }