[jboss-cvs] JBossAS SVN: r97851 - in projects/metadata/ejb/trunk/src: test/java/org/jboss/metadata/ejb/test and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Dec 15 12:02:37 EST 2009
Author: jaikiran
Date: 2009-12-15 12:02:36 -0500 (Tue, 15 Dec 2009)
New Revision: 97851
Added:
projects/metadata/ejb/trunk/src/main/java/org/jboss/metadata/process/processor/ejb/jboss/JNDIBindingPolicyProcessor.java
projects/metadata/ejb/trunk/src/test/java/org/jboss/metadata/ejb/test/jndibindingpolicy/
projects/metadata/ejb/trunk/src/test/java/org/jboss/metadata/ejb/test/jndibindingpolicy/unit/
projects/metadata/ejb/trunk/src/test/java/org/jboss/metadata/ejb/test/jndibindingpolicy/unit/JNDIBindingPolicyTestCase.java
projects/metadata/ejb/trunk/src/test/resources/org/jboss/metadata/ejb/test/jndibindingpolicy/
projects/metadata/ejb/trunk/src/test/resources/org/jboss/metadata/ejb/test/jndibindingpolicy/jboss.xml
Log:
JBMETA-232 Added a JBossMetadataProcessor which is responsible for setting the correct jndi-binding-policy on each bean metadata
Added: projects/metadata/ejb/trunk/src/main/java/org/jboss/metadata/process/processor/ejb/jboss/JNDIBindingPolicyProcessor.java
===================================================================
--- projects/metadata/ejb/trunk/src/main/java/org/jboss/metadata/process/processor/ejb/jboss/JNDIBindingPolicyProcessor.java (rev 0)
+++ projects/metadata/ejb/trunk/src/main/java/org/jboss/metadata/process/processor/ejb/jboss/JNDIBindingPolicyProcessor.java 2009-12-15 17:02:36 UTC (rev 97851)
@@ -0,0 +1,94 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.metadata.process.processor.ejb.jboss;
+
+import org.jboss.logging.Logger;
+import org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData;
+import org.jboss.metadata.ejb.jboss.JBossEnterpriseBeansMetaData;
+import org.jboss.metadata.ejb.jboss.JBossMetaData;
+import org.jboss.metadata.process.ProcessingException;
+import org.jboss.metadata.process.processor.JBossMetaDataProcessor;
+
+/**
+ * JNDIBindingPolicyProcessor
+ *
+ * A {@link JBossMetaDataProcessor} which ensures that if a bean does not set the
+ * jndi-binding-policy explicitly and if a jndi-binding-policy is set at the deployment level
+ * (through top-level in jboss.xml), then that deployment level jndi-binding-policy gets applied to
+ * the bean.
+ *
+ * @see JBMETA-232 https://jira.jboss.org/jira/browse/JBMETA-232
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+public class JNDIBindingPolicyProcessor implements JBossMetaDataProcessor<JBossMetaData>
+{
+
+ /**
+ * Logger
+ */
+ private static Logger logger = Logger.getLogger(JNDIBindingPolicyProcessor.class);
+
+ /**
+ * Applies a deployment level jndi-binding-policy (if any), to a bean, if the bean
+ * already hasn't explicitly set one.
+ *
+ * @see org.jboss.metadata.process.processor.JBossMetaDataProcessor#process(org.jboss.metadata.ejb.jboss.JBossMetaData)
+ */
+ @Override
+ public JBossMetaData process(JBossMetaData metadata) throws ProcessingException
+ {
+ // for each enterprise bean, if the jndi-binding-policy is not set at the
+ // bean level, then use the one that's set at the top-level of jboss.xml.
+ // The top level jndi-binding-policy applies to all beans which don't
+ // explicitly set the jndi-binding-policy
+ JBossEnterpriseBeansMetaData enterpriseBeans = metadata.getEnterpriseBeans();
+ if (enterpriseBeans == null || enterpriseBeans.isEmpty())
+ {
+ return metadata;
+ }
+ // for each bean, check if the jndi-binding-policy needs to be set
+ for (JBossEnterpriseBeanMetaData enterpriseBean : enterpriseBeans)
+ {
+ String jndiBindingPolicyOnBean = enterpriseBean.getJndiBindingPolicy();
+ // if the bean does *not* specify the jndi-binding-policy then look
+ // at the top-level jndi-binding-policy in jboss.xml
+ if (jndiBindingPolicyOnBean == null)
+ {
+ // the jndi-binding-policy at top-level in jboss.xml
+ String jndiBindingPolicyForAllBeansInTheDeployment = metadata.getJndiBindingPolicy();
+ if (jndiBindingPolicyForAllBeansInTheDeployment != null)
+ {
+ if (logger.isTraceEnabled())
+ {
+ logger.trace("Applying jndi-binding-policy " + jndiBindingPolicyForAllBeansInTheDeployment
+ + " to bean named " + enterpriseBean.getEjbName());
+ }
+ enterpriseBean.setJndiBindingPolicy(jndiBindingPolicyForAllBeansInTheDeployment);
+ }
+ }
+ }
+ return metadata;
+ }
+
+}
Added: projects/metadata/ejb/trunk/src/test/java/org/jboss/metadata/ejb/test/jndibindingpolicy/unit/JNDIBindingPolicyTestCase.java
===================================================================
--- projects/metadata/ejb/trunk/src/test/java/org/jboss/metadata/ejb/test/jndibindingpolicy/unit/JNDIBindingPolicyTestCase.java (rev 0)
+++ projects/metadata/ejb/trunk/src/test/java/org/jboss/metadata/ejb/test/jndibindingpolicy/unit/JNDIBindingPolicyTestCase.java 2009-12-15 17:02:36 UTC (rev 97851)
@@ -0,0 +1,131 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.metadata.ejb.test.jndibindingpolicy.unit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.net.URL;
+
+import org.jboss.metadata.ejb.jboss.JBoss50MetaData;
+import org.jboss.metadata.ejb.jboss.JBoss51MetaData;
+import org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData;
+import org.jboss.metadata.ejb.jboss.JBossMetaData;
+import org.jboss.metadata.process.processor.ejb.jboss.JNDIBindingPolicyProcessor;
+import org.jboss.xb.binding.JBossXBException;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MutableSchemaResolver;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * JNDIBindingPolicyTestCase
+ * Tests that the jndi-binding-policy is set correctly in the metadata.
+ *
+ * More specifically, this testcase is meant to test the fix for JBMETA-232
+ * @see JBMETA-232 https://jira.jboss.org/jira/browse/JBMETA-232
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+public class JNDIBindingPolicyTestCase
+{
+
+ private static MutableSchemaResolver schemaBindingResolver;
+
+ private static UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory.newInstance();
+
+ @BeforeClass
+ public static void beforeClass()
+ {
+ schemaBindingResolver = new MultiClassSchemaResolver();
+ schemaBindingResolver.mapLocationToClass("jboss_5_0.xsd", JBoss50MetaData.class);
+ schemaBindingResolver.mapLocationToClass("jboss_5_1.xsd", JBoss51MetaData.class);
+ }
+
+ /**
+ * Test that when a jndi-binding-policy is set at the deployment level (in jboss.xml),
+ * it gets applied to beans which don't explicitly specify one.
+ */
+ @Test
+ public void testJNDIBindingPolicyAtTopLevelOfJBossXml() throws Exception
+ {
+ JBoss50MetaData jbossMetadata = unmarshal(JBoss50MetaData.class,
+ "/org/jboss/metadata/ejb/test/jndibindingpolicy/jboss.xml");
+
+ assertNotNull("Metadata created out of jboss.xml was null", jbossMetadata);
+
+ // run the metadata through a processor which is responsible for setting the
+ // correct jndi-binding-policy on each bean
+ JBossMetaData processedMetadata = new JNDIBindingPolicyProcessor().process(jbossMetadata);
+
+ // make sure that the jndi-binding-policy at top level jboss.xml is set
+ String jndiBindingPolicy = processedMetadata.getJndiBindingPolicy();
+ assertNotNull("jndi-binding-policy not set in metadata created out of jboss.xml", jndiBindingPolicy);
+ // This is what is set in the jboss.xml
+ final String deploymentLevelJndiBindingPolicy = "org.jboss.metadata.ejb.test.jndibindingpolicy.DeploymentLevelPolicy";
+ // make sure the metadata contains the correct policy
+ assertEquals("Incorrect jndi-binding-policy set in metadata created out of jboss.xml",
+ deploymentLevelJndiBindingPolicy, jndiBindingPolicy);
+
+ // now make sure that each of the EJBs configured in the jboss.xml, uses the correct jndi-binding-policy
+ final String dummyBeanName = "DummyBean";
+ JBossEnterpriseBeanMetaData dummyBean = processedMetadata.getEnterpriseBean(dummyBeanName);
+ assertNotNull("Bean named " + dummyBeanName + " was not found in metadata, created out of jboss.xml", dummyBean);
+ // ensure it's the correct policy
+ assertEquals("Incorrect jndi-binding-policy set in enterprise bean metadata of bean " + dummyBeanName,
+ deploymentLevelJndiBindingPolicy, dummyBean.getJndiBindingPolicy());
+
+ // check the bean with the overriden jndi-binding-policy
+ final String overridenBeanName = "OverridenJndiBindingPolicyBean";
+ final String expectedJndiPolicyOnBean = "org.jboss.metadata.ejb.test.jndibindingpolicy.BeanSpecificPolicy";
+ JBossEnterpriseBeanMetaData beanWithOverridenJndiBindingPolicy = processedMetadata
+ .getEnterpriseBean(overridenBeanName);
+ assertNotNull("Bean named " + overridenBeanName + " was not found in metadata, created out of jboss.xml",
+ beanWithOverridenJndiBindingPolicy);
+ // ensure it's the correct policy
+ assertEquals("Incorrect jndi-binding-policy set in enterprise bean metadata of bean " + overridenBeanName,
+ expectedJndiPolicyOnBean, beanWithOverridenJndiBindingPolicy.getJndiBindingPolicy());
+ }
+
+ /**
+ * Utility method
+ *
+ * @param <T>
+ * @param type
+ * @param resource
+ * @return
+ * @throws JBossXBException
+ */
+ private static <T> T unmarshal(Class<T> type, String resource) throws JBossXBException
+ {
+ Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller();
+ unmarshaller.setValidation(false);
+ URL url = type.getResource(resource);
+ if (url == null)
+ throw new IllegalArgumentException("Failed to find resource " + resource);
+ return type.cast(unmarshaller.unmarshal(url.toString(), schemaBindingResolver));
+ }
+
+}
Added: projects/metadata/ejb/trunk/src/test/resources/org/jboss/metadata/ejb/test/jndibindingpolicy/jboss.xml
===================================================================
--- projects/metadata/ejb/trunk/src/test/resources/org/jboss/metadata/ejb/test/jndibindingpolicy/jboss.xml (rev 0)
+++ projects/metadata/ejb/trunk/src/test/resources/org/jboss/metadata/ejb/test/jndibindingpolicy/jboss.xml 2009-12-15 17:02:36 UTC (rev 97851)
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- $Id: jboss.xml 68870 2008-01-11 09:47:56Z wolfc $ -->
+<jboss xmlns="http://www.jboss.com/xml/ns/javaee"
+ xmlns:jee="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss_5_0.xsd"
+ version="5.0">
+
+ <jndi-binding-policy>org.jboss.metadata.ejb.test.jndibindingpolicy.DeploymentLevelPolicy</jndi-binding-policy>
+ <enterprise-beans>
+ <session>
+ <ejb-name>DummyBean</ejb-name>
+ </session>
+
+ <session>
+ <ejb-name>OverridenJndiBindingPolicyBean</ejb-name>
+ <jndi-binding-policy>org.jboss.metadata.ejb.test.jndibindingpolicy.BeanSpecificPolicy</jndi-binding-policy>
+ </session>
+
+ </enterprise-beans>
+</jboss>
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list