activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r524764 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/impl/converter/ camel-core/src/main/java/org/apache/camel/util/ camel-core/src/test/java/org/apache/camel/ camel-s...
Date Mon, 02 Apr 2007 10:50:47 GMT
Author: jstrachan
Date: Mon Apr  2 03:50:46 2007
New Revision: 524764

URL: http://svn.apache.org/viewvc?view=rev&rev=524764
Log:
allow the injector to be auto-discovered; also added a spring based camel context which is
aware of the application context and provides a spring based injector

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java
  (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
  (with props)
    activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
  (with props)
    activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringInjector.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?view=diff&rev=524764&r1=524763&r2=524764
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
Mon Apr  2 03:50:46 2007
@@ -18,12 +18,17 @@
 package org.apache.camel.impl;
 
 import org.apache.camel.*;
-import org.apache.camel.impl.converter.DefaultTypeConverter;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.converter.DefaultTypeConverter;
+import org.apache.camel.impl.converter.Injector;
+import org.apache.camel.impl.converter.ReflectionInjector;
+import org.apache.camel.util.FactoryFinder;
 import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.util.NoFactoryAvailableException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -49,6 +54,7 @@
     private TypeConverter typeConverter;
     private EndpointResolver endpointResolver;
     private ExchangeConverter exchangeConverter;
+    private Injector injector;
     private AtomicBoolean started = new AtomicBoolean(false);
 
     /**
@@ -271,6 +277,17 @@
         this.typeConverter = typeConverter;
     }
 
+    public Injector getInjector() {
+        if (injector == null) {
+            injector = createInjector();
+        }
+        return injector;
+    }
+
+    public void setInjector(Injector injector) {
+        this.injector = injector;
+    }
+
     // Implementation methods
     //-----------------------------------------------------------------------
 
@@ -291,7 +308,33 @@
     /**
      * Lazily create a default implementation
      */
-    private TypeConverter createTypeConverter() {
+    protected TypeConverter createTypeConverter() {
         return new DefaultTypeConverter();
+    }
+
+    /**
+     * Lazily create a default implementation
+     */
+    protected Injector createInjector() {
+        FactoryFinder finder = new FactoryFinder("META-INF/services/org/apache/camel/");
+        try {
+            return (Injector) finder.newInstance("Injector");
+        }
+        catch (NoFactoryAvailableException e) {
+            // lets use the default
+            return new ReflectionInjector();
+        }
+        catch (IllegalAccessException e) {
+            throw new RuntimeCamelException(e);
+        }
+        catch (InstantiationException e) {
+            throw new RuntimeCamelException(e);
+        }
+        catch (IOException e) {
+            throw new RuntimeCamelException(e);
+        }
+        catch (ClassNotFoundException e) {
+            throw new RuntimeCamelException(e);
+        }
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?view=diff&rev=524764&r1=524763&r2=524764
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
Mon Apr  2 03:50:46 2007
@@ -45,6 +45,11 @@
         fallbackConverters.add(new ToStringTypeConverter());
     }
 
+    public DefaultTypeConverter(Injector injector) {
+        this();
+        this.injector = injector;
+    }
+
     public <T> T convertTo(Class<T> toType, Object value) {
         if (toType.isInstance(value)) {
             return toType.cast(value);

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java?view=diff&rev=524764&r1=524763&r2=524764
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java
Mon Apr  2 03:50:46 2007
@@ -85,7 +85,7 @@
         if (in == null) {
             in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
             if (in == null) {
-                throw new IOException("Could not find factory class for resource: " + uri);
+                throw new NoFactoryAvailableException(uri);
             }
         }
 

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java?view=auto&rev=524764
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java
Mon Apr  2 03:50:46 2007
@@ -0,0 +1,38 @@
+/**
+ *
+ * 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.util;
+
+import java.io.IOException;
+
+/**
+ * Thrown if no factory resource is available for the given URI
+ *
+ * @version $Revision$
+ */
+public class NoFactoryAvailableException extends IOException {
+    private final String uri;
+
+    public NoFactoryAvailableException(String uri) {
+        super("Could not find factory class for resource: " + uri);
+        this.uri = uri;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/NoFactoryAvailableException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java?view=auto&rev=524764
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
Mon Apr  2 03:50:46 2007
@@ -0,0 +1,38 @@
+/**
+ *
+ * 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;
+
+import junit.framework.TestCase;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.converter.Injector;
+import org.apache.camel.impl.converter.ReflectionInjector;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @version $Revision$
+ */
+public class InjectorDefaultsToReflectionTest extends TestCase {
+    private static final transient Log log = LogFactory.getLog(InjectorDefaultsToReflectionTest.class);
+    
+    public void testInjectorIsReflectionByDefault() throws Exception {
+        Injector injector = new DefaultCamelContext().getInjector();
+        assertTrue("Injector should be reflection based but was: " + injector, injector instanceof
ReflectionInjector);
+        log.debug("Found injector: " + injector);
+    }
+}

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

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java?view=auto&rev=524764
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
(added)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
Mon Apr  2 03:50:46 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.impl.DefaultCamelContext;
+import org.apache.camel.impl.converter.Injector;
+import org.apache.camel.CamelContext;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.context.ApplicationContext;
+
+/**
+ * A Spring aware implementation of {@link CamelContext} which will automatically register
itself with Springs lifecycle
+ * methods  plus allows spring to be used to customize a any
+ * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
as well as supporting accessing components
+ * and beans via the Spring {@link ApplicationContext}
+ *
+ * @version $Revision$
+ */
+public class SpringCamelContext extends DefaultCamelContext implements InitializingBean,
DisposableBean, ApplicationContextAware {
+    private ApplicationContext applicationContext;
+
+    public void afterPropertiesSet() throws Exception {
+        // lets force lazy initialisation
+        getInjector();
+
+        start();
+    }
+
+    public void destroy() throws Exception {
+        stop();
+    }
+
+    public ApplicationContext getApplicationContext() {
+        return applicationContext;
+    }
+
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
+        this.applicationContext = applicationContext;
+    }
+
+    @Override
+    protected Injector createInjector() {
+        return new SpringInjector(getApplicationContext());
+    }
+}

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

Propchange: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringInjector.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringInjector.java?view=auto&rev=524764
==============================================================================
--- activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringInjector.java
(added)
+++ activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringInjector.java
Mon Apr  2 03:50:46 2007
@@ -0,0 +1,59 @@
+/**
+ *
+ * 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.impl.converter.Injector;
+import org.apache.camel.impl.converter.ReflectionInjector;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.context.ApplicationContext;
+
+import java.util.Arrays;
+
+/**
+ * A Spring implementation of {@link Injector} allowing Spring to be used to inject newly
constructed type converters
+ *
+ * @version $Revision$
+ */
+public class SpringInjector extends ReflectionInjector {
+    private static final transient Log log = LogFactory.getLog(SpringInjector.class);
+    private final ApplicationContext applicationContext;
+
+    public SpringInjector(ApplicationContext applicationContext) {
+        this.applicationContext = applicationContext;
+    }
+
+    public Object newInstance(Class type) {
+        String[] names = applicationContext.getBeanNamesForType(type, true, true);
+        if (names != null) {
+            if (names.length == 1) {
+                // lets instantiate the single bean
+                return applicationContext.getBean(names[0]);
+            }
+            else if (names.length > 1) {
+                log.warn("Too many beans of type: " + type.getName() + " available: " + Arrays.asList(names)
+ " so ignoring Spring configuration");
+            }
+        }
+
+        // lets instantiate the bean
+        Object answer = super.newInstance(type);
+
+        // TODO now lets inject spring...
+        return answer;
+    }
+}

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

Propchange: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringInjector.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-spring/src/main/java/org/apache/camel/spring/SpringInjector.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message