Isnt' that already done by the config:update command which actually
save to the disk and overwrite the properties file ?

On Fri, Feb 11, 2011 at 16:36,  <[email protected]> wrote:
> Author: iocanel
> Date: Fri Feb 11 15:36:18 2011
> New Revision: 1069840
>
> URL: http://svn.apache.org/viewvc?rev=1069840&view=rev
> Log:
> [KARAF-415] Added flag in config:edit command which allows editing the 
> configuration by leveraging the felix.fileinstall.filename property
>
> Modified:
>    
> karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
>    
> karaf/trunk/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
>
> Modified: 
> karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
> URL: 
> http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java?rev=1069840&r1=1069839&r2=1069840&view=diff
> ==============================================================================
> --- 
> karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
>  (original)
> +++ 
> karaf/trunk/shell/config/src/main/java/org/apache/karaf/shell/config/EditCommand.java
>  Fri Feb 11 15:36:18 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;
>
> +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.")
>  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 = "Force 
> the edition of this config, even if another one was under edition", 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/trunk/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
> URL: 
> http://svn.apache.org/viewvc/karaf/trunk/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml?rev=1069840&r1=1069839&r2=1069840&view=diff
> ==============================================================================
> --- 
> karaf/trunk/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
>  (original)
> +++ 
> karaf/trunk/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
>  Fri Feb 11 15:36:18 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/>
>
>
>



-- 
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
------------------------
Open Source SOA
http://fusesource.com

Reply via email to