[jboss-cvs] JBossAS SVN: r112692 - projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Feb 24 10:34:29 EST 2012
Author: jesper.pedersen
Date: 2012-02-24 10:34:29 -0500 (Fri, 24 Feb 2012)
New Revision: 112692
Added:
projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SecurityActions.java
Modified:
projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/EndpointImpl.java
projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SimpleResourceAdapterRepository.java
Log:
[JBJCA-743] Improve bean validation for inflow
Modified: projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/EndpointImpl.java
===================================================================
--- projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/EndpointImpl.java 2012-02-24 15:33:31 UTC (rev 112691)
+++ projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/EndpointImpl.java 2012-02-24 15:34:29 UTC (rev 112692)
@@ -23,10 +23,13 @@
package org.jboss.jca.core.rar;
import org.jboss.jca.core.CoreBundle;
+import org.jboss.jca.core.CoreLogger;
import org.jboss.jca.core.bv.BeanValidationUtil;
import org.jboss.jca.core.spi.rar.Endpoint;
import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Set;
import javax.resource.ResourceException;
@@ -38,6 +41,7 @@
import javax.validation.groups.Default;
import org.jboss.logging.Messages;
+import org.jboss.logging.Logger;
/**
* An endpoint representation
@@ -48,17 +52,30 @@
{
/** The reference to the resource adapter instance */
private WeakReference<ResourceAdapter> ra;
+
+ /** Is the resource adapter a 1.6 archive */
+ private boolean is16;
+
+ /** Bean validation groups */
+ private Set<String> beanValidationGroups;
/** The bundle */
private static CoreBundle bundle = Messages.getBundle(CoreBundle.class);
+ /** The logger */
+ private static CoreLogger log = Logger.getMessageLogger(CoreLogger.class, Endpoint.class.getName());
+
/**
* Constructor
* @param ra The resource adapter reference
+ * @param is26 Is the resource adapter a 1.6 archive
+ * @param bvg The bean validation groups
*/
- EndpointImpl(WeakReference<ResourceAdapter> ra)
+ EndpointImpl(WeakReference<ResourceAdapter> ra, boolean is16, Set<String> bvg)
{
this.ra = ra;
+ this.is16 = is16;
+ this.beanValidationGroups = bvg;
}
/**
@@ -81,12 +98,52 @@
spec.validate();
- Validator validator = BeanValidationUtil.createValidator();
- Set errors = validator.validate(spec, Default.class);
+ if (is16)
+ {
+ ClassLoader oldTCCL = SecurityActions.getThreadContextClassLoader();
+ try
+ {
+ SecurityActions.setThreadContextClassLoader(rar.getClass().getClassLoader());
+
+ List<Class<?>> groups = new ArrayList<Class<?>>(1);
- if (errors != null && errors.size() > 0)
- {
- throw new ResourceException(bundle.validationException(), new ConstraintViolationException(errors));
+ if (beanValidationGroups != null)
+ {
+ for (String group : beanValidationGroups)
+ {
+ try
+ {
+ Class<?> clz = Class.forName(group, true, rar.getClass().getClassLoader());
+ groups.add(clz);
+ }
+ catch (Throwable t)
+ {
+ log.debug("Unable to load bean validation group: " + group, t);
+ }
+ }
+ }
+
+ if (groups.isEmpty())
+ groups.add(Default.class);
+
+ Validator validator = BeanValidationUtil.createValidator();
+ Class[] vargs = groups.toArray(new Class[groups.size()]);
+
+ Set errors = validator.validate(spec, vargs);
+
+ if (errors != null && errors.size() > 0)
+ {
+ throw new ResourceException(bundle.validationException(), new ConstraintViolationException(errors));
+ }
+ }
+ catch (RuntimeException re)
+ {
+ throw new ResourceException(bundle.validationException(), re);
+ }
+ finally
+ {
+ SecurityActions.setThreadContextClassLoader(oldTCCL);
+ }
}
rar.endpointActivation(endpointFactory, spec);
Added: projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SecurityActions.java
===================================================================
--- projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SecurityActions.java (rev 0)
+++ projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SecurityActions.java 2012-02-24 15:34:29 UTC (rev 112692)
@@ -0,0 +1,80 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.jca.core.rar;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * Privileged Blocks
+ *
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+class SecurityActions
+{
+ /**
+ * Set the context classloader.
+ * @param cl classloader
+ */
+ public static void setThreadContextClassLoader(final ClassLoader cl)
+ {
+ if (System.getSecurityManager() == null)
+ {
+ Thread.currentThread().setContextClassLoader(cl);
+ }
+ else
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ Thread.currentThread().setContextClassLoader(cl);
+
+ return null;
+ }
+ });
+ }
+ }
+
+ /**
+ * Get the context classloader.
+ * @return The classloader
+ */
+ public static ClassLoader getThreadContextClassLoader()
+ {
+ if (System.getSecurityManager() == null)
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ else
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+ {
+ public ClassLoader run()
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ });
+ }
+ }
+}
Modified: projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SimpleResourceAdapterRepository.java
===================================================================
--- projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SimpleResourceAdapterRepository.java 2012-02-24 15:33:31 UTC (rev 112691)
+++ projects/jboss-jca/branches/Branch_1_0/core/src/main/java/org/jboss/jca/core/rar/SimpleResourceAdapterRepository.java 2012-02-24 15:34:29 UTC (rev 112692)
@@ -22,12 +22,15 @@
package org.jboss.jca.core.rar;
+import org.jboss.jca.common.api.metadata.ironjacamar.IronJacamar;
import org.jboss.jca.common.api.metadata.ra.ConfigProperty;
import org.jboss.jca.common.api.metadata.ra.Connector;
import org.jboss.jca.common.api.metadata.ra.RequiredConfigProperty;
import org.jboss.jca.common.api.metadata.ra.ResourceAdapter1516;
import org.jboss.jca.common.api.metadata.ra.ra15.Activationspec15;
+import org.jboss.jca.common.api.metadata.ra.ra15.Connector15;
import org.jboss.jca.common.api.metadata.ra.ra16.Activationspec16;
+import org.jboss.jca.common.api.metadata.ra.ra16.Connector16;
import org.jboss.jca.core.CoreBundle;
import org.jboss.jca.core.CoreLogger;
import org.jboss.jca.core.spi.mdr.MetadataRepository;
@@ -295,7 +298,11 @@
if (ra.get() == null)
throw new NotFoundException(bundle.keyNotRegistered(uniqueId));
- return new EndpointImpl(ra);
+ String mdrIdentifier = getMDRIdentifier(ra.get());
+ boolean is16 = is16(mdrIdentifier);
+ Set<String> beanValidationGroups = getBeanValidationGroups(mdrIdentifier);
+
+ return new EndpointImpl(ra, is16, beanValidationGroups);
}
/**
@@ -492,6 +499,98 @@
}
/**
+ * Get MDR identifier
+ * @param ra The resource adapter
+ * @return The identifier
+ */
+ private String getMDRIdentifier(ResourceAdapter ra)
+ {
+ for (String id : mdr.getResourceAdapters())
+ {
+ try
+ {
+ Connector raXml = mdr.getResourceAdapter(id);
+ if (raXml != null && raXml instanceof Connector15)
+ {
+ if (raXml.getResourceadapter() != null)
+ {
+ ResourceAdapter1516 ra1516 = (ResourceAdapter1516)raXml.getResourceadapter();
+ if (ra1516.getResourceadapterClass() != null && !ra1516.getResourceadapterClass().equals(""))
+ {
+ if (ra.getClass().getName().equals(ra1516.getResourceadapterClass()))
+ return id;
+ }
+ }
+ }
+ }
+ catch (Throwable t)
+ {
+ log.debug("Exception while loading id: " + id, t);
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Is the resource adapter a 1.6 archive
+ * @param id The MDR identifier
+ * @return True if 1.6; otherwise false
+ */
+ private boolean is16(String id)
+ {
+ if (id == null || id.equals(""))
+ return false;
+
+ try
+ {
+ Connector raXml = mdr.getResourceAdapter(id);
+ if (raXml != null)
+ {
+ return (raXml instanceof Connector16);
+ }
+ }
+ catch (Throwable t)
+ {
+ log.debug("Exception while loading ra.xml: " + id, t);
+ }
+
+ return false;
+ }
+
+ /**
+ * Get the bean validation groups
+ * @param id The MDR identifier
+ * @return The groups; <code>null</code> if none were found
+ */
+ private Set<String> getBeanValidationGroups(String id)
+ {
+ if (id == null || id.equals(""))
+ return null;
+
+ try
+ {
+ IronJacamar ij = mdr.getIronJacamar(id);
+ if (ij != null && ij.getBeanValidationGroups() != null && ij.getBeanValidationGroups().size() > 0)
+ {
+ Set<String> groups = new HashSet<String>();
+ for (String group : ij.getBeanValidationGroups())
+ {
+ groups.add(group);
+ }
+ return groups;
+ }
+ }
+ catch (Throwable t)
+ {
+ log.debug("Exception while loading ironjacamar.xml: " + id, t);
+ }
+
+ return null;
+ }
+
+
+ /**
* String representation
* @return The string
*/
More information about the jboss-cvs-commits
mailing list