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