2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.ivy.core.module.id;
20
import java.util.ArrayList;
21
import java.util.Collections;
22
import java.util.Iterator;
23
import java.util.LinkedHashMap;
24
import java.util.List;
26
import java.util.Map.Entry;
28
import org.apache.ivy.plugins.matcher.MapMatcher;
29
import org.apache.ivy.util.Checks;
30
import org.apache.ivy.util.Message;
31
import org.apache.ivy.util.filter.Filter;
32
import org.apache.ivy.util.filter.NoFilter;
35
* A list of module specific rules.
37
* This class defines a list of module specific rules. For each module only one rule apply,
41
* To know which rule to apply, they are configured using matchers. So you can define a rule
42
* applying to all module from one particular organization, or to all modules with a revisions
43
* matching a pattern, and so on.
46
* Rules condition are evaluated in order, so the first matching rule is returned.
49
* Rules themselves can be represented by any object, depending on the purpose of the rule (define
50
* which resolver to use, which TTL in cache, ...)
53
public class ModuleRules {
54
private Map/*<MapMatcher,Object>*/ rules = new LinkedHashMap();
57
* Constructs an empty ModuleRules.
59
public ModuleRules() {
62
private ModuleRules(Map/*<MapMatcher,Object>*/ rules) {
63
this.rules = new LinkedHashMap(rules);
67
* Defines a new rule for the given condition.
70
* the condition for which the rule should be applied. Must not be <code>null</code>.
72
* the rule to apply. Must not be <code>null</code>.
74
public void defineRule(MapMatcher condition, Object rule) {
75
Checks.checkNotNull(condition, "condition");
76
Checks.checkNotNull(rule, "rule");
78
rules.put(condition, rule);
82
* Returns the rule object matching the given {@link ModuleId}, or <code>null</code>
86
* the {@link ModuleId} to search the rule for.
87
* Must not be <code>null</code>.
88
* @return the rule object matching the given {@link ModuleId}, or <code>null</code>
90
* @see #getRule(ModuleId, Filter)
92
public Object getRule(ModuleId mid) {
93
return getRule(mid, NoFilter.INSTANCE);
97
* Returns the rules objects matching the given {@link ModuleId}, or an empty array
101
* the {@link ModuleId} to search the rule for.
102
* Must not be <code>null</code>.
103
* @return an array of rule objects matching the given {@link ModuleId}.
105
public Object[] getRules(ModuleId mid) {
106
return getRules(mid.getAttributes(), NoFilter.INSTANCE);
110
* Returns the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
111
* if no rule applies.
114
* the {@link ModuleRevisionId} to search the rule for.
115
* Must not be <code>null</code>.
116
* @return the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
117
* if no rule applies.
118
* @see #getRule(ModuleRevisionId, Filter)
120
public Object getRule(ModuleRevisionId mrid) {
121
return getRule(mrid, NoFilter.INSTANCE);
125
* Returns the rule object matching the given {@link ModuleId} and accepted by the given
126
* {@link Filter}, or <code>null</code> if no rule applies.
129
* the {@link ModuleRevisionId} to search the rule for.
130
* Must not be <code>null</code>.
132
* the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
133
* method will be called only with rule objects matching the given
134
* {@link ModuleId}, and the first rule object accepted by the filter will
135
* be returned. Must not be <code>null</code>.
136
* @return the rule object matching the given {@link ModuleId}, or <code>null</code>
137
* if no rule applies.
138
* @see #getRule(ModuleRevisionId, Filter)
140
public Object getRule(ModuleId mid, Filter filter) {
141
Checks.checkNotNull(mid, "mid");
142
return getRule(mid.getAttributes(), filter);
146
* Returns the rule object matching the given {@link ModuleRevisionId} and accepted by the given
147
* {@link Filter}, or <code>null</code> if no rule applies.
150
* the {@link ModuleRevisionId} to search the rule for.
151
* Must not be <code>null</code>.
153
* the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
154
* method will be called only with rule objects matching the given
155
* {@link ModuleRevisionId}, and the first rule object accepted by the filter will
156
* be returned. Must not be <code>null</code>.
157
* @return the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
158
* if no rule applies.
159
* @see #getRule(ModuleRevisionId)
161
public Object getRule(ModuleRevisionId mrid, Filter filter) {
162
Checks.checkNotNull(mrid, "mrid");
163
Checks.checkNotNull(filter, "filter");
164
Map moduleAttributes = mrid.getAttributes();
165
return getRule(moduleAttributes, filter);
168
private Object getRule(Map moduleAttributes, Filter filter) {
169
for (Iterator iter = rules.entrySet().iterator(); iter.hasNext();) {
170
Map.Entry ruleEntry = (Entry) iter.next();
171
MapMatcher midm = (MapMatcher) ruleEntry.getKey();
172
if (midm.matches(moduleAttributes)) {
173
Object rule = ruleEntry.getValue();
174
if (filter.accept(rule)) {
186
* Returns the rules object matching the given {@link ModuleRevisionId} and accepted by the
187
* given {@link Filter}, or an empty array if no rule applies.
190
* the {@link ModuleRevisionId} to search the rule for.
191
* Must not be <code>null</code>.
193
* the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
194
* method will be called only with rule objects matching the given
195
* {@link ModuleRevisionId}. Must not be <code>null</code>.
196
* @return an array of rule objects matching the given {@link ModuleRevisionId}.
198
public Object[] getRules(ModuleRevisionId mrid, Filter filter) {
199
Checks.checkNotNull(mrid, "mrid");
200
Checks.checkNotNull(filter, "filter");
201
Map moduleAttributes = mrid.getAttributes();
202
return getRules(moduleAttributes, filter);
206
private Object[] getRules(Map moduleAttributes, Filter filter) {
207
List matchingRules = new ArrayList();
208
for (Iterator iter = rules.entrySet().iterator(); iter.hasNext();) {
209
Map.Entry ruleEntry = (Entry) iter.next();
210
MapMatcher midm = (MapMatcher) ruleEntry.getKey();
211
if (midm.matches(moduleAttributes)) {
212
Object rule = ruleEntry.getValue();
213
if (filter.accept(rule)) {
214
matchingRules.add(rule);
218
return matchingRules.toArray();
222
* Dump the list of rules to {@link Message#debug(String)}
225
* the prefix to use for each line dumped
227
public void dump(String prefix) {
228
if (rules.isEmpty()) {
229
Message.debug(prefix + "NONE");
231
for (Iterator iter = rules.keySet().iterator(); iter.hasNext();) {
232
MapMatcher midm = (MapMatcher) iter.next();
233
Object rule = rules.get(midm);
234
Message.debug(prefix + midm + " -> " + rule);
240
* Returns an unmodifiable view of all the rules defined on this ModuleRules.
242
* The rules are returned in a Map where they keys are the MapMatchers matching the rules
243
* object, and the values are the rules object themselves.
246
* @return an unmodifiable view of all the rules defined on this ModuleRules.
248
public Map/*<MapMatcher,Object>*/ getAllRules() {
249
return Collections.unmodifiableMap(rules);
252
public Object clone() {
253
return new ModuleRules(rules);