org.jboss.jdf.stacks.StacksFactory.java Source code

Java tutorial

Introduction

Here is the source code for org.jboss.jdf.stacks.StacksFactory.java

Source

/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.jdf.stacks;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
 */
public class StacksFactory {

    // TODO delete
    public static void main(final String[] args) throws Exception {
        show(create());
        show(create(StacksConfigurationBuilder
                .create(Thread.currentThread().getContextClassLoader().getResource("stacks.yaml")).build()));
    }

    private static void show(final Stacks stacks) {
        for (ServerRuntime serverRuntime : stacks.getAvailableRuntimes()) {
            System.out.printf("ServerRuntime: %s - %s%n", serverRuntime.getName(), serverRuntime.getVersion());
            System.out.printf("    Group Id: %s - Artifact Id: %s%n%n", serverRuntime.getGroupId(),
                    serverRuntime.getArtifactId());
        }
    }

    public static Stacks create() throws IOException {
        return create(StacksConfiguration.DEFAULT);
    }

    public static Stacks create(final StacksConfiguration stacksConfig) throws IOException {
        InputStream inputStream = null;
        HttpResponse response = null;
        DefaultHttpClient client = null;
        final URL url = stacksConfig.getUrl();
        try {
            if (url.getProtocol().startsWith("file")) {
                inputStream = new FileInputStream(new File(url.toURI()));
            } else {
                client = new DefaultHttpClient();
                configureProxy(client, stacksConfig);
                final HttpGet method = new HttpGet(url.toURI());
                response = client.execute(method);
                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    inputStream = entity.getContent();
                }
            }
            return new Parser().parse(inputStream);
        } catch (URISyntaxException e) {
            // TODO cleanup
            throw new IllegalStateException(e);
        } finally {
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(client);
            Parser.safeClose(inputStream);
        }
    }

    private static void configureProxy(final DefaultHttpClient client, final StacksConfiguration stacksConfig) {
        if (stacksConfig.getProxyHost() != null) {
            final String proxyHost = stacksConfig.getProxyHost();
            final int proxyPort = stacksConfig.getProxyPort();
            final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            final String proxyUsername = stacksConfig.getProxyUser();
            if (proxyUsername != null && !proxyUsername.isEmpty()) {
                final String proxyPassword = stacksConfig.getProxyPassword();
                final AuthScope authScope = new AuthScope(proxyHost, proxyPort);
                final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(proxyUsername,
                        proxyPassword);
                client.getCredentialsProvider().setCredentials(authScope, credentials);
            }
        }
    }
}