001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name$
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.core.authentication.credentials;
029:
030: import java.io.Serializable;
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033: import java.util.logging.Logger;
034:
035: /**
036: * Class which wrap security credential.
037: * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
038: *
039: */
040: public class JGuardCredential implements Serializable, Cloneable {
041:
042: //TODO may implements Refreshable and Destroyable interfaces
043: // cf http://java.sun.com/security/jaas/doc/api.html
044: private static final long serialVersionUID = 2251806339549583892L;
045: private Logger logger = Logger.getLogger(JGuardCredential.class
046: .getName());
047: private String id = null;
048: private Object value = null;
049: private boolean digestNeeded;
050: private boolean identity;
051:
052: /**
053: * @return Returns the id.
054: */
055: public String getId() {
056: return id;
057: }
058:
059: /**
060: * @param id The id to set.
061: */
062: public void setId(String id) {
063: this .id = id;
064: }
065:
066: /**
067: * @return Returns the value.
068: */
069: public Object getValue() {
070: return value;
071: }
072:
073: /**
074: * @param value The value to set.
075: */
076: public void setValue(Object value) {
077: this .value = value;
078: }
079:
080: /**
081: * used to compare an object to this credential.
082: * @param obj object to compare
083: * @return <i>true</i> if equals, <i>false</i> otherwise
084: */
085: public boolean equals(Object obj) {
086: JGuardCredential cred = null;
087: if (obj instanceof JGuardCredential) {
088: cred = (JGuardCredential) obj;
089: } else {
090: return false;
091: }
092: if (this .id.equals(cred.id) && this .value.equals(cred.value)) {
093: return true;
094: }
095: return false;
096: }
097:
098: public int hashCode() {
099: if (id != null && value != null) {
100: return id.hashCode() + value.hashCode();
101: } else if (value == null) {
102: return id.hashCode();
103: } else {
104: return -1;
105: }
106: }
107:
108: public String toString() {
109: StringBuffer sb = new StringBuffer();
110: sb.append("\n");
111: sb.append("id=");
112: sb.append(id);
113: sb.append("\n");
114: sb.append("value=");
115: sb.append(value);
116: sb.append("\n");
117: return sb.toString();
118: }
119:
120: public boolean isDigestNeeded() {
121: return digestNeeded;
122: }
123:
124: public void setDigestNeeded(boolean digestNeeded) {
125: this .digestNeeded = digestNeeded;
126: }
127:
128: public boolean isIdentity() {
129: return identity;
130: }
131:
132: public void setIdentity(boolean identity) {
133: this .identity = identity;
134: }
135:
136: public Object clone() throws CloneNotSupportedException {
137: JGuardCredential clone = (JGuardCredential) super .clone();
138: clone.setDigestNeeded(digestNeeded);
139: clone.setId(id);
140: clone.setIdentity(identity);
141: if (value == null) {
142: clone.setValue(null);
143: return clone;
144: }
145: if (value instanceof Cloneable) {
146: Class[] clazz = new Class[] { null };
147: try {
148: Method cloneMethod = value.getClass().getMethod(
149: "clone", clazz);
150: Object clonedValue = cloneMethod.invoke(value, null);
151: clone.setValue(clonedValue);
152: } catch (SecurityException e) {
153: logger.severe(e.getMessage());
154: throw new CloneNotSupportedException(e.getMessage());
155: } catch (NoSuchMethodException e) {
156: logger.severe(e.getMessage());
157: throw new CloneNotSupportedException(e.getMessage());
158: } catch (IllegalArgumentException e) {
159: logger.severe(e.getMessage());
160: throw new CloneNotSupportedException(e.getMessage());
161: } catch (IllegalAccessException e) {
162: logger.severe(e.getMessage());
163: throw new CloneNotSupportedException(e.getMessage());
164: } catch (InvocationTargetException e) {
165: logger.severe(e.getMessage());
166: throw new CloneNotSupportedException(e.getMessage());
167: }
168:
169: } else {
170: throw new CloneNotSupportedException(value.getClass()
171: + " does not support cloning mechanism ");
172: }
173:
174: return clone;
175: }
176: }
|