o Introduced SettingsSource to enable non-file based content providers

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1029127 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2010-10-30 17:31:59 +00:00
parent d838106be6
commit 545fa2b101
7 changed files with 383 additions and 11 deletions

View File

@ -86,9 +86,13 @@ public class DefaultSettingsBuilder
{
DefaultSettingsProblemCollector problems = new DefaultSettingsProblemCollector( null );
Settings globalSettings = readSettings( request.getGlobalSettingsFile(), request, problems );
SettingsSource globalSettingsSource =
getSettingsSource( request.getGlobalSettingsFile(), request.getGlobalSettingsSource() );
Settings globalSettings = readSettings( globalSettingsSource, request, problems );
Settings userSettings = readSettings( request.getUserSettingsFile(), request, problems );
SettingsSource userSettingsSource =
getSettingsSource( request.getUserSettingsFile(), request.getUserSettingsSource() );
Settings userSettings = readSettings( userSettingsSource, request, problems );
settingsMerger.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL );
@ -131,15 +135,28 @@ public class DefaultSettingsBuilder
return false;
}
private Settings readSettings( File settingsFile, SettingsBuildingRequest request,
private SettingsSource getSettingsSource( File settingsFile, SettingsSource settingsSource )
{
if ( settingsSource != null )
{
return settingsSource;
}
else if ( settingsFile != null && settingsFile.exists() )
{
return new FileSettingsSource( settingsFile );
}
return null;
}
private Settings readSettings( SettingsSource settingsSource, SettingsBuildingRequest request,
DefaultSettingsProblemCollector problems )
{
if ( settingsFile == null || !settingsFile.exists() )
if ( settingsSource == null )
{
return new Settings();
}
problems.setSource( settingsFile.getAbsolutePath() );
problems.setSource( settingsSource.getLocation() );
Settings settings;
@ -149,13 +166,13 @@ public class DefaultSettingsBuilder
try
{
settings = settingsReader.read( settingsFile, options );
settings = settingsReader.read( settingsSource.getInputStream(), options );
}
catch ( SettingsParseException e )
{
options = Collections.singletonMap( SettingsReader.IS_STRICT, Boolean.FALSE );
settings = settingsReader.read( settingsFile, options );
settings = settingsReader.read( settingsSource.getInputStream(), options );
problems.add( SettingsProblem.Severity.WARNING, e.getMessage(), e.getLineNumber(), e.getColumnNumber(),
e );
@ -163,13 +180,13 @@ public class DefaultSettingsBuilder
}
catch ( SettingsParseException e )
{
problems.add( SettingsProblem.Severity.FATAL, "Non-parseable settings " + settingsFile + ": "
problems.add( SettingsProblem.Severity.FATAL, "Non-parseable settings " + settingsSource.getLocation() + ": "
+ e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
return new Settings();
}
catch ( IOException e )
{
problems.add( SettingsProblem.Severity.FATAL, "Non-readable settings " + settingsFile + ": "
problems.add( SettingsProblem.Severity.FATAL, "Non-readable settings " + settingsSource.getLocation() + ": "
+ e.getMessage(), -1, -1, e );
return new Settings();
}

View File

@ -35,6 +35,10 @@ public class DefaultSettingsBuildingRequest
private File userSettingsFile;
private SettingsSource globalSettingsSource;
private SettingsSource userSettingsSource;
private Properties systemProperties;
private Properties userProperties;
@ -51,6 +55,18 @@ public class DefaultSettingsBuildingRequest
return this;
}
public SettingsSource getGlobalSettingsSource()
{
return globalSettingsSource;
}
public DefaultSettingsBuildingRequest setGlobalSettingsSource( SettingsSource globalSettingsSource )
{
this.globalSettingsSource = globalSettingsSource;
return this;
}
public File getUserSettingsFile()
{
return userSettingsFile;
@ -63,6 +79,18 @@ public class DefaultSettingsBuildingRequest
return this;
}
public SettingsSource getUserSettingsSource()
{
return userSettingsSource;
}
public DefaultSettingsBuildingRequest setUserSettingsSource( SettingsSource userSettingsSource )
{
this.userSettingsSource = userSettingsSource;
return this;
}
public Properties getSystemProperties()
{
if ( systemProperties == null )

View File

@ -0,0 +1,79 @@
package org.apache.maven.settings.building;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache 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.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Wraps an ordinary {@link File} as a settings source.
*
* @author Benjamin Bentmann
*/
public class FileSettingsSource
implements SettingsSource
{
private File settingsFile;
/**
* Creates a new settings source backed by the specified file.
*
* @param settingsFile The settings file, must not be {@code null}.
*/
public FileSettingsSource( File settingsFile )
{
if ( settingsFile == null )
{
throw new IllegalArgumentException( "no settings file specified" );
}
this.settingsFile = settingsFile.getAbsoluteFile();
}
public InputStream getInputStream()
throws IOException
{
return new FileInputStream( settingsFile );
}
public String getLocation()
{
return settingsFile.getPath();
}
/**
* Gets the settings file of this model source.
*
* @return The underlying settings file, never {@code null}.
*/
public File getSettingsFile()
{
return settingsFile;
}
@Override
public String toString()
{
return getLocation();
}
}

View File

@ -38,14 +38,30 @@ public interface SettingsBuildingRequest
File getGlobalSettingsFile();
/**
* Sets the global settings file. A non-existent settings file is equivalent to empty settings. If both a user
* settings file and a global settings file are given, the user settings take precedence.
* Sets the global settings file. A non-existent settings file is equivalent to empty settings. If both user
* settings and global settings are given, the user settings take precedence.
*
* @param globalSettingsFile The global settings file, may be {@code null} to disable global settings.
* @return This request, never {@code null}.
*/
SettingsBuildingRequest setGlobalSettingsFile( File globalSettingsFile );
/**
* Gets the global settings source.
*
* @return The global settings source or {@code null} if none.
*/
SettingsSource getGlobalSettingsSource();
/**
* Sets the global settings source. If both user settings and a global settings are given, the user settings take
* precedence.
*
* @param globalSettingsSource The global settings source, may be {@code null} to disable global settings.
* @return This request, never {@code null}.
*/
SettingsBuildingRequest setGlobalSettingsSource( SettingsSource globalSettingsSource );
/**
* Gets the user settings file.
*
@ -62,6 +78,22 @@ public interface SettingsBuildingRequest
*/
SettingsBuildingRequest setUserSettingsFile( File userSettingsFile );
/**
* Gets the user settings source.
*
* @return The user settings source or {@code null} if none.
*/
SettingsSource getUserSettingsSource();
/**
* Sets the user settings source. If both user settings and a global settings are given, the user settings take
* precedence.
*
* @param userSettingsSource The user settings source, may be {@code null} to disable user settings.
* @return This request, never {@code null}.
*/
SettingsBuildingRequest setUserSettingsSource( SettingsSource userSettingsSource );
/**
* Gets the system properties to use for interpolation. The system properties are collected from the runtime
* environment like {@link System#getProperties()} and environment variables.

View File

@ -0,0 +1,50 @@
package org.apache.maven.settings.building;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache 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.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
import java.io.IOException;
import java.io.InputStream;
/**
* Provides access to the contents of settings independently of the backing store (e.g. file system, database, memory).
*
* @author Benjamin Bentmann
*/
public interface SettingsSource
{
/**
* Gets a byte stream to the settings contents. Closing the returned stream is the responsibility of the caller.
* Note that each invocation of this method returns a new/fresh stream which represents the entire contents.
*
* @return A byte stream to the settings contents, never {@code null}.
*/
InputStream getInputStream()
throws IOException;
/**
* Provides a user-friendly hint about the location of the settings. This could be a local file path, a URI or just
* an empty string. The intention is to assist users during error reporting.
*
* @return A user-friendly hint about the location of the settings, never {@code null}.
*/
String getLocation();
}

View File

@ -0,0 +1,88 @@
package org.apache.maven.settings.building;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache 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.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Wraps an ordinary {@link CharSequence} as a settings source.
*
* @author Benjamin Bentmann
*/
public class StringSettingsSource
implements SettingsSource
{
private String settings;
private String location;
/**
* Creates a new settings source backed by the specified string.
*
* @param settings The settings' string representation, may be empty or {@code null}.
*/
public StringSettingsSource( CharSequence settings )
{
this( settings, null );
}
/**
* Creates a new settings source backed by the specified string.
*
* @param settings The settings' string representation, may be empty or {@code null}.
* @param location The location to report for this use, may be {@code null}.
*/
public StringSettingsSource( CharSequence settings, String location )
{
this.settings = ( settings != null ) ? settings.toString() : "";
this.location = ( location != null ) ? location : "(memory)";
}
public InputStream getInputStream()
throws IOException
{
return new ByteArrayInputStream( settings.getBytes( "UTF-8" ) );
}
public String getLocation()
{
return location;
}
/**
* Gets the character sequence of this settings source.
*
* @return The underlying character stream, never {@code null}.
*/
public String getSettings()
{
return settings;
}
@Override
public String toString()
{
return getLocation();
}
}

View File

@ -0,0 +1,78 @@
package org.apache.maven.settings.building;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache 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.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Wraps an ordinary {@link URL} as a settings source.
*
* @author Benjamin Bentmann
*/
public class UrlSettingsSource
implements SettingsSource
{
private URL settingsUrl;
/**
* Creates a new model source backed by the specified URL.
*
* @param settingsUrl The settings URL, must not be {@code null}.
*/
public UrlSettingsSource( URL settingsUrl )
{
if ( settingsUrl == null )
{
throw new IllegalArgumentException( "no settings URL specified" );
}
this.settingsUrl = settingsUrl;
}
public InputStream getInputStream()
throws IOException
{
return settingsUrl.openStream();
}
public String getLocation()
{
return settingsUrl.toString();
}
/**
* Gets the settings URL of this model source.
*
* @return The underlying settings URL, never {@code null}.
*/
public URL getSettingsUrl()
{
return settingsUrl;
}
@Override
public String toString()
{
return getLocation();
}
}