Source Code Cross Referenced for Proxy.java in  » 6.0-JDK-Core » net » java » net » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
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
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » net » java.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.net;
027
028        /**
029         * This class represents a proxy setting, typically a type (http, socks) and
030         * a socket address. 
031         * A <code>Proxy</code> is an immutable object.
032         *
033         * @version 1.3, 08/09/03
034         * @see     java.net.ProxySelector
035         * @author Yingxian Wang
036         * @author Jean-Christophe Collet
037         * @since   1.5
038         */
039        public class Proxy {
040
041            /**
042             * Represents the proxy type.
043             *
044             * @since 1.5
045             */
046            public enum Type {
047                /**
048                 * Represents a direct connection, or the absence of a proxy.
049                 */
050                DIRECT,
051                /**
052                 * Represents proxy for high level protocols such as HTTP or FTP.
053                 */
054                HTTP,
055                /**
056                 * Represents a SOCKS (V4 or V5) proxy.
057                 */
058                SOCKS
059            };
060
061            private Type type;
062            private SocketAddress sa;
063
064            /**
065             * A proxy setting that represents a <code>DIRECT</code> connection,
066             * basically telling the protocol handler not to use any proxying.
067             * Used, for instance, to create sockets bypassing any other global
068             * proxy settings (like SOCKS):
069             * <P>
070             * <code>Socket s = new Socket(Proxy.NO_PROXY);</code><br>
071             * <P>
072             */
073            public final static Proxy NO_PROXY = new Proxy();
074
075            // Creates the proxy that represents a <code>DIRECT</code> connection.
076            private Proxy() {
077                type = type.DIRECT;
078                sa = null;
079            }
080
081            /**
082             * Creates an entry representing a PROXY connection.
083             * Certain combinations are illegal. For instance, for types Http, and
084             * Socks, a SocketAddress <b>must</b> be provided. 
085             * <P>
086             * Use the <code>Proxy.NO_PROXY</code> constant
087             * for representing a direct connection.
088             *
089             * @param type the <code>Type</code> of the proxy
090             * @param sa the <code>SocketAddress</code> for that proxy
091             * @throws IllegalArgumentException when the type and the address are
092             * incompatible
093             */
094            public Proxy(Type type, SocketAddress sa) {
095                if ((type == Type.DIRECT) || !(sa instanceof  InetSocketAddress))
096                    throw new IllegalArgumentException("type " + type
097                            + " is not compatible with address " + sa);
098                this .type = type;
099                this .sa = sa;
100            }
101
102            /**
103             * Returns the proxy type.
104             *
105             * @return a Type representing the proxy type 
106             */
107            public Type type() {
108                return type;
109            }
110
111            /**
112             * Returns the socket address of the proxy, or 
113             * <code>null</code> if its a direct connection.
114             *
115             * @return a <code>SocketAddress</code> representing the socket end
116             *         point of the proxy
117             */
118            public SocketAddress address() {
119                return sa;
120            }
121
122            /**
123             * Constructs a string representation of this Proxy.
124             * This String is constructed by calling toString() on its type
125             * and concatenating " @ " and the toString() result from its address
126             * if its type is not <code>DIRECT</code>.
127             *
128             * @return  a string representation of this object.
129             */
130            public String toString() {
131                if (type() == Type.DIRECT)
132                    return "DIRECT";
133                return type() + " @ " + address();
134            }
135
136            /**
137             * Compares this object against the specified object.
138             * The result is <code>true</code> if and only if the argument is
139             * not <code>null</code> and it represents the same proxy as
140             * this object.
141             * <p>
142             * Two instances of <code>Proxy</code> represent the same
143             * address if both the SocketAddresses and type are equal.
144             *
145             * @param   obj   the object to compare against.
146             * @return  <code>true</code> if the objects are the same;
147             *          <code>false</code> otherwise.
148             * @see java.net.InetSocketAddress#equals(java.lang.Object)
149             */
150            public final boolean equals(Object obj) {
151                if (obj == null || !(obj instanceof  Proxy))
152                    return false;
153                Proxy p = (Proxy) obj;
154                if (p.type() == type()) {
155                    if (address() == null) {
156                        return (p.address() == null);
157                    } else
158                        return address().equals(p.address());
159                }
160                return false;
161            }
162
163            /**
164             * Returns a hashcode for this Proxy.
165             *
166             * @return  a hash code value for this Proxy.
167             */
168            public final int hashCode() {
169                if (address() == null)
170                    return type().hashCode();
171                return type().hashCode() + address().hashCode();
172            }
173        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.