GERONIMO-5230 Add Bean Validation Support to the connector 1.6 implementation.
Changes to the connector component to support validation.
git-svn-id: https://svn.apache.org/repos/asf/geronimo/components/txmanager/trunk@1005848 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/geronimo-connector/pom.xml b/geronimo-connector/pom.xml
index 74a4c13..22ba2c9 100644
--- a/geronimo-connector/pom.xml
+++ b/geronimo-connector/pom.xml
@@ -60,6 +60,11 @@
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-j2ee-connector_1.6_spec</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-validation_1.0_spec</artifactId>
+ </dependency>
<dependency>
<groupId>junit</groupId>
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/ActivationSpecWrapper.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/ActivationSpecWrapper.java
index aa0c4bf..7b34c29 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/ActivationSpecWrapper.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/ActivationSpecWrapper.java
@@ -17,10 +17,17 @@
package org.apache.geronimo.connector;
+import java.util.Set;
+
import javax.resource.ResourceException;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.ResourceAdapter;
+import javax.resource.spi.InvalidPropertyException;
import javax.resource.spi.endpoint.MessageEndpointFactory;
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
/**
* Wrapper for ActivationSpec instances.
@@ -35,6 +42,9 @@
private final ResourceAdapterWrapper resourceAdapterWrapper;
private final String containerId;
+ private final ValidatorFactory validatorFactory;
+ // indicates we've validated the spec on the first usage.
+ private boolean validated = false;
/**
* Default constructor required when a class is used as a GBean Endpoint.
@@ -43,6 +53,8 @@
activationSpec = null;
containerId = null;
resourceAdapterWrapper = null;
+ validatorFactory = null;
+ validated = false;
}
/**
@@ -55,19 +67,24 @@
public ActivationSpecWrapper(final String activationSpecClass,
final String containerId,
final ResourceAdapterWrapper resourceAdapterWrapper,
- final ClassLoader cl) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
+ final ClassLoader cl,
+ final ValidatorFactory validatorFactory) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Class clazz = cl.loadClass(activationSpecClass);
this.activationSpec = (ActivationSpec) clazz.newInstance();
this.containerId = containerId;
this.resourceAdapterWrapper = resourceAdapterWrapper;
+ this.validatorFactory = validatorFactory;
+ this.validated = false;
}
/**
*/
- public ActivationSpecWrapper(ActivationSpec activationSpec, ResourceAdapterWrapper resourceAdapterWrapper) {
+ public ActivationSpecWrapper(ActivationSpec activationSpec, ResourceAdapterWrapper resourceAdapterWrapper, ValidatorFactory validatorFactory) {
this.activationSpec = activationSpec;
this.resourceAdapterWrapper = resourceAdapterWrapper;
this.containerId = null;
+ this.validatorFactory = validatorFactory;
+ this.validated = false;
}
/**
@@ -90,6 +107,8 @@
//GBeanLifecycle implementation
public void activate(final MessageEndpointFactory messageEndpointFactory) throws ResourceException {
+ checkConstraints(activationSpec);
+
ResourceAdapter resourceAdapter = activationSpec.getResourceAdapter();
if (resourceAdapter == null) {
resourceAdapterWrapper.registerResourceAdapterAssociation(activationSpec);
@@ -108,5 +127,33 @@
throw new IllegalStateException("ActivationSpec was never registered with ResourceAdapter");
}
}
+
+ /**
+ * Validate an ActivationSpec instance on the first usage.
+ *
+ * @param spec The spec instance to validate.
+ */
+ private void checkConstraints(ActivationSpec spec) throws InvalidPropertyException {
+ if (!validated) {
+ // There are two potential validations, self validation via the
+ // validate() method and container-driven validation using bean validation.
+ try {
+ spec.validate();
+ } catch (UnsupportedOperationException uoe) {
+ // ignore if not implemented.
+ }
+ // if we have a validator factory at this point, then validate
+ // the resource adaptor instance
+ if (validatorFactory != null) {
+ Validator validator = validatorFactory.getValidator();
+
+ Set generalSet = validator.validate(spec);
+ if (!generalSet.isEmpty()) {
+ throw new ConstraintViolationException("Constraint violation for ActitvationSpec", generalSet);
+ }
+ }
+ validated = true;
+ }
+ }
}
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/AdminObjectWrapper.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/AdminObjectWrapper.java
new file mode 100644
index 0000000..4b2eefe
--- /dev/null
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/AdminObjectWrapper.java
@@ -0,0 +1,115 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.geronimo.connector;
+
+import java.util.Set;
+
+import javax.resource.spi.ResourceAdapterAssociation;
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+
+
+/**
+ * Wrapper around AdminObject that manages the AdminObject lifecycle.
+ *
+ * @version $Rev$ $Date$
+ */
+public class AdminObjectWrapper {
+
+ private final String adminObjectInterface;
+ private final String adminObjectClass;
+ protected ResourceAdapterWrapper resourceAdapterWrapper;
+
+ protected Object adminObject;
+ private final ValidatorFactory validatorFactory;
+
+ /**
+ * Normal managed constructor.
+ *
+ * @param adminObjectInterface Interface the proxy will implement.
+ * @param adminObjectClass Class of admin object to be wrapped.
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ */
+ public AdminObjectWrapper(final String adminObjectInterface,
+ final String adminObjectClass,
+ final ResourceAdapterWrapper resourceAdapterWrapper,
+ final ClassLoader cl,
+ final ValidatorFactory validatorFactory) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
+ this.adminObjectInterface = adminObjectInterface;
+ this.adminObjectClass = adminObjectClass;
+ this.resourceAdapterWrapper = resourceAdapterWrapper;
+ this.validatorFactory = validatorFactory;
+ Class clazz = cl.loadClass(adminObjectClass);
+ adminObject = clazz.newInstance();
+ }
+
+ public String getAdminObjectInterface() {
+ return adminObjectInterface;
+ }
+
+ /**
+ * Returns class of wrapped AdminObject.
+ * @return class of wrapped AdminObject
+ */
+ public String getAdminObjectClass() {
+ return adminObjectClass;
+ }
+
+
+ /**
+ * Starts the AdminObject. This will validate the instance and register the object instance
+ * with its ResourceAdapter
+ *
+ * @throws Exception if the target failed to start; this will cause a transition to the failed state
+ */
+ public void doStart() throws Exception {
+ // if we have a validator factory at this point, then validate
+ // the resource adaptor instance
+ if (validatorFactory != null) {
+ Validator validator = validatorFactory.getValidator();
+
+ Set generalSet = validator.validate(adminObject);
+ if (!generalSet.isEmpty()) {
+ throw new ConstraintViolationException("Constraint violation for AdminObject " + adminObjectClass, generalSet);
+ }
+ }
+ if (adminObject instanceof ResourceAdapterAssociation) {
+ if (resourceAdapterWrapper == null) {
+ throw new IllegalStateException("Admin object expects to be registered with a ResourceAdapter, but there is no ResourceAdapter");
+ }
+ resourceAdapterWrapper.registerResourceAdapterAssociation((ResourceAdapterAssociation) adminObject);
+ }
+ }
+
+ /**
+ * Stops the target. This is a NOP for AdminObjects
+ *
+ * @throws Exception if the target failed to stop; this will cause a transition to the failed state
+ */
+ public void doStop() throws Exception {
+ }
+
+ /**
+ * Fails the target. This is a nop for an AdminObject.
+ */
+ public void doFail() {
+ }
+}
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/ResourceAdapterWrapper.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/ResourceAdapterWrapper.java
index b8b6113..f41a32c 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/ResourceAdapterWrapper.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/ResourceAdapterWrapper.java
@@ -18,6 +18,7 @@
package org.apache.geronimo.connector;
import java.util.Map;
+import java.util.Set;
import javax.resource.ResourceException;
import javax.resource.spi.ActivationSpec;
@@ -28,6 +29,10 @@
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.transaction.SystemException;
import javax.transaction.xa.XAResource;
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
import org.apache.geronimo.transaction.manager.NamedXAResource;
import org.apache.geronimo.transaction.manager.NamedXAResourceFactory;
@@ -53,6 +58,8 @@
private final Map<String,String> messageListenerToActivationSpecMap;
private final RecoverableTransactionManager transactionManager;
+
+ private final ValidatorFactory validatorFactory;
/**
@@ -65,6 +72,7 @@
this.resourceAdapter = null;
this.messageListenerToActivationSpecMap = null;
this.transactionManager = null;
+ this.validatorFactory = null;
}
public ResourceAdapterWrapper(String name,
@@ -72,7 +80,8 @@
Map<String, String> messageListenerToActivationSpecMap,
BootstrapContext bootstrapContext,
RecoverableTransactionManager transactionManager,
- ClassLoader cl) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
+ ClassLoader cl,
+ ValidatorFactory validatorFactory) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
this.name = name;
this.resourceAdapterClass = resourceAdapterClass;
this.bootstrapContext = bootstrapContext;
@@ -80,15 +89,17 @@
resourceAdapter = (ResourceAdapter) clazz.newInstance();
this.messageListenerToActivationSpecMap = messageListenerToActivationSpecMap;
this.transactionManager = transactionManager;
+ this.validatorFactory = validatorFactory;
}
- public ResourceAdapterWrapper(String name, ResourceAdapter resourceAdapter, Map<String, String> messageListenerToActivationSpecMap, BootstrapContext bootstrapContext, RecoverableTransactionManager transactionManager) {
+ public ResourceAdapterWrapper(String name, ResourceAdapter resourceAdapter, Map<String, String> messageListenerToActivationSpecMap, BootstrapContext bootstrapContext, RecoverableTransactionManager transactionManager, ValidatorFactory validatorFactory) {
this.name = name;
this.resourceAdapterClass = resourceAdapter.getClass().getName();
this.bootstrapContext = bootstrapContext;
this.resourceAdapter = resourceAdapter;
this.messageListenerToActivationSpecMap = messageListenerToActivationSpecMap;
this.transactionManager = transactionManager;
+ this.validatorFactory = validatorFactory;
}
public String getName() {
@@ -141,6 +152,16 @@
}
public void doStart() throws Exception {
+ // if we have a validator factory at this point, then validate
+ // the resource adaptor instance
+ if (validatorFactory != null) {
+ Validator validator = validatorFactory.getValidator();
+
+ Set generalSet = validator.validate(resourceAdapter);
+ if (!generalSet.isEmpty()) {
+ throw new ConstraintViolationException("Constraint violation for ResourceAdapter " + resourceAdapterClass, generalSet);
+ }
+ }
resourceAdapter.start(bootstrapContext);
}
diff --git a/pom.xml b/pom.xml
index 23dbb88..96b09f0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -89,6 +89,12 @@
<artifactId>geronimo-j2ee-connector_1.6_spec</artifactId>
<version>1.0</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-validation_1.0_spec</artifactId>
+ <version>1.1</version>
+ </dependency>
<dependency>
<groupId>org.objectweb.howl</groupId>