Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added BSN validation

Pursuant to bug #307:
* added validation framework to builder;
* added BsnValidator.
  • Loading branch information...
commit e967e5a045e396870e5530191293d07a4a3555c1 1 parent 4ab500c
@njbartlett njbartlett authored
View
4 bndtools.core/_plugin.xml
@@ -734,5 +734,9 @@
code="0"
/>
</extension>
+
+ <extension point="bndtools.core.validators">
+ <validator name="BSN check" class="org.bndtools.core.build.validate.BsnValidator"/>
+ </extension>
</plugin>
View
112 bndtools.core/resources/schema/validators.exsd
@@ -0,0 +1,112 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!-- Schema file written by PDE -->
+<schema targetNamespace="bndtools.core" xmlns="http://www.w3.org/2001/XMLSchema">
+<annotation>
+ <appinfo>
+ <meta.schema plugin="bndtools.core" id="validators" name="Build Validators"/>
+ </appinfo>
+ <documentation>
+
+ </documentation>
+ </annotation>
+
+ <element name="extension">
+ <annotation>
+ <appinfo>
+ <meta.element />
+ </appinfo>
+ </annotation>
+ <complexType>
+ <sequence>
+ <element ref="validator" minOccurs="0" maxOccurs="unbounded"/>
+ </sequence>
+ <attribute name="point" type="string" use="required">
+ <annotation>
+ <documentation>
+
+ </documentation>
+ </annotation>
+ </attribute>
+ <attribute name="id" type="string">
+ <annotation>
+ <documentation>
+
+ </documentation>
+ </annotation>
+ </attribute>
+ <attribute name="name" type="string">
+ <annotation>
+ <documentation>
+
+ </documentation>
+ <appinfo>
+ <meta.attribute translatable="true"/>
+ </appinfo>
+ </annotation>
+ </attribute>
+ </complexType>
+ </element>
+
+ <element name="validator">
+ <complexType>
+ <attribute name="class" type="string" use="required">
+ <annotation>
+ <documentation>
+
+ </documentation>
+ <appinfo>
+ <meta.attribute kind="java" basedOn=":bndtools.api.IValidator"/>
+ </appinfo>
+ </annotation>
+ </attribute>
+ <attribute name="name" type="string">
+ <annotation>
+ <documentation>
+
+ </documentation>
+ <appinfo>
+ <meta.attribute translatable="true"/>
+ </appinfo>
+ </annotation>
+ </attribute>
+ </complexType>
+ </element>
+
+ <annotation>
+ <appinfo>
+ <meta.section type="since"/>
+ </appinfo>
+ <documentation>
+ 0.0.0
+ </documentation>
+ </annotation>
+
+ <annotation>
+ <appinfo>
+ <meta.section type="examples"/>
+ </appinfo>
+ <documentation>
+ [Enter extension point usage example here.]
+ </documentation>
+ </annotation>
+
+ <annotation>
+ <appinfo>
+ <meta.section type="apiinfo"/>
+ </appinfo>
+ <documentation>
+ No programmatic API is available.
+ </documentation>
+ </annotation>
+
+ <annotation>
+ <appinfo>
+ <meta.section type="implementation"/>
+ </appinfo>
+ <documentation>
+ No implementation is supplied.
+ </documentation>
+ </annotation>
+
+
+</schema>
View
9 bndtools.core/src/bndtools/api/IValidator.java
@@ -0,0 +1,9 @@
+package bndtools.api;
+
+import org.eclipse.core.runtime.IStatus;
+
+import aQute.lib.osgi.Builder;
+
+public interface IValidator {
+ IStatus validate(Builder builder);
+}
View
63 bndtools.core/src/bndtools/builder/NewBuilder.java
@@ -23,10 +23,13 @@
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.core.IJavaModelMarker;
@@ -38,6 +41,7 @@
import aQute.lib.osgi.Builder;
import bndtools.Central;
import bndtools.Plugin;
+import bndtools.api.IValidator;
import bndtools.classpath.BndContainerInitializer;
public class NewBuilder extends IncrementalProjectBuilder {
@@ -52,7 +56,7 @@
private Project model;
private List<String> classpathErrors;
- private List<IStatus> validationResults;
+ private MultiStatus validationResults;
private List<String> buildLog;
private int logLevel = LOG_NONE;
@@ -65,7 +69,7 @@
logLevel = prefs.getInt(Plugin.PREF_BUILD_LOGGING);
classpathErrors = new LinkedList<String>();
- validationResults = new LinkedList<IStatus>();
+ validationResults = new MultiStatus(Plugin.PLUGIN_ID, 0, "Validation errors in bnd project", null);
buildLog = new ArrayList<String>(5);
// Initialise workspace OBR index (should only happen once)
@@ -432,6 +436,15 @@ private boolean rebuild(boolean force) throws Exception {
File[] built;
+ // Validate
+ List<IValidator> validators = loadValidators();
+ if (validators != null) {
+ Collection<? extends Builder> builders = model.getSubBuilders();
+ for (Builder builder : builders) {
+ validate(builder, validators);
+ }
+ }
+
// Build!
model.clear();
model.setTrace(true);
@@ -470,6 +483,30 @@ private boolean rebuild(boolean force) throws Exception {
return built.length > 0;
}
+ List<IValidator> loadValidators() {
+ List<IValidator> validators = null;
+ IConfigurationElement[] validatorElems = Platform.getExtensionRegistry().getConfigurationElementsFor(Plugin.PLUGIN_ID, "validators");
+ if (validatorElems != null && validatorElems.length > 0) {
+ validators = new ArrayList<IValidator>(validatorElems.length);
+ for (IConfigurationElement elem : validatorElems) {
+ try {
+ validators.add((IValidator) elem.createExecutableExtension("class"));
+ } catch (Exception e) {
+ Plugin.logError("Unable to instantiate validator: " + elem.getAttribute("name"), e);
+ }
+ }
+ }
+ return validators;
+ }
+
+ void validate(Builder builder, List<IValidator> validators) {
+ for (IValidator validator : validators) {
+ IStatus status = validator.validate(builder);
+ if (!status.isOK())
+ validationResults.add(status);
+ }
+ }
+
private IProject[] calculateDependsOn() throws Exception {
Collection<Project> dependsOn = model.getDependson();
List<IProject> result = new ArrayList<IProject>(dependsOn.size() + 1);
@@ -530,6 +567,12 @@ private void createBuildMarkers(Collection<? extends String> errors, Collection<
for (String error : classpathErrors) {
addClasspathMarker(error, IMarker.SEVERITY_ERROR);
}
+
+ if (!validationResults.isOK()) {
+ for (IStatus status : validationResults.getChildren()) {
+ addClasspathMarker(status);
+ }
+ }
}
private void clearBuildMarkers() throws CoreException {
@@ -566,6 +609,22 @@ private void addClasspathMarker(String message, int severity) throws CoreExcepti
// marker.setAttribute(IMarker.LINE_NUMBER, 1);
}
+ private void addClasspathMarker(IStatus status) throws CoreException {
+ int severity;
+ switch (status.getSeverity()) {
+ case IStatus.CANCEL:
+ case IStatus.ERROR:
+ severity = IMarker.SEVERITY_ERROR;
+ break;
+ case IStatus.WARNING:
+ severity = IMarker.SEVERITY_WARNING;
+ break;
+ default:
+ severity = IMarker.SEVERITY_INFO;
+ }
+ addClasspathMarker(status.getMessage(), severity);
+ }
+
private void log(int level, String message, Object... args) {
if (logLevel >= level)
buildLog.add(String.format(message, args));
View
38 bndtools.core/src/org/bndtools/core/build/validate/BsnValidator.java
@@ -0,0 +1,38 @@
+package org.bndtools.core.build.validate;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+
+import aQute.lib.osgi.Builder;
+import bndtools.Plugin;
+import bndtools.api.IValidator;
+
+public class BsnValidator implements IValidator {
+
+ public IStatus validate(Builder builder) {
+ IStatus status = Status.OK_STATUS;
+
+ // Get actual BSN
+ String actual = builder.getBsn();
+
+ // Get expected BSN from file name
+ String expected = null;
+ if (builder.getPropertiesFile() != null)
+ expected = builder.getPropertiesFile().getName();
+ String projectName = builder.getBase().getName();
+ if (expected == null || expected.equals("bnd.bnd")) {
+ expected = projectName;
+ } else if (expected.endsWith(".bnd")) {
+ expected = expected.substring(0, expected.length() - ".bnd".length());
+ if (!expected.startsWith(builder.getBase().getName()))
+ expected = projectName + "." + expected;
+ }
+
+ // Report error if not matching
+ if (!actual.equals(expected))
+ status = new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, String.format("Bundle-SymbolicName '%s' is not valid for builder: %s", actual, builder.getPropertiesFile().getName()), null);
+
+ return status;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.