001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.2 $
022: */package org.apache.harmony.rmi.transport.proxy;
023:
024: import java.io.IOException;
025: import java.net.NoRouteToHostException;
026: import java.net.Socket;
027: import java.security.AccessController;
028:
029: import org.apache.harmony.rmi.common.GetBooleanPropAction;
030: import org.apache.harmony.rmi.common.GetLongPropAction;
031: import org.apache.harmony.rmi.common.GetStringPropAction;
032: import org.apache.harmony.rmi.common.RMIConstants;
033: import org.apache.harmony.rmi.common.RMILog;
034: import org.apache.harmony.rmi.internal.nls.Messages;
035:
036: /**
037: * Provides access to Java proxy system properties.
038: *
039: * Note: instances of this class and values returned by their methods
040: * should not be stored longer than is required to establish a particular
041: * connection. Instead, a new instance should be created and the methods
042: * of that instance called each time new connection is established.
043: *
044: * @author Vasily Zakharov
045: * @version $Revision: 1.1.2.2 $
046: */
047: public final class Proxy implements ProxyConstants {
048:
049: /**
050: * HTTP proxy host name.
051: */
052: private final String proxyHost;
053:
054: /**
055: * HTTP proxy port number.
056: */
057: private final int proxyPort;
058:
059: /**
060: * If proxy is set.
061: */
062: private final boolean proxySet;
063:
064: /**
065: * Should we enable direct (non-proxy) HTTP connections.
066: */
067: private final boolean enableDirect;
068:
069: /**
070: * Creates instance of this class.
071: *
072: * Note: instances of this class and values returned by their methods
073: * should not be stored longer than is required to establish a particular
074: * connection. Instead, a new instance should be created and the methods
075: * of that instance called each time new connection is established.
076: */
077: public Proxy() {
078: proxyHost = getProxyHost();
079:
080: if (proxyHost != null) {
081: proxySet = true;
082: proxyPort = getProxyPort();
083: enableDirect = false;
084: } else {
085: proxySet = false;
086: proxyPort = (-1);
087: enableDirect = isDirectEnabled();
088: }
089: // rmi.log.11D=Proxy configuration:
090: // rmi.log.11E=proxy disabled, direct HTTP connections
091: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
092: proxyTransportLog
093: .log(
094: RMILog.VERBOSE,
095: Messages.getString("rmi.log.11D") //$NON-NLS-1$
096: + (proxySet ? (proxyHost + ':' + proxyPort)
097: : (Messages
098: .getString("rmi.log.11E") //$NON-NLS-1$
099: + (enableDirect ? "enabled" : "disabled") //$NON-NLS-1$ //$NON-NLS-2$
100: + '.')));
101: }
102: }
103:
104: /**
105: * Returns proxy host name or <code>null</code> if proxy host is not set.
106: *
107: * @return Proxy host name or <code>null</code> if proxy host is not set.
108: */
109: public String getHost() {
110: return proxyHost;
111: }
112:
113: /**
114: * Returns proxy port number or {@link #HTTP_DEFAULT_PORT} if proxy port
115: * is not set or <code>-1</code> if proxy host is not set.
116: *
117: * @return Proxy port number or {@link #HTTP_DEFAULT_PORT} if proxy port
118: * is not set or <code>-1</code> if proxy host is not set.
119: */
120: public int getPort() {
121: return proxyPort;
122: }
123:
124: /**
125: * Returns <code>true</code> if proxy host is set in system environment,
126: * <code>false</code> otherwise.
127: *
128: * @return <code>true</code> if proxy host is set in system environment,
129: * <code>false</code> otherwise.
130: */
131: public boolean isSet() {
132: return proxySet;
133: }
134:
135: /**
136: * Returns new socket connected to specified host and port, probably
137: * through a proxy (if proxy is set). If proxy is not set, then if direct
138: * HTTP connections are enabled, connection is established directly,
139: * otherwise {@link IOException} is thrown.
140: *
141: * @param host
142: * Host to connect to.
143: *z
144: * @param port
145: * Port to connect to.
146: *
147: * @return New socket connected to the specified host and port,
148: * probably through a proxy.
149: *
150: * @throws IOException
151: * If I/O error occurs.
152: */
153: public Socket getSocket(String host, int port) throws IOException {
154: if (proxySet) {
155: return new Socket(proxyHost, proxyPort);
156: } else if (enableDirect) {
157: return new Socket(host, port);
158: } else {
159: // rmi.81=HTTP proxy is not set
160: throw new NoRouteToHostException(Messages
161: .getString("rmi.81")); //$NON-NLS-1$
162: }
163: }
164:
165: /**
166: * Accesses {@link #PROXY_HOST_PROP} system property
167: * and retrieves the proxy host name.
168: *
169: * @return Proxy host name or <code>null</code> if proxy host is not set.
170: */
171: private static String getProxyHost() {
172: String host = (String) AccessController
173: .doPrivileged(new GetStringPropAction(PROXY_HOST_PROP));
174:
175: if ((host == null) || (host.length() < 1)) {
176: return null;
177: }
178:
179: host = host.trim();
180:
181: return ((host.length() < 1) ? null : host);
182: }
183:
184: /**
185: * Accesses {@link #PROXY_PORT_PROP} system property
186: * and retrieves the proxy port number.
187: *
188: * @return Proxy port number or {@link #HTTP_DEFAULT_PORT}
189: * if proxy port is not set.
190: */
191: private static int getProxyPort() {
192: return ((Long) AccessController
193: .doPrivileged(new GetLongPropAction(PROXY_PORT_PROP,
194: RMIConstants.HTTP_DEFAULT_PORT))).intValue();
195: }
196:
197: /**
198: * Accesses {@link #ENABLE_DIRECT_HTTP_PROP} system property
199: * to find out if direct connections are allowed.
200: *
201: * @return <code>true</code> if direct connections are allowed,
202: * <code>false</code> otherwise.
203: */
204: private static boolean isDirectEnabled() {
205: return ((Boolean) AccessController
206: .doPrivileged(new GetBooleanPropAction(
207: ENABLE_DIRECT_HTTP_PROP))).booleanValue();
208: }
209: }
|