ForwarderManager.java revision c6a341d34c690c2b5948977cd803ec2ff668c4b7
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dumprendertree2.forwarder;
18
19import java.net.MalformedURLException;
20import java.net.URL;
21import java.util.HashSet;
22import java.util.Set;
23
24/**
25 * A simple class to start and stop Forwarders running on some ports.
26 *
27 * It uses a singleton pattern and is thread safe.
28 */
29public class ForwarderManager {
30    /**
31     * The IP address of the server serving the tests.
32     */
33    private static final String HOST_IP = "127.0.0.1";
34
35    /**
36     * We use these ports because other webkit platforms do. They are set up in
37     * external/webkit/LayoutTests/http/conf/apache2-debian-httpd.conf
38     */
39    public static final int HTTP_PORT = 8080;
40    public static final int HTTPS_PORT = 8443;
41
42    public static final String HOST = "localhost";
43
44    private static ForwarderManager forwarderManager;
45
46    private Set<Forwarder> mServers;
47
48    private ForwarderManager() {
49        mServers = new HashSet<Forwarder>(2);
50        mServers.add(new Forwarder(HTTP_PORT, HOST_IP));
51        mServers.add(new Forwarder(HTTPS_PORT, HOST_IP));
52    }
53
54    /**
55     * Returns the main part of the URL with the trailing slash
56     *
57     * @param isHttps
58     * @return
59     */
60    public static final String getHostSchemePort(boolean isHttps) {
61        int port;
62        String protocol;
63        if (isHttps) {
64            protocol = "https";
65            port = HTTPS_PORT;
66        } else {
67            protocol = "http";
68            port = HTTP_PORT;
69        }
70
71        URL url = null;
72        try {
73            url = new URL(protocol, HOST, port, "/");
74        } catch (MalformedURLException e) {
75            assert false : "isHttps=" + isHttps;
76        }
77
78        return url.toString();
79    }
80
81    public static synchronized ForwarderManager getForwarderManager() {
82        if (forwarderManager == null) {
83            forwarderManager = new ForwarderManager();
84        }
85        return forwarderManager;
86    }
87
88    @Override
89    public Object clone() throws CloneNotSupportedException {
90        throw new CloneNotSupportedException();
91    }
92
93    public synchronized void start() {
94        for (Forwarder server : mServers) {
95            server.start();
96        }
97    }
98
99    public synchronized void stop() {
100        for (Forwarder server : mServers) {
101            server.finish();
102        }
103    }
104}