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