1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.nextgrid.api.ram;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 import nextgrid.api.env.DiscoveryException;
25 import nextgrid.api.pom.AbstractProcess;
26 import nextgrid.api.pom.Process;
27 import nextgrid.api.pom.Query;
28 import nextgrid.api.pom.QueryProfile;
29 import nextgrid.api.ram.Discoverer;
30 import nextgrid.api.ram.Registry;
31
32
33
34
35
36
37 public class DefaultDiscoverer implements Discoverer {
38
39
40
41
42 private static final long serialVersionUID = -1271543363150251301L;
43
44
45
46
47 private final List<Registry> registries;
48
49
50
51
52 private boolean merge = true;
53
54
55
56
57 public DefaultDiscoverer() {
58 this(new ArrayList<Registry>(), true);
59 }
60
61
62
63
64
65
66
67 public DefaultDiscoverer(boolean merge) {
68 this(new ArrayList<Registry>(), merge);
69 }
70
71
72
73
74
75
76
77
78
79
80 public DefaultDiscoverer(List<Registry> registries, boolean merge) {
81 this.registries = registries;
82 this.merge = merge;
83 }
84
85
86
87
88 public List<Registry> getRegistries() {
89 return this.registries;
90 }
91
92
93
94
95
96
97 public void setMergeEnabled(boolean merge) {
98 this.merge = merge;
99 }
100
101
102
103
104
105
106 public boolean isMergeEnabled() {
107 return this.merge;
108 }
109
110
111
112
113
114
115 public void addRegistry(Registry reg) {
116 this.registries.add(reg);
117 }
118
119
120
121
122 public Collection<Process> discover(AbstractProcess process)
123 throws DiscoveryException {
124
125 if (process == null) {
126 throw new DiscoveryException("null process");
127 }
128
129 return discover(process.getProfile());
130 }
131
132
133
134
135 public Collection<Process> discover(QueryProfile profile)
136 throws DiscoveryException {
137
138 if (profile == null) {
139 throw new DiscoveryException("null profile");
140 }
141
142 final Collection<Process> results = new ArrayList<Process>();
143
144 for (final Registry r : this.registries) {
145 final Collection<Process> col = search(profile, r);
146 if (col != null) {
147 if (merge) {
148 results.addAll(col);
149 } else {
150 return col;
151 }
152 }
153 }
154
155 return results;
156 }
157
158
159
160
161
162
163
164
165
166 private Collection<Process> search(QueryProfile profile, Registry r)
167 throws DiscoveryException {
168
169 for (final Query query : profile.getConstraints()) {
170 if (r.supports(query.getLang())) {
171 return r.query(query.getText(), query.getLang());
172 }
173 }
174
175 return null;
176 }
177 }