svn commit: r638810 - in /activemq/camel/trunk/components/camel-spring/src: main/java/org/apache/camel/spring/handler/ test/java/org/apache/camel/spring/config/ test/resources/org/apache/camel/spring/config/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r638810 - in /activemq/camel/trunk/components/camel-spring/src: main/java/org/apache/camel/spring/handler/ test/java/org/apache/camel/spring/config/ test/resources/org/apache/camel/spring/config/

jstrachan-2
Author: jstrachan
Date: Wed Mar 19 05:11:42 2008
New Revision: 638810

URL: http://svn.apache.org/viewvc?rev=638810&view=rev
Log:
added test case and fix for https://issues.apache.org/activemq/browse/CAMEL-391

Added:
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/NamespacePrefixTest.java
      - copied, changed from r638333, activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest.java
    activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/NamespacePrefixTest-context.xml
      - copied, changed from r638333, activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest-context.xml
Modified:
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/BeanDefinitionParser.java

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/BeanDefinitionParser.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/BeanDefinitionParser.java?rev=638810&r1=638809&r2=638810&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/BeanDefinitionParser.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/BeanDefinitionParser.java Wed Mar 19 05:11:42 2008
@@ -16,16 +16,24 @@
  */
 package org.apache.camel.spring.handler;
 
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
+import org.springframework.core.Conventions;
+import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
+import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
-
-import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
+import org.w3c.dom.NamedNodeMap;
 
 /**
  * A base class for a parser for a bean.
  *
  * @version $Revision$
  */
-public class BeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {
+// TODO cannot use AbstractSimpleBeanDefinitionParser
+// as doParse() is final and isEligableAttribute does not allow us to filter out attributes
+// with the name "xmlns:"
+public class BeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
     private Class type;
 
     protected BeanDefinitionParser() {
@@ -46,8 +54,76 @@
         throw new IllegalArgumentException("No type specified!");
     }
 
-    @Override
     protected boolean isEligibleAttribute(String attributeName) {
-        return attributeName != null && super.isEligibleAttribute(attributeName) && !attributeName.equals("xmlns");
+        return attributeName != null && !ID_ATTRIBUTE.equals(attributeName)
+                && !attributeName.equals("xmlns") && !attributeName.startsWith("xmlns:");
     }
+
+    // TODO the following code is copied from AbstractSimpleBeanDefinitionParser
+    // it can be removed if ever the doParse() method is not final!
+    // or the Spring bug http://jira.springframework.org/browse/SPR-4599 is resolved
+
+    /**
+     * Parse the supplied {@link Element} and populate the supplied
+     * {@link BeanDefinitionBuilder} as required.
+     * <p>This implementation maps any attributes present on the
+     * supplied element to {@link org.springframework.beans.PropertyValue}
+     * instances, and
+     * {@link BeanDefinitionBuilder#addPropertyValue(String, Object) adds them}
+     * to the
+     * {@link org.springframework.beans.factory.config.BeanDefinition builder}.
+     * <p>The {@link #extractPropertyName(String)} method is used to
+     * reconcile the name of an attribute with the name of a JavaBean
+     * property.
+     *
+     * @param element the XML element being parsed
+     * @param builder used to define the <code>BeanDefinition</code>
+     * @see #extractPropertyName(String)
+     */
+    protected final void doParse(Element element, BeanDefinitionBuilder builder) {
+        NamedNodeMap attributes = element.getAttributes();
+        for (int x = 0; x < attributes.getLength(); x++) {
+            Attr attribute = (Attr) attributes.item(x);
+            String name = attribute.getLocalName();
+            String fullName = attribute.getName();
+            if (!fullName.startsWith("xmlns:") && !fullName.equals("xmlns") && isEligibleAttribute(name)) {
+                String propertyName = extractPropertyName(name);
+                Assert.state(StringUtils.hasText(propertyName),
+                        "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
+                builder.addPropertyValue(propertyName, attribute.getValue());
+            }
+        }
+        postProcess(builder, element);
+    }
+
+
+    /**
+     * Extract a JavaBean property name from the supplied attribute name.
+     * <p>The default implementation uses the
+     * {@link Conventions#attributeNameToPropertyName(String)}
+     * method to perform the extraction.
+     * <p>The name returned must obey the standard JavaBean property name
+     * conventions. For example for a class with a setter method
+     * '<code>setBingoHallFavourite(String)</code>', the name returned had
+     * better be '<code>bingoHallFavourite</code>' (with that exact casing).
+     *
+     * @param attributeName the attribute name taken straight from the
+     *                      XML element being parsed (never <code>null</code>)
+     * @return the extracted JavaBean property name (must never be <code>null</code>)
+     */
+    protected String extractPropertyName(String attributeName) {
+        return Conventions.attributeNameToPropertyName(attributeName);
+    }
+
+    /**
+     * Hook method that derived classes can implement to inspect/change a
+     * bean definition after parsing is complete.
+     * <p>The default implementation does nothing.
+     *
+     * @param beanDefinition the parsed (and probably totally defined) bean definition being built
+     * @param element        the XML element that was the source of the bean definition's metadata
+     */
+    protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
+ }
+
 }

Copied: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/NamespacePrefixTest.java (from r638333, activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/NamespacePrefixTest.java?p2=activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/NamespacePrefixTest.java&p1=activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest.java&r1=638333&r2=638810&rev=638810&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/NamespacePrefixTest.java Wed Mar 19 05:11:42 2008
@@ -28,26 +28,19 @@
  * @version $Revision: 1.1 $
  */
 @ContextConfiguration
-public class DependencyInjectCamelTemplateTest extends AbstractJUnit38SpringContextTests {
+public class NamespacePrefixTest extends AbstractJUnit38SpringContextTests {
 
     @Autowired
-    private TemplateUsingBean bean;
+    private CamelTemplate template;
 
     @EndpointInject(uri = "mock:results")
     private MockEndpoint endpoint;
 
     protected String body = "Hello";
 
-    public void testBeanHasCamelTemplateInjected() throws Exception {
-        assertNotNull("Bean should be injected", bean);
-        CamelTemplate template = bean.getTemplate();
-        assertNotNull("Bean should have a CamelTemplate", template);
-
-        endpoint.expectedBodiesReceived(body);
-
-        template.sendBody(body);
-
-        endpoint.assertIsSatisfied();
+    public void testAssertThatInjectionWorks() throws Exception {
+        assertNotNull("Bean should be injected", template);
+        assertNotNull("endpoint should be injected", endpoint);
     }
 
-}
+}
\ No newline at end of file

Copied: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/NamespacePrefixTest-context.xml (from r638333, activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest-context.xml)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/NamespacePrefixTest-context.xml?p2=activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/NamespacePrefixTest-context.xml&p1=activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest-context.xml&r1=638333&r2=638810&rev=638810&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/DependencyInjectCamelTemplateTest-context.xml (original)
+++ activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/NamespacePrefixTest-context.xml Wed Mar 19 05:11:42 2008
@@ -22,13 +22,8 @@
        http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
     ">
 
+  <c:camelContext id="camel" xmlns:c="http://activemq.apache.org/camel/schema/spring">
+    <c:template id="myTemplate" defaultEndpoint="mock:results"/>
+  </c:camelContext>
 
-
-  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
-    <template id="myTemplate" defaultEndpoint="mock:results"/>
-  </camelContext>
-
-  <bean id="myBean" class="org.apache.camel.spring.config.TemplateUsingBean">
-    <property name="template" ref="myTemplate"/>
-  </bean>
 </beans>