activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r534561 - in /activemq/camel/trunk/camel-spring/src: main/java/org/apache/camel/spring/ main/java/org/apache/camel/spring/xml/ main/resources/org/apache/camel/spring/ test/java/org/apache/camel/spring/ test/java/org/apache/camel/spring/exam...
Date Wed, 02 May 2007 17:30:49 GMT
Author: jstrachan
Date: Wed May  2 10:30:48 2007
New Revision: 534561

URL: http://svn.apache.org/viewvc?view=rev&rev=534561
Log:
added support for nested <endpoint> elements within a camelContext for creating top
level endpoints that can be referred to in POJOs

Added:
    activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java
  (with props)
    activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/BeanDefinitionParserSupport.java
  (with props)
    activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/EndpointXmlConfigTest.java
  (with props)
    activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java
  (with props)
    activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/DummyBean.java
  (with props)
    activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/endpointReference.xml
  (with props)
Modified:
    activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java
    activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd

Added: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java?view=auto&rev=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java
(added)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java
Wed May  2 10:30:48 2007
@@ -0,0 +1,95 @@
+/**
+ *
+ * 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.spring;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import static org.apache.camel.util.ObjectHelper.notNull;
+import org.springframework.beans.factory.FactoryBean;
+
+/**
+ * A {@link FactoryBean} which instantiates {@link Endpoint} objects
+ *
+ * @version $Revision: 1.1 $
+ */
+public class EndpointFactoryBean implements FactoryBean {
+    private CamelContext context;
+    private String uri;
+    private Endpoint endpoint;
+    private boolean singleton;
+
+    public Object getObject() throws Exception {
+        if (endpoint == null) {
+            endpoint = createEndpoint();
+        }
+        return endpoint;
+    }
+
+    public Class getObjectType() {
+        return Endpoint.class;
+    }
+
+    public boolean isSingleton() {
+        return singleton;
+    }
+
+    public CamelContext getContext() {
+        return context;
+    }
+
+    /**
+     * Sets the context to use to resolve endpoints
+     *
+     * @param context the context used to resolve endpoints
+     */
+    public void setContext(CamelContext context) {
+        this.context = context;
+    }
+
+    public Endpoint getEndpoint() {
+        return endpoint;
+    }
+
+    public void setEndpoint(Endpoint endpoint) {
+        this.endpoint = endpoint;
+    }
+
+    public void setSingleton(boolean singleton) {
+        this.singleton = singleton;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+
+    /**
+     * Sets the URI to use to resolve the endpoint
+     *
+     * @param uri the URI used to set the endpoint
+     */
+    public void setUri(String uri) {
+        this.uri = uri;
+    }
+
+    protected Endpoint createEndpoint() {
+        notNull(context, "context");
+        notNull(uri, "uri");
+        return context.getEndpoint(uri);
+    }
+
+}

Propchange: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/EndpointFactoryBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/BeanDefinitionParserSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/BeanDefinitionParserSupport.java?view=auto&rev=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/BeanDefinitionParserSupport.java
(added)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/BeanDefinitionParserSupport.java
Wed May  2 10:30:48 2007
@@ -0,0 +1,45 @@
+/**
+ *
+ * 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.spring.xml;
+
+import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
+import org.w3c.dom.Element;
+
+/**
+ * A base class for a parser for a bean.
+ *
+ * @version $Revision: 1.1 $
+ */
+public class BeanDefinitionParserSupport extends AbstractSimpleBeanDefinitionParser {
+    private Class type;
+
+    public BeanDefinitionParserSupport(Class type) {
+        this.type = type;
+    }
+
+    protected Class getBeanClass(Element element) {
+        return type;
+    }
+
+    @Override
+    protected boolean isEligibleAttribute(String attributeName) {
+        return super.isEligibleAttribute(attributeName) && !attributeName.equals("xmlns");
+    }
+
+    
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/BeanDefinitionParserSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java?view=diff&rev=534561&r1=534560&r2=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java
(original)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/xml/CamelNamespaceHandler.java
Wed May  2 10:30:48 2007
@@ -1,32 +1,26 @@
 package org.apache.camel.spring.xml;
 
-import org.apache.camel.spring.SpringCamelContext;
 import org.apache.camel.spring.CamelContextFactoryBean;
-import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
+import org.apache.camel.spring.EndpointFactoryBean;
 import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
 import org.springframework.beans.factory.xml.ParserContext;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
-import org.springframework.beans.factory.support.AbstractBeanDefinition;
 import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.parsing.BeanComponentDefinition;
+import org.springframework.beans.PropertyValue;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
 public class CamelNamespaceHandler extends NamespaceHandlerSupport {
-    protected CamelBeanDefinitionParser camelBeanDefinitionParser = new CamelBeanDefinitionParser();
+    protected CamelBeanDefinitionParser routesParser = new CamelBeanDefinitionParser();
+    protected BeanDefinitionParserSupport endpointParser = new BeanDefinitionParserSupport(EndpointFactoryBean.class);
 
     public void init() {
-        registerBeanDefinitionParser("routes", camelBeanDefinitionParser);
-        registerBeanDefinitionParser("routeBuilder", camelBeanDefinitionParser);
-        
-        registerBeanDefinitionParser("camelContext", new AbstractSimpleBeanDefinitionParser()
{
-            protected Class getBeanClass(Element element) {
-                return CamelContextFactoryBean.class;
-            }
+        registerBeanDefinitionParser("routes", routesParser);
+        registerBeanDefinitionParser("routeBuilder", routesParser);
+        registerBeanDefinitionParser("endpoint", endpointParser);
 
-            @Override
-            protected boolean isEligibleAttribute(String attributeName) {
-                return super.isEligibleAttribute(attributeName) && !attributeName.equals("xmlns");
-            }
+        registerBeanDefinitionParser("camelContext", new BeanDefinitionParserSupport(CamelContextFactoryBean.class)
{
 
             @Override
             protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder
builder) {
@@ -35,8 +29,19 @@
                 NodeList list = element.getElementsByTagName("routes");
                 for (int size = list.getLength(), i = 0; i < size; i++) {
                     Element node = (Element) list.item(i);
-                    BeanDefinition definition = camelBeanDefinitionParser.parseInternal(node,
parserContext);
+                    BeanDefinition definition = routesParser.parseInternal(node, parserContext);
                     builder.addPropertyValue("routeBuilder", definition);
+                }
+
+                list = element.getElementsByTagName("endpoint");
+                for (int size = list.getLength(), i = 0; i < size; i++) {
+                    Element node = (Element) list.item(i);
+                    BeanDefinition definition = endpointParser.parse(node, parserContext);
+                    String id = node.getAttribute("id");
+                    if (id != null) {
+                        definition.getPropertyValues().addPropertyValue("context", builder.getBeanDefinition());

+                        parserContext.registerComponent(new BeanComponentDefinition(definition,
id));
+                    }
                 }
             }
         });

Modified: activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd?view=diff&rev=534561&r1=534560&r2=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd
(original)
+++ activemq/camel/trunk/camel-spring/src/main/resources/org/apache/camel/spring/camel-1.0.xsd
Wed May  2 10:30:48 2007
@@ -14,10 +14,21 @@
     <xs:complexType>
       <xs:complexContent>
         <xs:extension base="s:identifiedType">
-          <xs:sequence>
-            <xs:element minOccurs="0" maxOccurs="unbounded" ref="c:routes"/>
-          </xs:sequence>
+            <xs:choice minOccurs="0" maxOccurs="unbounded">
+              <xs:element minOccurs="0" maxOccurs="1" ref="c:endpoint"/>
+              <xs:element minOccurs="0" maxOccurs="1" ref="c:routes"/>
+            </xs:choice>
           <xs:attribute name="packages" type="xs:NMTOKEN"/>
+        </xs:extension>
+      </xs:complexContent>
+    </xs:complexType>
+  </xs:element>
+
+  <xs:element name="endpoint">
+    <xs:complexType>
+      <xs:complexContent>
+        <xs:extension base="s:identifiedType">
+          <xs:attribute name="uri" type="xs:anyURI"/>
         </xs:extension>
       </xs:complexContent>
     </xs:complexType>

Added: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/EndpointXmlConfigTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/EndpointXmlConfigTest.java?view=auto&rev=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/EndpointXmlConfigTest.java
(added)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/EndpointXmlConfigTest.java
Wed May  2 10:30:48 2007
@@ -0,0 +1,43 @@
+/**
+ *
+ * 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.spring;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.spring.example.DummyBean;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version $Revision: 521586 $
+ */
+public class EndpointXmlConfigTest extends SpringTestSupport {
+    public void testEndpointConfiguration() throws Exception {
+        Endpoint endpoint = getMandatoryBean(Endpoint.class, "endpoint1");
+
+        assertEquals("endpoint URI", "mock:endpoint1", endpoint.getEndpointUri());
+
+        DummyBean dummyBean = getMandatoryBean(DummyBean.class, "mybean");
+        assertNotNull("The bean should have an endpoint injected", dummyBean.getEndpoint());
+        assertEquals("endpoint URI", "mock:endpoint1", dummyBean.getEndpoint().getEndpointUri());
+
+        log.debug("Found dummy bean: " + dummyBean);
+    }
+
+    protected ClassPathXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/spring/endpointReference.xml");
+    }
+}

Propchange: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/EndpointXmlConfigTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java?view=auto&rev=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java
(added)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java
Wed May  2 10:30:48 2007
@@ -0,0 +1,63 @@
+/**
+ *
+ * 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.spring;
+
+import org.apache.camel.TestSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public abstract class SpringTestSupport extends TestSupport {
+    protected AbstractXmlApplicationContext applicationContext;
+
+    protected abstract ClassPathXmlApplicationContext createApplicationContext();
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        applicationContext = createApplicationContext();
+        assertNotNull("Should have created a valid spring context", applicationContext);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        if (applicationContext != null) {
+            applicationContext.destroy();
+        }
+    }
+
+    /**
+     * Looks up the mandatory spring bean of the given name and type, failing if it is not
present or the correct type
+     */
+    public <T> T getMandatoryBean(Class<T> type, String name) {
+        Object value = applicationContext.getBean(name);
+        assertNotNull("No spring bean found for name <" + name + ">", value);
+        if (type.isInstance(value)) {
+            return type.cast(value);
+        }
+        else {
+            fail("Spring bean <" + name + "> is not an instanceof " + type.getName()
+ " but is of type " + ObjectHelper.className(value));
+            return null;
+        }
+    }
+}

Propchange: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/SpringTestSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/DummyBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/DummyBean.java?view=auto&rev=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/DummyBean.java
(added)
+++ activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/DummyBean.java
Wed May  2 10:30:48 2007
@@ -0,0 +1,40 @@
+/**
+ *
+ * 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.spring.example;
+
+import org.apache.camel.Endpoint;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class DummyBean {
+    private Endpoint endpoint;
+
+    public Endpoint getEndpoint() {
+        return endpoint;
+    }
+
+    public void setEndpoint(Endpoint endpoint) {
+        this.endpoint = endpoint;
+    }
+
+    @Override
+    public String toString() {
+        return "DummyBean: " + endpoint;
+    }
+}

Propchange: activemq/camel/trunk/camel-spring/src/test/java/org/apache/camel/spring/example/DummyBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/endpointReference.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/endpointReference.xml?view=auto&rev=534561
==============================================================================
--- activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/endpointReference.xml
(added)
+++ activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/endpointReference.xml
Wed May  2 10:30:48 2007
@@ -0,0 +1,37 @@
+<?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/camel-1.0.xsd http://activemq.apache.org/camel/schema/camel-1.0.xsd
+    ">
+
+  <!-- START SNIPPET: example -->
+  <bean id="mybean" class="org.apache.camel.spring.example.DummyBean">
+    <property name="endpoint" ref="endpoint1"/>
+  </bean>
+
+  <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/camel-1.0.xsd">
+    <endpoint id="endpoint1" uri="mock:endpoint1"/>
+    <routes>
+    </routes>
+  </camelContext>
+  <!-- END SNIPPET: example -->
+  
+</beans>

Propchange: activemq/camel/trunk/camel-spring/src/test/resources/org/apache/camel/spring/endpointReference.xml
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message