1 /*
2 Copyright (C) 2006 Grid Systems, S.A.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 * Type description.
34 *
35 * @author Rodrigo Ruiz
36 */
37 public class DefaultDiscoverer implements Discoverer {
38
39 /**
40 * <code>serialVersionUID</code> attribute.
41 */
42 private static final long serialVersionUID = -1271543363150251301L;
43
44 /**
45 * List of associated registries.
46 */
47 private final List<Registry> registries;
48
49 /**
50 * Flag for "results merging".
51 */
52 private boolean merge = true;
53
54 /**
55 * Creates a new instance with a default list implementation (ArrayList).
56 */
57 public DefaultDiscoverer() {
58 this(new ArrayList<Registry>(), true);
59 }
60
61 /**
62 * Creates a new instance with a default list implementation (ArrayList), and
63 * the specified merge flag value.
64 *
65 * @param merge The initial merge flag value
66 */
67 public DefaultDiscoverer(boolean merge) {
68 this(new ArrayList<Registry>(), merge);
69 }
70
71 /**
72 * Creates a new instance.
73 * <p>
74 * Using this constructor the programmer can specify a list implementation
75 * with a custom iteration policy, or with a different performance profile.
76 *
77 * @param registries A list instance for storing registries
78 * @param merge Results merge flag
79 */
80 public DefaultDiscoverer(List<Registry> registries, boolean merge) {
81 this.registries = registries;
82 this.merge = merge;
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 public List<Registry> getRegistries() {
89 return this.registries;
90 }
91
92 /**
93 * Sets the merge flag value.
94 *
95 * @param merge The new flag value
96 */
97 public void setMergeEnabled(boolean merge) {
98 this.merge = merge;
99 }
100
101 /**
102 * Gets the merge flag value.
103 *
104 * @return The flag value
105 */
106 public boolean isMergeEnabled() {
107 return this.merge;
108 }
109
110 /**
111 * Adds a new registry to the managed list.
112 *
113 * @param reg The registry to add
114 */
115 public void addRegistry(Registry reg) {
116 this.registries.add(reg);
117 }
118
119 /**
120 * {@inheritDoc}
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 * {@inheritDoc}
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 * Performs a semantic search.
160 *
161 * @param profile The profile to search for
162 * @param r The registry to scan
163 * @return A collection containing all found processes
164 * @throws DiscoveryException If an error occurs
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 }