Repository: karaf-decanter
Updated Branches:
  refs/heads/master 1f79bc545 -> f19041a49


[KARAF-3608] Adding config admin support to elasticsearcg appender with thanks 
to Morgan Hautman


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

Branch: refs/heads/master
Commit: f19041a499e6f39e0dba7d999f7bc9ae1ef5bc6a
Parents: 1f79bc5
Author: Christian Schneider <[email protected]>
Authored: Sun Mar 15 17:39:27 2015 +0100
Committer: Christian Schneider <[email protected]>
Committed: Sun Mar 15 17:39:27 2015 +0100

----------------------------------------------------------------------
 appender/elasticsearch/pom.xml                  | 174 +++++++++++--------
 ...he.karaf.decanter.appender.elasticsearch.cfg |   2 +
 .../appender/elasticsearch/Activator.java       |  45 ++++-
 assembly/src/main/feature/feature.xml           |   1 +
 4 files changed, 141 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/f19041a4/appender/elasticsearch/pom.xml
----------------------------------------------------------------------
diff --git a/appender/elasticsearch/pom.xml b/appender/elasticsearch/pom.xml
index 09291dc..7fb4ee8 100644
--- a/appender/elasticsearch/pom.xml
+++ b/appender/elasticsearch/pom.xml
@@ -1,88 +1,114 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
-       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+<project
+    xmlns="http://maven.apache.org/POM/4.0.0";
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
 
-       <!-- 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. -->
+    <!-- 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. -->
 
-       <modelVersion>4.0.0</modelVersion>
+    <modelVersion>4.0.0</modelVersion>
 
-       <parent>
-               <groupId>org.apache.karaf.decanter</groupId>
-               <artifactId>appender</artifactId>
-               <version>3.0.0-SNAPSHOT</version>
-               <relativePath>../pom.xml</relativePath>
-       </parent>
+    <parent>
+        <groupId>org.apache.karaf.decanter</groupId>
+        <artifactId>appender</artifactId>
+        <version>3.0.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
 
-       <groupId>org.apache.karaf.decanter.appender</groupId>
-       
<artifactId>org.apache.karaf.decanter.appender.elasticsearch</artifactId>
-       <packaging>bundle</packaging>
-       <name>Apache Karaf :: Decanter :: Appender :: Elasticsearch</name>
+    <groupId>org.apache.karaf.decanter.appender</groupId>
+    <artifactId>org.apache.karaf.decanter.appender.elasticsearch</artifactId>
+    <packaging>bundle</packaging>
+    <name>Apache Karaf :: Decanter :: Appender :: Elasticsearch</name>
 
-       <dependencies>
+    <dependencies>
 
-               <!-- Decanter -->
-               <dependency>
-                       <groupId>org.apache.karaf.decanter</groupId>
-                       <artifactId>org.apache.karaf.decanter.api</artifactId>
-               </dependency>
+        <!-- Decanter -->
+        <dependency>
+            <groupId>org.apache.karaf.decanter</groupId>
+            <artifactId>org.apache.karaf.decanter.api</artifactId>
+        </dependency>
 
-               <!-- Elasticsearch -->
-               <dependency>
-                       <groupId>org.elasticsearch</groupId>
-                       <artifactId>elasticsearch</artifactId>
-                       <version>${elasticsearch.version}</version>
-               </dependency>
+        <!-- Elasticsearch -->
+        <dependency>
+            <groupId>org.elasticsearch</groupId>
+            <artifactId>elasticsearch</artifactId>
+            <version>${elasticsearch.version}</version>
+        </dependency>
 
-               <!-- OSGi -->
-               <dependency>
-                       <groupId>org.osgi</groupId>
-                       <artifactId>org.osgi.core</artifactId>
-               </dependency>
+        <!-- OSGi -->
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
 
-               <!-- JSON builder -->
-               <dependency>
-                       <groupId>javax.json</groupId>
-                       <artifactId>javax.json-api</artifactId>
-               </dependency>
-               <!-- SLF4J -->
-               <dependency>
-                       <groupId>org.slf4j</groupId>
-                       <artifactId>slf4j-api</artifactId>
-               </dependency>
+        <!-- JSON builder -->
+        <dependency>
+            <groupId>javax.json</groupId>
+            <artifactId>javax.json-api</artifactId>
+        </dependency>
+        <!-- SLF4J -->
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
 
-       </dependencies>
+    </dependencies>
 
-       <build>
-               <plugins>
-                       <plugin>
-                               <groupId>org.apache.felix</groupId>
-                               <artifactId>maven-bundle-plugin</artifactId>
-                               <inherited>true</inherited>
-                               <extensions>true</extensions>
-                               <configuration>
-                                       <instructions>
-                                               
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
-                                               
<Bundle-Version>${project.version}</Bundle-Version>
-                                               
<Bundle-Activator>org.apache.karaf.decanter.appender.elasticsearch.Activator</Bundle-Activator>
-                                               <Import-Package>
-                                                       
org.apache.karaf.decanter.api;version=${project.version},
-                                                       
org.slf4j;version="[1.7,2)";resolution:=optional,
-                                                       
org.elasticsearch*;version="[1,2)",
-                                                       *
-                                               </Import-Package>
-                                       </instructions>
-                               </configuration>
-                       </plugin>
-               </plugins>
-       </build>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <inherited>true</inherited>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+                        <Bundle-Version>${project.version}</Bundle-Version>
+                        
<Bundle-Activator>org.apache.karaf.decanter.appender.elasticsearch.Activator</Bundle-Activator>
+                        <Import-Package>
+                            
org.apache.karaf.decanter.api;version=${project.version},
+                            org.slf4j;version="[1.7,2)";resolution:=optional,
+                            org.elasticsearch*;version="[1,2)",
+                            *
+                        </Import-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>build-helper-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>attach-artifact</goal>
+                        </goals>
+                        <configuration>
+                            <artifacts>
+                                <artifact>
+                                    
<file>src/main/cfg/org.apache.karaf.decanter.appender.elasticsearch.cfg</file>
+                                    <type>cfg</type>
+                                </artifact>
+                            </artifacts>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/f19041a4/appender/elasticsearch/src/main/cfg/org.apache.karaf.decanter.appender.elasticsearch.cfg
----------------------------------------------------------------------
diff --git 
a/appender/elasticsearch/src/main/cfg/org.apache.karaf.decanter.appender.elasticsearch.cfg
 
b/appender/elasticsearch/src/main/cfg/org.apache.karaf.decanter.appender.elasticsearch.cfg
new file mode 100644
index 0000000..344f56a
--- /dev/null
+++ 
b/appender/elasticsearch/src/main/cfg/org.apache.karaf.decanter.appender.elasticsearch.cfg
@@ -0,0 +1,2 @@
+host=localhost
+port=9300
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/f19041a4/appender/elasticsearch/src/main/java/org/apache/karaf/decanter/appender/elasticsearch/Activator.java
----------------------------------------------------------------------
diff --git 
a/appender/elasticsearch/src/main/java/org/apache/karaf/decanter/appender/elasticsearch/Activator.java
 
b/appender/elasticsearch/src/main/java/org/apache/karaf/decanter/appender/elasticsearch/Activator.java
index f43d137..0aad636 100644
--- 
a/appender/elasticsearch/src/main/java/org/apache/karaf/decanter/appender/elasticsearch/Activator.java
+++ 
b/appender/elasticsearch/src/main/java/org/apache/karaf/decanter/appender/elasticsearch/Activator.java
@@ -22,22 +22,53 @@ import java.util.Hashtable;
 import org.apache.karaf.decanter.api.Appender;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
 
 public class Activator implements BundleActivator {
 
     private ElasticsearchAppender appender;
+    private static final String CONFIG_PID = 
"org.apache.karaf.decanter.appender.elasticsearch";
 
-    public void start(BundleContext bundleContext) {
-        // TODO embed mode and configuration admin support for location of 
Elasticsearch
-        appender = new ElasticsearchAppender("localhost", 9300);
-        appender.open();
+    public void start(final BundleContext bundleContext) {
+        // TODO embed mode of Elasticsearch
         Dictionary<String, String> properties = new Hashtable<>();
-        properties.put("name", "elasticsearch");
-        bundleContext.registerService(Appender.class, appender, properties);
+        properties.put(Constants.SERVICE_PID, CONFIG_PID);
+        bundleContext.registerService(ManagedService.class.getName(), new 
ConfigUpdater(bundleContext),
+                                      properties);
     }
 
     public void stop(BundleContext bundleContext) {
-        appender.close();;
+        if (appender != null) {
+            appender.close();
+        }
     }
 
+    private final class ConfigUpdater implements ManagedService {
+        private BundleContext bundleContext;
+        private ServiceRegistration<?> serviceReg;
+
+        public ConfigUpdater(BundleContext bundleContext) {
+            this.bundleContext = bundleContext;
+        }
+
+        @SuppressWarnings("rawtypes")
+        @Override
+        public void updated(Dictionary config) throws ConfigurationException {
+            if (appender != null) {
+                appender.close();
+                serviceReg.unregister();
+            }
+
+            String host = config != null ? (String)config.get("host") : 
"localhost";
+            int port = config != null ? 
Integer.parseInt((String)config.get("port")) : 9300;
+            appender = new ElasticsearchAppender(host, port);
+            appender.open();
+            Dictionary<String, String> properties = new Hashtable<>();
+            properties.put("name", "elasticsearch");
+            serviceReg = bundleContext.registerService(Appender.class, 
appender, properties);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/f19041a4/assembly/src/main/feature/feature.xml
----------------------------------------------------------------------
diff --git a/assembly/src/main/feature/feature.xml 
b/assembly/src/main/feature/feature.xml
index 98eba4a..c034871 100644
--- a/assembly/src/main/feature/feature.xml
+++ b/assembly/src/main/feature/feature.xml
@@ -48,6 +48,7 @@
         <bundle>mvn:org.glassfish/javax.json/${glassfish-json.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch/${elasticsearch.bundle.version}</bundle>
         
<bundle>mvn:org.apache.karaf.decanter.appender/org.apache.karaf.decanter.appender.elasticsearch/${project.version}</bundle>
+        <configfile 
finalname="/etc/org.apache.karaf.decanter.appender.elasticsearch.cfg">mvn:org.apache.karaf.decanter.appender/org.apache.karaf.decanter.appender.elasticsearch/${project.version}/cfg</configfile>
     </feature>
 
     <feature name="elasticsearch" version="${elasticsearch.version}" 
description="Embedded Elasticsearch node">

Reply via email to