Today I integrated javax.validation into Vaadin. Vaadin offers built-in support for it. But since I am using OSGi, I had to do it a bit differently.
The idea was the following:
- register an instance of javax.validation.ValidatorFactory as an OSGi service
- write a Vaadin Validator that will consume this OSGi service
And it was pretty fast. It took me about 30 min and I was done:
javax.validation.ValidatorFactory as an OSGi service
Therefore I have created a bundle org.lunifera.runtime.jsr303.validation, put in an activator and registered the OSGi service.
public class Activator implements BundleActivator { private ServiceRegistration<ValidatorFactory> registry; @Override public void start(BundleContext context) throws Exception { // provide the bean validation factory ValidatorFactory avf = Validation .byProvider(ApacheValidationProvider.class).configure() .buildValidatorFactory(); registry = context.registerService(ValidatorFactory.class, avf, null); } @Override public void stop(BundleContext context) throws Exception { if (registry != null) { registry.unregister(); registry = null; } } }
Prepare VaadinBeanValidator
Vaadin comes with a com.vaadin.data.Validator implementation for JSR303 (bean validation) called BeanValidator. Since it is licensed under the Apache License v2, I copied the validator and changed the way how the implementation will get access to the validation factory.
// access the OSGi service registry to get an instance of ValidatorFactory BundleContext context = FrameworkUtil.getBundle(BeanValidationValidator.class) .getBundleContext(); ServiceReference<ValidatorFactory> ref = context.getServiceReference(ValidatorFactory.class); javaxBeanValidatorFactory = context.getService(ref);
It was really fast and straightforward to combine Vaadin and OSGi for javax.validation.
Available by P2
If you would like to use the org.lunifera.runtime.jsr303.validation bundle, it is available the Lunifera P2.
The feature is called org.lunifera.runtime.jsr303.validation – Lunifera runtime: javax.validation ValidationFactory provider. And it is licensed under the EPL Eclipse Public License v1.
So feel free to use it…
Tags: Florian Pirchner, lunifera, OSGi, Vaadin
March 18, 2015 at 4:50 pm |
[…] Have a look at his solution on his blog! […]