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 17 package com.android.dumprendertree2.forwarder; 18 19 import java.net.MalformedURLException; 20 import java.net.URL; 21 import android.util.Log; 22 23 import java.util.HashSet; 24 import 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 */ 31 public 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 = 8000; 44 public static final int HTTPS_PORT = 8443; 45 46 private static ForwarderManager forwarderManager; 47 48 private Set<Forwarder> mForwarders; 49 private boolean mIsStarted; 50 51 private ForwarderManager() { 52 mForwarders = new HashSet<Forwarder>(2); 53 mForwarders.add(new Forwarder(HTTP_PORT, HOST_IP)); 54 mForwarders.add(new Forwarder(HTTPS_PORT, HOST_IP)); 55 } 56 57 /** 58 * Returns the main part of the URL with the trailing slash 59 * 60 * @param isHttps 61 * @return 62 */ 63 public static final String getHostSchemePort(boolean isHttps) { 64 int port; 65 String protocol; 66 if (isHttps) { 67 protocol = "https"; 68 port = HTTPS_PORT; 69 } else { 70 protocol = "http"; 71 port = HTTP_PORT; 72 } 73 74 URL url = null; 75 try { 76 url = new URL(protocol, HOST_IP, port, "/"); 77 } catch (MalformedURLException e) { 78 assert false : "isHttps=" + isHttps; 79 } 80 81 return url.toString(); 82 } 83 84 public static synchronized ForwarderManager getForwarderManager() { 85 if (forwarderManager == null) { 86 forwarderManager = new ForwarderManager(); 87 } 88 return forwarderManager; 89 } 90 91 @Override 92 public Object clone() throws CloneNotSupportedException { 93 throw new CloneNotSupportedException(); 94 } 95 96 public synchronized void start() { 97 if (mIsStarted) { 98 Log.w(LOG_TAG, "start(): ForwarderManager already running! NOOP."); 99 return; 100 } 101 102 for (Forwarder forwarder : mForwarders) { 103 forwarder.start(); 104 } 105 106 mIsStarted = true; 107 Log.i(LOG_TAG, "ForwarderManager started."); 108 } 109 110 public synchronized void stop() { 111 if (!mIsStarted) { 112 Log.w(LOG_TAG, "stop(): ForwarderManager already stopped! NOOP."); 113 return; 114 } 115 116 for (Forwarder forwarder : mForwarders) { 117 forwarder.finish(); 118 } 119 120 mIsStarted = false; 121 Log.i(LOG_TAG, "ForwarderManager stopped."); 122 } 123 } 124