Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.SetCommand

package org.jboss.fresh.shell.commands;

import org.jboss.fresh.io.BufferOutputStream;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.shell.impl.Process;
import org.jboss.fresh.shell.impl.ShellImpl;
import org.jboss.fresh.shell.impl.SystemShellImpl;

import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;


// EX OK

public class SetCommand extends AbstractExecutable {
  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SetCommand.class);

  /**

   Sets the new system property value.

   */


  public void process(String exepath, String[] params) throws Exception {
    log.debug("entered");

    if (helpRequested()) {
      PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
      out.println("Usage: set [-s] PROPERTY = NEW_VALUE");
      out.println("              set property value");
      out.println();
      out.println("   -s          : Set system property (System.setProperty)");
      out.println("   -p <procid> : process id - apply set operation to a process / shell ");
      out.println("                 with the specified id");
            out.println();
            out.println("       set");
            out.println("       lists all environment properties - ordered alphabetically");
            out.println();
            out.println("       set --help");
            out.println("       this help");
      out.close();
      log.debug("done");
      return;
    }



    // parametri se s presledki - whitespace se ignorira zato utegnemo uporabiti ""
    // "" se morajo potem odstraniti. Mislim, da se.
    // Mi torej dobimo parameter kot celoto
    // En parameter mora biti =
    // �e da� samo set name se izpi�e vrednost za name
    boolean systemProp = false;
        boolean eq = false;
        String key = null;
        String val = null;
        String procid = null;

        for(int i=0; i<params.length; i++) {
            String temp = params[i].trim();
            if("-s".equals(temp)) {
                systemProp = true;
            } else if("-p".equals(temp)) {
                if(i<params.length-1) {
                  procid = params[++i];
                } else {
                  error("-p parameter must be followed by a process id");
                  return;
                }
            } else if(key == null) {
                int pos = temp.indexOf("=");
                if(pos == -1) {
                    key = temp;
                } else {
                    key = temp.substring(0, pos).trim();
                    val = temp.substring(pos+1).trim();
                    eq = true;
                }
            } else if("=".equals(temp)) {
                eq = true;
            } else if(eq && val == null) {
                val = temp;
            }
        }

       
    // if no name
    if (key == null) {
      // display all props
      // name=value
            PrintWriter2 pout = new PrintWriter2(new BufferedOutputStream(new BufferOutputStream(getStdOut())));
      Properties p = null;
      if(procid == null) {
        p = getProcess().getEnv().getEnvProperties();
      } else {
        SystemShellImpl ss = (SystemShellImpl) ((ShellImpl) getShell()).getSystemShell();
        Process proc = ss.findProcess(procid);
        if (proc == null) {
          Map m = ss.getShellMap();
          ShellImpl shel = (ShellImpl) m.get(procid);
          if(shel == null) {
            error("No process / shell found for id: " + procid);
            return;
          }
          p = shell.getEnv().getEnvProperties();
        } else {
          if(proc.getEnv() != null) {
            error("Process has no environment - it means it exited but wasn't yet evicted");
            return;
          }
          p = proc.getEnv().getEnvProperties();
        }
       
      }
      HashMap map = new HashMap();
      Enumeration enu = p.propertyNames();
      while (enu.hasMoreElements()) {
        String nm = (String) enu.nextElement();
        map.put(nm, p.getProperty(nm));
      }

      TreeMap sm = new TreeMap(map);
      Iterator it = sm.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry ent = (Map.Entry) it.next();
        pout.println(ent.getKey() + "=" + ent.getValue());
      }

      pout.close();
      log.debug("done");
      return;
    }

        if(procid == null) {
          if(key == null) {
              error("Nothing to do");
              return;
          }
 
          if(key.length() == 0) {
              error("ENV variable name not specified");
              return;
          }
        }

    if(!eq) {
            // just display the value
      if(procid == null) {
        val = getProcess().getEnv().getEnvProperty(key);
      } else {
        SystemShellImpl ss = (SystemShellImpl) ((ShellImpl) getShell()).getSystemShell();
        Process proc = ss.findProcess(procid);
        if (proc == null) {
          Map m = ss.getShellMap();
          ShellImpl shel = (ShellImpl) m.get(procid);
          if(shel == null) {
            error("No process / shell found for id: " + procid);
            return;
          }
          val = shell.getEnv().getEnvProperty(key);
        } else {
          val = proc.getEnv().getEnvProperty(key);
        }
      }

      getStdOut().put(key + "=" + (val == null ? "" : val), 10000L);
            return;
        }


        if(val == null)
            val = "";

        // �e da� samo set name = se name anulira
    if(systemProp)
            System.setProperty(key, val);
        getShell().setEnvProperty(key, val);

    //log.debug("[SetCommand] done.");
    log.debug("done");
  }
}
TOP

Related Classes of org.jboss.fresh.shell.commands.SetCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.