1 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.logging.Logger ; 34 35 40 public class JGuardCredential implements Serializable ,Cloneable { 41 42 private static final long serialVersionUID = 2251806339549583892L; 45 private Logger logger = Logger.getLogger(JGuardCredential.class.getName()); 46 private String id = null; 47 private Object value = null; 48 private boolean digestNeeded; 49 private boolean identity; 50 51 52 55 public String getId() { 56 return id; 57 } 58 61 public void setId(String id) { 62 this.id = id; 63 } 64 67 public Object getValue() { 68 return value; 69 } 70 73 public void setValue(Object value) { 74 this.value = value; 75 } 76 77 82 public boolean equals(Object 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 toString(){ 107 StringBuffer sb = new StringBuffer (); 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 clone()throws CloneNotSupportedException { 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 ){ 141 Class [] clazz = new Class []{null}; 142 try { 143 Method cloneMethod = value.getClass().getMethod("clone",clazz); 144 Object clonedValue = cloneMethod.invoke(value,null); 145 clone.setValue(clonedValue); 146 } catch (SecurityException e) { 147 logger.severe(e.getMessage()); 148 throw new CloneNotSupportedException (e.getMessage()); 149 } catch (NoSuchMethodException e) { 150 logger.severe(e.getMessage()); 151 throw new CloneNotSupportedException (e.getMessage()); 152 } catch (IllegalArgumentException e) { 153 logger.severe(e.getMessage()); 154 throw new CloneNotSupportedException (e.getMessage()); 155 } catch (IllegalAccessException e) { 156 logger.severe(e.getMessage()); 157 throw new CloneNotSupportedException (e.getMessage()); 158 } catch (InvocationTargetException e) { 159 logger.severe(e.getMessage()); 160 throw new CloneNotSupportedException (e.getMessage()); 161 } 162 163 164 }else{ 165 throw new CloneNotSupportedException (value.getClass()+" does not support cloning mechanism "); 166 } 167 168 return clone; 169 } 170 } 171 | Popular Tags |