Package org.platformlayer.keystone.cli

Source Code of org.platformlayer.keystone.cli.KeystoneCliOptions

package org.platformlayer.keystone.cli;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.kohsuke.args4j.Option;

import com.fathomdb.cli.CliOptions;
import com.fathomdb.io.IoUtils;
import com.fathomdb.io.NoCloseInputStream;

public class KeystoneCliOptions extends CliOptions {
  @Option(name = "-c", aliases = "--config", usage = "specify configuration file")
  String configFile;

  @Option(name = "-u", aliases = "--username", usage = "login username")
  String username;

  @Option(name = "-p", aliases = "--password", usage = "login password")
  String password;

  Properties config;

  public Properties getConfigurationProperties() {
    if (config == null) {
      Properties build = new Properties();

      if (configFile != null) {
        InputStream is = null;
        try {
          if (configFile.equals("-")) {
            // Read from stdin
            // Don't auto-close it, and that terminates nailgun
            is = new NoCloseInputStream(System.in);
          } else {
            if (isServerMode()) {
              throw new IllegalArgumentException("Must pass config file over stdin in server mode");
            }
            File file = IoUtils.resolve(configFile);
            if (!file.exists()) {
              throw new FileNotFoundException("Configuration file not found: " + file);
            }

            is = new FileInputStream(file);
          }

          try {
            build.load(is);
          } catch (IOException e) {
            throw new IOException("Error reading configuration file", e);
          }
        } catch (IOException e) {
          throw new IllegalArgumentException("Error reading configuration file", e);
        } finally {
          IoUtils.safeClose(is);
        }
      }

      if (this.username != null) {
        build.setProperty("platformlayer.username", this.username);
      }
      if (this.password != null) {
        build.setProperty("platformlayer.password", this.password);
      }

      this.config = build;
    }

    return config;
  }

  public String getUsername() {
    return getConfigurationProperties().getProperty("platformlayer.username");
  }

  public String getPassword() {
    return getConfigurationProperties().getProperty("platformlayer.password");
  }

}
TOP

Related Classes of org.platformlayer.keystone.cli.KeystoneCliOptions

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.