CAMEL-8379: Camel catalog - Now has JMX api

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

Branch: refs/heads/master
Commit: 4921ffe8c5ac2a56aa13b5fa8791ca9994bfe84f
Parents: 00b82cc
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Feb 19 11:24:55 2015 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Feb 19 11:24:55 2015 +0100

----------------------------------------------------------------------
 platforms/catalog/pom.xml                       | 12 +++
 .../catalog/CamelCatalogMBeanExporter.java      | 85 +++++++++++++++++++
 .../camel/catalog/CamelComponentCatalog.java    |  2 +
 .../catalog/DefaultCamelComponentCatalog.java   |  2 +-
 .../catalog/CamelCatalogMBeanExporterTest.java  | 44 ++++++++++
 .../apache/camel/catalog/CamelCatalogTest.java  | 89 ++++++++++++++++++++
 .../catalog/src/test/resources/log4j.properties | 41 +++++++++
 7 files changed, 274 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4921ffe8/platforms/catalog/pom.xml
----------------------------------------------------------------------
diff --git a/platforms/catalog/pom.xml b/platforms/catalog/pom.xml
index aaac676..0cb442c 100644
--- a/platforms/catalog/pom.xml
+++ b/platforms/catalog/pom.xml
@@ -46,6 +46,18 @@
 
     <!-- no dependency -->
 
+    <!-- testing -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-log4j12</artifactId>
+      <scope>test</scope>
+    </dependency>
+
   </dependencies>
 
   <build>

http://git-wip-us.apache.org/repos/asf/camel/blob/4921ffe8/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalogMBeanExporter.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalogMBeanExporter.java
 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalogMBeanExporter.java
new file mode 100644
index 0000000..063011b
--- /dev/null
+++ 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelCatalogMBeanExporter.java
@@ -0,0 +1,85 @@
+/**
+ * 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.lang.management.ManagementFactory;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanServer;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+
+/**
+ * MBean exporter to register the {@link CamelComponentCatalog} in JMX.
+ */
+public class CamelCatalogMBeanExporter {
+
+    public static final String MBEAN_NAME = 
"org.apache.camel.catalog:type=catalog,name=catalog";
+
+    private CamelComponentCatalog catalog;
+    private ObjectName objectName;
+    private MBeanServer mBeanServer;
+
+    /**
+     * Initializes and exports the {@link 
org.apache.camel.catalog.CamelComponentCatalog} in JMX using the domain name,
+     * which can be obtained using {@link #getObjectName()}.
+     *
+     * @throws Exception
+     */
+    public void init() throws Exception {
+        catalog = new DefaultCamelComponentCatalog();
+
+        if (objectName == null) {
+            objectName = getObjectName();
+        }
+
+        if (mBeanServer == null) {
+            mBeanServer = ManagementFactory.getPlatformMBeanServer();
+        }
+
+        if (mBeanServer != null) {
+            try {
+                // notice some mbean servers may register using a changed 
object name
+                ObjectInstance oi = mBeanServer.registerMBean(catalog, 
objectName);
+                if (oi != null && oi.getObjectName() != null) {
+                    objectName = oi.getObjectName();
+                }
+            } catch (InstanceAlreadyExistsException iaee) {
+                // Try to remove and re-register
+                mBeanServer.unregisterMBean(objectName);
+                mBeanServer.registerMBean(catalog, objectName);
+            }
+        }
+    }
+
+    /**
+     * Destroyes and unregisteres the {@link 
org.apache.camel.catalog.CamelComponentCatalog} from JMX.
+     *
+     * @throws Exception is thrown if error during unregistration
+     */
+    public void destroy() throws Exception {
+        if (mBeanServer != null) {
+            if (objectName != null) {
+                mBeanServer.unregisterMBean(objectName);
+            }
+        }
+    }
+
+    protected ObjectName getObjectName() throws Exception {
+        return new ObjectName(MBEAN_NAME);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4921ffe8/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
index 027cf83..bae84e0 100644
--- 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
+++ 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/CamelComponentCatalog.java
@@ -18,10 +18,12 @@ package org.apache.camel.catalog;
 
 import java.util.List;
 import java.util.Set;
+import javax.management.MXBean;
 
 /**
  * Catalog of all the Camel components from this Apache Camel release.
  */
+@MXBean
 public interface CamelComponentCatalog {
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/4921ffe8/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
index a633b98..7ccfc3a 100644
--- 
a/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
+++ 
b/platforms/catalog/src/main/java/org/apache/camel/catalog/DefaultCamelComponentCatalog.java
@@ -42,7 +42,7 @@ public class DefaultCamelComponentCatalog implements 
CamelComponentCatalog {
     private static final String COMPONENTS_JSON = 
"org/apache/camel/catalog/components";
     private static final String DATA_FORMATS_JSON = 
"org/apache/camel/catalog/dataformats";
     private static final String LANGUAGE_JSON = 
"org/apache/camel/catalog/languages";
-    private static final String ARCHETYPES_CATALOG = 
"org/apache/camel/catalog/archetypes/camel-catalog.xml";
+    private static final String ARCHETYPES_CATALOG = 
"org/apache/camel/catalog/archetypes/archetype-catalog.xml";
     private static final String SCHEMAS_XML = 
"org/apache/camel/catalog/schemas";
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/4921ffe8/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogMBeanExporterTest.java
----------------------------------------------------------------------
diff --git 
a/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogMBeanExporterTest.java
 
b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogMBeanExporterTest.java
new file mode 100644
index 0000000..7492c04
--- /dev/null
+++ 
b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogMBeanExporterTest.java
@@ -0,0 +1,44 @@
+/**
+ * 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.lang.management.ManagementFactory;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import junit.framework.TestCase;
+
+public class CamelCatalogMBeanExporterTest extends TestCase {
+
+    private CamelCatalogMBeanExporter exporter = new 
CamelCatalogMBeanExporter();
+
+    public void testMBeanExporter() throws Exception {
+        exporter.init();
+
+        ObjectName on = exporter.getObjectName();
+
+        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
+        assertTrue("MBean should be regsistered", 
mBeanServer.isRegistered(on));
+
+        String schema = (String) mBeanServer.invoke(on, "componentJSonSchema", 
new Object[]{"docker"}, new String[]{"java.lang.String"});
+        assertNotNull(schema);
+        assertTrue("Should be docker schema", 
schema.contains("org.apache.camel.component.docker.DockerComponent"));
+
+        exporter.destroy();
+        assertFalse("MBean should be unregsistered", 
mBeanServer.isRegistered(on));
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4921ffe8/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
new file mode 100644
index 0000000..e40f212
--- /dev/null
+++ 
b/platforms/catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -0,0 +1,89 @@
+/**
+ * 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.util.List;
+
+import junit.framework.TestCase;
+import org.junit.Test;
+
+public class CamelCatalogTest extends TestCase {
+
+    private CamelComponentCatalog catalog = new DefaultCamelComponentCatalog();
+
+    @Test
+    public void testFindNames() throws Exception {
+        List<String> names = catalog.findComponentNames();
+        assertNotNull(names);
+        assertTrue(names.contains("file"));
+        assertTrue(names.contains("log"));
+        assertTrue(names.contains("docker"));
+        assertTrue(names.contains("jms"));
+
+        names = catalog.findDataFormatNames();
+        assertNotNull(names);
+        assertTrue(names.contains("bindy-csv"));
+        assertTrue(names.contains("hl7"));
+        assertTrue(names.contains("jaxb"));
+        assertTrue(names.contains("syslog"));
+
+        names = catalog.findLanguageNames();
+        assertNotNull(names);
+        assertTrue(names.contains("simple"));
+        assertTrue(names.contains("groovy"));
+        assertTrue(names.contains("mvel"));
+
+        names = catalog.findModelNames();
+        assertNotNull(names);
+        assertTrue(names.contains("from"));
+        assertTrue(names.contains("to"));
+        assertTrue(names.contains("recipientList"));
+        assertTrue(names.contains("aggregate"));
+        assertTrue(names.contains("split"));
+        assertTrue(names.contains("loadBalance"));
+    }
+
+    @Test
+    public void testJsonSchema() throws Exception {
+        String schema = catalog.componentJSonSchema("docker");
+        assertNotNull(schema);
+
+        schema = catalog.dataFormatJSonSchema("hl7");
+        assertNotNull(schema);
+
+        schema = catalog.languageJSonSchema("groovy");
+        assertNotNull(schema);
+
+        schema = catalog.modelJSonSchema("aggregate");
+        assertNotNull(schema);
+    }
+
+    @Test
+    public void testXmlSchema() throws Exception {
+        String schema = catalog.blueprintSchemaAsXml();
+        assertNotNull(schema);
+
+        schema = catalog.springSchemaAsXml();
+        assertNotNull(schema);
+    }
+
+    @Test
+    public void testArchetypeCatalog() throws Exception {
+        String schema = catalog.archetypeCatalogAsXml();
+        assertNotNull(schema);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4921ffe8/platforms/catalog/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/platforms/catalog/src/test/resources/log4j.properties 
b/platforms/catalog/src/test/resources/log4j.properties
new file mode 100644
index 0000000..8aebc93
--- /dev/null
+++ b/platforms/catalog/src/test/resources/log4j.properties
@@ -0,0 +1,41 @@
+## ------------------------------------------------------------------------
+## 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.
+## ------------------------------------------------------------------------
+
+#
+# The logging properties used for testing.
+#
+log4j.rootLogger=INFO, file
+
+# uncomment the following to enable camel debugging
+#log4j.logger.org.apache.camel.catalog=TRACE
+
+# CONSOLE appender not used by default
+log4j.appender.out=org.apache.log4j.ConsoleAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - 
%m%n
+# MDC
+#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - 
%-10.10X{camel.breadcrumbId} - %-10.10X{camelexchangeId} - 
%-10.10X{camel.correlationId} - %-10.10X{camel.routeId} - %m%n
+
+# File appender
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.file=target/camel-catalog-test.log
+log4j.appender.file.append=true
+log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - 
%m%n
+# MDC
+#log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - 
%-10.10X{camel.breadcrumbId} - %-10.10X{camel.exchangeId} - 
%-10.10X{camel.correlationId} - %-10.10X{camel.routeId} - %m%n
+

Reply via email to