001package org.apache.maven.rtinfo.internal;
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.io.IOException;
023import java.io.InputStream;
024import java.util.Properties;
025
026import org.apache.maven.rtinfo.RuntimeInformation;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029import org.codehaus.plexus.logging.Logger;
030import org.codehaus.plexus.util.IOUtil;
031import org.codehaus.plexus.util.StringUtils;
032import org.eclipse.aether.util.version.GenericVersionScheme;
033import org.eclipse.aether.version.InvalidVersionSpecificationException;
034import org.eclipse.aether.version.Version;
035import org.eclipse.aether.version.VersionConstraint;
036import org.eclipse.aether.version.VersionScheme;
037
038/**
039 * Provides information about the current Maven runtime.
040 */
041@Component( role = RuntimeInformation.class )
042public class DefaultRuntimeInformation
043    implements RuntimeInformation
044{
045
046    @Requirement
047    private Logger logger;
048
049    private String mavenVersion;
050
051    public String getMavenVersion()
052    {
053        if ( mavenVersion == null )
054        {
055            Properties props = new Properties();
056
057            String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
058
059            InputStream is = DefaultRuntimeInformation.class.getResourceAsStream( "/" + resource );
060            if ( is != null )
061            {
062                try
063                {
064                    props.load( is );
065                }
066                catch ( IOException e )
067                {
068                    String msg = "Could not parse " + resource + ", Maven runtime information not available";
069                    if ( logger.isDebugEnabled() )
070                    {
071                        logger.warn( msg, e );
072                    }
073                    else
074                    {
075                        logger.warn( msg );
076                    }
077                }
078                finally
079                {
080                    IOUtil.close( is );
081                }
082            }
083            else
084            {
085                logger.warn( "Could not locate " + resource
086                             + " on classpath, Maven runtime information not available" );
087            }
088
089            String version = props.getProperty( "version", "" ).trim();
090
091            if ( !version.startsWith( "${" ) )
092            {
093                mavenVersion = version;
094            }
095            else
096            {
097                mavenVersion = "";
098            }
099        }
100
101        return mavenVersion;
102    }
103
104    public boolean isMavenVersion( String versionRange )
105    {
106        VersionScheme versionScheme = new GenericVersionScheme();
107
108        if ( versionRange == null )
109        {
110            throw new IllegalArgumentException( "Version range must not be null" );
111        }
112        if ( StringUtils.isBlank( versionRange ) )
113        {
114            throw new IllegalArgumentException( "Version range must not be empty" );
115        }
116
117        VersionConstraint constraint;
118        try
119        {
120            constraint = versionScheme.parseVersionConstraint( versionRange );
121        }
122        catch ( InvalidVersionSpecificationException e )
123        {
124            throw new IllegalArgumentException( e.getMessage(), e );
125        }
126
127        Version current;
128        try
129        {
130            String mavenVersion = getMavenVersion();
131            if ( mavenVersion.length() <= 0 )
132            {
133                throw new IllegalStateException( "Could not determine current Maven version" );
134            }
135
136            current = versionScheme.parseVersion( mavenVersion );
137        }
138        catch ( InvalidVersionSpecificationException e )
139        {
140            throw new IllegalStateException( "Could not parse current Maven version: " + e.getMessage(), e );
141        }
142
143        if ( constraint.getRange() == null )
144        {
145            return constraint.getVersion().compareTo( current ) <= 0;
146        }
147        return constraint.containsVersion( current );
148    }
149
150}