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