From 8980f67b9b1d0dbc06b68f74a88b9f8589c40e23 Mon Sep 17 00:00:00 2001 From: Igor Fedorenko Date: Wed, 30 Jul 2014 07:57:53 +0400 Subject: [PATCH] MNG-5670 guard against ConcurrentModificationException iterating over System properties Signed-off-by: Igor Fedorenko --- .../project/artifact/MavenMetadataSource.java | 3 +- .../properties/internal/SystemProperties.java | 51 +++++++++++++++++++ .../settings/DefaultMavenSettingsBuilder.java | 3 +- .../java/org/apache/maven/cli/MavenCli.java | 5 +- .../DefaultSettingsBuildingRequest.java | 6 ++- 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/properties/internal/SystemProperties.java diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index f2fc322b34..1750a7b15a 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -68,6 +68,7 @@ import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.properties.internal.EnvironmentUtils; +import org.apache.maven.properties.internal.SystemProperties; import org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest; import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.codehaus.plexus.PlexusContainer; @@ -752,7 +753,7 @@ public class MavenMetadataSource EnvironmentUtils.addEnvVars( props ); - props.putAll( System.getProperties() ); + SystemProperties.addSystemProperties( props ); return props; } diff --git a/maven-core/src/main/java/org/apache/maven/properties/internal/SystemProperties.java b/maven-core/src/main/java/org/apache/maven/properties/internal/SystemProperties.java new file mode 100644 index 0000000000..9cb07c4d12 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/properties/internal/SystemProperties.java @@ -0,0 +1,51 @@ +package org.apache.maven.properties.internal; + +import java.util.Properties; + +/* + * 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. + */ + +/** + * @since 3.2.3 + */ +public class SystemProperties +{ + /** + * Thread-safe System.properties copy implementation. + * + * @see http://jira.codehaus.org/browse/MNG-5670 + */ + public static void addSystemProperties( Properties props ) + { + for ( String key : System.getProperties().stringPropertyNames() ) + { + props.put( key, System.getProperty( key ) ); + } + } + + /** + * Returns System.properties copy. + */ + public static Properties getSystemProperties() + { + Properties systemProperties = new Properties(); + addSystemProperties( systemProperties ); + return systemProperties; + } +} diff --git a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java index f11c4418fd..820d886e29 100644 --- a/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java @@ -23,6 +23,7 @@ import java.io.File; import java.io.IOException; import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.properties.internal.SystemProperties; import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; import org.apache.maven.settings.building.SettingsBuilder; import org.apache.maven.settings.building.SettingsBuildingException; @@ -71,7 +72,7 @@ public class DefaultMavenSettingsBuilder SettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); request.setUserSettingsFile( userSettingsFile ); request.setGlobalSettingsFile( globalSettingsFile ); - request.setSystemProperties( System.getProperties() ); + request.setSystemProperties( SystemProperties.getSystemProperties() ); return build( request ); } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 123b01aa81..9e64dd1aa3 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -59,6 +59,7 @@ import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.model.building.ModelProcessor; import org.apache.maven.project.MavenProject; import org.apache.maven.properties.internal.EnvironmentUtils; +import org.apache.maven.properties.internal.SystemProperties; import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; import org.apache.maven.settings.building.SettingsBuilder; import org.apache.maven.settings.building.SettingsBuildingRequest; @@ -1163,8 +1164,8 @@ public class MavenCli } } - systemProperties.putAll( System.getProperties() ); - + SystemProperties.addSystemProperties( systemProperties ); + // ---------------------------------------------------------------------- // Properties containing info about the currently running version of Maven // These override any corresponding properties set on the command line diff --git a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java index 6b7e2f4faf..08c93fce43 100644 --- a/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java +++ b/maven-settings-builder/src/main/java/org/apache/maven/settings/building/DefaultSettingsBuildingRequest.java @@ -106,7 +106,11 @@ public class DefaultSettingsBuildingRequest if ( systemProperties != null ) { this.systemProperties = new Properties(); - this.systemProperties.putAll( systemProperties ); + // MNG-5670 guard against ConcurrentModificationException + for ( String key : System.getProperties().stringPropertyNames() ) + { + this.systemProperties.put( key, System.getProperty( key ) ); + } } else {