ForwarderManager.java revision ef9a2175ee29b9a9c84ddd0a02d1ec80ab095222
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 android.util.Log; 22 23import java.util.HashSet; 24import java.util.Set; 25 26/** 27 * A simple class to start and stop Forwarders running on some ports. 28 * 29 * It uses a singleton pattern and is thread safe. 30 */ 31public class ForwarderManager { 32 private static final String LOG_TAG = "ForwarderManager"; 33 34 /** 35 * The IP address of the server serving the tests. 36 */ 37 private static final String HOST_IP = "127.0.0.1"; 38 39 /** 40 * We use these ports because other webkit platforms do. They are set up in 41 * external/webkit/LayoutTests/http/conf/apache2-debian-httpd.conf 42 */ 43 public static final int HTTP_PORT = 8080; 44 public static final int HTTPS_PORT = 8443; 45 46 public static final String HOST = "localhost"; 47 48 private static ForwarderManager forwarderManager; 49 50 private Set<Forwarder> mForwarders; 51 private boolean mIsStarted; 52 53 private ForwarderManager() { 54 mForwarders = new HashSet<Forwarder>(2); 55 mForwarders.add(new Forwarder(HTTP_PORT, HOST_IP)); 56 mForwarders.add(new Forwarder(HTTPS_PORT, HOST_IP)); 57 } 58 59 /** 60 * Returns the main part of the URL with the trailing slash 61 * 62 * @param isHttps 63 * @return 64 */ 65 public static final String getHostSchemePort(boolean isHttps) { 66 int port; 67 String protocol; 68 if (isHttps) { 69 protocol = "https"; 70 port = HTTPS_PORT; 71 } else { 72 protocol = "http"; 73 port = HTTP_PORT; 74 } 75 76 URL url = null; 77 try { 78 url = new URL(protocol, HOST, port, "/"); 79 } catch (MalformedURLException e) { 80 assert false : "isHttps=" + isHttps; 81 } 82 83 return url.toString(); 84 } 85 86 public static synchronized ForwarderManager getForwarderManager() { 87 if (forwarderManager == null) { 88 forwarderManager = new ForwarderManager(); 89 } 90 return forwarderManager; 91 } 92 93 @Override 94 public Object clone() throws CloneNotSupportedException { 95 throw new CloneNotSupportedException(); 96 } 97 98 public synchronized void start() { 99 if (mIsStarted) { 100 Log.w(LOG_TAG, "start(): ForwarderManager already running! NOOP."); 101 return; 102 } 103 104 for (Forwarder forwarder : mForwarders) { 105 forwarder.start(); 106 } 107 108 mIsStarted = true; 109 Log.i(LOG_TAG, "ForwarderManager started."); 110 } 111 112 public synchronized void stop() { 113 if (!mIsStarted) { 114 Log.w(LOG_TAG, "stop(): ForwarderManager already stopped! NOOP."); 115 return; 116 } 117 118 for (Forwarder forwarder : mForwarders) { 119 forwarder.finish(); 120 } 121 122 mIsStarted = false; 123 Log.i(LOG_TAG, "ForwarderManager stopped."); 124 } 125}