You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by io...@apache.org on 2011/02/11 17:42:38 UTC

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

Author: iocanel
Date: Fri Feb 11 16:42:37 2011
New Revision: 1069878

URL: http://svn.apache.org/viewvc?rev=1069878&view=rev
Log:
Merged from trunk
[KARAF-415] Added flag in config:edit command which allows editing the configuration by leveraging the felix.fileinstall.filename property [from revision 1069840]
[KARAF-415] Edited the description of the useFile option. Added detailed description that explains the use case of that flag. [from revision 1069862]

Added:
    karaf/branches/karaf-2.2.x/shell/config/src/main/resources/org/
      - copied from r1069862, karaf/trunk/shell/config/src/main/resources/org/
    karaf/branches/karaf-2.2.x/shell/config/src/main/resources/org/apache/
      - copied from r1069862, karaf/trunk/shell/config/src/main/resources/org/apache/
    karaf/branches/karaf-2.2.x/shell/config/src/main/resources/org/apache/karaf/
      - copied from r1069862, karaf/trunk/shell/config/src/main/resources/org/apache/karaf/
    karaf/branches/karaf-2.2.x/shell/config/src/main/resources/org/apache/karaf/shell/
      - copied from r1069862, karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/
    karaf/branches/karaf-2.2.x/shell/config/src/main/resources/org/apache/karaf/shell/config/
      - copied from r1069862, karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/config/
    karaf/branches/karaf-2.2.x/shell/config/src/main/resources/org/apache/karaf/shell/config/edit.txt
      - copied unchanged from r1069862, karaf/trunk/shell/config/src/main/resources/org/apache/karaf/shell/config/edit.txt
Modified:
    karaf/branches/karaf-2.2.x/   (props changed)
    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

Propchange: karaf/branches/karaf-2.2.x/
------------------------------------------------------------------------------
    svn:mergeinfo = /karaf/trunk:1069840,1069862

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=1069878&r1=1069877&r2=1069878&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 Feb 11 16:42:37 2011
@@ -16,34 +16,97 @@
  */
 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.Option;
 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;
 
-@Command(scope = "config", name = "edit", description = "Creates or edits a configuration.")
+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;
 
     @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;
+
+	 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) {
             System.err.println("Another config is being edited.  Cancel / update first, or use the --force option");
             return;
         }
-        Dictionary props = admin.getConfiguration(pid).getProperties();
-        if (props == null) {
-            props = new Properties();
-        }
+	    Dictionary props;
+
+	    //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;
+		    }
+		    props = configuration.getProperties();
+		    pid = (String) configuration.getPid();
+	    } else {
+		    props = admin.getConfiguration(pid).getProperties();
+		    if (props == null) {
+			    props = new Properties();
+		    }
+	    }
         this.session.put(PROPERTY_CONFIG_PID, pid);
         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=1069878&r1=1069877&r2=1069878&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 Feb 11 16:42:37 2011
@@ -27,7 +27,9 @@
             <action class="org.apache.karaf.shell.config.CancelCommand"/>
         </command>
         <command name="config/edit">
-            <action class="org.apache.karaf.shell.config.EditCommand"/>
+            <action class="org.apache.karaf.shell.config.EditCommand">
+                <property name="storage" value="${storage}" />
+            </action>
             <completers>
                 <ref component-id="configCompleter" />
                 <null/>