KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > authentication > credentials > JGuardCredential


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 JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 /**
36  * Class which wrap security credential.
37  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
38  *
39  */

40 public class JGuardCredential implements Serializable JavaDoc,Cloneable JavaDoc{
41
42     //TODO may implements Refreshable and Destroyable interfaces
43
// cf http://java.sun.com/security/jaas/doc/api.html
44
private static final long serialVersionUID = 2251806339549583892L;
45     private Logger JavaDoc logger = Logger.getLogger(JGuardCredential.class.getName());
46     private String JavaDoc id = null;
47     private Object JavaDoc value = null;
48     private boolean digestNeeded;
49     private boolean identity;
50
51
52     /**
53      * @return Returns the id.
54      */

55     public String JavaDoc getId() {
56         return id;
57     }
58     /**
59      * @param id The id to set.
60      */

61     public void setId(String JavaDoc id) {
62         this.id = id;
63     }
64     /**
65      * @return Returns the value.
66      */

67     public Object JavaDoc getValue() {
68         return value;
69     }
70     /**
71      * @param value The value to set.
72      */

73     public void setValue(Object JavaDoc value) {
74         this.value = value;
75     }
76
77     /**
78      * used to compare an object to this credential.
79      * @param obj object to compare
80      * @return <i>true</i> if equals, <i>false</i> otherwise
81      */

82     public boolean equals(Object JavaDoc obj){
83         JGuardCredential cred = null;
84         if( obj instanceof JGuardCredential){
85             cred = (JGuardCredential)obj;
86         }else{
87             return false;
88         }
89         if(this.id.equals(cred.id)&&this.value.equals(cred.value)){
90             return true;
91         }
92       return false;
93     }
94
95
96     public int hashCode(){
97         if(id!=null && value!=null){
98         return id.hashCode()+value.hashCode();
99         }else if(value==null){
100             return id.hashCode();
101         }else{
102             return -1;
103         }
104     }
105
106     public String JavaDoc toString(){
107         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
108         sb.append("\n");
109         sb.append("id=");
110         sb.append(id);
111         sb.append("\n");
112         sb.append("value=");
113         sb.append(value);
114         sb.append("\n");
115         return sb.toString();
116     }
117     public boolean isDigestNeeded() {
118         return digestNeeded;
119     }
120     public void setDigestNeeded(boolean digestNeeded) {
121         this.digestNeeded = digestNeeded;
122     }
123     public boolean isIdentity() {
124         return identity;
125     }
126     public void setIdentity(boolean identity) {
127         this.identity = identity;
128     }
129
130
131     public Object JavaDoc clone()throws CloneNotSupportedException JavaDoc{
132         JGuardCredential clone = (JGuardCredential) super.clone();
133         clone.setDigestNeeded(digestNeeded);
134         clone.setId(id);
135         clone.setIdentity(identity);
136         if(value== null){
137             clone.setValue(null);
138             return clone;
139 }
140         if(value instanceof Cloneable JavaDoc){
141             Class JavaDoc[] clazz = new Class JavaDoc[]{null};
142             try {
143                 Method JavaDoc cloneMethod = value.getClass().getMethod("clone",clazz);
144                 Object JavaDoc clonedValue = cloneMethod.invoke(value,null);
145                 clone.setValue(clonedValue);
146             } catch (SecurityException JavaDoc e) {
147                 logger.severe(e.getMessage());
148                 throw new CloneNotSupportedException JavaDoc(e.getMessage());
149             } catch (NoSuchMethodException JavaDoc e) {
150                 logger.severe(e.getMessage());
151                 throw new CloneNotSupportedException JavaDoc(e.getMessage());
152             } catch (IllegalArgumentException JavaDoc e) {
153                 logger.severe(e.getMessage());
154                 throw new CloneNotSupportedException JavaDoc(e.getMessage());
155             } catch (IllegalAccessException JavaDoc e) {
156                 logger.severe(e.getMessage());
157                 throw new CloneNotSupportedException JavaDoc(e.getMessage());
158             } catch (InvocationTargetException JavaDoc e) {
159                 logger.severe(e.getMessage());
160                 throw new CloneNotSupportedException JavaDoc(e.getMessage());
161             }
162
163
164         }else{
165             throw new CloneNotSupportedException JavaDoc(value.getClass()+" does not support cloning mechanism ");
166         }
167
168         return clone;
169     }
170 }
171
Popular Tags