View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.toolchain;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.Objects;
25  import java.util.Properties;
26  
27  import org.apache.maven.toolchain.model.ToolchainModel;
28  import org.slf4j.Logger;
29  
30  /**
31   * Default abstract toolchain implementation, to be used as base class for any toolchain implementation
32   * to avoid rewriting usual code.
33   *
34   * @since 2.0.9
35   */
36  public abstract class DefaultToolchain // should have been AbstractToolchain...
37  implements Toolchain, ToolchainPrivate {
38      private final Logger logger;
39  
40      private String type;
41  
42      private Map<String, RequirementMatcher> provides = new HashMap<>();
43  
44      public static final String KEY_TYPE = "type"; // NOI18N
45  
46      private ToolchainModel model;
47  
48      /**
49       *
50       * @param model the model, must not be {@code null}
51       * @param logger the logger, must not be {@code null}
52       */
53      protected DefaultToolchain(ToolchainModel model, Logger logger) {
54          this.model = model;
55          this.logger = logger;
56      }
57  
58      /**
59       *
60       * @param model the model, must not be {@code null}
61       * @param type the type
62       * @param logger the logger, must not be {@code null}
63       */
64      protected DefaultToolchain(ToolchainModel model, String type, Logger logger) {
65          this(model, logger);
66          this.type = type;
67      }
68  
69      @Override
70      public final String getType() {
71          return type != null ? type : model.getType();
72      }
73  
74      @Override
75      public final ToolchainModel getModel() {
76          return model;
77      }
78  
79      public final void addProvideToken(String type, RequirementMatcher matcher) {
80          provides.put(type, matcher);
81      }
82  
83      @Override
84      public boolean matchesRequirements(Map<String, String> requirements) {
85          for (Map.Entry<String, String> requirement : requirements.entrySet()) {
86              String key = requirement.getKey();
87  
88              RequirementMatcher matcher = provides.get(key);
89  
90              if (matcher == null) {
91                  getLog().debug("Toolchain {} is missing required property: {}", this, key);
92                  return false;
93              }
94              if (!matcher.matches(requirement.getValue())) {
95                  getLog().debug("Toolchain {} doesn't match required property: {}", this, key);
96                  return false;
97              }
98          }
99          return true;
100     }
101 
102     protected Logger getLog() {
103         return logger;
104     }
105 
106     @Override
107     public boolean equals(Object obj) {
108         if (obj == null) {
109             return false;
110         }
111 
112         if (this == obj) {
113             return true;
114         }
115 
116         if (!(obj instanceof DefaultToolchain)) {
117             return false;
118         }
119 
120         DefaultToolchain other = (DefaultToolchain) obj;
121 
122         if (!Objects.equals(type, other.type)) {
123             return false;
124         }
125 
126         Properties thisProvides = this.getModel().getProvides();
127         Properties otherProvides = other.getModel().getProvides();
128 
129         return Objects.equals(thisProvides, otherProvides);
130     }
131 
132     @Override
133     public int hashCode() {
134         int hashCode = (type == null) ? 0 : type.hashCode();
135 
136         if (this.getModel().getProvides() != null) {
137             hashCode = 31 * hashCode + this.getModel().getProvides().hashCode();
138         }
139         return hashCode;
140     }
141 
142     @Override
143     public String toString() {
144         StringBuilder builder = new StringBuilder();
145         builder.append("type:").append(getType());
146         builder.append('{');
147 
148         Iterator<Map.Entry<String, RequirementMatcher>> providesIter =
149                 provides.entrySet().iterator();
150         while (providesIter.hasNext()) {
151             Map.Entry<String, RequirementMatcher> provideEntry = providesIter.next();
152             builder.append(provideEntry.getKey()).append(" = ").append(provideEntry.getValue());
153             if (providesIter.hasNext()) {
154                 builder.append(';');
155             }
156         }
157 
158         builder.append('}');
159 
160         return builder.toString();
161     }
162 }