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   * @deprecated Use {@link org.apache.maven.api.Toolchain} instead.
36   */
37  @Deprecated(since = "4.0.0")
38  public abstract class DefaultToolchain // should have been AbstractToolchain...
39  implements Toolchain, ToolchainPrivate {
40      private final Logger logger;
41  
42      private String type;
43  
44      private Map<String, RequirementMatcher> provides = new HashMap<>();
45  
46      public static final String KEY_TYPE = "type"; // NOI18N
47  
48      private ToolchainModel model;
49  
50      /**
51       *
52       * @param model the model, must not be {@code null}
53       * @param logger the logger, must not be {@code null}
54       */
55      protected DefaultToolchain(ToolchainModel model, Logger logger) {
56          this.model = model;
57          this.logger = logger;
58      }
59  
60      /**
61       *
62       * @param model the model, must not be {@code null}
63       * @param type the type
64       * @param logger the logger, must not be {@code null}
65       */
66      protected DefaultToolchain(ToolchainModel model, String type, Logger logger) {
67          this(model, logger);
68          this.type = type;
69      }
70  
71      @Override
72      public final String getType() {
73          return type != null ? type : model.getType();
74      }
75  
76      @Override
77      public final ToolchainModel getModel() {
78          return model;
79      }
80  
81      public final void addProvideToken(String type, RequirementMatcher matcher) {
82          provides.put(type, matcher);
83      }
84  
85      @Override
86      public boolean matchesRequirements(Map<String, String> requirements) {
87          for (Map.Entry<String, String> requirement : requirements.entrySet()) {
88              String key = requirement.getKey();
89  
90              RequirementMatcher matcher = provides.get(key);
91  
92              if (matcher == null) {
93                  getLog().debug("Toolchain {} is missing required property: {}", this, key);
94                  return false;
95              }
96              if (!matcher.matches(requirement.getValue())) {
97                  getLog().debug("Toolchain {} doesn't match required property: {}", this, key);
98                  return false;
99              }
100         }
101         return true;
102     }
103 
104     protected Logger getLog() {
105         return logger;
106     }
107 
108     @Override
109     public boolean equals(Object obj) {
110         if (obj == null) {
111             return false;
112         }
113 
114         if (this == obj) {
115             return true;
116         }
117 
118         if (obj instanceof DefaultToolchain other) {
119             if (!Objects.equals(type, other.type)) {
120                 return false;
121             }
122 
123             Properties thisProvides = this.getModel().getProvides();
124             Properties otherProvides = other.getModel().getProvides();
125 
126             return Objects.equals(thisProvides, otherProvides);
127         } else {
128             return false;
129         }
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 }