You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/10/11 20:38:27 UTC

svn commit: r583907 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/model/dataformat/ components/camel-jaxb/src/test/java/org/apache/camel/example/ components/camel-jaxb/src/test/re...

Author: jstrachan
Date: Thu Oct 11 11:38:25 2007
New Revision: 583907

URL: http://svn.apache.org/viewvc?rev=583907&view=rev
Log:
fix for https://issues.apache.org/activemq/browse/CAMEL-176 to allow marshal/unmarshal to use a referenced data format configured using a separate tag and referenced by ID

Added:
    activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/SpringDataFormatWithMarshalAndUnmarshalUsingRefTest.java   (with props)
    activemq/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/marshalAndUnmarshalWithRef.xml   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatType.java
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalType.java?rev=583907&r1=583906&r2=583907&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalType.java Thu Oct 11 11:38:25 2007
@@ -19,14 +19,19 @@
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
-import javax.xml.bind.annotation.XmlElementRef;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElements;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.camel.Processor;
-import org.apache.camel.processor.MarshalProcessor;
-import org.apache.camel.spi.DataFormat;
 import org.apache.camel.impl.RouteContext;
+import org.apache.camel.model.dataformat.ArtixDSDataFormat;
 import org.apache.camel.model.dataformat.DataFormatType;
+import org.apache.camel.model.dataformat.JaxbDataFormat;
+import org.apache.camel.model.dataformat.SerializationDataFormat;
+import org.apache.camel.model.dataformat.XMLBeansDataFormat;
+import org.apache.camel.processor.MarshalProcessor;
+import org.apache.camel.spi.DataFormat;
 import static org.apache.camel.util.ObjectHelper.notNull;
 
 /**
@@ -39,7 +44,14 @@
 public class MarshalType extends OutputType {
     @XmlAttribute(required = false)
     private String ref;
-    @XmlElementRef
+    // TODO cannot use @XmlElementRef as it doesn't allow optional properties
+    // @XmlElementRef
+    @XmlElements({
+    @XmlElement(required = false, name = "artixDS", type = ArtixDSDataFormat.class),
+    @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class),
+    @XmlElement(required = false, name = "serialization", type = SerializationDataFormat.class),
+    @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class)}
+    )
     private DataFormatType dataFormatType;
 
     public MarshalType() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalType.java?rev=583907&r1=583906&r2=583907&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalType.java Thu Oct 11 11:38:25 2007
@@ -21,10 +21,16 @@
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElementRef;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlElements;
+import javax.xml.bind.annotation.XmlElement;
 
 import org.apache.camel.Processor;
 import org.apache.camel.impl.RouteContext;
 import org.apache.camel.model.dataformat.DataFormatType;
+import org.apache.camel.model.dataformat.ArtixDSDataFormat;
+import org.apache.camel.model.dataformat.JaxbDataFormat;
+import org.apache.camel.model.dataformat.SerializationDataFormat;
+import org.apache.camel.model.dataformat.XMLBeansDataFormat;
 import org.apache.camel.processor.UnmarshalProcessor;
 import org.apache.camel.spi.DataFormat;
 import static org.apache.camel.util.ObjectHelper.notNull;
@@ -39,7 +45,14 @@
 public class UnmarshalType extends OutputType {
     @XmlAttribute(required = false)
     private String ref;
-    @XmlElementRef
+    // TODO cannot use @XmlElementRef as it doesn't allow optional properties
+    // @XmlElementRef
+    @XmlElements({
+    @XmlElement(required = false, name = "artixDS", type = ArtixDSDataFormat.class),
+    @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class),
+    @XmlElement(required = false, name = "serialization", type = SerializationDataFormat.class),
+    @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class)}
+    )
     private DataFormatType dataFormatType;
 
     public UnmarshalType() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatType.java?rev=583907&r1=583906&r2=583907&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatType.java Thu Oct 11 11:38:25 2007
@@ -23,16 +23,17 @@
 import javax.xml.bind.annotation.XmlType;
 
 import org.apache.camel.impl.RouteContext;
+import org.apache.camel.model.IdentifiedType;
 import org.apache.camel.spi.DataFormat;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * @version $Revision: 1.1 $
  */
 @XmlType(name = "dataFormatType")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class DataFormatType {
+public class DataFormatType extends IdentifiedType {
     @XmlTransient
     private DataFormat dataFormat;
     @XmlTransient

Added: activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/SpringDataFormatWithMarshalAndUnmarshalUsingRefTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/SpringDataFormatWithMarshalAndUnmarshalUsingRefTest.java?rev=583907&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/SpringDataFormatWithMarshalAndUnmarshalUsingRefTest.java (added)
+++ activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/SpringDataFormatWithMarshalAndUnmarshalUsingRefTest.java Thu Oct 11 11:38:25 2007
@@ -0,0 +1,34 @@
+/**
+ * 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.camel.example;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spring.SpringTestSupport;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class SpringDataFormatWithMarshalAndUnmarshalUsingRefTest extends SpringDataFormatTest {
+
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/example/marshalAndUnmarshalWithRef.xml");
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/example/SpringDataFormatWithMarshalAndUnmarshalUsingRefTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/marshalAndUnmarshalWithRef.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/marshalAndUnmarshalWithRef.xml?rev=583907&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/marshalAndUnmarshalWithRef.xml (added)
+++ activemq/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/marshalAndUnmarshalWithRef.xml Thu Oct 11 11:38:25 2007
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+       http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
+    ">
+
+  <!-- START SNIPPET: example -->
+  <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
+    <route>
+      <from uri="direct:start"/>
+      <marshal ref="myJaxb"/>
+      <to uri="direct:marshalled"/>
+    </route>
+    <route>
+      <from uri="direct:marshalled"/>
+      <unmarshal ref="myJaxb"/>
+      <to uri="mock:result"/>
+    </route>
+
+    <jaxb id="myJaxb" prettyPrint="true" contextPath="org.apache.camel.example"/>
+  </camelContext>
+  <!-- END SNIPPET: example -->
+
+</beans>

Propchange: activemq/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/marshalAndUnmarshalWithRef.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java?rev=583907&r1=583906&r2=583907&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java Thu Oct 11 11:38:25 2007
@@ -16,11 +16,26 @@
  */
 package org.apache.camel.spring;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementRef;
+import javax.xml.bind.annotation.XmlElements;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.IdentifiedType;
 import org.apache.camel.model.RouteContainer;
 import org.apache.camel.model.RouteType;
+import org.apache.camel.model.dataformat.DataFormatType;
 import org.apache.camel.spi.InstrumentationAgent;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -33,12 +48,6 @@
 import org.springframework.context.ApplicationListener;
 import org.springframework.context.event.ContextRefreshedEvent;
 
-import javax.management.MBeanServer;
-import javax.xml.bind.annotation.*;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
 /**
  * A Spring {@link FactoryBean} to create and initialize a
  * {@link SpringCamelContext} and install routes either explicitly configured in
@@ -60,6 +69,8 @@
     private List<EndpointFactoryBean> endpoints;
     @XmlElement(name = "route", required = false)
     private List<RouteType> routes = new ArrayList<RouteType>();
+    @XmlElementRef
+    private List<DataFormatType> dataFormats;
     @XmlAttribute(required = false)
     private Boolean useJmx;
     @XmlAttribute(required = false)

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java?rev=583907&r1=583906&r2=583907&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java Thu Oct 11 11:38:25 2007
@@ -16,7 +16,9 @@
  */
 package org.apache.camel.spring.handler;
 
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import javax.xml.bind.JAXBContext;
@@ -24,13 +26,16 @@
 import javax.xml.bind.Unmarshaller;
 
 import org.apache.camel.builder.xml.XPathBuilder;
+import org.apache.camel.model.dataformat.ArtixDSDataFormat;
+import org.apache.camel.model.dataformat.JaxbDataFormat;
+import org.apache.camel.model.dataformat.SerializationDataFormat;
+import org.apache.camel.model.dataformat.XMLBeansDataFormat;
 import org.apache.camel.spring.CamelBeanPostProcessor;
 import org.apache.camel.spring.CamelContextFactoryBean;
 import org.apache.camel.spring.EndpointFactoryBean;
 import org.apache.camel.spring.remoting.CamelProxyFactoryBean;
 import org.apache.camel.spring.remoting.CamelServiceExporter;
 import org.apache.camel.util.ObjectHelper;
-import static org.apache.camel.util.ObjectHelper.isNotNullAndNonEmpty;
 import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.config.RuntimeBeanReference;
@@ -47,17 +52,24 @@
     public static final String JAXB_PACKAGES = "org.apache.camel.spring:org.apache.camel.model:org.apache.camel.model.config:org.apache.camel.model.dataformat:org.apache.camel.model.language";
 
     protected BeanDefinitionParser endpointParser = new BeanDefinitionParser(EndpointFactoryBean.class);
-    protected BeanDefinitionParser proxyParser = new BeanDefinitionParser(CamelProxyFactoryBean.class);
-    protected BeanDefinitionParser exportParser = new BeanDefinitionParser(CamelServiceExporter.class);
     protected BeanDefinitionParser beanPostProcessorParser = new BeanDefinitionParser(CamelBeanPostProcessor.class);
 
     protected Set<String> parserElementNames = new HashSet<String>();
     private JAXBContext jaxbContext;
+    private Map<String, BeanDefinitionParser> parserMap =new HashMap<String, BeanDefinitionParser>();
 
     public void init() {
+        // remoting
+        addBeanDefinitionParser("proxy", CamelProxyFactoryBean.class);
+        addBeanDefinitionParser("export", CamelServiceExporter.class);
+
+        // data types
+        addBeanDefinitionParser("artixDS", ArtixDSDataFormat.class);
+        addBeanDefinitionParser("jaxb", JaxbDataFormat.class);
+        addBeanDefinitionParser("serialization", SerializationDataFormat.class);
+        addBeanDefinitionParser("xmlBeans", XMLBeansDataFormat.class);
+
         registerParser("endpoint", endpointParser);
-        registerParser("proxy", proxyParser);
-        registerParser("export", exportParser);
 
         registerParser("camelContext", new CamelContextBeanDefinitionParser(CamelContextFactoryBean.class));
 
@@ -73,6 +85,12 @@
         });
     }
 
+    private void addBeanDefinitionParser(String elementName, Class<?> type) {
+        BeanDefinitionParser parser = new BeanDefinitionParser(type);
+        registerParser(elementName, parser);
+        parserMap.put(elementName, parser);
+    }
+
     protected void createBeanPostProcessor(ParserContext parserContext, String contextId, Element childElement) {
         String beanPostProcessorId = contextId + ":beanPostProcessor";
         childElement.setAttribute("id", beanPostProcessorId);
@@ -164,17 +182,14 @@
                             // builder.getBeanDefinition());
                             parserContext.registerComponent(new BeanComponentDefinition(definition, id));
                         }
-                    } else if (localName.equals("proxy")) {
-                        BeanDefinition definition = proxyParser.parse(childElement, parserContext);
-                        String id = childElement.getAttribute("id");
-                        if (ObjectHelper.isNotNullAndNonEmpty(id)) {
-                            parserContext.registerComponent(new BeanComponentDefinition(definition, id));
-                        }
-                    } else if (localName.equals("export")) {
-                        BeanDefinition definition = exportParser.parse(childElement, parserContext);
-                        String id = childElement.getAttribute("id");
-                        if (ObjectHelper.isNotNullAndNonEmpty(id)) {
-                            parserContext.registerComponent(new BeanComponentDefinition(definition, id));
+                    } else {
+                        BeanDefinitionParser parser = parserMap.get(localName);
+                        if (parser != null) {
+                            BeanDefinition definition = parser.parse(childElement, parserContext);
+                            String id = childElement.getAttribute("id");
+                            if (ObjectHelper.isNotNullAndNonEmpty(id)) {
+                                parserContext.registerComponent(new BeanComponentDefinition(definition, id));
+                            }
                         }
                     }
                 }

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java?rev=583907&r1=583906&r2=583907&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/remoting/CamelServiceExporter.java Thu Oct 11 11:38:25 2007
@@ -22,15 +22,14 @@
 import org.apache.camel.Endpoint;
 import org.apache.camel.component.bean.BeanProcessor;
 import org.apache.camel.util.CamelContextHelper;
-
+import static org.apache.camel.util.ObjectHelper.notNull;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.FactoryBean;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.remoting.support.RemoteExporter;
-
-import static org.apache.camel.util.ObjectHelper.notNull;
 
 /**
  * A {@link FactoryBean} to create a proxy to a service exposing a given {@link #getServiceInterface()}