You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2010/10/30 19:31:59 UTC

svn commit: r1029127 - /maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/

Author: bentmann
Date: Sat Oct 30 17:31:59 2010
New Revision: 1029127

URL: http://svn.apache.org/viewvc?rev=1029127&view=rev
Log:
o Introduced SettingsSource to enable non-file based content providers

Added:
    maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/FileSettingsSource.java   (with props)
    maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsSource.java   (with props)
    maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/StringSettingsSource.java   (with props)
    maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/UrlSettingsSource.java   (with props)
Modified:
    maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java
    maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java
    maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java

Modified: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java?rev=1029127&r1=1029126&r2=1029127&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java (original)
+++ maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuilder.java Sat Oct 30 17:31:59 2010
@@ -86,9 +86,13 @@ public class DefaultSettingsBuilder
     {
         DefaultSettingsProblemCollector problems = new DefaultSettingsProblemCollector( null );
 
-        Settings globalSettings = readSettings( request.getGlobalSettingsFile(), request, problems );
-
-        Settings userSettings = readSettings( request.getUserSettingsFile(), request, problems );
+        SettingsSource globalSettingsSource =
+            getSettingsSource( request.getGlobalSettingsFile(), request.getGlobalSettingsSource() );
+        Settings globalSettings = readSettings( globalSettingsSource, 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();
         }

Modified: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java?rev=1029127&r1=1029126&r2=1029127&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java (original)
+++ maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java Sat Oct 30 17:31:59 2010
@@ -35,6 +35,10 @@ public class DefaultSettingsBuildingRequ
 
     private File userSettingsFile;
 
+    private SettingsSource globalSettingsSource;
+
+    private SettingsSource userSettingsSource;
+
     private Properties systemProperties;
 
     private Properties userProperties;
@@ -51,6 +55,18 @@ public class DefaultSettingsBuildingRequ
         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 DefaultSettingsBuildingRequ
         return this;
     }
 
+    public SettingsSource getUserSettingsSource()
+    {
+        return userSettingsSource;
+    }
+
+    public DefaultSettingsBuildingRequest setUserSettingsSource( SettingsSource userSettingsSource )
+    {
+        this.userSettingsSource = userSettingsSource;
+
+        return this;
+    }
+
     public Properties getSystemProperties()
     {
         if ( systemProperties == null )

Added: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/FileSettingsSource.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/FileSettingsSource.java?rev=1029127&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/FileSettingsSource.java (added)
+++ maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/FileSettingsSource.java Sat Oct 30 17:31:59 2010
@@ -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();
+    }
+
+}

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/FileSettingsSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/FileSettingsSource.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java?rev=1029127&r1=1029126&r2=1029127&view=diff
==============================================================================
--- maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java (original)
+++ maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsBuildingRequest.java Sat Oct 30 17:31:59 2010
@@ -38,8 +38,8 @@ 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}.
@@ -47,6 +47,22 @@ public interface SettingsBuildingRequest
     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.
      * 
      * @return The user settings file or {@code null} if none.
@@ -63,6 +79,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.
      * 

Added: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsSource.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsSource.java?rev=1029127&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsSource.java (added)
+++ maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsSource.java Sat Oct 30 17:31:59 2010
@@ -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();
+
+}

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/SettingsSource.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/StringSettingsSource.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/StringSettingsSource.java?rev=1029127&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/StringSettingsSource.java (added)
+++ maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/StringSettingsSource.java Sat Oct 30 17:31:59 2010
@@ -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();
+    }
+
+}

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/StringSettingsSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/StringSettingsSource.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/UrlSettingsSource.java
URL: http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/UrlSettingsSource.java?rev=1029127&view=auto
==============================================================================
--- maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/UrlSettingsSource.java (added)
+++ maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/UrlSettingsSource.java Sat Oct 30 17:31:59 2010
@@ -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();
+    }
+
+}

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/UrlSettingsSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/maven-3/trunk/maven-settings-builder/src/main/java/org/apache/maven/settings/building/UrlSettingsSource.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision