KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > net > ProxyManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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
18 /* $Id: ProxyManager.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.net;
21
22 import java.io.File JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Vector JavaDoc;
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 JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31
32
33 /**
34  * The <code>ProxyManager</code> Class is used to set or unset the java systems proxy settings
35  * based on the hostname of the host that want to be reached.
36  */

37 public class ProxyManager {
38     static Category log = Category.getInstance(ProxyManager.class);
39     Vector JavaDoc proxies = null;
40
41     /**
42      * Creating an instance of ProxyManager without argurments reads the configuration from the
43      * default configuration file ($XPS_HOME/xml/xps/proxyconf.xml)
44      */

45     public ProxyManager() {
46         log.debug("" + new Configuration().configurationPath);
47         proxies = readConfig(new Configuration().configurationPath);
48     }
49
50     /**
51      * The <code>ProxyManager</code> is created using the customized <code>conffile</code>
52      *
53      * @param conffile configuration file to use
54      */

55     public ProxyManager(String JavaDoc conffile) {
56         proxies = readConfig(conffile);
57     }
58
59     /**
60      * DOCUMENT ME!
61      *
62      * @param args DOCUMENT ME!
63      */

64     public static void main(String JavaDoc[] 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     /**
88      * Check if one of the configured proxies is appropriate for this host and setup the system
89      * configuration accordingly.
90      *
91      * @param host name of the host the connection should be initiated to
92      *
93      * @return DOCUMENT ME!
94      */

95     public boolean set(String JavaDoc host) {
96         Properties JavaDoc 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     /**
120      * Read proxy configuration
121      *
122      * @param fname Filename of proxy configuration
123      *
124      * @return proxies
125      */

126     public Vector JavaDoc readConfig(String JavaDoc fname) {
127         Document JavaDoc document = null;
128         File JavaDoc configFile = null;
129
130         try {
131         configFile = new File JavaDoc(new java.net.URI JavaDoc(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 JavaDoc e) {
139             log.error(e);
140             return null;
141         }
142
143
144         Vector JavaDoc 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 JavaDoc e) {
151             log.error(e);
152             return null;
153         }
154
155         Vector JavaDoc proxies = new Vector JavaDoc();
156         for (int i = 0; i < proxyElements.size(); i++) {
157             ProxyConf proxy = new ProxyConf((Element JavaDoc) proxyElements.elementAt(i));
158
159             proxies.addElement(proxy);
160         }
161
162         return proxies;
163     }
164 }
165
Popular Tags