1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
41
42
43
44 public class JGuardCredential implements Serializable, Cloneable {
45
46
47
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
68
69 public String getName() {
70 return name;
71 }
72
73
74
75
76 public void setName(String id) {
77 this.name = id;
78 }
79
80
81
82
83 public Object getValue() {
84 return value;
85 }
86
87
88
89
90 public void setValue(Object value) {
91 this.value = value;
92 }
93
94
95
96
97
98
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 }