camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] git commit: CAMEL-6346 supports to set the ConfigAdmin persistent information to the camel blueprint main
Date Wed, 08 May 2013 09:22:21 GMT
Updated Branches:
  refs/heads/master f2811fdfc -> c85466f38


CAMEL-6346 supports to set the ConfigAdmin persistent information to the camel blueprint main


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c85466f3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c85466f3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c85466f3

Branch: refs/heads/master
Commit: c85466f38da32d898282e721774ecf34600f71a4
Parents: 4736fd6
Author: Willem Jiang <ningjiang@apache.org>
Authored: Wed May 8 17:19:12 2013 +0800
Committer: Willem Jiang <ningjiang@apache.org>
Committed: Wed May 8 17:19:59 2013 +0800

----------------------------------------------------------------------
 .../camel/test/blueprint/CamelBlueprintHelper.java |   38 +++++++++++
 .../test/blueprint/CamelBlueprintTestSupport.java  |   25 +-------
 .../java/org/apache/camel/test/blueprint/Main.java |   41 +++++++++++-
 .../org/apache/camel/test/blueprint/MainTest.java  |   51 +++++++++++++++
 .../apache/camel/test/blueprint/MyMainAppTest.java |   26 ++++++--
 .../apache/camel/test/blueprint/main-loadfile.xml  |   42 ++++++++++++
 .../main/java/org/apache/camel/maven/RunMojo.java  |   31 ++++++++-
 7 files changed, 221 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c85466f3/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
index 5a37b1b..a1c60bf 100644
--- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
+++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -34,6 +35,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Properties;
 import java.util.jar.JarInputStream;
 
 import de.kalpatec.pojosr.framework.PojoServiceRegistryFactoryImpl;
@@ -57,6 +59,8 @@ import org.osgi.framework.Filter;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -162,6 +166,40 @@ public final class CamelBlueprintHelper {
             }
         }
     }
+    
+    // pick up persistent file configuration
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public static void setPersistentFileForConfigAdmin(BundleContext bundleContext, String
pid,
+                                                       String fileName, Dictionary props)
throws IOException {
+        if (pid != null) {
+            if (fileName == null) {
+                throw new IllegalArgumentException("The persistent file should not be null");
+            } else {
+                File load = new File(fileName);
+                LOG.debug("Loading properties from OSGi config admin file: {}", load);
+                org.apache.felix.utils.properties.Properties cfg = new org.apache.felix.utils.properties.Properties(load);
+                Iterator<String> it = cfg.keySet().iterator();
+                while (it.hasNext()) {
+                    String key = it.next();
+                    // must force type cast to have code compile with both java6
+                    // and 7 with the
+                    // (org.apache.felix.utils.properties.Properties)
+                    String value = (String)cfg.get(key);
+                    props.put(key, value);
+                }
+
+                ConfigurationAdmin configAdmin = CamelBlueprintHelper
+                    .getOsgiService(bundleContext, ConfigurationAdmin.class);
+                if (configAdmin != null) {
+                    // ensure we update
+                    Configuration config = configAdmin.getConfiguration(pid);
+                    LOG.info("Updating ConfigAdmin {} by overriding properties {}", config,
props);
+                    config.update(props);
+                }
+
+            }
+        }
+    }
 
     public static <T> T getOsgiService(BundleContext bundleContext, Class<T>
type, long timeout) {
         return getOsgiService(bundleContext, type, null, timeout);

http://git-wip-us.apache.org/repos/asf/camel/blob/c85466f3/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
index bdbd45d..eb970f5 100644
--- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
+++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java
@@ -16,9 +16,7 @@
  */
 package org.apache.camel.test.blueprint;
 
-import java.io.File;
 import java.util.Dictionary;
-import java.util.Iterator;
 import java.util.Properties;
 
 import org.apache.camel.CamelContext;
@@ -40,6 +38,7 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport
{
     private static ThreadLocal<BundleContext> threadLocalBundleContext = new ThreadLocal<BundleContext>();
     private volatile BundleContext bundleContext;
     
+    @SuppressWarnings({"rawtypes", "unchecked"})
     protected BundleContext createBundleContext() throws Exception {
         String symbolicName = getClass().getSimpleName();
         BundleContext answer = CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(),
@@ -61,27 +60,7 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport
{
         }
 
         if (file != null) {
-            String fileName = file[0];
-            String pid = file[1];
-
-            File load = new File(fileName);
-            log.debug("Loading properties from OSGi config admin file: {}", load);
-            org.apache.felix.utils.properties.Properties cfg = new org.apache.felix.utils.properties.Properties(load);
-            Iterator<String> it = cfg.keySet().iterator();
-            while (it.hasNext()) {
-                String key = it.next();
-                // must force type cast to have code compile with both java6 and 7 with the
(org.apache.felix.utils.properties.Properties)
-                String value = (String) cfg.get(key);
-                props.put(key, value);
-            }
-
-            ConfigurationAdmin configAdmin = CamelBlueprintHelper.getOsgiService(answer,
ConfigurationAdmin.class);
-            if (configAdmin != null) {
-                // ensure we update
-                Configuration config = configAdmin.getConfiguration(pid);
-                log.info("Updating ConfigAdmin {} by overriding properties {}", config, props);
-                config.update(props);
-            }
+            CamelBlueprintHelper.setPersistentFileForConfigAdmin(answer, file[1], file[0],
props);
         }
 
         // allow end user to override properties

http://git-wip-us.apache.org/repos/asf/camel/blob/c85466f3/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
index 19a4465..d62908c 100644
--- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
+++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
@@ -19,6 +19,8 @@ package org.apache.camel.test.blueprint;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.Properties;
+
 import javax.xml.bind.JAXBException;
 
 import org.apache.camel.CamelContext;
@@ -38,6 +40,8 @@ public class Main extends MainSupport {
     private CamelContext camelContext;
     private String bundleName = "MyBundle";
     private boolean includeSelfAsBundle;
+    private String configAdminPid;
+    private String configAdminFileName;
 
     public Main() {
 
@@ -54,6 +58,20 @@ public class Main extends MainSupport {
                 setDescriptors(parameter);
             }
         });
+        
+        addOption(new ParameterOption("pid", "persistentId", 
+                 "Sets the ConfigAdmin persistentId", "persistentId") {
+            protected void doProcess(String arg, String parameter, LinkedList<String>
remainingArgs) {
+                setConfigAdminPid(parameter);
+            }
+        });
+        
+        addOption(new ParameterOption("pf", "persistentFileName", 
+                  "Sets the ConfigAdmin persistent file name", "persistentFileName") {
+            protected void doProcess(String arg, String parameter, LinkedList<String>
remainingArgs) {
+                setConfigAdminFileName(parameter);
+            }
+        });
 
     }
 
@@ -82,7 +100,7 @@ public class Main extends MainSupport {
             }
             LOG.debug("Starting Blueprint XML file: " + descriptors);
             bundleContext = createBundleContext(bundleName);
-
+            CamelBlueprintHelper.setPersistentFileForConfigAdmin(bundleContext, configAdminPid,
configAdminFileName, new Properties());
             camelContext = CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class);
             if (camelContext == null) {
                 throw new IllegalArgumentException("Cannot find CamelContext in blueprint
XML file: " + descriptors);
@@ -102,6 +120,8 @@ public class Main extends MainSupport {
         // call completed to properly stop as we count down the waiting latch
         completed();
     }
+    
+   
 
     @Override
     protected ProducerTemplate findOrCreateCamelTemplate() {
@@ -119,6 +139,9 @@ public class Main extends MainSupport {
     protected BundleContext createBundleContext(String name) throws Exception {
         return CamelBlueprintHelper.createBundleContext(name, descriptors, isIncludeSelfAsBundle());
     }
+    
+   
+    
 
     @Override
     protected Map<String, CamelContext> getCamelContextMap() {
@@ -157,4 +180,20 @@ public class Main extends MainSupport {
     public void setIncludeSelfAsBundle(boolean includeSelfAsBundle) {
         this.includeSelfAsBundle = includeSelfAsBundle;
     }
+
+    public String getConfigAdminPid() {
+        return configAdminPid;
+    }
+
+    public void setConfigAdminPid(String pid) {
+        this.configAdminPid = pid;
+    }
+
+    public String getConfigAdminFileName() {
+        return configAdminFileName;
+    }
+
+    public void setConfigAdminFileName(String fileName) {
+        this.configAdminFileName = fileName;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c85466f3/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
new file mode 100644
index 0000000..67c305e
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
@@ -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.test.blueprint;
+
+
+import org.apache.camel.ProducerTemplate;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+
+public class MainTest {
+    
+    @Test
+    public void testMyMain() throws Exception {
+        Main main = new Main();
+        main.setBundleName("MyMainBundle");
+        // as we run this test without packing ourselves as bundle, then include ourselves
+        main.setIncludeSelfAsBundle(true);
+        // setup the blueprint file here
+        main.setDescriptors("org/apache/camel/test/blueprint/main-loadfile.xml");
+        // set the adminConfig persistent id
+        main.setPid("stuff");
+        // set the adminConfig persistent file name
+        main.setPFileName("src/test/resources/etc/stuff.cfg");
+        main.start();
+        
+        ProducerTemplate template = main.getCamelTemplate();
+        assertNotNull("We should get the template here", template);
+        
+        String result = template.requestBody("direct:start", "hello", String.class);
+        assertEquals("Get a wrong response", "Bye hello", result);
+        main.stop();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c85466f3/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java
index 554952f..f417dd4 100644
--- a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyMainAppTest.java
@@ -16,25 +16,37 @@
  */
 package org.apache.camel.test.blueprint;
 
+
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
 import org.junit.Test;
 
-/**
- *
- */
+import static org.junit.Assert.assertTrue;
+
+
 public class MyMainAppTest {
 
     public static void main(String[] args) throws Exception {
         MyMainAppTest me = new MyMainAppTest();
-        me.run();
+        me.testMyMain();
     }
 
     @Test
     public void testMyMain() throws Exception {
-        run();
+        Main main = new Main();
+        run(main);
+        
+        Map<String, CamelContext> contexts = main.getCamelContextMap();
+        // we should get at least one CamelContext
+        assertTrue("We should get at least one camelcontext", contexts.size() >= 1);
+        
+        
     }
 
-    public void run() throws Exception {
-        Main main = new Main();
+    public void run(Main main) throws Exception {
+        
         main.setBundleName("MyMainBundle");
         // as we run this test without packing ourselves as bundle, then include ourselves
         main.setIncludeSelfAsBundle(true);

http://git-wip-us.apache.org/repos/asf/camel/blob/c85466f3/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/main-loadfile.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/main-loadfile.xml
b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/main-loadfile.xml
new file mode 100644
index 0000000..1e99278
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/main-loadfile.xml
@@ -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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
+           xsi:schemaLocation="
+             http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
+             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+  
+  <!-- blueprint property placeholders which we can configure from outside -->
+  <cm:property-placeholder persistent-id="stuff"/>
+
+  <!-- a bean that uses a blueprint property placeholder -->
+  <bean id="myCoolBean" class="org.apache.camel.test.blueprint.MyCoolBean">
+    <property name="say" value="${greeting}"/>
+  </bean>
+
+  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
+     <route>
+      <from uri="direct:start"/>
+      <bean ref="myCoolBean" method="saySomething"/>
+      <to uri="{{destination}}"/>
+    </route>
+
+  </camelContext>
+  
+</blueprint>

http://git-wip-us.apache.org/repos/asf/camel/blob/c85466f3/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
index 476abaa..ba60d54 100644
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
@@ -218,6 +218,22 @@ public class RunMojo extends AbstractExecMojo {
      * @parameter property="camel.fileApplicationContextUri"
      */
     private String fileApplicationContextUri;
+    
+    /**
+     * The configureAdmin persistent id, it will be used when loading the 
+     * camel context from blueprint.
+     * 
+     * @parameter property="camel.configAdminPid"
+     */
+    private String configAdminPid;
+    
+    /**
+     * The configureAdmin persistent file name, it will be used when 
+     * loading the camel context from blueprint.
+     * 
+     * * @parameter property="camel.configAdminFileName"
+     */
+    private String configAdminFileName;
 
     /**
      * The class arguments.
@@ -406,8 +422,6 @@ public class RunMojo extends AbstractExecMojo {
         if (arguments != null) {
             args.addAll(Arrays.asList(arguments));
         }
-        arguments = new String[args.size()];
-        args.toArray(arguments);
         
         if (usingSpringJavaConfigureMain) {
             mainClass = "org.apache.camel.spring.javaconfig.Main";
@@ -421,6 +435,16 @@ public class RunMojo extends AbstractExecMojo {
             mainClass = "org.apache.camel.test.blueprint.Main";
             // must include plugin dependencies for blueprint
             extraPluginDependencyArtifactId = "camel-test-blueprint";
+            // set the configAdmin pid
+            if (configAdminPid != null) {
+                args.add("-pid");
+                args.add(configAdminPid);
+            }
+            // set the configAdmin pFile
+            if (configAdminFileName != null) {
+                args.add("-pf");
+                args.add(configAdminFileName);
+            }
             getLog().info("Using org.apache.camel.test.blueprint.Main to initiate a CamelContext");
         } else if (mainClass != null) {
             getLog().info("Using custom " + mainClass + " to initiate a CamelContext");
@@ -430,6 +454,9 @@ public class RunMojo extends AbstractExecMojo {
             mainClass = "org.apache.camel.spring.Main";
         }
         
+        arguments = new String[args.size()];
+        args.toArray(arguments);
+        
         if (getLog().isDebugEnabled()) {
             StringBuilder msg = new StringBuilder("Invoking: ");
             msg.append(mainClass);


Mime
View raw message