diff --git a/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java b/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java index ebd29d0d17..12c9081628 100644 --- a/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java +++ b/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java @@ -22,7 +22,6 @@ import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource; import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -30,6 +29,8 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.Iterator; import java.util.List; +import org.apache.maven.settings.validation.SettingsValidationResult; +import org.apache.maven.settings.validation.SettingsValidator; /** * @author jdcasey @@ -39,6 +40,9 @@ public class DefaultMavenSettingsBuilder extends AbstractLogEnabled implements MavenSettingsBuilder { + + private SettingsValidator validator; + // ---------------------------------------------------------------------- // MavenProfilesBuilder Implementation // ---------------------------------------------------------------------- @@ -50,7 +54,7 @@ public class DefaultMavenSettingsBuilder throws IOException, XmlPullParserException { Settings globalSettings = readSettings( globalSettingsFile ); - + if ( userSettingsFile == null ) { userSettingsFile = new File( new File( System.getProperty( "user.home" ) ), ".m2/settings.xml" ); @@ -69,6 +73,10 @@ public class DefaultMavenSettingsBuilder userSettings.setRuntimeInfo( new RuntimeInfo( userSettings ) ); } + + validateSettings( globalSettings, globalSettingsFile ); + + validateSettings( userSettings, userSettingsFile ); SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL ); @@ -167,4 +175,15 @@ public class DefaultMavenSettingsBuilder } } } + + private void validateSettings(Settings settings, File location) throws IOException { + SettingsValidationResult validationResult = validator.validate( settings ); + + if ( validationResult.getMessageCount() > 0 ) + { + throw new IOException( "Failed to validate Settings file at " + location + + "\n" + validationResult.render("\n") ); + } + + } } diff --git a/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java b/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java new file mode 100644 index 0000000000..7896ca41cd --- /dev/null +++ b/maven-settings/src/main/java/org/apache/maven/settings/validation/DefaultSettingsValidator.java @@ -0,0 +1,135 @@ +package org.apache.maven.settings.validation; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.util.Iterator; +import java.util.List; +import org.apache.maven.settings.Profile; +import org.apache.maven.settings.Repository; +import org.apache.maven.settings.Settings; + +/** + * @author Milos Kleint + */ +public class DefaultSettingsValidator + implements SettingsValidator +{ + + + public SettingsValidationResult validate( Settings model ) + { + SettingsValidationResult result = new SettingsValidationResult(); + + List profiles = model.getProfiles(); + if (profiles != null) { + Iterator it = profiles.iterator(); + while (it.hasNext()) { + Profile prof = (Profile)it.next(); + validateRepositories( result, prof.getRepositories(), "repositories.repository" ); + validateRepositories( result, prof.getPluginRepositories(), "pluginRepositories.pluginRepository" ); + } + + } + + + return result; + } + + private void validateRepositories( SettingsValidationResult result, List repositories, String prefix ) + { + for ( Iterator it = repositories.iterator(); it.hasNext(); ) + { + Repository repository = (Repository) it.next(); + + validateStringNotEmpty( prefix + ".id", result, repository.getId() ); + + validateStringNotEmpty( prefix + ".url", result, repository.getUrl() ); + } + } + + + + // ---------------------------------------------------------------------- + // Field validation + // ---------------------------------------------------------------------- + + + private boolean validateStringNotEmpty( String fieldName, SettingsValidationResult result, String string ) + { + return validateStringNotEmpty( fieldName, result, string, null ); + } + + /** + * Asserts: + *

+ *

+ */ + private boolean validateStringNotEmpty( String fieldName, SettingsValidationResult result, String string, String sourceHint ) + { + if ( !validateNotNull( fieldName, result, string, sourceHint ) ) + { + return false; + } + + if ( string.length() > 0 ) + { + return true; + } + + if ( sourceHint != null ) + { + result.addMessage( "'" + fieldName + "' is missing for " + sourceHint ); + } + else + { + result.addMessage( "'" + fieldName + "' is missing." ); + } + + + return false; + } + + /** + * Asserts: + *

+ *

+ */ + private boolean validateNotNull( String fieldName, SettingsValidationResult result, Object object, String sourceHint ) + { + if ( object != null ) + { + return true; + } + + if ( sourceHint != null ) + { + result.addMessage( "'" + fieldName + "' is missing for " + sourceHint ); + } + else + { + result.addMessage( "'" + fieldName + "' is missing." ); + } + + return false; + } + +} diff --git a/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java b/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java new file mode 100644 index 0000000000..1c55a6d027 --- /dev/null +++ b/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidationResult.java @@ -0,0 +1,90 @@ +package org.apache.maven.settings.validation; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * @author Trygve Laugstøl + * @author Milos Kleint + */ +public final class SettingsValidationResult +{ + /** */ + private final static String NEWLINE = System.getProperty( "line.separator" ); + + /** */ + private List messages; + + public SettingsValidationResult() + { + messages = new ArrayList(); + } + + public int getMessageCount() + { + return messages.size(); + } + + public String getMessage( int i ) + { + return messages.get( i ).toString(); + } + + public List getMessages() + { + return Collections.unmodifiableList( messages ); + } + + public void addMessage( String message ) + { + messages.add( message ); + } + + public String toString() + { + return render( "" ); + } + + public String render( String indentation ) + { + if ( messages.size() == 0 ) + { + return indentation + "There were no validation errors."; + } + + StringBuffer message = new StringBuffer(); + +// if ( messages.size() == 1 ) +// { +// message.append( "There was 1 validation error: " ); +// } +// else +// { +// message.append( "There was " + messages.size() + " validation errors: " + NEWLINE ); +// } +// + for ( int i = 0; i < messages.size(); i++ ) + { + message.append( indentation + "[" + i + "] " + messages.get( i ).toString() + NEWLINE ); + } + + return message.toString(); + } +} diff --git a/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java b/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java new file mode 100644 index 0000000000..931c86e9de --- /dev/null +++ b/maven-settings/src/main/java/org/apache/maven/settings/validation/SettingsValidator.java @@ -0,0 +1,30 @@ +package org.apache.maven.settings.validation; + + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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 org.apache.maven.settings.Settings; + +/** + * @author Milos Kleint + */ +public interface SettingsValidator +{ + String ROLE = SettingsValidator.class.getName(); + + SettingsValidationResult validate( Settings model ); +} diff --git a/maven-settings/src/main/resources/META-INF/plexus/components.xml b/maven-settings/src/main/resources/META-INF/plexus/components.xml index 7f0d3ee6f7..56e31fa628 100644 --- a/maven-settings/src/main/resources/META-INF/plexus/components.xml +++ b/maven-settings/src/main/resources/META-INF/plexus/components.xml @@ -1,9 +1,19 @@ + + + org.apache.maven.settings.validation.SettingsValidator + org.apache.maven.settings.validation.DefaultSettingsValidator + org.apache.maven.settings.MavenSettingsBuilder org.apache.maven.settings.DefaultMavenSettingsBuilder + + + org.apache.maven.settings.validation.SettingsValidator + +