View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26  
27  */
28  package net.sf.jguard.core.authentication.credentials;
29  
30  import java.io.Serializable;
31  import java.lang.reflect.InvocationTargetException;
32  import java.lang.reflect.Method;
33  import java.util.HashSet;
34  import java.util.Iterator;
35  import java.util.Set;
36  import java.util.logging.Level;
37  import java.util.logging.Logger;
38  
39  /**
40   * Class which wrap security credential.
41   *
42   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
43   */
44  public class JGuardCredential implements Serializable, Cloneable {
45  
46      //TODO may implements Refreshable and Destroyable interfaces
47      // cf http://java.sun.com/security/jaas/doc/api.html
48      private static final long serialVersionUID = 2251806339749583892L;
49      private Logger logger = Logger.getLogger(JGuardCredential.class.getName());
50      private String name = null;
51      private Object value = null;
52      private Long id;
53      private boolean publicVisibility;
54  
55      public JGuardCredential() {
56          super();
57      }
58  
59      public JGuardCredential(String name, Object value) {
60          super();
61          this.name = name;
62          this.value = value;
63      }
64  
65  
66      /**
67       * @return Returns the id.
68       */
69      public String getName() {
70          return name;
71      }
72  
73      /**
74       * @param id The id to set.
75       */
76      public void setName(String id) {
77          this.name = id;
78      }
79  
80      /**
81       * @return Returns the value.
82       */
83      public Object getValue() {
84          return value;
85      }
86  
87      /**
88       * @param value The value to set.
89       */
90      public void setValue(Object value) {
91          this.value = value;
92      }
93  
94      /**
95       * used to compare an object to this credential.
96       *
97       * @param obj object to compare
98       * @return <i>true</i> if equals, <i>false</i> otherwise
99       */
100     public boolean equals(Object obj) {
101         JGuardCredential cred;
102         if (obj instanceof JGuardCredential) {
103             cred = (JGuardCredential) obj;
104         } else {
105             return false;
106         }
107         return this.name.equals(cred.name) && this.value.equals(cred.value);
108     }
109 
110 
111     public int hashCode() {
112         if (name != null && value != null) {
113             return name.hashCode() + value.hashCode();
114         } else if (value == null) {
115             return name.hashCode();
116         } else {
117             return -1;
118         }
119     }
120 
121     public String toString() {
122         StringBuffer sb = new StringBuffer();
123         sb.append("\n");
124         sb.append("id=");
125         sb.append(name);
126         sb.append("\n");
127         sb.append("value=");
128         sb.append(value);
129         sb.append("\n");
130         sb.append("identity=");
131         sb.append("\n");
132         return sb.toString();
133     }
134 
135 
136     @Override
137     public Object clone() throws CloneNotSupportedException {
138         JGuardCredential clone = new JGuardCredential();
139         clone.setName(name);
140         if (value == null) {
141             clone.setValue(null);
142             return clone;
143         }
144         if (value instanceof Cloneable) {
145             Class[] clazz = new Class[]{null};
146             try {
147                 Method cloneMethod = value.getClass().getMethod("clone", clazz);
148                 Object clonedValue = cloneMethod.invoke(value, null);
149                 clone.setValue(clonedValue);
150             } catch (SecurityException e) {
151                 logger.log(Level.SEVERE, e.getMessage(), e);
152                 throw new CloneNotSupportedException(e.getMessage());
153             } catch (NoSuchMethodException e) {
154                 logger.log(Level.SEVERE, e.getMessage(), e);
155                 throw new CloneNotSupportedException(e.getMessage());
156             } catch (IllegalArgumentException e) {
157                 logger.log(Level.SEVERE, e.getMessage(), e);
158                 throw new CloneNotSupportedException(e.getMessage());
159             } catch (IllegalAccessException e) {
160                 logger.log(Level.SEVERE, e.getMessage(), e);
161                 throw new CloneNotSupportedException(e.getMessage());
162             } catch (InvocationTargetException e) {
163                 logger.log(Level.SEVERE, e.getMessage(), e);
164                 throw new CloneNotSupportedException(e.getMessage());
165             }
166 
167 
168         } else if (value instanceof String) {
169             clone.setValue(value);
170         } else {
171 
172             throw new CloneNotSupportedException(value.getClass() + " does not support cloning mechanism ");
173         }
174 
175         return clone;
176     }
177 
178     public static Set cloneCredentialsSet(Set credentials) throws CloneNotSupportedException {
179         Set clonedCredentials = new HashSet();
180         Iterator itCredentials = credentials.iterator();
181         while (itCredentials.hasNext()) {
182             JGuardCredential credential = (JGuardCredential) itCredentials.next();
183             clonedCredentials.add(credential.clone());
184         }
185         return clonedCredentials;
186     }
187 
188     public Long getId() {
189         return id;
190     }
191 
192     private void setId(Long id) {
193         this.id = id;
194     }
195 
196     public boolean isPublicVisibility() {
197         return publicVisibility;
198     }
199 
200     public void setPublicVisibility(boolean publicVisibility) {
201         this.publicVisibility = publicVisibility;
202     }
203 }