OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.android.configurators; |
| 19 |
| 20 import java.lang.reflect.Constructor; |
| 21 import java.net.InetAddress; |
| 22 import java.util.ArrayList; |
| 23 import java.util.HashMap; |
| 24 |
| 25 import org.adblockplus.android.Utils; |
| 26 |
| 27 import android.content.Context; |
| 28 import android.util.Log; |
| 29 |
| 30 /** |
| 31 * Common proxy configurator methods. |
| 32 * |
| 33 * @author rjeschke |
| 34 */ |
| 35 public final class ProxyConfigurators |
| 36 { |
| 37 private static final String TAG = Utils.getTag(ProxyConfigurators.class); |
| 38 private static final ArrayList<Class<?>> CONFIGURATORS = new ArrayList<Class<?
>>(); |
| 39 private static final HashMap<ProxyRegistrationType, Class<?>> CONFIGURATOR_MAP
= new HashMap<ProxyRegistrationType, Class<?>>(); |
| 40 private static final HashMap<Class<?>, ProxyRegistrationType> CONFIGURATOR_INV
_MAP = new HashMap<Class<?>, ProxyRegistrationType>(); |
| 41 |
| 42 static |
| 43 { |
| 44 /* |
| 45 * Add new proxy-configurators here. |
| 46 * |
| 47 * Note: order of adding is important (as later added configurators act as f
allbacks for earlier ones). |
| 48 */ |
| 49 add(CyanogenProxyConfigurator.class); |
| 50 add(IptablesProxyConfigurator.class); |
| 51 add(ReflectionProxyConfigurator.class); |
| 52 add(ManualProxyConfigurator.class); |
| 53 } |
| 54 |
| 55 private ProxyConfigurators() |
| 56 { |
| 57 // no instantiation |
| 58 } |
| 59 |
| 60 private static void add(Class<?> clazz) |
| 61 { |
| 62 try |
| 63 { |
| 64 final Constructor<?> ctor = clazz.getConstructor(Context.class); |
| 65 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance((Context
) null); |
| 66 final ProxyRegistrationType type = pc.getType(); |
| 67 |
| 68 if (!CONFIGURATOR_MAP.containsKey(type)) |
| 69 { |
| 70 CONFIGURATORS.add(clazz); |
| 71 CONFIGURATOR_MAP.put(type, clazz); |
| 72 CONFIGURATOR_INV_MAP.put(clazz, type); |
| 73 } |
| 74 else |
| 75 { |
| 76 // We fail hard here. |
| 77 throw new IllegalArgumentException("Duplicate proxy configurator (" + cl
azz + ") for type: " + type); |
| 78 } |
| 79 } |
| 80 catch (Exception e) |
| 81 { |
| 82 // We fail hard here. |
| 83 throw new IllegalStateException("Failed to register proxy configurator ("
+ clazz + ")", e); |
| 84 } |
| 85 } |
| 86 |
| 87 private static ProxyConfigurator initialize(Context context, ProxyConfigurator
from) |
| 88 { |
| 89 int i = 0; |
| 90 |
| 91 if (from != null) |
| 92 { |
| 93 // Scan through configurators to find last used one |
| 94 while (i < CONFIGURATORS.size() && !CONFIGURATORS.get(i).equals(from.getCl
ass())) |
| 95 { |
| 96 i++; |
| 97 } |
| 98 // One after last-used |
| 99 i++; |
| 100 } |
| 101 |
| 102 // Loop through configurators until one succeeds to initialize |
| 103 while (i < CONFIGURATORS.size()) |
| 104 { |
| 105 try |
| 106 { |
| 107 final Class<?> clazz = CONFIGURATORS.get(i); |
| 108 final Constructor<?> ctor = clazz.getConstructor(Context.class); |
| 109 final ProxyConfigurator pc = (ProxyConfigurator) ctor.newInstance(contex
t); |
| 110 if (pc.initialize()) |
| 111 { |
| 112 return pc; |
| 113 } |
| 114 } |
| 115 catch (Exception e) |
| 116 { |
| 117 Log.d(TAG, "Configurator exception", e); |
| 118 // We don't need to handle exceptions here, only success matters |
| 119 } |
| 120 |
| 121 i++; |
| 122 } |
| 123 |
| 124 return null; |
| 125 } |
| 126 |
| 127 public static ProxyConfigurator registerProxy(final Context context, final Ine
tAddress address, final int port) |
| 128 { |
| 129 ProxyConfigurator pc = null; |
| 130 |
| 131 do |
| 132 { |
| 133 pc = initialize(context, pc); |
| 134 |
| 135 if (pc != null) |
| 136 { |
| 137 if (pc.registerProxy(address, port)) |
| 138 { |
| 139 Log.d(TAG, "Using '" + pc.toString() + "'"); |
| 140 return pc; |
| 141 } |
| 142 |
| 143 pc.shutdown(); |
| 144 } |
| 145 } |
| 146 while (pc != null); |
| 147 |
| 148 return null; |
| 149 } |
| 150 } |
OLD | NEW |