You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2011/03/11 12:06:23 UTC

svn commit: r1080522 - in /karaf/branches/karaf-2.2.x/shell/config/src/main: java/org/apache/karaf/shell/config/ resources/OSGI-INF/blueprint/

Author: gnodet
Date: Fri Mar 11 11:06:23 2011
New Revision: 1080522

URL: http://svn.apache.org/viewvc?rev=1080522&view=rev
Log:
[KARAF-506] The config shell commands offer no way to delete a command

Added:
    karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/DeleteCommand.java
Modified:
    karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/ConfigCommandSupport.java
    karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
    karaf/branches/karaf-2.2.x/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml

Modified: karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/ConfigCommandSupport.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/ConfigCommandSupport.java?rev=1080522&r1=1080521&r2=1080522&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/ConfigCommandSupport.java (original)
+++ karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/ConfigCommandSupport.java Fri Mar 11 11:06:23 2011
@@ -16,10 +16,14 @@
  */
 package org.apache.karaf.shell.config;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.Dictionary;
 
 import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 
 /**
@@ -32,6 +36,13 @@ public abstract class ConfigCommandSuppo
 
     public static final String PROPERTY_CONFIG_PID = "ConfigCommand.PID";
     public static final String PROPERTY_CONFIG_PROPS = "ConfigCommand.Props";
+    private static final String PID_FILTER="(service.pid=%s*)";
+    private static final String FILE_PREFIX="file:";
+    private static final String CONFIG_SUFFIX=".cfg";
+    private static final String FACTORY_SEPARATOR = "-";
+    private static final String FILEINSTALL_FILE_NAME="felix.fileinstall.filename";
+
+    private File storage;
 
     protected Object doExecute() throws Exception {
         // Get config admin service.
@@ -61,4 +72,38 @@ public abstract class ConfigCommandSuppo
 
     protected abstract void doExecute(ConfigurationAdmin admin) throws Exception;
 
+    /**
+     * <p>
+     * Returns the Configuration object of the given (felix fileinstall) file name.
+     * </p>
+     * @param fileName
+     * @return
+     */
+    public Configuration findConfigurationByFileName(ConfigurationAdmin admin, String fileName) throws IOException, InvalidSyntaxException {
+        if (fileName != null && fileName.contains(FACTORY_SEPARATOR)) {
+            String factoryPid = fileName.substring(0, fileName.lastIndexOf(FACTORY_SEPARATOR));
+            String absoluteFileName = FILE_PREFIX +storage.getAbsolutePath() + File.separator + fileName + CONFIG_SUFFIX;
+            Configuration[] configurations = admin.listConfigurations(String.format(PID_FILTER, factoryPid));
+            if (configurations != null) {
+                for (Configuration configuration : configurations) {
+                    Dictionary dictionary = configuration.getProperties();
+                    if (dictionary != null) {
+                        String fileInstallFileName = (String) dictionary.get(FILEINSTALL_FILE_NAME);
+                        if (absoluteFileName.equals(fileInstallFileName)) {
+                            return configuration;
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    public File getStorage() {
+        return storage;
+    }
+
+    public void setStorage(File storage) {
+        this.storage = storage;
+    }
 }

Added: karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/DeleteCommand.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/DeleteCommand.java?rev=1080522&view=auto
==============================================================================
--- karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/DeleteCommand.java (added)
+++ karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/DeleteCommand.java Fri Mar 11 11:06:23 2011
@@ -0,0 +1,62 @@
+/*
+ * 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.shell.config;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+@Command(scope = "config", name = "delete", description = "Delete a configuration.")
+public class DeleteCommand extends ConfigCommandSupport {
+
+    @Argument(index = 0, name = "pid", description = "PID of the configuration", required = true, multiValued = false)
+    String pid;
+
+    @Option(name = "--force", aliases = {}, description = "Force the edition of this config, even if another one was under edition", required = false, multiValued = false)
+    boolean force;
+
+    @Option(name = "-f", aliases = {"--use-file"}, description = "Configuration lookup using the filename instead of the pid", required = false, multiValued = false)
+    boolean useFile;
+
+    protected void doExecute(ConfigurationAdmin admin) throws Exception {
+        String oldPid = (String) this.session.get(PROPERTY_CONFIG_PID);
+        if (oldPid != null && oldPid.equals(pid) && !force) {
+            System.err.println("This config is being edited.  Cancel / update first, or use the --force option");
+            return;
+        }
+
+        // User selected to use file instead.
+        if (useFile) {
+            Configuration configuration = this.findConfigurationByFileName(admin, pid);
+            if(configuration == null) {
+                System.err.println("Could not find configuration with file install property set to: " + pid);
+                return;
+            }
+            configuration.delete();
+        } else {
+            Configuration configuration = admin.getConfiguration(pid);
+            configuration.delete();
+        }
+        if (oldPid != null && oldPid.equals(pid) && !force) {
+            this.session.put(PROPERTY_CONFIG_PID, null);
+            this.session.put(PROPERTY_CONFIG_PROPS, null);
+        }
+    }
+
+}

Modified: karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java?rev=1080522&r1=1080521&r2=1080522&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java (original)
+++ karaf/branches/karaf-2.2.x/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java Fri Mar 11 11:06:23 2011
@@ -16,27 +16,18 @@
  */
 package org.apache.karaf.shell.config;
 
+import java.util.Dictionary;
+import java.util.Properties;
+
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.felix.gogo.commands.Option;
-import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.Dictionary;
-import java.util.Properties;
-
 @Command(scope = "config", name = "edit", description = "Creates or edits a configuration.", detailedDescription="classpath:edit.txt")
 public class EditCommand extends ConfigCommandSupport {
 
-	private static final String PID_FILTER="(service.pid=%s*)";
-	private static final String FILE_PREFIX="file:";
-	private static final String CONFIG_SUFFIX=".cfg";
-	private static final String FACTORY_SEPARATOR = "-";
-	private static final String FILEINSTALL_FILE_NAME="felix.fileinstall.filename";
-
     @Argument(index = 0, name = "pid", description = "PID of the configuration", required = true, multiValued = false)
     String pid;
 
@@ -46,8 +37,6 @@ public class EditCommand extends ConfigC
 	@Option(name = "-f", aliases = {"--use-file"}, description = "Configuration lookup using the filename instead of the pid", required = false, multiValued = false)
     boolean useFile;
 
-	 private File storage;
-
     protected void doExecute(ConfigurationAdmin admin) throws Exception {
         String oldPid = (String) this.session.get(PROPERTY_CONFIG_PID);
         if (oldPid != null && !oldPid.equals(pid) && !force) {
@@ -60,11 +49,11 @@ public class EditCommand extends ConfigC
 	    if (useFile) {
 		    Configuration configuration = this.findConfigurationByFileName(admin, pid);
 		    if(configuration == null) {
-			    System.err.println("Could not find configuration with file install property set to:"+pid);
+			    System.err.println("Could not find configuration with file install property set to: " + pid);
 			    return;
 		    }
 		    props = configuration.getProperties();
-		    pid = (String) configuration.getPid();
+		    pid = configuration.getPid();
 	    } else {
 		    props = admin.getConfiguration(pid).getProperties();
 		    if (props == null) {
@@ -75,38 +64,4 @@ public class EditCommand extends ConfigC
         this.session.put(PROPERTY_CONFIG_PROPS, props);
     }
 
-	/**
-	 * <p>
-	 * Returns the Configuration object of the given (felix fileinstall) file name.
-	 * </p>
-	 * @param fileName
-	 * @return
-	 */
-	public Configuration findConfigurationByFileName(ConfigurationAdmin admin, String fileName) throws IOException, InvalidSyntaxException {
-		if (fileName != null && fileName.contains(FACTORY_SEPARATOR)) {
-			String factoryPid = fileName.substring(0, fileName.lastIndexOf(FACTORY_SEPARATOR));
-			String absoluteFileName = FILE_PREFIX+storage.getAbsolutePath() + File.separator + fileName + CONFIG_SUFFIX;
-			Configuration[] configurations = admin.listConfigurations(String.format(PID_FILTER, factoryPid));
-			if (configurations != null) {
-				for (Configuration configuration : configurations) {
-					Dictionary dictionary = configuration.getProperties();
-					if (dictionary != null) {
-						String fileInstallFileName = (String) dictionary.get(FILEINSTALL_FILE_NAME);
-						if (absoluteFileName.equals(fileInstallFileName)) {
-							return configuration;
-						}
-					}
-				}
-			}
-		}
-		return null;
-	}
-
-	public File getStorage() {
-        return storage;
-    }
-
-    public void setStorage(File storage) {
-        this.storage = storage;
-    }
 }

Modified: karaf/branches/karaf-2.2.x/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml?rev=1080522&r1=1080521&r2=1080522&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml (original)
+++ karaf/branches/karaf-2.2.x/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml Fri Mar 11 11:06:23 2011
@@ -26,6 +26,15 @@
         <command name="config/cancel">
             <action class="org.apache.karaf.shell.config.CancelCommand"/>
         </command>
+        <command name="config/delete">
+            <action class="org.apache.karaf.shell.config.DeleteCommand">
+                <property name="storage" value="${storage}" />
+            </action>
+            <completers>
+                <ref component-id="configCompleter" />
+                <null/>
+            </completers>
+        </command>
         <command name="config/edit">
             <action class="org.apache.karaf.shell.config.EditCommand">
                 <property name="storage" value="${storage}" />