1 package fr.in2p3.jsaga.impl.attributes;
2
3 import fr.in2p3.jsaga.helpers.SAGAPattern;
4 import fr.in2p3.jsaga.helpers.cloner.AttributeCloner;
5 import fr.in2p3.jsaga.impl.AbstractSagaObjectImpl;
6 import org.ogf.saga.SagaObject;
7 import org.ogf.saga.attributes.Attributes;
8 import org.ogf.saga.error.*;
9 import org.ogf.saga.session.Session;
10
11 import java.util.*;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public abstract class AbstractAttributesImpl extends AbstractSagaObjectImpl implements Attributes {
28 private Map<String,Attribute> m_attributes;
29 private boolean m_isExtensible;
30
31
32 public AbstractAttributesImpl(Session session, boolean isExtensible) {
33 super(session);
34 m_attributes = new HashMap<String, Attribute>();
35 m_isExtensible = isExtensible;
36 }
37
38 public AbstractAttributesImpl(Session session) {
39 this(session, false);
40 }
41
42
43 public SagaObject clone() throws CloneNotSupportedException {
44 AbstractAttributesImpl clone = (AbstractAttributesImpl) super.clone();
45 clone.m_attributes = new AttributeCloner<String>().cloneMap(m_attributes);
46 clone.m_isExtensible = m_isExtensible;
47 return clone;
48 }
49
50 public void setAttribute(String key, String value) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
51 Attribute attribute = m_attributes.get(key);
52 if (attribute != null) {
53 if (attribute instanceof AttributeScalar) {
54 ((AttributeScalar)attribute).setValue(value);
55 } else {
56 throw new IncorrectStateException("Attempted to set scalar value on a vector attribute: "+key, this);
57 }
58 } else if (m_isExtensible) {
59 m_attributes.put(key, new DefaultAttributeScalar(key, value));
60 } else {
61 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
62 }
63 }
64
65 public String getAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
66 Attribute attribute = m_attributes.get(key);
67 if (attribute != null) {
68 if (attribute instanceof AttributeScalar) {
69 return ((AttributeScalar)attribute).getValue();
70 } else {
71 throw new IncorrectStateException("Attempted to get scalar value from a vector attribute: "+key, this);
72 }
73 } else {
74 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
75 }
76 }
77
78 public void setVectorAttribute(String key, String[] values) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
79 Attribute attribute = m_attributes.get(key);
80 if (attribute != null) {
81 if (attribute instanceof AttributeVector) {
82 ((AttributeVector)attribute).setValues(values);
83 } else {
84 throw new IncorrectStateException("Attempted to set vector value on a scalar attribute: "+key, this);
85 }
86 } else if (m_isExtensible) {
87 m_attributes.put(key, new DefaultAttributeVector(key, values));
88 } else {
89 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
90 }
91 }
92
93 public String[] getVectorAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
94 Attribute attribute = m_attributes.get(key);
95 if (attribute != null) {
96 if (attribute instanceof AttributeVector) {
97 return ((AttributeVector)attribute).getValues();
98 } else {
99 throw new IncorrectStateException("Attempted to get vector value from a scalar attribute: "+key, this);
100 }
101 } else {
102 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
103 }
104 }
105
106 public void removeAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
107 if (!m_isExtensible) {
108 throw new NoSuccessException("Object does not support removing attributes", this);
109 }
110 Attribute attribute = m_attributes.get(key);
111 if (attribute != null) {
112 if (!attribute.isReadOnly()) {
113 m_attributes.remove(key);
114 } else {
115 throw new PermissionDeniedException("Attribute "+key+" not removable", this);
116 }
117 } else {
118 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
119 }
120 }
121
122 public String[] listAttributes() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, TimeoutException, NoSuccessException {
123 return m_attributes.keySet().toArray(new String[m_attributes.keySet().size()]);
124 }
125
126 public String[] findAttributes(String... patterns) throws NotImplementedException, BadParameterException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, TimeoutException, NoSuccessException {
127 List<String> foundKeys = new ArrayList<String>();
128 for (Attribute attribute : m_attributes.values()) {
129 if (matches(attribute, patterns)) {
130 foundKeys.add(attribute.getKey());
131 }
132 }
133 return foundKeys.toArray(new String[foundKeys.size()]);
134 }
135
136 public boolean existsAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, TimeoutException, NoSuccessException {
137 return m_attributes.containsKey(key);
138 }
139
140 private static final Pattern PATTERN = Pattern.compile("([^=]*)(=(.*))?");
141 private static boolean matches(Attribute attribute, String... patterns) throws NotImplementedException, NoSuccessException {
142 for (String pattern : patterns) {
143 Matcher matcher = PATTERN.matcher(pattern);
144 if (matcher.matches()) {
145 String keyPattern = matcher.group(1);
146 Pattern keyRegexp = SAGAPattern.toRegexp(keyPattern);
147 if (keyRegexp == null) {
148 return true;
149 } else {
150 if (keyRegexp.matcher(attribute.getKey()).matches()) {
151 String valuePattern = matcher.group(3);
152 Pattern valueRegexp = SAGAPattern.toRegexp(valuePattern);
153 if (valueRegexp == null) {
154 return true;
155 } else {
156 try {
157 if (attribute instanceof AttributeScalar) {
158 String value = ((AttributeScalar)attribute).getValue();
159 if (valueRegexp.matcher(value).matches()) {
160 return true;
161 }
162 } else {
163 String[] values = ((AttributeVector)attribute).getValues();
164 for (String value : values) {
165 if (valueRegexp.matcher(value).matches()) {
166 return true;
167 }
168 }
169 }
170 } catch (IncorrectStateException e) {
171 throw new NoSuccessException(e);
172 }
173 }
174 }
175 }
176 }
177 }
178 return false;
179 }
180
181 public boolean isReadOnlyAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
182 Attribute attribute = m_attributes.get(key);
183 if (attribute != null) {
184 return attribute.isReadOnly();
185 } else {
186 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
187 }
188 }
189
190 public boolean isWritableAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
191 Attribute attribute = m_attributes.get(key);
192 if (attribute != null) {
193 return !attribute.isReadOnly();
194 } else {
195 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
196 }
197 }
198
199 public boolean isRemovableAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
200 Attribute attribute = m_attributes.get(key);
201 if (attribute != null) {
202 return m_isExtensible;
203 } else {
204 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
205 }
206 }
207
208 public boolean isVectorAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
209 Attribute attribute = m_attributes.get(key);
210 if (attribute != null) {
211 return attribute instanceof AttributeVector;
212 } else {
213 throw new DoesNotExistException("Attribute "+key+" does not exist", this);
214 }
215 }
216
217 public boolean equals(AbstractAttributesImpl attributes) {
218 return m_attributes.equals(attributes.m_attributes);
219 }
220
221
222
223 public AttributeScalar _addAttribute(AttributeScalar scalarAttribute) {
224 m_attributes.put(scalarAttribute.getKey(), scalarAttribute);
225 return scalarAttribute;
226 }
227 public AttributeVector _addVectorAttribute(AttributeVector vectorAttribute) {
228 m_attributes.put(vectorAttribute.getKey(), vectorAttribute);
229 return vectorAttribute;
230 }
231 public ScalarAttributeImpl _addAttribute(ScalarAttributeImpl scalarAttribute) {
232 m_attributes.put(scalarAttribute.getKey(), scalarAttribute);
233 return scalarAttribute;
234 }
235 public VectorAttributeImpl _addVectorAttribute(VectorAttributeImpl vectorAttribute) {
236 m_attributes.put(vectorAttribute.getKey(), vectorAttribute);
237 return vectorAttribute;
238 }
239
240 protected void _addAttribute(String key, String value) {
241 m_attributes.put(key, new DefaultAttributeScalar(key, value));
242 }
243 protected void _addUnsupportedAttribute(String key) {
244 m_attributes.put(key, new DefaultAttributeScalar(key, false, false, null));
245 }
246 public void _addReadOnlyAttribute(String key, String constantValue) {
247 m_attributes.put(key, new DefaultAttributeScalar(key, true, true, constantValue));
248 }
249 public void _addReadOnlyAttribute(String key, String[] constantValues) {
250 m_attributes.put(key, new DefaultAttributeVector(key, true, true, constantValues));
251 }
252 public Map _getAttributesMap() throws NotImplementedException, IncorrectStateException, NoSuccessException {
253 Map<String,String> map = new HashMap<String,String>();
254 for (Map.Entry<String, Attribute> entry : m_attributes.entrySet()) {
255 String key = entry.getKey();
256 Attribute attr = entry.getValue();
257 if (attr instanceof AttributeScalar) {
258 String value = ((AttributeScalar)attr).getValue();
259 if (value != null) {
260 map.put(key, value);
261 }
262 } else {
263
264 }
265 }
266 return map;
267 }
268 protected boolean _containsAttributeKey(String key) {
269 return m_attributes.containsKey(key);
270 }
271 }