001: /*
002: * Reads the following properties from the Gateway profile - stored in DSAME
003: *
004: * 1. sunPortalNetletProxyHost
005: * List of netlet proxy instances that this gateway is configured to service
006: *
007: * 2. sunPortalGatewayServerRetryInterval
008: * Time interval - frequency at which the gateway should ping the netlet
009: * proxy to find whether the netlet proxy is available
010: *
011: * Maintains two list list of available and unavailable netlet proxies and pings
012: * at the frequency as defined by the server retry interval. Creates a worker thread
013: * to continously carry out this activity
014: *
015: * TBD:
016: *
017: * 1. There is a similiar class like this in rproxy/connectionhandler called ServersList.java,
018: * both these are to be combined into a single entity as there share most of functionality
019: *
020: * 2. Need a protocol representation for netlet, something like netlet://host:port and
021: * corresponding URL Handlers
022: *
023: * 3. Tunneling Netlet request to Netlet Proxy from GW via third party standard proxy.
024: *
025: * @ author Rajesh T
026: * @ date 29-7-2002
027: */
028:
029: package com.sun.portal.netlet.eproxy;
030:
031: import java.io.IOException;
032: import java.net.URL;
033: import java.util.HashMap;
034: import java.util.HashSet;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Map;
038: import java.util.Set;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: import com.sun.portal.log.common.PortalLogger;
043: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
044: import com.sun.portal.util.HostAvailabilityEvent;
045: import com.sun.portal.util.HostAvailabilityListener;
046: import com.sun.portal.util.HostChecker;
047:
048: public class NetletProxyRouter extends HostAvailabilityListener {
049:
050: static Set netlet_proxies = new HashSet();
051:
052: public static Map _unavailable_netlet_proxies = new HashMap();
053:
054: private static int _retry;
055:
056: private static Iterator iterator;
057:
058: private static int noOfNetletProxies = 0;
059:
060: private static String onlyOneNetletProxy = null;
061:
062: private static final int PROXY_HTTP_PORT = 8080;
063:
064: private static NetletProxyRouter instance;
065:
066: // private static Logger logger =
067: // Logger.getLogger("com.sun.portal.sra.netlet");
068: private static Logger logger = PortalLogger
069: .getLogger(NetletProxyRouter.class);
070:
071: static {
072: instance = new NetletProxyRouter();
073: _retry = GatewayProfile.getInt("ServerRetryInterval", 5);
074: List allnetlet_proxies = GatewayProfile
075: .getStringList("NetletProxyHost");
076: Iterator it = allnetlet_proxies.iterator();
077: noOfNetletProxies = allnetlet_proxies.size();
078:
079: /*
080: * Spoofing the third party proxy as Netlet Proxy for a HTTPS request so
081: * that the proxy just tunnels all request thinks its https.
082: */
083:
084: if (noOfNetletProxies == 1) {
085: onlyOneNetletProxy = "https://" + it.next().toString();
086: } else {
087: while (it.hasNext())
088: netlet_proxies.add("https://"
089: + it.next().toString().toLowerCase());
090: iterator = netlet_proxies.iterator();
091: }
092: }
093:
094: private NetletProxyRouter() {
095: addHostAvailabilityListener(this );
096: }
097:
098: public void hostAvailabilityChanged(HostAvailabilityEvent hae) {
099: // TODO :: check for proper host type and host status
100: if (hae.getHostType() == hae.NP_HOST) {
101: synchronized (_unavailable_netlet_proxies) {
102: if (hae.getHostStatus() == hae.HOST_AVAILABLE) {
103: _unavailable_netlet_proxies.remove(hae.getHost());
104: } else if (hae.getHostStatus() == hae.HOST_UNAVAILABLE) {
105: long time = System.currentTimeMillis();
106: _unavailable_netlet_proxies.put(hae.getHost(),
107: (new Long(time)));
108: }
109: }
110: }
111: }
112:
113: /*
114: * @ returns true if the specified netlet proxy is present in the netlet
115: * proxy host list
116: */
117:
118: public static boolean contains(String rwp) {
119: if (rwp == null || rwp.trim().length() == 0) {
120: return false;
121: }
122: if (noOfNetletProxies == 1) {
123: return onlyOneNetletProxy.equalsIgnoreCase(rwp);
124: } else {
125: return (netlet_proxies.contains(rwp.toLowerCase()));
126: }
127: }
128:
129: /*
130: * @returns the next available netlet proxy . A simple round-robin load
131: * balancing algorithm implementation with iterators.
132: */
133:
134: public static synchronized String getServer() {
135:
136: /*
137: * There is no need for load balancing as there is only one netlet proxy
138: * so return the only available netlet proxy
139: */
140:
141: if (noOfNetletProxies < 2) {
142: return onlyOneNetletProxy;
143: }
144:
145: if (iterator.hasNext()) {
146: return iterator.next().toString();
147: } else {
148: iterator = netlet_proxies.iterator();
149: return iterator.next().toString();
150: }
151: }
152:
153: /*
154: * @ returns true if the specified netlet proxy is alive Do not create
155: * socket directly to the netlet proxy coz there may be a external proxy
156: * server between the DMZ and intranet, typically specified as java vm
157: * parameters -Djava.proxySet, -Djava.proxyHost, -Djava.proxyPort
158: *
159: * URL factory implementation respects these parameter hence no extra
160: * tunneling code.
161: *
162: */
163:
164: private static boolean isAlive(String nlp) {
165: synchronized (_unavailable_netlet_proxies) {
166: if (!_unavailable_netlet_proxies.isEmpty()
167: && _unavailable_netlet_proxies.get(nlp) != null) {
168: return false; // do not use it!
169: }
170: }
171: try {
172: URL url = new URL(nlp);
173:
174: return HostChecker.isHostAvailable(url,
175: HostAvailabilityEvent.NP_HOST);
176: } catch (IOException e) {
177: // logger.log(Level.SEVERE, "Proxy Authentication Failed", e);
178: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX049");
179: }
180: return false;
181: }
182:
183: /*
184: * get a alive Netlet proxy and return
185: */
186:
187: static String getNetletProxyAlive() {
188:
189: String nlp = null;
190: boolean alive = false;
191: if (noOfNetletProxies == 1) {
192: if (isAlive(onlyOneNetletProxy)) {
193: return onlyOneNetletProxy;
194:
195: } else {
196: return null;
197: }
198: }
199:
200: int alive_servers = netlet_proxies.size();
201:
202: while (!alive && alive_servers > 0) {
203: nlp = getServer();
204: alive = isAlive(nlp);
205: alive_servers--;
206: }
207:
208: if (alive == false)
209: return null;
210:
211: return nlp;
212: }
213:
214: static boolean isNetletProxyAlive(String nlp) {
215: return isAlive(nlp);
216: }
217:
218: /*
219: * If the requested netlet proxy is alive return it else return any other
220: * live netlet proxy
221: */
222:
223: static String getNetletProxyAlive(String nlp) {
224: if (isAlive(nlp)) {
225: return nlp;
226: }
227:
228: return (getNetletProxyAlive());
229: }
230: }
|