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.Map;
24  import java.util.Properties;
25  
26  import org.apache.maven.toolchain.model.ToolchainModel;
27  import org.codehaus.plexus.logging.Logger;
28  
29  /**
30   * Default abstract toolchain implementation, to be used as base class for any toolchain implementation
31   * to avoid rewriting usual code.
32   *
33   * @author mkleint
34   * @since 2.0.9
35   */
36  public abstract class DefaultToolchain // should have been AbstractToolchain...
37      implements Toolchain, ToolchainPrivate
38  {
39  
40      private String type;
41  
42      private Map<String, RequirementMatcher> provides = new HashMap<String, RequirementMatcher>();
43  
44      public static final String KEY_TYPE = "type"; //NOI18N
45  
46      private ToolchainModel model;
47  
48      private Logger logger;
49  
50      protected DefaultToolchain( ToolchainModel model, Logger logger )
51      {
52          this.model = model;
53  
54          this.logger = logger;
55      }
56  
57      protected DefaultToolchain( ToolchainModel model, String type, Logger logger )
58      {
59          this( model, logger );
60          this.type = type;
61      }
62  
63      @Override
64      public final String getType()
65      {
66          return type != null ? type : model.getType();
67      }
68  
69      @Override
70      public final ToolchainModel getModel()
71      {
72          return model;
73      }
74  
75      public final void addProvideToken( String type, RequirementMatcher matcher )
76      {
77          provides.put( type, matcher );
78      }
79  
80      @Override
81      public boolean matchesRequirements( Map<String, String> requirements )
82      {
83          for ( Map.Entry<String, String> requirement : requirements.entrySet() )
84          {
85              String key = requirement.getKey();
86  
87              RequirementMatcher matcher = provides.get( key );
88  
89              if ( matcher == null )
90              {
91                  getLog().debug( "Toolchain " + this + " is missing required property: " + key );
92                  return false;
93              }
94              if ( !matcher.matches( requirement.getValue() ) )
95              {
96                  getLog().debug( "Toolchain " + this + " doesn't match required property: " + key );
97                  return false;
98              }
99          }
100         return true;
101     }
102 
103     protected Logger getLog()
104     {
105         return logger;
106     }
107 
108     @Override
109     public boolean equals( Object obj )
110     {
111         if ( obj == null )
112         {
113             return false;
114         }
115 
116         if ( this == obj )
117         {
118             return true;
119         }
120 
121         if ( !( obj instanceof DefaultToolchain ) )
122         {
123             return false;
124         }
125 
126         DefaultToolchain other = (DefaultToolchain) obj;
127 
128         if ( type == null ? other.type != null : !type.equals( other.type ) )
129         {
130             return false;
131         }
132 
133         Properties thisProvides = this.getModel().getProvides();
134         Properties otherProvides = other.getModel().getProvides();
135 
136         if ( thisProvides == null ? otherProvides != null : !thisProvides.equals( otherProvides ) )
137         {
138             return false;
139         }
140 
141         return true;
142     }
143 
144     @Override
145     public int hashCode()
146     {
147         int hashCode = ( type == null ) ? 0 : type.hashCode();
148 
149         if ( this.getModel().getProvides() != null )
150         {
151             hashCode = 31 * hashCode + this.getModel().getProvides().hashCode();
152         }
153         return hashCode;
154     }
155 }