org.codehaus.mojo.license.model.LicenseRepository Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of license-maven-plugin Show documentation
Show all versions of license-maven-plugin Show documentation
Maven plugin to download and collect license files from project dependencies.
/**
* Copyright 2010-2012 The Kuali Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ecl2.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.mojo.license.model;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.mojo.license.MojoHelper;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author tchemit
* @since 1.0
*/
public class LicenseRepository
implements Iterable
{
/**
* Logger
*/
private static final Log log = LogFactory.getLog( LicenseRepository.class );
public static final String REPOSITORY_DEFINITION_FILE = "licenses.properties";
public static final Pattern LICENSE_DESCRIPTION_PATTERN =
Pattern.compile( "(.*)\\s*~~\\s*license\\s*:\\s*(.*)\\s*~~\\s*header\\s*:\\s*(.*)\\s*" );
/**
* the base url of the licenses repository
*/
protected URL baseURL;
/**
* licenses of this repository
*/
protected List licenses;
/**
* flag to known if repository was init (pass to {@code true} when invoking
* the method {@link #load()}).
*/
protected boolean init;
public LicenseRepository()
{
}
public URL getBaseURL()
{
return baseURL;
}
public void setBaseURL( URL baseURL )
{
checkNotInit( "setBaseURL" );
this.baseURL = baseURL;
}
public void load()
throws IOException
{
checkNotInit( "load" );
try
{
if ( baseURL == null || StringUtils.isEmpty( baseURL.toString() ) )
{
throw new IllegalStateException( "no baseURL defined in " + this );
}
URL definitionURL = MojoHelper.getUrl( getBaseURL(), REPOSITORY_DEFINITION_FILE );
if ( licenses != null )
{
licenses.clear();
}
else
{
licenses = new ArrayList();
}
if ( !checkExists( definitionURL ) )
{
throw new IllegalArgumentException(
"no licenses.properties found with url [" + definitionURL + "] for resolver " + this );
}
Properties p = new Properties();
p.load( definitionURL.openStream() );
for ( Entry