1 17 18 19 20 package org.apache.lenya.net; 21 22 import java.io.File ; 23 import java.util.Properties ; 24 import java.util.Vector ; 25 26 import org.apache.lenya.xml.DocumentHelper; 27 import org.apache.lenya.xml.XPointerFactory; 28 import org.apache.log4j.Category; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 32 33 37 public class ProxyManager { 38 static Category log = Category.getInstance(ProxyManager.class); 39 Vector proxies = null; 40 41 45 public ProxyManager() { 46 log.debug("" + new Configuration().configurationPath); 47 proxies = readConfig(new Configuration().configurationPath); 48 } 49 50 55 public ProxyManager(String conffile) { 56 proxies = readConfig(conffile); 57 } 58 59 64 public static void main(String [] args) { 65 if ((args.length > 2) || (args.length < 1)) { 66 System.err.println( 67 "Usage: java org.apache.lenya.net.ProxyManager host [configfile.xml]"); 68 69 return; 70 } 71 72 ProxyManager pm = null; 73 74 if (args.length > 1) { 75 pm = new ProxyManager(args[1]); 76 } else { 77 pm = new ProxyManager(); 78 } 79 80 if (pm.set(args[0])) { 81 System.out.println("Proxy set: "); 82 } else { 83 System.out.println("No proxy set."); 84 } 85 } 86 87 95 public boolean set(String host) { 96 Properties sp = System.getProperties(); 97 98 for (int i = 0; i < proxies.size(); i++) { 99 ProxyConf proxy = (ProxyConf) proxies.elementAt(i); 100 101 if (proxy.check(host)) { 102 sp.put("proxySet", "true"); 103 sp.put("proxyHost", proxy.getHostName()); 104 sp.put("proxyPort", proxy.getHostPort()); 105 System.setProperties(sp); 106 107 return true; 108 } 109 } 110 111 sp.remove("proxySet"); 112 sp.put("proxyHost", ""); 113 sp.put("proxyPort", ""); 114 System.setProperties(sp); 115 116 return false; 117 } 118 119 126 public Vector readConfig(String fname) { 127 Document document = null; 128 File configFile = null; 129 130 try { 131 configFile = new File (new java.net.URI (ProxyManager.class.getClassLoader().getResource(fname).toString())); 132 if (configFile.exists()) { 133 document = DocumentHelper.readDocument(configFile); 134 } else { 135 log.warn("No such file or directory: " + configFile.getAbsolutePath()); 136 return null; 137 } 138 } catch (Exception e) { 139 log.error(e); 140 return null; 141 } 142 143 144 Vector proxyElements = null; 145 XPointerFactory xpf = new XPointerFactory(); 146 147 try { 148 proxyElements = xpf.select(document.getDocumentElement(), "xpointer(/conf/Proxy)"); 149 if (proxyElements.size() == 0) log.info("No proxy defined (" + configFile + ")"); 150 } catch (Exception e) { 151 log.error(e); 152 return null; 153 } 154 155 Vector proxies = new Vector (); 156 for (int i = 0; i < proxyElements.size(); i++) { 157 ProxyConf proxy = new ProxyConf((Element ) proxyElements.elementAt(i)); 158 159 proxies.addElement(proxy); 160 } 161 162 return proxies; 163 } 164 } 165 | Popular Tags |