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   * @author mkleint
35   * @since 2.0.9
36   */
37  public abstract class DefaultToolchain // should have been AbstractToolchain...
38  implements Toolchain, ToolchainPrivate {
39      private final Logger logger;
40  
41      private String type;
42  
43      private Map<String, RequirementMatcher> provides = new HashMap<>();
44  
45      public static final String KEY_TYPE = "type"; // NOI18N
46  
47      private ToolchainModel model;
48  
49      /**
50       *
51       * @param model the model, must not be {@code null}
52       * @param logger the logger, must not be {@code null}
53       */
54      protected DefaultToolchain(ToolchainModel model, Logger logger) {
55          this.model = model;
56          this.logger = logger;
57      }
58  
59      /**
60       *
61       * @param model the model, must not be {@code null}
62       * @param type the type
63       * @param logger the logger, must not be {@code null}
64       */
65      protected DefaultToolchain(ToolchainModel model, String type, Logger logger) {
66          this(model, logger);
67          this.type = type;
68      }
69  
70      @Override
71      public final String getType() {
72          return type != null ? type : model.getType();
73      }
74  
75      @Override
76      public final ToolchainModel getModel() {
77          return model;
78      }
79  
80      public final void addProvideToken(String type, RequirementMatcher matcher) {
81          provides.put(type, matcher);
82      }
83  
84      @Override
85      public boolean matchesRequirements(Map<String, String> requirements) {
86          for (Map.Entry<String, String> requirement : requirements.entrySet()) {
87              String key = requirement.getKey();
88  
89              RequirementMatcher matcher = provides.get(key);
90  
91              if (matcher == null) {
92                  getLog().debug("Toolchain " + this + " is missing required property: " + key);
93                  return false;
94              }
95              if (!matcher.matches(requirement.getValue())) {
96                  getLog().debug("Toolchain " + this + " doesn't match required property: " + key);
97                  return false;
98              }
99          }
100         return true;
101     }
102 
103     protected Logger getLog() {
104         return logger;
105     }
106 
107     @Override
108     public boolean equals(Object obj) {
109         if (obj == null) {
110             return false;
111         }
112 
113         if (this == obj) {
114             return true;
115         }
116 
117         if (!(obj instanceof DefaultToolchain)) {
118             return false;
119         }
120 
121         DefaultToolchain other = (DefaultToolchain) obj;
122 
123         if (!Objects.equals(type, other.type)) {
124             return false;
125         }
126 
127         Properties thisProvides = this.getModel().getProvides();
128         Properties otherProvides = other.getModel().getProvides();
129 
130         return Objects.equals(thisProvides, otherProvides);
131     }
132 
133     @Override
134     public int hashCode() {
135         int hashCode = (type == null) ? 0 : type.hashCode();
136 
137         if (this.getModel().getProvides() != null) {
138             hashCode = 31 * hashCode + this.getModel().getProvides().hashCode();
139         }
140         return hashCode;
141     }
142 
143     @Override
144     public String toString() {
145         StringBuilder builder = new StringBuilder();
146         builder.append("type:").append(getType());
147         builder.append('{');
148 
149         Iterator<Map.Entry<String, RequirementMatcher>> providesIter =
150                 provides.entrySet().iterator();
151         while (providesIter.hasNext()) {
152             Map.Entry<String, RequirementMatcher> provideEntry = providesIter.next();
153             builder.append(provideEntry.getKey()).append(" = ").append(provideEntry.getValue());
154             if (providesIter.hasNext()) {
155                 builder.append(';');
156             }
157         }
158 
159         builder.append('}');
160 
161         return builder.toString();
162     }
163 }