/*
 * 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.felix.shell.impl;


import java.io.PrintStream;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.felix.shell.Command;


/**
 * Command to display, set and modify system properties
 * Usage:
 * sysprop                 -> displays all the system properties
 * sysprop [key]           -> displays the [key] property 
 * sysprop [key] [value]   -> set the property [key] to [value]
 */
public class SystemPropertiesCommandImpl implements Command
{

    public void execute( String line, PrintStream out, PrintStream err )
    {
        StringTokenizer st = new StringTokenizer( line );
        int tokens = st.countTokens();

        if ( tokens == 1 )
        {
            printAll( out );
        }
        else
        {
            st.nextToken();
            String propertyKey = st.nextToken();

            if ( tokens == 2 )
            {
                out.println( propertyKey + "=" + System.getProperty( propertyKey ) );
            }
            else
            {
                String value = st.nextToken();
                System.setProperty( propertyKey, value );
                out.println( "Set " + propertyKey + "=" + value );
            }
        }
    }


    private void printAll( PrintStream out )
    {
        out.println( "-------System properties------" );
        for ( Iterator keyIterator = System.getProperties().keySet().iterator(); keyIterator.hasNext(); )
        {
            Object key = keyIterator.next();
            out.println( key.toString() + "=" + System.getProperty( key.toString() ) );
        }
    }


    public String getName()
    {
        return "sysprop";
    }


    public String getShortDescription()
    {
        return "Display, set and modify system properties";
    }


    public String getUsage()
    {
        return "sysprop [<key>] [<value>]";
    }

}

@@ -839,17 +839,16 @@
 .impl;%0A%0A
-%0A
 import j
@@ -969,17 +969,16 @@
 mmand;%0A%0A
-%0A
 /**%0A * C
@@ -1293,17 +1293,16 @@
 mmand%0A%7B%0A
-%0A
     publ
@@ -1317,17 +1317,16 @@
 execute(
- 
 String l
@@ -1362,17 +1362,16 @@
 ream err
- 
 )%0A    %7B%0A
@@ -1423,14 +1423,12 @@
 zer(
- 
 line
- 
 );%0A 
@@ -1470,33 +1470,32 @@
 );%0A%0A        if (
- 
 tokens == 1 )%0A  
@@ -1489,17 +1489,16 @@
 ens == 1
- 
 )%0A      
@@ -1522,21 +1522,19 @@
 rintAll(
- 
 out
- 
 );%0A     
@@ -1655,17 +1655,16 @@
     if (
- 
 tokens =
@@ -1666,17 +1666,16 @@
 ens == 2
- 
 )%0A      
@@ -1710,17 +1710,16 @@
 println(
- 
 property
@@ -1741,33 +1741,32 @@
 tem.getProperty(
- 
 propertyKey ) );
@@ -1760,19 +1760,17 @@
 pertyKey
- ) 
+)
 );%0A     
@@ -1891,17 +1891,16 @@
 roperty(
- 
 property
@@ -1905,25 +1905,24 @@
 tyKey, value
- 
 );%0A         
@@ -1940,17 +1940,16 @@
 println(
- 
 %22Set %22 +
@@ -1974,17 +1974,16 @@
  + value
- 
 );%0A     
@@ -2004,25 +2004,24 @@
    %7D%0A    %7D%0A%0A
-%0A
     private 
@@ -2034,17 +2034,16 @@
 rintAll(
- 
 PrintStr
@@ -2049,17 +2049,16 @@
 ream out
- 
 )%0A    %7B%0A
@@ -2077,17 +2077,16 @@
 println(
- 
 %22-------
@@ -2109,17 +2109,16 @@
 s------%22
- 
 );%0A     
@@ -2125,17 +2125,16 @@
    for (
- 
 Iterator
@@ -2296,17 +2296,16 @@
 println(
- 
 key.toSt
@@ -2338,17 +2338,16 @@
 roperty(
- 
 key.toSt
@@ -2356,11 +2356,9 @@
 ng()
- ) 
+)
 );%0A 
@@ -2365,33 +2365,32 @@
        %7D%0A    %7D%0A%0A
-%0A
     public Strin
@@ -2432,33 +2432,32 @@
 ysprop%22;%0A    %7D%0A%0A
-%0A
     public Strin
@@ -2553,17 +2553,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -2642,7 +2642,5 @@
   %7D%0A
-%0A%7D%0A
+%7D

Reformatted code to Felix coding style (mostly removed extra whitespace).