1 package fr.in2p3.jsaga.impl.context.attrs;
2
3 import fr.in2p3.jsaga.impl.attributes.Attribute;
4 import fr.in2p3.jsaga.impl.attributes.AttributeVector;
5 import fr.in2p3.jsaga.impl.context.ContextImpl;
6 import fr.in2p3.jsaga.impl.monitoring.MetricMode;
7 import fr.in2p3.jsaga.impl.monitoring.MetricType;
8 import org.ogf.saga.error.*;
9
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.*;
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public abstract class ServiceConfigAttribute implements AttributeVector {
26 private Map<String, Properties> m_values;
27
28 public ServiceConfigAttribute() {
29 m_values = new HashMap<String, Properties>();
30 }
31
32 public boolean isReadOnly() {
33 return false;
34 }
35
36 public MetricMode getMetricMode() {
37 return MetricMode.Final;
38 }
39
40 public MetricType getMetricType() {
41 return MetricType.String;
42 }
43
44 public Attribute clone() throws CloneNotSupportedException {
45 Class clazz;
46 try {
47 clazz = Class.forName(this.getClass().getName());
48 } catch (ClassNotFoundException e1) {
49 throw new CloneNotSupportedException();
50 }
51
52
53 java.lang.reflect.Constructor constructor;
54 try {
55 constructor = clazz.getConstructor();
56 } catch (SecurityException e) {
57 throw new CloneNotSupportedException();
58 } catch (NoSuchMethodException e) {
59 throw new CloneNotSupportedException();
60 }
61
62
63 ServiceConfigAttribute clone;
64 try {
65 clone = (ServiceConfigAttribute)constructor.newInstance();
66 } catch (IllegalArgumentException e) {
67 throw new CloneNotSupportedException();
68 } catch (InstantiationException e) {
69 throw new CloneNotSupportedException();
70 } catch (IllegalAccessException e) {
71 throw new CloneNotSupportedException();
72 } catch (InvocationTargetException e) {
73 throw new CloneNotSupportedException();
74 }
75 clone.m_values = m_values;
76 return clone;
77 }
78
79 public void setValues(String[] values) throws NotImplementedException, IncorrectStateException, PermissionDeniedException, BadParameterException {
80 m_values.clear();
81 for (String v : values) {
82 int equals = v.indexOf('=');
83 if (equals == -1) {
84 throw new BadParameterException("Syntax error in expression: "+v);
85 } else {
86
87 String service;
88 String attrKey;
89 String attrValue = v.substring(equals+1);
90 String remains = v.substring(0, equals);
91 int dot = remains.indexOf('.');
92 if (dot == -1) {
93 service = "*";
94 attrKey = remains;
95 } else {
96 service = remains.substring(0, dot);
97 attrKey = remains.substring(dot+1);
98 }
99 if ("".equals(service) || "".equals(attrKey)) {
100 throw new BadParameterException("Syntax error in expression: "+v);
101 }
102
103
104 Properties config = m_values.get(service);
105 if (config == null) {
106 config = new Properties();
107 m_values.put(service, config);
108 }
109 config.setProperty(attrKey, attrValue);
110 }
111 }
112 }
113
114 public String[] getValues() throws NotImplementedException, IncorrectStateException, NoSuccessException {
115 List<String> values = new ArrayList<String>();
116 for (String serviceKey : m_values.keySet()) {
117 Properties service = m_values.get(serviceKey);
118 for (Object attrKey : service.keySet()) {
119 String attrValue = service.getProperty((String) attrKey);
120 String v = serviceKey+"."+attrKey+"="+attrValue;
121 values.add(v);
122 }
123 }
124 return values.toArray(new String[values.size()]);
125 }
126
127 public Properties getServiceConfig(String scheme) {
128 if (m_values.containsKey("*")) {
129 Properties clone = new Properties();
130 clone.putAll(m_values.get("*"));
131 if(m_values.get(scheme) != null){
132 clone.putAll(m_values.get(scheme));
133 }
134 return clone;
135 } else {
136 return m_values.get(scheme);
137 }
138 }
139 }