Search in sources :

Example 1 with HttpPlatformLayerClient

use of org.platformlayer.HttpPlatformLayerClient in project platformlayer by platformlayer.

the class ConfigurationOptions method buildPlatformLayerClient.

public PlatformLayerClient buildPlatformLayerClient() throws IOException, OpsException {
    HttpPlatformLayerClient client;
    if (configFile == null) {
        throw new IllegalArgumentException("Config file is required");
    }
    InputStream is = null;
    try {
        if (configFile.equals("-")) {
            // Read from stdin
            // Don't auto-close it: 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);
        }
        final Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new IOException("Error reading configuration file", e);
        }
        if (properties.getProperty("platformlayer.username") == null) {
            throw new CliException("User property not set in configuration file");
        }
        if (debug) {
            client = buildPlatformLayerClient(properties, debug);
        } else {
            String propertiesKey = PropertyUtils.serialize(properties);
            try {
                client = platformLayerClientCache.get(propertiesKey, new Callable<HttpPlatformLayerClient>() {

                    @Override
                    public HttpPlatformLayerClient call() throws Exception {
                        return buildPlatformLayerClient(properties, false);
                    }
                });
            } catch (ExecutionException e) {
                throw new CliException("Error building platformlayer client", e);
            }
        }
    } finally {
        if (is != System.in) {
            Closeables.closeQuietly(is);
        }
    }
    return client;
}
Also used : HttpPlatformLayerClient(org.platformlayer.HttpPlatformLayerClient) FileInputStream(java.io.FileInputStream) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Callable(java.util.concurrent.Callable) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) CliException(com.fathomdb.cli.CliException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 2 with HttpPlatformLayerClient

use of org.platformlayer.HttpPlatformLayerClient in project platformlayer by platformlayer.

the class ConfigurationOptions method buildPlatformLayerClient.

private HttpPlatformLayerClient buildPlatformLayerClient(Properties properties, boolean debug) {
    HttpStrategy httpStrategy = new JreHttpStrategy();
    // HttpStrategy httpStrategy = new ApacheCommonsHttpStrategy();
    HttpPlatformLayerClient client = HttpPlatformLayerClient.buildUsingProperties(httpStrategy, properties);
    if (debug) {
        client.setDebug(System.err);
    } else {
        // We don't want debug messages to interfere with our output
        // TODO: Fix this so debug output doesn't interfere (stderr?)
        // TODO: Maybe output the debug info only in case of failure?
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        client.setDebug(new PrintStream(baos));
    }
    return client;
}
Also used : HttpPlatformLayerClient(org.platformlayer.HttpPlatformLayerClient) PrintStream(java.io.PrintStream) JreHttpStrategy(org.platformlayer.http.jre.JreHttpStrategy) HttpStrategy(org.platformlayer.http.HttpStrategy) JreHttpStrategy(org.platformlayer.http.jre.JreHttpStrategy) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

HttpPlatformLayerClient (org.platformlayer.HttpPlatformLayerClient)2 CliException (com.fathomdb.cli.CliException)1 NoCloseInputStream (com.fathomdb.io.NoCloseInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 Properties (java.util.Properties)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 HttpStrategy (org.platformlayer.http.HttpStrategy)1 JreHttpStrategy (org.platformlayer.http.jre.JreHttpStrategy)1