Source Code Cross Referenced for NbProxySelector.java in  » IDE-Netbeans » library » org » netbeans » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » library » org.netbeans.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.core;
043:
044:        import java.io.IOException;
045:        import java.lang.reflect.Method;
046:        import java.net.InetSocketAddress;
047:        import java.net.Proxy;
048:        import java.net.ProxySelector;
049:        import java.net.SocketAddress;
050:        import java.net.URI;
051:        import java.util.ArrayList;
052:        import java.util.Collections;
053:        import java.util.List;
054:        import java.util.Locale;
055:        import java.util.StringTokenizer;
056:        import java.util.logging.Level;
057:        import java.util.logging.Logger;
058:        import java.util.prefs.PreferenceChangeEvent;
059:        import java.util.prefs.PreferenceChangeListener;
060:
061:        /**
062:         *
063:         * @author Jiri Rechtacek
064:         */
065:        public final class NbProxySelector extends ProxySelector {
066:
067:            private ProxySelector original = null;
068:            private Logger log = Logger.getLogger(NbProxySelector.class
069:                    .getName());
070:            private Object useSystemProxies;
071:
072:            /** Creates a new instance of NbProxySelector */
073:            public NbProxySelector() {
074:                original = ProxySelector.getDefault();
075:                log.fine("Override the original ProxySelector: " + original);
076:                log.fine("java.net.useSystemProxies has been set to "
077:                        + useSystemProxies());
078:                ProxySettings
079:                        .addPreferenceChangeListener(new ProxySettingsListener());
080:                copySettingsToSystem();
081:            }
082:
083:            public List<Proxy> select(URI uri) {
084:                List<Proxy> res = new ArrayList<Proxy>();
085:                int proxyType = ProxySettings.getProxyType();
086:                if (ProxySettings.DIRECT_CONNECTION == proxyType) {
087:                    res = Collections.singletonList(Proxy.NO_PROXY);
088:                } else if (ProxySettings.AUTO_DETECT_PROXY == proxyType) {
089:                    if (useSystemProxies()) {
090:                        res = original.select(uri);
091:                    } else {
092:                        String protocol = uri.getScheme();
093:                        assert protocol != null : "Invalid scheme of uri "
094:                                + uri + ". Scheme cannot be null!";
095:                        // handling nonProxyHosts first
096:                        if (dontUseProxy(ProxySettings.SystemProxySettings
097:                                .getNonProxyHosts(), uri.getHost())) {
098:                            res.add(Proxy.NO_PROXY);
099:                        }
100:                        if (protocol.toLowerCase(Locale.US).startsWith("http")) {
101:                            String ports = ProxySettings.SystemProxySettings
102:                                    .getHttpPort();
103:                            if (ports != null
104:                                    && ports.length() > 0
105:                                    && ProxySettings.SystemProxySettings
106:                                            .getHttpHost().length() > 0) {
107:                                int porti = Integer.parseInt(ports);
108:                                Proxy p = new Proxy(
109:                                        Proxy.Type.HTTP,
110:                                        new InetSocketAddress(
111:                                                ProxySettings.SystemProxySettings
112:                                                        .getHttpHost(), porti));
113:                                res.add(p);
114:                            }
115:                        } else { // supposed SOCKS
116:                            String ports = ProxySettings.SystemProxySettings
117:                                    .getSocksPort();
118:                            String hosts = ProxySettings.SystemProxySettings
119:                                    .getSocksHost();
120:                            if (ports != null && ports.length() > 0
121:                                    && hosts.length() > 0) {
122:                                int porti = Integer.parseInt(ports);
123:                                Proxy p = new Proxy(Proxy.Type.SOCKS,
124:                                        new InetSocketAddress(hosts, porti));
125:                                res.add(p);
126:                            }
127:                        }
128:                        res.addAll(original.select(uri));
129:                    }
130:                } else if (ProxySettings.MANUAL_SET_PROXY == proxyType) {
131:                    String protocol = uri.getScheme();
132:                    assert protocol != null : "Invalid scheme of uri " + uri
133:                            + ". Scheme cannot be null!";
134:
135:                    // handling nonProxyHosts first
136:                    if (dontUseProxy(ProxySettings.getNonProxyHosts(), uri
137:                            .getHost())) {
138:                        res.add(Proxy.NO_PROXY);
139:                    }
140:                    if (protocol.toLowerCase(Locale.US).startsWith("http")) {
141:                        String hosts = ProxySettings.getHttpHost();
142:                        String ports = ProxySettings.getHttpPort();
143:                        if (ports != null && ports.length() > 0
144:                                && hosts.length() > 0) {
145:                            int porti = Integer.parseInt(ports);
146:                            Proxy p = new Proxy(Proxy.Type.HTTP,
147:                                    new InetSocketAddress(hosts, porti));
148:                            res.add(p);
149:                        } else {
150:                            log.info("Incomplete HTTP Proxy [" + hosts + "/"
151:                                    + ports + "] found in ProxySelector[Type: "
152:                                    + ProxySettings.getProxyType()
153:                                    + "] for uri " + uri + ". ");
154:                            log
155:                                    .finest("Fallback to the default ProxySelector which returns "
156:                                            + original.select(uri));
157:                            res.addAll(original.select(uri));
158:                        }
159:                    } else { // supposed SOCKS
160:                        String ports = ProxySettings.getSocksPort();
161:                        String hosts = ProxySettings.getSocksHost();
162:                        if (ports != null && ports.length() > 0
163:                                && hosts.length() > 0) {
164:                            int porti = Integer.parseInt(ports);
165:                            Proxy p = new Proxy(Proxy.Type.SOCKS,
166:                                    new InetSocketAddress(hosts, porti));
167:                            res.add(p);
168:                        } else {
169:                            log.info("Incomplete SOCKS Server [" + hosts + "/"
170:                                    + ports + "] found in ProxySelector[Type: "
171:                                    + ProxySettings.getProxyType()
172:                                    + "] for uri " + uri + ". ");
173:                            log
174:                                    .finest("Fallback to the default ProxySelector which returns "
175:                                            + original.select(uri));
176:                            res.addAll(original.select(uri));
177:                        }
178:                    }
179:                    res.add(Proxy.NO_PROXY);
180:                } else {
181:                    assert false : "Invalid proxy type: "
182:                            + ProxySettings.getProxyType();
183:                }
184:                log.finest("NbProxySelector[Type: "
185:                        + ProxySettings.getProxyType()
186:                        + ", Use HTTP for all protocols: "
187:                        + ProxySettings.useProxyAllProtocols() + "] returns "
188:                        + res + " for URI " + uri);
189:                return res;
190:            }
191:
192:            public void connectFailed(URI arg0, SocketAddress arg1,
193:                    IOException arg2) {
194:                log.log(Level.INFO, "connectionFailed(" + arg0 + ", " + arg1
195:                        + ")", arg2);
196:            }
197:
198:            // several modules listenes on these properties and propagates it futher
199:            private class ProxySettingsListener implements 
200:                    PreferenceChangeListener {
201:                public void preferenceChange(PreferenceChangeEvent evt) {
202:                    if (evt.getKey().startsWith("proxy")
203:                            || evt.getKey().startsWith("useProxy")) {
204:                        copySettingsToSystem();
205:                    }
206:                }
207:            }
208:
209:            private void copySettingsToSystem() {
210:                String host = null, port = null, nonProxyHosts = null;
211:                String sHost = null, sPort = null;
212:                String httpsHost = null, httpsPort = null;
213:                int proxyType = ProxySettings.getProxyType();
214:                if (ProxySettings.DIRECT_CONNECTION == proxyType) {
215:                    host = null;
216:                    port = null;
217:                    httpsHost = null;
218:                    httpsPort = null;
219:                    nonProxyHosts = null;
220:                    sHost = null;
221:                    sPort = null;
222:                } else if (ProxySettings.AUTO_DETECT_PROXY == proxyType) {
223:                    host = ProxySettings.SystemProxySettings.getHttpHost();
224:                    port = ProxySettings.SystemProxySettings.getHttpPort();
225:                    httpsHost = ProxySettings.SystemProxySettings
226:                            .getHttpsHost();
227:                    httpsPort = ProxySettings.SystemProxySettings
228:                            .getHttpsPort();
229:                    nonProxyHosts = ProxySettings.SystemProxySettings
230:                            .getNonProxyHosts();
231:                    sHost = ProxySettings.SystemProxySettings.getSocksHost();
232:                    sPort = ProxySettings.SystemProxySettings.getSocksPort();
233:                } else if (ProxySettings.MANUAL_SET_PROXY == proxyType) {
234:                    host = ProxySettings.getHttpHost();
235:                    port = ProxySettings.getHttpPort();
236:                    httpsHost = ProxySettings.getHttpsHost();
237:                    httpsPort = ProxySettings.getHttpsPort();
238:                    nonProxyHosts = ProxySettings.getNonProxyHosts();
239:                    sHost = ProxySettings.getSocksHost();
240:                    sPort = ProxySettings.getSocksPort();
241:                } else {
242:                    assert false : "Invalid proxy type: " + proxyType;
243:                }
244:                setOrClearProperty("http.proxyHost", host, false);
245:                setOrClearProperty("http.proxyPort", port, true);
246:                setOrClearProperty("http.nonProxyHosts", nonProxyHosts, false);
247:                setOrClearProperty("https.proxyHost", httpsHost, false);
248:                setOrClearProperty("https.proxyPort", httpsPort, true);
249:                setOrClearProperty("https.nonProxyHosts", nonProxyHosts, false);
250:                setOrClearProperty("socksProxyHost", sHost, false);
251:                setOrClearProperty("socksProxyPort", sPort, true);
252:                log.fine("Set System's http.proxyHost/Port/NonProxyHost to "
253:                        + host + "/" + port + "/" + nonProxyHosts);
254:                log.fine("Set System's https.proxyHost/Port to " + httpsHost
255:                        + "/" + httpsPort);
256:                log.fine("Set System's socksProxyHost/Port to " + sHost + "/"
257:                        + sPort);
258:            }
259:
260:            private void setOrClearProperty(String key, String value,
261:                    boolean isInteger) {
262:                assert key != null;
263:                if (value == null || value.length() == 0) {
264:                    System.clearProperty(key);
265:                } else {
266:                    if (isInteger) {
267:                        try {
268:                            Integer.parseInt(value);
269:                        } catch (NumberFormatException nfe) {
270:                            log.log(Level.INFO, nfe.getMessage(), nfe);
271:                        }
272:                    }
273:                    System.setProperty(key, value);
274:                }
275:            }
276:
277:            private boolean dontUseProxy(String nonProxyHosts, String host) {
278:                if (host == null)
279:                    return false;
280:
281:                boolean dontUseProxy = false;
282:                StringTokenizer st = new StringTokenizer(nonProxyHosts, "|",
283:                        false);
284:                while (st.hasMoreTokens() && !dontUseProxy) {
285:                    String token = st.nextToken();
286:                    int star = token.indexOf("*");
287:                    if (star == -1) {
288:                        dontUseProxy = token.equals(host);
289:                        if (dontUseProxy) {
290:                            log.finest("NbProxySelector[Type: "
291:                                    + ProxySettings.getProxyType() + "]. Host "
292:                                    + host + " found in nonProxyHosts: "
293:                                    + nonProxyHosts);
294:                        }
295:                    } else {
296:                        String start = token.substring(0, star - 1 < 0 ? 0
297:                                : star - 1);
298:                        String end = token
299:                                .substring(star + 1 > token.length() ? token
300:                                        .length() : star + 1);
301:                        dontUseProxy = host.startsWith(start)
302:                                && host.endsWith(end);
303:                        if (dontUseProxy) {
304:                            log.finest("NbProxySelector[Type: "
305:                                    + ProxySettings.getProxyType() + "]. Host "
306:                                    + host + " found in nonProxyHosts: "
307:                                    + nonProxyHosts);
308:                        }
309:                    }
310:                }
311:                return dontUseProxy;
312:            }
313:
314:            // NetProperties is JDK vendor specific, access only by reflection
315:            private boolean useSystemProxies() {
316:                if (useSystemProxies == null) {
317:                    try {
318:                        Class clazz = Class.forName("sun.net.NetProperties");
319:                        Method getBoolean = clazz.getMethod("getBoolean",
320:                                String.class);
321:                        useSystemProxies = getBoolean.invoke(null,
322:                                "java.net.useSystemProxies");
323:                    } catch (Exception x) {
324:                        log.log(Level.FINEST,
325:                                "Cannot get value of java.net.useSystemProxies bacause "
326:                                        + x.getMessage(), x);
327:                    }
328:                }
329:                return useSystemProxies != null
330:                        && "true".equalsIgnoreCase(useSystemProxies.toString());
331:            }
332:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.