Author: davsclaus
Date: Sat Apr  7 10:36:28 2012
New Revision: 1310724

URL: http://svn.apache.org/viewvc?rev=1310724&view=rev
Log:
CAMEL-5149: The default injector should perform bean post processing to setup 
@Produce and the likes on the bean

Added:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsTest.java
      - copied, changed from r1310679, 
camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultInjectorTest.java
Removed:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1310724&r1=1310723&r2=1310724&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 Sat Apr  7 10:36:28 2012
@@ -120,7 +120,6 @@ import org.apache.camel.util.CastUtils;
 import org.apache.camel.util.EndpointHelper;
 import org.apache.camel.util.EventHelper;
 import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.ReflectionInjector;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.camel.util.StopWatch;
 import org.apache.camel.util.TimeUtils;
@@ -2098,8 +2097,8 @@ public class DefaultCamelContext extends
         try {
             return (Injector) finder.newInstance("Injector");
         } catch (NoFactoryAvailableException e) {
-            // lets use the default
-            return new ReflectionInjector();
+            // lets use the default injector
+            return new DefaultInjector(this);
         }
     }
 

Added: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java?rev=1310724&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java 
(added)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultInjector.java 
Sat Apr  7 10:36:28 2012
@@ -0,0 +1,70 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.Injector;
+import org.apache.camel.util.ReflectionInjector;
+
+/**
+ * A default implementation of {@link Injector} which just uses reflection to
+ * instantiate new objects using their zero argument constructor,
+ * and then performing bean post processing using {@link 
DefaultCamelBeanPostProcessor}.
+ * <p/>
+ * For more complex implementations try the Spring or Guice implementations.
+ *
+ * @see ReflectionInjector
+ */
+public class DefaultInjector implements Injector  {
+
+    // use the reflection injector
+    private final Injector delegate = new ReflectionInjector();
+    private final DefaultCamelBeanPostProcessor postProcessor;
+
+    public DefaultInjector(CamelContext context) {
+        postProcessor = new DefaultCamelBeanPostProcessor(context);
+    }
+
+    @Override
+    public <T> T newInstance(Class<T> type) {
+        T answer = delegate.newInstance(type);
+        if (answer != null) {
+            try {
+                postProcessor.postProcessBeforeInitialization(answer, 
answer.getClass().getName());
+                postProcessor.postProcessAfterInitialization(answer, 
answer.getClass().getName());
+            } catch (Exception e) {
+                throw new RuntimeCamelException("Error during post processing 
of bean " + answer, e);
+            }
+        }
+        return answer;
+    }
+
+    @Override
+    public <T> T newInstance(Class<T> type, Object instance) {
+        T answer = delegate.newInstance(type, instance);
+        if (answer != null) {
+            try {
+                postProcessor.postProcessBeforeInitialization(answer, 
answer.getClass().getName());
+                postProcessor.postProcessAfterInitialization(answer, 
answer.getClass().getName());
+            } catch (Exception e) {
+                throw new RuntimeCamelException("Error during post processing 
of bean " + answer, e);
+            }
+        }
+        return answer;
+    }
+}

Copied: 
camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsTest.java 
(from r1310679, 
camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java&r1=1310679&r2=1310724&rev=1310724&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsToReflectionTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/InjectorDefaultsTest.java 
Sat Apr  7 10:36:28 2012
@@ -16,17 +16,17 @@
  */
 package org.apache.camel;
 
+import org.apache.camel.impl.DefaultInjector;
 import org.apache.camel.spi.Injector;
-import org.apache.camel.util.ReflectionInjector;
 
 /**
  * @version 
  */
-public class InjectorDefaultsToReflectionTest extends ContextTestSupport {
+public class InjectorDefaultsTest extends ContextTestSupport {
 
-    public void testInjectorIsReflectionByDefault() throws Exception {
+    public void testInjectorIsDefaultByDefault() throws Exception {
         Injector injector = context.getInjector();
-        assertIsInstanceOf(ReflectionInjector.class, injector);
+        assertIsInstanceOf(DefaultInjector.class, injector);
     }
 
     public void testNewInstance() throws Exception {

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultInjectorTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultInjectorTest.java?rev=1310724&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultInjectorTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultInjectorTest.java
 Sat Apr  7 10:36:28 2012
@@ -0,0 +1,51 @@
+/**
+ * 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.impl;
+
+import junit.framework.TestCase;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+
+/**
+ *
+ */
+public class DefaultInjectorTest extends TestCase {
+
+    public void testDefaultInjector() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+        context.start();
+
+        // use the injector (will use the default)
+        // which should post process the bean to inject the @Produce
+        MyBean bean = context.getInjector().newInstance(MyBean.class);
+
+        Object reply = bean.doSomething("World");
+        assertEquals("WorldWorld", reply);
+    }
+
+    public static class MyBean {
+
+        @Produce(uri = "language:simple:${body}${body}")
+        ProducerTemplate template;
+
+        public Object doSomething(String body) {
+            return template.requestBody(body);
+        }
+    }
+
+}


Reply via email to