1 package fr.in2p3.jsaga.impl.context.attrs;
2
3 import fr.in2p3.jsaga.engine.session.BaseUrlItem;
4 import fr.in2p3.jsaga.engine.session.BaseUrlPattern;
5 import fr.in2p3.jsaga.engine.session.item.SchemeItem;
6 import fr.in2p3.jsaga.generated.parser.BaseUrlParser;
7 import fr.in2p3.jsaga.generated.parser.ParseException;
8 import fr.in2p3.jsaga.impl.attributes.Attribute;
9 import fr.in2p3.jsaga.impl.attributes.AttributeVector;
10 import fr.in2p3.jsaga.impl.monitoring.MetricMode;
11 import fr.in2p3.jsaga.impl.monitoring.MetricType;
12 import org.ogf.saga.error.*;
13
14 import java.util.*;
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class BaseUrlPatternAttribute implements AttributeVector {
29 private String m_key;
30 private String m_description;
31 private BaseUrlPattern[] m_values;
32 private Properties m_aliases;
33
34 public BaseUrlPatternAttribute(String key, String description) {
35 m_key = key;
36 m_description = description;
37 m_values = new BaseUrlPattern[]{};
38 m_aliases = new Properties();
39 }
40
41 public String getKey() {
42 return m_key;
43 }
44
45 public String getDescription() {
46 return m_description;
47 }
48
49 public boolean isReadOnly() {
50 return false;
51 }
52
53 public MetricMode getMetricMode() {
54 return MetricMode.Final;
55 }
56
57 public MetricType getMetricType() {
58 return MetricType.String;
59 }
60
61 public Attribute clone() throws CloneNotSupportedException {
62 BaseUrlPatternAttribute clone = new BaseUrlPatternAttribute(m_key, m_description);
63 clone.m_values = m_values;
64 return clone;
65 }
66
67 public void setValues(String[] values) throws NotImplementedException, IncorrectStateException, PermissionDeniedException, BadParameterException {
68
69 List<BaseUrlPattern> list = new ArrayList<BaseUrlPattern>();
70 for (String v : values) {
71 List<String> splitted = split(v);
72 for (String url : splitted) {
73 try {
74 BaseUrlItem[] items = BaseUrlParser.parse(url);
75 list.add(new BaseUrlPattern(items));
76 } catch (ParseException e) {
77 throw new BadParameterException(e);
78 }
79 }
80 }
81 m_values = list.toArray(new BaseUrlPattern[list.size()]);
82
83
84 for (BaseUrlPattern p : list) {
85 SchemeItem item = (SchemeItem) p.getItems()[0];
86 if (item.getSchemeOrNull() != null) {
87 m_aliases.setProperty(item.getValue(), item.getSchemeOrNull());
88 }
89 }
90 }
91
92 public String[] getValues() throws NotImplementedException, IncorrectStateException, NoSuccessException {
93 String[] values = new String[m_values.length];
94 for (int i=0; i<m_values.length; i++) {
95 values[i] = m_values[i].toString();
96 }
97 return values;
98 }
99
100 public void throwIfConflictsWith(String currentLabel, String refLabel, BaseUrlPatternAttribute refIncludes, BaseUrlPatternAttribute refExcludes, BaseUrlPatternAttribute currentExcludes) throws NoSuccessException {
101 mainloop: for (BaseUrlPattern pattern : m_values) {
102
103 for (BaseUrlPattern currentPattern: currentExcludes.m_values) {
104 if (currentPattern.conflictsWith(currentPattern)) {
105 continue mainloop;
106 }
107 }
108 for (BaseUrlPattern refPattern: refExcludes.m_values) {
109 if (pattern.conflictsWith(refPattern)) {
110 continue mainloop;
111 }
112 }
113
114 for (BaseUrlPattern refPattern : refIncludes.m_values) {
115 if (pattern.conflictsWith(refPattern)) {
116 throw new NoSuccessException("Pattern '"+pattern.toString()+"' of context "+currentLabel+
117 " conflicts with pattern '"+refPattern.toString()+"' of context "+refLabel);
118 }
119 }
120 }
121 }
122
123 public boolean matches(String url) {
124 for (BaseUrlPattern pattern : m_values) {
125 if (pattern.matches(url)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 public String getSchemeFromAlias(String alias) {
133 return m_aliases.getProperty(alias);
134 }
135
136 private static List<String> split(String url) {
137 List<String> list = new ArrayList<String>();
138 int open = url.indexOf('{');
139 if (open > -1) {
140 String begin = url.substring(0, open);
141 String remains = url.substring(open+1);
142 int close = remains.indexOf('}');
143 if (close > -1) {
144 String or = remains.substring(0, close);
145 String end = remains.substring(close+1);
146
147 for (String child : or.split(",")) {
148 String childUrl = begin.trim()+child.trim()+end.trim();
149 childUrl = childUrl.replaceAll("\\*\\*", "*");
150 List<String> childList = split(childUrl);
151 list.addAll(childList);
152 }
153 }
154 } else {
155 list.add(url);
156 }
157 return list;
158 }
159 }