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.net.InetSocketAddress;
040: import java.net.Proxy;
041: import java.net.Proxy.Type;
042: import java.util.HashMap;
043: import java.util.Map;
044: import org.netbeans.installer.utils.ResourceUtils;
045: import org.netbeans.installer.utils.StringUtils;
046: import org.netbeans.installer.utils.xml.DomExternalizable;
047: import org.netbeans.installer.utils.xml.DomUtil;
048: import org.netbeans.installer.utils.xml.visitors.DomVisitor;
049: import org.netbeans.installer.utils.xml.visitors.RecursiveDomVisitor;
050: import org.w3c.dom.Document;
051: import org.w3c.dom.Element;
052:
053: /**
054: *
055: * @author Danila_Dugurov
056: */
057: public class MyProxy implements DomExternalizable {
058: /////////////////////////////////////////////////////////////////////////////////
059: // Static
060: private static final Map<Type, MyProxyType> type2Type = new HashMap<Type, MyProxyType>();
061:
062: static {
063: type2Type.put(Type.DIRECT, MyProxyType.DIRECT);
064: type2Type.put(Type.HTTP, MyProxyType.HTTP);
065: type2Type.put(Type.SOCKS, MyProxyType.SOCKS);
066: }
067:
068: /////////////////////////////////////////////////////////////////////////////////
069: // Instance
070: int port = -1;
071: String host = StringUtils.EMPTY_STRING;
072: MyProxyType type = MyProxyType.DIRECT;
073:
074: public MyProxy() {
075: }
076:
077: public MyProxy(Proxy proxy) {
078: type = type2Type.get(proxy.type());
079:
080: final InetSocketAddress address = (InetSocketAddress) proxy
081: .address();
082: if (address != null) {
083: host = address.getHostName();
084: port = address.getPort();
085: }
086: }
087:
088: public MyProxy(Proxy proxy, MyProxyType type)
089: throws IllegalArgumentException {
090: this (proxy);
091:
092: if (!proxy.type().equals(type.getType())) {
093: throw new IllegalArgumentException(ResourceUtils.getString(
094: MyProxy.class, ERROR_TYPES_CONFLICT_KEY, proxy
095: .type(), type.getType()));
096: }
097: this .type = type;
098: }
099:
100: public Proxy getProxy() {
101: return type == MyProxyType.DIRECT ? Proxy.NO_PROXY : new Proxy(
102: type.getType(), new InetSocketAddress(host, port));
103: }
104:
105: public void readXML(Element element) {
106: final DomVisitor visitor = new RecursiveDomVisitor() {
107: @Override
108: public void visit(Element element) {
109: if (PROXY_TYPE_TAG.equals(element.getNodeName())) {
110: type = MyProxyType.valueOf(element.getTextContent()
111: .trim().toUpperCase());
112: } else if (PROXY_HOST_TAG.equals(element.getNodeName())) {
113: host = element.getTextContent().trim();
114: } else if (PROXY_PORT_TAG.equals(element.getNodeName())) {
115: port = Integer.parseInt(element.getTextContent()
116: .trim());
117: } else {
118: super .visit(element);
119: }
120: }
121: };
122:
123: visitor.visit(element);
124: }
125:
126: public Element writeXML(Document document) {
127: final Element root = document.createElement(PROXY_TAG);
128: DomUtil.addElement(root, PROXY_TYPE_TAG, type.toString());
129: DomUtil.addElement(root, PROXY_HOST_TAG, host);
130: DomUtil.addElement(root, PROXY_PORT_TAG, String.valueOf(port));
131: return root;
132: }
133:
134: @Override
135: public boolean equals(Object proxy) {
136: if (this == proxy) {
137: return true;
138: }
139:
140: if (proxy == null) {
141: return false;
142: }
143:
144: if (proxy instanceof MyProxy) {
145: final MyProxy prox = (MyProxy) proxy;
146: if (port == prox.port && type == prox.type
147: && host.equals(prox.host)) {
148: return true;
149: }
150: }
151: return false;
152: }
153:
154: @Override
155: public int hashCode() {
156: int result;
157: result = (host != null ? host.hashCode() : 0);
158: result = 29 * result + (type != null ? type.hashCode() : 0);
159: result = 29 * result + port;
160: return result;
161: }
162:
163: /////////////////////////////////////////////////////////////////////////////////
164: // Constants
165: public static final String ERROR_TYPES_CONFLICT_KEY = "MP.error.types.conflict"; // NOI18N
166:
167: public static final String PROXY_TAG = "proxy"; // NOI18N
168:
169: public static final String PROXY_TYPE_TAG = "proxy-type"; // NOI18N
170:
171: public static final String PROXY_HOST_TAG = "proxy-host"; // NOI18N
172:
173: public static final String PROXY_PORT_TAG = "proxy-port"; // NOI18N
174:
175: public static final String SELECTOR_PROXIES_TAG = "selector-proxies"; // NOI18N
176: }
|