001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.core.module.id;
019:
020: import java.util.Iterator;
021: import java.util.LinkedHashMap;
022: import java.util.Map;
023:
024: import org.apache.ivy.plugins.matcher.MapMatcher;
025: import org.apache.ivy.util.Checks;
026: import org.apache.ivy.util.Message;
027: import org.apache.ivy.util.filter.Filter;
028: import org.apache.ivy.util.filter.NoFilter;
029:
030: /**
031: * A list of module specific rules.
032: * <p>
033: * This class defines a list of module specific rules. For each module only one rule apply,
034: * sometimes none.
035: * </p>
036: * <p>
037: * To know which rule to apply, they are configured using matchers. So you can define a rule
038: * applying to all module from one particular organization, or to all modules with a revisions
039: * matching a pattern, and so on.
040: * </p>
041: * <p>
042: * Rules condition are evaluated in order, so the first matching rule is returned.
043: * </p>
044: * <p>
045: * Rules themselves can be represented by any object, depending on the purpose of the rule (define
046: * which resolver to use, which TTL in cache, ...)
047: * </p>
048: */
049: public class ModuleRules {
050: private Map/*<MapMatcher,Object>*/rules = new LinkedHashMap();
051:
052: /**
053: * Defines a new rule for the given condition.
054: *
055: * @param condition
056: * the condition for which the rule should be applied. Must not be <code>null</code>.
057: * @param rule
058: * the rule to apply. Must not be <code>null</code>.
059: */
060: public void defineRule(MapMatcher condition, Object rule) {
061: Checks.checkNotNull(condition, "condition");
062: Checks.checkNotNull(rule, "rule");
063:
064: rules.put(condition, rule);
065: }
066:
067: /**
068: * Returns the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
069: * if no rule applies.
070: *
071: * @param mrid
072: * the {@link ModuleRevisionId} to search the rule for.
073: * Must not be <code>null</code>.
074: * @return the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
075: * if no rule applies.
076: * @see #getRule(ModuleRevisionId, Filter)
077: */
078: public Object getRule(ModuleRevisionId mrid) {
079: return getRule(mrid, NoFilter.INSTANCE);
080: }
081:
082: /**
083: * Returns the rule object matching the given {@link ModuleId} and accepted by the given
084: * {@link Filter}, or <code>null</code> if no rule applies.
085: *
086: * @param mrid
087: * the {@link ModuleRevisionId} to search the rule for.
088: * Must not be <code>null</code>.
089: * @param filter
090: * the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
091: * method will be called only with rule objects matching the given
092: * {@link ModuleId}, and the first rule object accepted by the filter will
093: * be returned. Must not be <code>null</code>.
094: * @return the rule object matching the given {@link ModuleId}, or <code>null</code>
095: * if no rule applies.
096: * @see #getRule(ModuleRevisionId, Filter)
097: */
098: public Object getRule(ModuleId mid, Filter filter) {
099: Checks.checkNotNull(mid, "mid");
100: return getRule(new ModuleRevisionId(mid, "", ""), filter);
101: }
102:
103: /**
104: * Returns the rule object matching the given {@link ModuleRevisionId} and accepted by the given
105: * {@link Filter}, or <code>null</code> if no rule applies.
106: *
107: * @param mrid
108: * the {@link ModuleRevisionId} to search the rule for.
109: * Must not be <code>null</code>.
110: * @param filter
111: * the filter to use to filter the rule to return. The {@link Filter#accept(Object)}
112: * method will be called only with rule objects matching the given
113: * {@link ModuleRevisionId}, and the first rule object accepted by the filter will
114: * be returned. Must not be <code>null</code>.
115: * @return the rule object matching the given {@link ModuleRevisionId}, or <code>null</code>
116: * if no rule applies.
117: * @see #getRule(ModuleRevisionId)
118: */
119: public Object getRule(ModuleRevisionId mrid, Filter filter) {
120: Checks.checkNotNull(mrid, "mrid");
121: Checks.checkNotNull(filter, "filter");
122:
123: for (Iterator iter = rules.keySet().iterator(); iter.hasNext();) {
124: MapMatcher midm = (MapMatcher) iter.next();
125: if (midm.matches(mrid.getAttributes())) {
126: Object rule = rules.get(midm);
127: if (filter.accept(rule)) {
128: return rule;
129: }
130: }
131: }
132: return null;
133: }
134:
135: /**
136: * Dump the list of rules to {@link Message#debug(String)}
137: *
138: * @param prefix
139: * the prefix to use for each line dumped
140: */
141: public void dump(String prefix) {
142: if (rules.isEmpty()) {
143: Message.debug(prefix + "NONE");
144: } else {
145: for (Iterator iter = rules.keySet().iterator(); iter
146: .hasNext();) {
147: MapMatcher midm = (MapMatcher) iter.next();
148: Object rule = rules.get(midm);
149: Message.debug(prefix + midm + " -> " + rule);
150: }
151: }
152: }
153:
154: }
|