You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2017/03/28 05:48:30 UTC

karaf git commit: [KARAF-4514] Add config:install command and MBean operation

Repository: karaf
Updated Branches:
  refs/heads/master 1c6b46323 -> 9cf319f12


[KARAF-4514]�Add config:install command and MBean operation


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

Branch: refs/heads/master
Commit: 9cf319f129410c9756d1453c8d6082167a4f372f
Parents: 1c6b463
Author: Jean-Baptiste Onofr� <jb...@apache.org>
Authored: Tue Mar 28 07:42:08 2017 +0200
Committer: Jean-Baptiste Onofr� <jb...@apache.org>
Committed: Tue Mar 28 07:42:08 2017 +0200

----------------------------------------------------------------------
 config/pom.xml                                  |  1 +
 .../karaf/config/command/InstallCommand.java    | 77 ++++++++++++++++++++
 .../apache/karaf/config/core/ConfigMBean.java   | 10 +++
 .../karaf/config/core/impl/ConfigMBeanImpl.java | 35 ++++++++-
 4 files changed, 122 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/9cf319f1/config/pom.xml
----------------------------------------------------------------------
diff --git a/config/pom.xml b/config/pom.xml
index 5974eb3..f2af2ba 100644
--- a/config/pom.xml
+++ b/config/pom.xml
@@ -110,6 +110,7 @@
                         <Private-Package>
                             org.apache.karaf.config.core.impl,
                             org.apache.karaf.config.core.impl.osgi,
+                            org.apache.karaf.util,
                             org.apache.felix.utils.properties
                         </Private-Package>
                     </instructions>

http://git-wip-us.apache.org/repos/asf/karaf/blob/9cf319f1/config/src/main/java/org/apache/karaf/config/command/InstallCommand.java
----------------------------------------------------------------------
diff --git a/config/src/main/java/org/apache/karaf/config/command/InstallCommand.java b/config/src/main/java/org/apache/karaf/config/command/InstallCommand.java
new file mode 100644
index 0000000..9e56cb1
--- /dev/null
+++ b/config/src/main/java/org/apache/karaf/config/command/InstallCommand.java
@@ -0,0 +1,77 @@
+/*
+ * 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.karaf.config.command;
+
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.util.StreamUtils;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+@Command(scope = "config", name = "install", description = "Install a cfg file in the Karaf etc folder.")
+@Service
+public class InstallCommand implements Action {
+
+    @Argument(index = 0, name = "url", description = "The URL of the cfg file.", required = true, multiValued = false)
+    private String url;
+
+    @Argument(index = 1, name = "finalname", description = "Final name of the cfg file", required = true, multiValued = false)
+    private String finalname;
+
+    @Option(name = "-o", aliases = { "--override" }, description = "Override the target cfg file", required = false, multiValued = false)
+    private boolean override;
+
+    @Override
+    public Object execute() throws Exception {
+        File etcFolder = new File(System.getProperty("karaf.etc"));
+        File file = new File(etcFolder, finalname);
+        if (file.exists()) {
+            if (!override) {
+                throw new IllegalArgumentException("Configuration file {} already exists " + finalname);
+            } else {
+                System.out.println("Overriding configuration file " + finalname);
+            }
+        } else {
+            System.out.println("Creating configuration file " + finalname);
+        }
+
+        try (InputStream is = new BufferedInputStream(new URL(url).openStream())) {
+            if (!file.exists()) {
+                File parentFile = file.getParentFile();
+                if (parentFile != null) {
+                    parentFile.mkdirs();
+                }
+                file.createNewFile();
+            }
+            try (FileOutputStream fop = new FileOutputStream(file)) {
+                StreamUtils.copy(is, fop);
+            }
+        } catch (RuntimeException | MalformedURLException e) {
+            throw e;
+        }
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/9cf319f1/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java
----------------------------------------------------------------------
diff --git a/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java b/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java
index 91454ce..283f677 100644
--- a/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java
+++ b/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java
@@ -39,6 +39,16 @@ public interface ConfigMBean {
     void create(String pid) throws MBeanException;
 
     /**
+     * Install a cfg file.
+     *
+     * @param url The location of the cfg file.
+     * @param finalname The final name of the cfg file in the etc folder.
+     * @param override True to override the cfg file if it already exists, false else.
+     * @throws MBeanException in case of MBean failure.
+     */
+    void install(String url, String finalname, boolean override) throws MBeanException;
+
+    /**
      * Delete a configuration identified by the given PID.
      *
      * @param pid the configuration PID to delete.

http://git-wip-us.apache.org/repos/asf/karaf/blob/9cf319f1/config/src/main/java/org/apache/karaf/config/core/impl/ConfigMBeanImpl.java
----------------------------------------------------------------------
diff --git a/config/src/main/java/org/apache/karaf/config/core/impl/ConfigMBeanImpl.java b/config/src/main/java/org/apache/karaf/config/core/impl/ConfigMBeanImpl.java
index 489834b..5688370 100644
--- a/config/src/main/java/org/apache/karaf/config/core/impl/ConfigMBeanImpl.java
+++ b/config/src/main/java/org/apache/karaf/config/core/impl/ConfigMBeanImpl.java
@@ -13,7 +13,9 @@
  */
 package org.apache.karaf.config.core.impl;
 
-import java.io.IOException;
+import java.io.*;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.Enumeration;
@@ -28,6 +30,7 @@ import javax.management.StandardMBean;
 
 import org.apache.karaf.config.core.ConfigMBean;
 import org.apache.karaf.config.core.ConfigRepository;
+import org.apache.karaf.util.StreamUtils;
 import org.osgi.service.cm.Configuration;
 
 /**
@@ -87,6 +90,36 @@ public class ConfigMBeanImpl extends StandardMBean implements ConfigMBean {
     }
 
     @Override
+    public void install(String url, String finalname, boolean override) throws MBeanException {
+        try {
+            File etcFolder = new File(System.getProperty("karaf.etc"));
+            File file = new File(etcFolder, finalname);
+            if (file.exists()) {
+                if (!override) {
+                    throw new IllegalArgumentException("Configuration file {} already exists " + finalname);
+                }
+            }
+
+            try (InputStream is = new BufferedInputStream(new URL(url).openStream())) {
+                if (!file.exists()) {
+                    File parentFile = file.getParentFile();
+                    if (parentFile != null) {
+                        parentFile.mkdirs();
+                    }
+                    file.createNewFile();
+                }
+                try (FileOutputStream fop = new FileOutputStream(file)) {
+                    StreamUtils.copy(is, fop);
+                }
+            } catch (RuntimeException | MalformedURLException e) {
+                throw e;
+            }
+        } catch (Exception e) {
+            throw new MBeanException(null, e.toString());
+        }
+    }
+
+    @Override
     public void delete(String pid) throws MBeanException {
         try {
             this.configRepo.delete(pid);