For the first time, I am trying to use a Dynamic Constraint Provider for Java not OCL constraints. The approach with static constraints that I have used before worked. Then I replaced the static configuration in the plugin.xml by a Dynamic Constraint Provider:
<constraintProvider class="com.example.XAValidationConstraintProvider"
category="com.example.MyTestConstraint"
cache="true">
<package namespaceUri="http://java.sun.com/xml/ns/persistence"/>
<!-- <constraints categories="com.example.MyTestConstraint">
<constraint
lang="Java"
class="com.example.Constraint"
severity="ERROR"
mode="Batch"
name="MyTest Constraint"
id="com.example.Constraint"
statusCode="1">
<description>
Hard coded constraint
</description>
<message>
Some message
</message>
</constraint>
</constraints> -->
</constraintProvider>
</extension>
<extension
point="org.eclipse.emf.validation.constraintBindings">
<clientContext
default="false"
id="com.example.Context">
<selector class="com.example.Selector"/>
</clientContext>
<binding
context="com.example.Context"
category="com.example.MyTestConstraint"/>
</extension>
XAValidationConstraintProvider:
package com.example;
import java.util.Collection;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.validation.model.Category;
import org.eclipse.emf.validation.model.CategoryManager;
import org.eclipse.emf.validation.model.IModelConstraint;
import org.eclipse.emf.validation.service.AbstractConstraintProvider;
import org.eclipse.emf.validation.service.ConstraintExistsException;
import org.eclipse.emf.validation.service.ConstraintRegistry;
import org.osgi.framework.Bundle;
import testplugin2.Activator;
public class XAValidationConstraintProvider extends AbstractConstraintProvider {
private static final String A_CATEGORY = "category"; //$NON-NLS-1$
@Override
public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
System.out.println("setInitializationData");
super.setInitializationData(config, propertyName, data);
// create the constraint category
String categoryID = config.getDeclaringExtension().getUniqueIdentifier();
if (categoryID == null) {
categoryID = "XAProvider@" + Long.toHexString(System.identityHashCode(this)); //$NON-NLS-1$
}
categoryID = "xa/" + categoryID; //$NON-NLS-1$
Category category = CategoryManager.getInstance().getCategory(categoryID);
//prints: com.example.MyTestConstraint
System.out.println("config.getAttribute(A_CATEGORY): " + config.getAttribute(A_CATEGORY));
category.setName(config.getAttribute(A_CATEGORY));
Bundle contributor = Platform.getBundle(
config.getDeclaringExtension().getNamespaceIdentifier());
ConstraintDescriptor constraintDescriptor = new ConstraintDescriptor();
constraintDescriptor.addCategory(category);
try {
Collection<IModelConstraint> constraints = getConstraints();
constraints.add(createModelConstraint(constraintDescriptor));
registerConstraints(getConstraints());
//proofs that the constraint is registered
System.out.println("reg: " + ConstraintRegistry.getInstance().getDescriptor("com.example.Constraint"));
} catch (ConstraintExistsException e) {
throw new CoreException(new Status(
IStatus.ERROR,
Activator.PLUGIN_ID,
1,
"Registration of XA constraints failed",
e));
}
}
}
If I call in Eclipse Version: 3.6.2 Build id: M20110210-1200 EMF SDK 2.6.1):
IBatchValidator validator = (IBatchValidator)ModelValidationService.getInstance()
.newValidator(EvaluationMode.BATCH);
IStatus status = validator.validate(fetchEObject());
under Window->Preferences->Model Validation->Constraints an entry for constraint generated by the XAValidationConstraintProvider will be added without any error in the error log. However, the validate() of the constraint in com.example.Constraint will neither be triggered by the EMF Validation Framework nor is there any error. What do I have to do to tell the EMF Validation Framework to call my constraint (it is enabled)? Thank you for your help!