001    package 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    
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.Map;
025    
026    import org.apache.maven.toolchain.model.ToolchainModel;
027    import org.codehaus.plexus.logging.Logger;
028    
029    /**
030     *
031     * @author mkleint
032     */
033    public abstract class DefaultToolchain
034        implements Toolchain, ToolchainPrivate
035    {
036    
037        private String type;
038    
039        private Map<String, RequirementMatcher> provides = new HashMap<String, RequirementMatcher>();
040    
041        public static final String KEY_TYPE = "type"; //NOI18N
042    
043        private ToolchainModel model;
044    
045        private Logger logger;
046    
047        protected DefaultToolchain( ToolchainModel model, Logger logger )
048        {
049            this.model = model;
050    
051            this.logger = logger;
052        }
053    
054        protected DefaultToolchain( ToolchainModel model, String type, Logger logger )
055        {
056            this( model, logger );
057            this.type = type;
058        }
059    
060        public final String getType()
061        {
062            return type != null ? type : model.getType();
063        }
064    
065    
066        public final ToolchainModel getModel()
067        {
068            return model;
069        }
070    
071        public final void addProvideToken( String type, RequirementMatcher matcher )
072        {
073            provides.put( type, matcher );
074        }
075    
076    
077        public boolean matchesRequirements( Map requirements )
078        {
079            Iterator it = requirements.keySet().iterator();
080            while ( it.hasNext() )
081            {
082                String key = (String) it.next();
083    
084                RequirementMatcher matcher = provides.get( key );
085    
086                if ( matcher == null )
087                {
088                    getLog().debug( "Toolchain " + this + " is missing required property: " + key );
089                    return false;
090                }
091                if ( !matcher.matches( (String) requirements.get( key ) ) )
092                {
093                    getLog().debug( "Toolchain " + this + " doesn't match required property: " + key );
094                    return false;
095                }
096            }
097            return true;
098        }
099    
100        protected Logger getLog()
101        {
102            return logger;
103        }
104    }