001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.downloader.connector;
038:
039: import java.io.IOException;
040: import java.net.Proxy;
041: import java.net.ProxySelector;
042: import java.net.SocketAddress;
043: import java.net.URI;
044: import java.util.Collections;
045: import java.util.HashMap;
046: import java.util.HashSet;
047: import java.util.List;
048: import java.util.Map;
049: import java.util.Set;
050: import org.netbeans.installer.utils.FileProxy;
051: import org.netbeans.installer.utils.ResourceUtils;
052: import org.netbeans.installer.utils.xml.DomExternalizable;
053: import org.netbeans.installer.utils.xml.DomUtil;
054: import org.netbeans.installer.utils.xml.visitors.DomVisitor;
055: import org.netbeans.installer.utils.xml.visitors.RecursiveDomVisitor;
056: import org.w3c.dom.Document;
057: import org.w3c.dom.Element;
058:
059: /**
060: *
061: * @author Danila_Dugurov
062: */
063: public class MyProxySelector extends ProxySelector implements
064: DomExternalizable {
065: /////////////////////////////////////////////////////////////////////////////////
066: // Instance
067: private final Map<MyProxyType, MyProxy> proxies = new HashMap<MyProxyType, MyProxy>();
068:
069: private transient Set<String> byPassSet = new HashSet<String>();
070:
071: public void add(MyProxy proxy) {
072: proxies.put(proxy.type, proxy);
073: }
074:
075: public void remove(MyProxyType type) {
076: proxies.remove(type);
077: }
078:
079: public MyProxy getForType(MyProxyType type) {
080: return proxies.get(type);
081: }
082:
083: public void addByPassHost(String host) {
084: byPassSet.add(host);
085: }
086:
087: public void clearByPassList() {
088: byPassSet.clear();
089: }
090:
091: public String[] getByPass() {
092: return byPassSet.toArray(new String[0]);
093: }
094:
095: public List<Proxy> select(URI uri) {
096: if (uri == null) {
097: throw new IllegalArgumentException(ResourceUtils
098: .getString(MyProxySelector.class,
099: ERROR_URI_CANNOT_BE_NULL_KEY));
100: }
101: Proxy proxy = Proxy.NO_PROXY;
102: if (byPassSet.contains(uri.getHost())) {
103: return Collections.singletonList(proxy);
104: }
105: if (HTTP_SCHEME.equalsIgnoreCase(uri.getScheme())
106: || HTTPS_SCHEME.equalsIgnoreCase(uri.getScheme())) {
107: if (proxies.containsKey(MyProxyType.HTTP)) {
108: proxy = proxies.get(MyProxyType.HTTP).getProxy();
109: }
110: } else if (FTP_SCHEME.equalsIgnoreCase(uri.getScheme())) {
111: if (proxies.containsKey(MyProxyType.FTP)) {
112: proxy = proxies.get(MyProxyType.FTP).getProxy();
113: }
114: } else {
115: if (proxies.containsKey(MyProxyType.SOCKS)) {
116: proxy = proxies.get(MyProxyType.SOCKS).getProxy();
117: }
118: }
119: return Collections.singletonList(proxy);
120: }
121:
122: public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
123: //TODO: now very silly selector no any rerang!
124: }
125:
126: public void readXML(Element element) {
127: final DomVisitor visitor = new RecursiveDomVisitor() {
128: @Override
129: public void visit(Element element) {
130: if (MyProxy.PROXY_TAG.equals(element.getNodeName())) {
131: final MyProxy proxy = new MyProxy();
132: proxy.readXML(element);
133: add(proxy);
134: } else {
135: super .visit(element);
136: }
137: }
138: };
139: visitor.visit(element);
140: }
141:
142: public Element writeXML(Document document) {
143: final Element root = document
144: .createElement(MyProxy.SELECTOR_PROXIES_TAG);
145: for (MyProxy proxy : proxies.values()) {
146: DomUtil.addChild(root, proxy);
147: }
148: return root;
149: }
150:
151: /////////////////////////////////////////////////////////////////////////////////
152: // Constants
153: public static final String ERROR_URI_CANNOT_BE_NULL_KEY = "MPS.error.uri.cannot.be.null"; // NOI18N
154:
155: public static final String HTTP_SCHEME = "http"; // NOI18N
156:
157: public static final String HTTPS_SCHEME = "https"; // NOI18N
158:
159: public static final String FTP_SCHEME = "ftp"; // NOI18N
160: }
|