Repository: camel
Updated Branches:
  refs/heads/master 349e7a5c9 -> e39cdf5f6


CAMEL-9729: Camel catalog - Allow to query older versions


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

Branch: refs/heads/master
Commit: 9082de7146687d4ede5cd1e5e64c5cda7e63dc69
Parents: 349e7a5
Author: Claus Ibsen <[email protected]>
Authored: Sun Jun 26 12:39:26 2016 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Sun Jun 26 12:42:39 2016 +0200

----------------------------------------------------------------------
 .../org/apache/camel/catalog/CamelCatalog.java  | 24 ++++++++-
 .../camel/catalog/DefaultCamelCatalog.java      | 42 +++++++++++-----
 .../camel/catalog/DefaultVersionManager.java    | 43 +++++++++++++++++
 .../apache/camel/catalog/VersionManager.java    | 51 ++++++++++++++++++++
 .../apache/camel/catalog/CamelCatalogTest.java  | 14 ++++++
 5 files changed, 160 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9082de71/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
index 4308d37..944e63d 100644
--- a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
+++ b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalog.java
@@ -23,7 +23,7 @@ import java.util.Set;
 import javax.management.MXBean;
 
 /**
- * Catalog of components, data formats, models (EIPs), languages, and more  
from this Apache Camel release.
+ * Catalog of components, data formats, models (EIPs), languages, and more 
from this Apache Camel release.
  */
 @MXBean
 public interface CamelCatalog {
@@ -41,6 +41,11 @@ public interface CamelCatalog {
     void setSuggestionStrategy(SuggestionStrategy suggestionStrategy);
 
     /**
+     * To plugin a custom {@link VersionManager} to load other versions of 
Camel the catalog should use.
+     */
+    void setVersionManager(VersionManager versionManager);
+
+    /**
      * Adds a 3rd party component to this catalog.
      *
      * @param name      the component name
@@ -62,6 +67,23 @@ public interface CamelCatalog {
     String getCatalogVersion();
 
     /**
+     * Attempt to load the Camel version to be used by the catalog.
+     * <p/>
+     * Loading the camel-catalog JAR of the given version of choice may 
require internet access
+     * to download the JAR from Maven central. You can pre download the JAR 
and install in a local
+     * Maven repository to avoid internet access for offline environments.
+     *
+     * @param version  the Camel version such as <tt>2.17.1</tt>
+     * @return <tt>true</tt> if the version was loaded, <tt>false</tt> if not.
+     */
+    boolean loadVersion(String version);
+
+    /**
+     * Gets the current loaded Camel version used by the catalog.
+     */
+    String getLoadedVersion();
+
+    /**
      * Find all the component names from the Camel catalog
      */
     List<String> findComponentNames();

http://git-wip-us.apache.org/repos/asf/camel/blob/9082de71/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
index 6163b05..becac72 100644
--- 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
+++ 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelCatalog.java
@@ -96,6 +96,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
     private boolean caching;
     private SuggestionStrategy suggestionStrategy;
+    private VersionManager versionManager = new DefaultVersionManager(this);
 
     /**
      * Creates the {@link CamelCatalog} without caching enabled.
@@ -123,6 +124,11 @@ public class DefaultCamelCatalog implements CamelCatalog {
     }
 
     @Override
+    public void setVersionManager(VersionManager versionManager) {
+        this.versionManager = versionManager;
+    }
+
+    @Override
     public void addComponent(String name, String className) {
         extraComponents.put(name, className);
         // invalidate the cache
@@ -146,6 +152,16 @@ public class DefaultCamelCatalog implements CamelCatalog {
     }
 
     @Override
+    public boolean loadVersion(String version) {
+        return versionManager.loadVersion(version);
+    }
+
+    @Override
+    public String getLoadedVersion() {
+        return versionManager.getLoadedVersion();
+    }
+
+    @Override
     @SuppressWarnings("unchecked")
     public List<String> findComponentNames() {
         List<String> names = null;
@@ -155,7 +171,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
         if (names == null) {
             names = new ArrayList<String>();
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(COMPONENTS_CATALOG);
+            InputStream is = 
versionManager.getResourceAsStream(COMPONENTS_CATALOG);
             if (is != null) {
                 try {
                     CatalogHelper.loadLines(is, names);
@@ -188,7 +204,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
         if (names == null) {
             names = new ArrayList<String>();
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(DATA_FORMATS_CATALOG);
+            InputStream is = 
versionManager.getResourceAsStream(DATA_FORMATS_CATALOG);
             if (is != null) {
                 try {
                     CatalogHelper.loadLines(is, names);
@@ -221,7 +237,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
         if (names == null) {
             names = new ArrayList<String>();
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(LANGUAGE_CATALOG);
+            InputStream is = 
versionManager.getResourceAsStream(LANGUAGE_CATALOG);
             if (is != null) {
                 try {
                     CatalogHelper.loadLines(is, names);
@@ -246,7 +262,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
 
         if (names == null) {
             names = new ArrayList<String>();
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(MODELS_CATALOG);
+            InputStream is = 
versionManager.getResourceAsStream(MODELS_CATALOG);
             if (is != null) {
                 try {
                     CatalogHelper.loadLines(is, names);
@@ -395,7 +411,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
         }
 
         if (answer == null) {
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(file);
+            InputStream is = versionManager.getResourceAsStream(file);
             if (is != null) {
                 try {
                     answer = CatalogHelper.loadText(is);
@@ -421,7 +437,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
         }
 
         if (answer == null) {
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(file);
+            InputStream is = versionManager.getResourceAsStream(file);
             if (is != null) {
                 try {
                     answer = CatalogHelper.loadText(is);
@@ -435,7 +451,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
                     String packageName = className.substring(0, 
className.lastIndexOf('.'));
                     packageName = packageName.replace('.', '/');
                     String path = packageName + "/" + name + ".json";
-                    is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(path);
+                    is = versionManager.getResourceAsStream(path);
                     if (is != null) {
                         try {
                             answer = CatalogHelper.loadText(is);
@@ -463,7 +479,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
         }
 
         if (answer == null) {
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(file);
+            InputStream is = versionManager.getResourceAsStream(file);
             if (is != null) {
                 try {
                     answer = CatalogHelper.loadText(is);
@@ -477,7 +493,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
                     String packageName = className.substring(0, 
className.lastIndexOf('.'));
                     packageName = packageName.replace('.', '/');
                     String path = packageName + "/" + name + ".json";
-                    is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(path);
+                    is = versionManager.getResourceAsStream(path);
                     if (is != null) {
                         try {
                             answer = CatalogHelper.loadText(is);
@@ -505,7 +521,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
         }
 
         if (answer == null) {
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(file);
+            InputStream is = versionManager.getResourceAsStream(file);
             if (is != null) {
                 try {
                     answer = CatalogHelper.loadText(is);
@@ -667,7 +683,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
         }
 
         if (answer == null) {
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(file);
+            InputStream is = versionManager.getResourceAsStream(file);
             if (is != null) {
                 try {
                     answer = CatalogHelper.loadText(is);
@@ -693,7 +709,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
         }
 
         if (answer == null) {
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(file);
+            InputStream is = versionManager.getResourceAsStream(file);
             if (is != null) {
                 try {
                     answer = CatalogHelper.loadText(is);
@@ -719,7 +735,7 @@ public class DefaultCamelCatalog implements CamelCatalog {
         }
 
         if (answer == null) {
-            InputStream is = 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(file);
+            InputStream is = versionManager.getResourceAsStream(file);
             if (is != null) {
                 try {
                     answer = CatalogHelper.loadText(is);

http://git-wip-us.apache.org/repos/asf/camel/blob/9082de71/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultVersionManager.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultVersionManager.java
 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultVersionManager.java
new file mode 100644
index 0000000..0f6e62e
--- /dev/null
+++ 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultVersionManager.java
@@ -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.catalog;
+
+import java.io.InputStream;
+
+public class DefaultVersionManager implements VersionManager {
+
+    private final CamelCatalog camelCatalog;
+
+    public DefaultVersionManager(CamelCatalog camelCatalog) {
+        this.camelCatalog = camelCatalog;
+    }
+
+    @Override
+    public String getLoadedVersion() {
+        return camelCatalog.getCatalogVersion();
+    }
+
+    @Override
+    public boolean loadVersion(String version) {
+        return getLoadedVersion().equals(version);
+    }
+
+    @Override
+    public InputStream getResourceAsStream(String name) {
+        return 
DefaultCamelCatalog.class.getClassLoader().getResourceAsStream(name);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/9082de71/platforms/catalog/src/main/java/org/apache/camel/catalog/VersionManager.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/VersionManager.java 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/VersionManager.java
new file mode 100644
index 0000000..4dcd926
--- /dev/null
+++ 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/VersionManager.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.catalog;
+
+import java.io.InputStream;
+
+/**
+ * Strategy to manage and load Camel versions the catalog uses.
+ */
+public interface VersionManager {
+
+    /**
+     * Gets the current loaded Camel version used by the catalog.
+     */
+    String getLoadedVersion();
+
+    /**
+     * Attempt to load the Camel version to be used by the catalog.
+     * <p/>
+     * Loading the camel-catalog JAR of the given version of choice may 
require internet access
+     * to download the JAR from Maven central. You can pre download the JAR 
and install in a local
+     * Maven repository to avoid internet access for offline environments.
+     *
+     * @param version  the Camel version such as <tt>2.17.1</tt>
+     * @return <tt>true</tt> if the version was loaded, <tt>false</tt> if not.
+     */
+    boolean loadVersion(String version);
+
+    /**
+     * Returns an input stream for reading the specified resource from the 
loaded Catalog version.
+     *
+     * @param name the resource name
+     * @return the stream if found, or <tt>null</tt> if not found.
+     */
+    InputStream getResourceAsStream(String name);
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/9082de71/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
 
b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
index 1da20e7..e14115e 100644
--- 
a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
+++ 
b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -48,6 +48,20 @@ public class CamelCatalogTest {
     public void testGetVersion() throws Exception {
         String version = catalog.getCatalogVersion();
         assertNotNull(version);
+
+        String loaded = catalog.getLoadedVersion();
+        assertNotNull(loaded);
+        assertEquals(version, loaded);
+    }
+
+    @Test
+    public void testLoadVersion() throws Exception {
+        boolean result = catalog.loadVersion("1.0");
+        assertFalse(result);
+
+        String version = catalog.getCatalogVersion();
+        result = catalog.loadVersion(version);
+        assertTrue(result);
     }
 
     @Test

Reply via email to