001package org.apache.maven.toolchain; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.HashMap; 023import java.util.Map; 024import java.util.Properties; 025 026import org.apache.maven.toolchain.model.ToolchainModel; 027import org.codehaus.plexus.logging.Logger; 028 029/** 030 * Default abstract toolchain implementation, to be used as base class for any toolchain implementation 031 * to avoid rewriting usual code. 032 * 033 * @author mkleint 034 * @since 2.0.9 035 */ 036public abstract class DefaultToolchain // should have been AbstractToolchain... 037 implements Toolchain, ToolchainPrivate 038{ 039 040 private String type; 041 042 private Map<String, RequirementMatcher> provides = new HashMap<String, RequirementMatcher>(); 043 044 public static final String KEY_TYPE = "type"; //NOI18N 045 046 private ToolchainModel model; 047 048 private Logger logger; 049 050 protected DefaultToolchain( ToolchainModel model, Logger logger ) 051 { 052 this.model = model; 053 054 this.logger = logger; 055 } 056 057 protected DefaultToolchain( ToolchainModel model, String type, Logger logger ) 058 { 059 this( model, logger ); 060 this.type = type; 061 } 062 063 @Override 064 public final String getType() 065 { 066 return type != null ? type : model.getType(); 067 } 068 069 @Override 070 public final ToolchainModel getModel() 071 { 072 return model; 073 } 074 075 public final void addProvideToken( String type, RequirementMatcher matcher ) 076 { 077 provides.put( type, matcher ); 078 } 079 080 @Override 081 public boolean matchesRequirements( Map<String, String> requirements ) 082 { 083 for ( Map.Entry<String, String> requirement : requirements.entrySet() ) 084 { 085 String key = requirement.getKey(); 086 087 RequirementMatcher matcher = provides.get( key ); 088 089 if ( matcher == null ) 090 { 091 getLog().debug( "Toolchain " + this + " is missing required property: " + key ); 092 return false; 093 } 094 if ( !matcher.matches( requirement.getValue() ) ) 095 { 096 getLog().debug( "Toolchain " + this + " doesn't match required property: " + key ); 097 return false; 098 } 099 } 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}