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