Skip to content

Commit

Permalink
[m2e] Add the validation the version of the SARL bundle and the Maven…
Browse files Browse the repository at this point in the history
… dependency.

see #256

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Dec 11, 2014
1 parent 3828c4c commit 23f9fa3
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 2 deletions.
49 changes: 49 additions & 0 deletions plugins/io.sarl.m2e/src/io/sarl/m2e/Messages.java
@@ -0,0 +1,49 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND.
*
* 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.
*/

package io.sarl.m2e;

import org.eclipse.osgi.util.NLS;

/** Localized Messages.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("all")
public class Messages extends NLS {
private static final String BUNDLE_NAME = "io.sarl.m2e.messages"; //$NON-NLS-1$
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}

private Messages() {
}

public static String SARLMavenEclipsePlugin_0;
public static String SARLProjectConfigurator_0;
public static String SARLProjectConfigurator_1;
public static String SARLProjectConfigurator_2;
public static String SARLProjectConfigurator_3;
}
42 changes: 41 additions & 1 deletion plugins/io.sarl.m2e/src/io/sarl/m2e/SARLMavenEclipsePlugin.java
Expand Up @@ -20,10 +20,14 @@
*/
package io.sarl.m2e;

import java.text.MessageFormat;

import org.apache.maven.artifact.ArtifactUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.Version;

import com.google.common.base.Strings;

Expand Down Expand Up @@ -75,6 +79,16 @@ public static IStatus createStatus(int severity, Throwable cause) {
return new Status(severity, PLUGIN_ID, m, cause);
}

/** Create a status.
*
* @param severity - the severity level, see {@link IStatus}.
* @param message - the status message.
* @return the status.
*/
public static IStatus createStatus(int severity, String message) {
return new Status(severity, PLUGIN_ID, message);
}

/**
* Logs an internal error with the specified throwable.
*
Expand All @@ -86,7 +100,7 @@ public static void log(Throwable e) {
e.getMessage(), e.getCause()));
} else {
log(new Status(IStatus.ERROR, PLUGIN_ID,
"Internal Error", e)); //$NON-NLS-1$
MessageFormat.format(Messages.SARLMavenEclipsePlugin_0, e.getMessage()), e));
}
}

Expand All @@ -99,4 +113,30 @@ public static void log(IStatus status) {
getDefault().getLog().log(status);
}

/** Maven version parser.
*
* @param version - the version string.
* @return the version.
*/
public static Version parseMavenVersion(String version) {
boolean isSnapshot = ArtifactUtils.isSnapshot(version);
String[] parts = version.split("[.]"); //$NON-NLS-1$
int minor = 0;
int micro = 0;
int major = Integer.parseInt(parts[0]);
if (parts.length > 1) {
minor = Integer.parseInt(parts[1]);
if (parts.length > 1) {
if (isSnapshot) {
parts[2] = parts[2].replaceFirst("\\-.+$", ""); //$NON-NLS-1$//$NON-NLS-2$
}
micro = Integer.parseInt(parts[2]);
}
}
if (isSnapshot) {
return new Version(major, minor, micro, "qualifier"); //$NON-NLS-1$
}
return new Version(major, minor, micro);
}

}
72 changes: 71 additions & 1 deletion plugins/io.sarl.m2e/src/io/sarl/m2e/SARLProjectConfigurator.java
Expand Up @@ -21,10 +21,13 @@
package io.sarl.m2e;

import io.sarl.eclipse.SARLConfig;
import io.sarl.eclipse.SARLEclipsePlugin;
import io.sarl.eclipse.buildpath.SARLClasspathContainerInitializer;
import io.sarl.lang.ui.preferences.SARLProjectPreferences;

import java.io.File;
import java.text.MessageFormat;
import java.util.BitSet;
import java.util.List;
import java.util.Properties;

Expand All @@ -35,7 +38,9 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -48,6 +53,7 @@
import org.eclipse.m2e.jdt.IClasspathEntryDescriptor;
import org.eclipse.m2e.jdt.IJavaProjectConfigurator;
import org.eclipse.m2e.jdt.internal.ClasspathDescriptor;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;

/** Project configuration for the M2E.
Expand All @@ -59,6 +65,13 @@
*/
public class SARLProjectConfigurator extends AbstractProjectConfigurator implements IJavaProjectConfigurator {

private static final String SARL_LANG_BUNDLE_NAME = "io.sarl.lang.core"; //$NON-NLS-1$
private static final String SARL_GROUP_ID = "io.sarl.lang"; //$NON-NLS-1$
private static final String SARL_ARTIFACT_ID = "io.sarl.lang.core"; //$NON-NLS-1$
private static final String GROUPID_ATTR_NAME = "maven.groupId"; //$NON-NLS-1$
private static final String ARTIFACTID_ATTR_NAME = "maven.artifactId"; //$NON-NLS-1$
private static final String VERSION_ATTR_NAME = "maven.version"; //$NON-NLS-1$

/** Invoked to add the preferences dedicated to SARL, JRE, etc.
*
* @param facade - the Maven face.
Expand Down Expand Up @@ -373,11 +386,68 @@ public void unconfigure(ProjectConfigurationRequest request,
addSarlLibraries(classpath);
}

private static void validateSARLLibraryVersion(String mavenVersion) throws CoreException {
Bundle bundle = Platform.getBundle(SARL_LANG_BUNDLE_NAME);
if (bundle == null) {
throw new CoreException(SARLMavenEclipsePlugin.createStatus(IStatus.ERROR,
MessageFormat.format(Messages.SARLProjectConfigurator_0, SARL_LANG_BUNDLE_NAME)));
}
Version bundleVersion = bundle.getVersion();
if (bundleVersion == null) {
throw new CoreException(SARLMavenEclipsePlugin.createStatus(IStatus.ERROR,
MessageFormat.format(Messages.SARLProjectConfigurator_1, SARL_LANG_BUNDLE_NAME)));
}
Version minVersion = new Version(bundleVersion.getMajor(), bundleVersion.getMinor(), 0);
Version maxVersion = new Version(bundleVersion.getMajor(), bundleVersion.getMinor() + 1, 0);

assert (minVersion != null && maxVersion != null);
Version mvnVersion = SARLMavenEclipsePlugin.parseMavenVersion(mavenVersion);
int compare = SARLEclipsePlugin.compareVersionToRange(mvnVersion, minVersion, maxVersion);
if (compare < 0) {
throw new CoreException(SARLMavenEclipsePlugin.createStatus(IStatus.ERROR,
MessageFormat.format(
Messages.SARLProjectConfigurator_2,
SARL_GROUP_ID, SARL_ARTIFACT_ID, mavenVersion, minVersion.toString())));
} else if (compare > 0) {
throw new CoreException(SARLMavenEclipsePlugin.createStatus(IStatus.ERROR,
MessageFormat.format(
Messages.SARLProjectConfigurator_3,
SARL_GROUP_ID, SARL_ARTIFACT_ID, mavenVersion, maxVersion.toString())));
}
}

private static boolean validateSARLLibraryVersion(IClasspathDescriptor classpath) throws CoreException {
for (IClasspathEntry dep : classpath.getEntries()) {
IClasspathAttribute[] attrs = dep.getExtraAttributes();
BitSet flags = new BitSet(3);
String version = null;
for (int i = 0; version == null && flags.cardinality() != 3 && i < attrs.length; ++i) {
IClasspathAttribute attr = attrs[i];
if (GROUPID_ATTR_NAME.equals(attr.getName())
&& SARL_GROUP_ID.equals(attr.getValue())) {
flags.set(0);
} else if (ARTIFACTID_ATTR_NAME.equals(attr.getName())
&& SARL_ARTIFACT_ID.equals(attr.getValue())) {
flags.set(1);
} else if (VERSION_ATTR_NAME.equals(attr.getName())) {
flags.set(2);
version = attr.getValue();
}
}
if (flags.cardinality() == 3 && version != null) {
validateSARLLibraryVersion(version);
return true;
}
}

return false;
}

@Override
public void configureClasspath(IMavenProjectFacade facade,
IClasspathDescriptor classpath, IProgressMonitor monitor)
throws CoreException {
//
validateSARLLibraryVersion(classpath);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions plugins/io.sarl.m2e/src/io/sarl/m2e/messages.properties
@@ -0,0 +1,5 @@
SARLMavenEclipsePlugin_0=Internal Error: {0}
SARLProjectConfigurator_0=Unable to find the bundle ''{0}''
SARLProjectConfigurator_1=Unable to extract the version of the bundle ''{0}''
SARLProjectConfigurator_2=The maven artifact {0}:{1} has a too old version.\nCurrent version: {2}\nMinimal expected version: {3}
SARLProjectConfigurator_3=The maven artifact {0}:{1} has a too recent version.\nCurrent version: {2}\nMaximal expected version: {3}

0 comments on commit 23f9fa3

Please sign in to comment.