ForwarderManager.java revision 56d7e400ece64591685c8a21dbb82a94a7bd8010
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.util.HashSet;
20import java.util.Set;
21
22/**
23 * A simple class to start and stop Forwarders running on some ports.
24 *
25 * It uses a singleton pattern and is thread safe.
26 */
27public class ForwarderManager {
28    /**
29     * The IP address of the server serving the tests.
30     */
31    private static final String HOST_IP = "127.0.0.1";
32
33    /**
34     * We use these ports because other webkit platforms do. They are set up in
35     * external/webkit/LayoutTests/http/conf/apache2-debian-httpd.conf
36     */
37    public static final int HTTP_PORT = 8080;
38    public static final int HTTPS_PORT = 8443;
39
40    private static ForwarderManager forwarderManager;
41
42    private Set<Forwarder> mServers;
43
44    private ForwarderManager() {
45        mServers = new HashSet<Forwarder>(2);
46        mServers.add(new Forwarder(HTTP_PORT, HOST_IP));
47        mServers.add(new Forwarder(HTTPS_PORT, HOST_IP));
48    }
49
50    public static synchronized ForwarderManager getForwarderManager() {
51        if (forwarderManager == null) {
52            forwarderManager = new ForwarderManager();
53        }
54        return forwarderManager;
55    }
56
57    @Override
58    public Object clone() throws CloneNotSupportedException {
59        throw new CloneNotSupportedException();
60    }
61
62    public synchronized void start() {
63        for (Forwarder server : mServers) {
64            server.start();
65        }
66    }
67
68    public synchronized void stop() {
69        for (Forwarder server : mServers) {
70            server.finish();
71        }
72    }
73}