karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject karaf git commit: [KARAF-4514] Add config:install command and MBean operation
Date Tue, 28 Mar 2017 05:56:03 GMT
Repository: karaf
Updated Branches:
  refs/heads/karaf-4.0.x 49ae5c991 -> 16c7fc33b


[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/16c7fc33
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/16c7fc33
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/16c7fc33

Branch: refs/heads/karaf-4.0.x
Commit: 16c7fc33b43077665a57f53b1bc94a8b356516e4
Parents: 49ae5c9
Author: Jean-Baptiste Onofré <jbonofre@apache.org>
Authored: Tue Mar 28 07:42:08 2017 +0200
Committer: Jean-Baptiste Onofré <jbonofre@apache.org>
Committed: Tue Mar 28 07:50:00 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 | 36 ++++++++-
 4 files changed, 123 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/16c7fc33/config/pom.xml
----------------------------------------------------------------------
diff --git a/config/pom.xml b/config/pom.xml
index 02f6787..e195d2d 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/16c7fc33/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/16c7fc33/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 58afb2d..0da96a9 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/16c7fc33/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 cc30538..4d2d291 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;
 
 /**
@@ -85,6 +88,37 @@ 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);


Mime
View raw message