001: /*
002: * HostChecker.java
003: *
004: * Created on November 20, 2003, 3:36 PM
005: */
006:
007: package com.sun.portal.util;
008:
009: import java.io.IOException;
010: import java.net.MalformedURLException;
011: import java.net.URL;
012: import java.util.logging.Level;
013: import java.util.logging.Logger;
014:
015: import sun.misc.BASE64Decoder;
016:
017: import com.sun.portal.log.common.PortalLogger;
018: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
019:
020: /**
021: *
022: * @author Mridul Muralidharan
023: */
024: public class HostChecker {
025:
026: private static int _retry;
027:
028: private static final int PROXY_HTTP_PORT = 8080;
029:
030: // static Logger logger = Logger.getLogger("com.sun.portal.sra.rproxy");
031: private static Logger logger = PortalLogger
032: .getLogger(HostChecker.class);
033:
034: static {
035: _retry = GatewayProfile.getInt("ServerRetryInterval", 5);
036: }
037:
038: public static boolean isHostAvailable(URL host, int hostType) {
039:
040: try {
041:
042: String proxy = DomainWebProxyConfig.getWebProxy(host
043: .getProtocol(), host.getHost());
044: String proxyAuth = null;
045: if (proxy != null) {
046: String proxyHost = null;
047: int portIndex = proxy.indexOf(':');
048:
049: if (portIndex == -1) {
050: proxyHost = proxy;
051: } else {
052: proxyHost = proxy.substring(0, proxy.indexOf(':'));
053: }
054: proxyAuth = DomainWebProxyConfig
055: .getProxyPassword(proxyHost);
056: }
057:
058: String proxyHost = null;
059: int proxyPort = -1;
060:
061: String proxyUser = null;
062: PingServiceRequest psr = null;
063: String proxyPassword = null;
064:
065: if (proxy == null) {
066: psr = new PingServiceRequest(host);
067: } else {
068: int portIndex = proxy.indexOf(':');
069:
070: if (portIndex == -1) {
071: proxyPort = PROXY_HTTP_PORT;
072: proxyHost = proxy;
073: } else {
074: proxyHost = proxy.substring(0, proxy.indexOf(':'));
075: proxyPort = Integer.parseInt(proxy.substring(proxy
076: .indexOf(':') + 1));
077: }
078:
079: BASE64Decoder decoder = new BASE64Decoder();
080: if (proxyAuth != null) {
081: try {
082: proxyAuth = new String(decoder
083: .decodeBuffer(proxyAuth));
084: } catch (IOException ioe) {
085: // logger.log(Level.SEVERE, "Password decode failed:",
086: // ioe);
087: logger.log(Level.SEVERE, "PSSR_CSPU071", ioe);
088: }
089: }
090:
091: if (proxyAuth == null) {
092: psr = new PingServiceRequest(host, proxyHost,
093: proxyPort);
094: } else {
095: proxyUser = proxyAuth.substring(0,
096: proxyAuth.indexOf(':')).trim();
097: proxyPassword = proxyAuth.substring(
098: proxyAuth.indexOf(':') + 1).trim();
099: psr = new PingServiceRequest(host, proxyHost,
100: proxyPort, proxyUser, proxyPassword);
101: }
102: }
103:
104: boolean isAlive = psr.isServiceAlive();
105: if (!isAlive) {
106: HostAvailabilityEventImpl event = new HostAvailabilityEventImpl();
107: // event.setHost(host.getHost());
108: event.setHost(host.toString());
109: event.setHostStatus(event.HOST_UNAVAILABLE);
110: event.setHostType(hostType);
111: HostAvailabilityMediator.getHostAvailabilityMediator()
112: .notifyListeners(event);
113: watch_unavailable wus = new watch_unavailable(host
114: .toString(), _retry, proxyHost, proxyPort,
115: proxyUser, proxyPassword, hostType);
116: wus.start();
117: }
118: return isAlive;
119: } catch (NumberFormatException e) {
120: // logger.log(Level.SEVERE, "Bad Proxy Port", e);
121:
122: logger.log(Level.SEVERE, "PSSR_CSPU072", e);
123: } catch (ProxyUnreachableException e) {
124: // logger.log(Level.SEVERE, "Proxy used for reaching Portal Server
125: // Proxy is down:", e);
126: logger.log(Level.SEVERE, "PSSR_CSPU073", e);
127: } catch (ProxyAuthNeededException e) {
128: // logger.log(Level.SEVERE, "Proxy password not found for
129: // authentication", e);
130: logger.log(Level.SEVERE, "PSSR_CSPU074", e);
131: } catch (ProxyAuthFailedException e) {
132: // logger.log(Level.SEVERE, "Proxy Authentication Failed", e);
133: logger.log(Level.SEVERE, "PSSR_CSPU075", e);
134: }
135: return false;
136: }
137: }
138:
139: /*
140: * Watch dog thread, Pings the server to find whether the server is back online
141: */
142:
143: class watch_unavailable extends Thread {
144:
145: private String wu_server;
146:
147: private int wu_retry;
148:
149: private URL wu_url;
150:
151: private String proxyHost;
152:
153: private int proxyPort;
154:
155: private String proxyPassword;
156:
157: private String proxyUser;
158:
159: private int hostType;
160:
161: // static Logger logger = Logger.getLogger("com.sun.portal.sra.rproxy");
162: private static Logger logger = PortalLogger
163: .getLogger(watch_unavailable.class);
164:
165: public watch_unavailable(String server, int rt, String proxyHost,
166: int proxyPort, String proxyUser, String proxyPasswd,
167: int hostType) {
168: try {
169: wu_url = new URL(server);
170: wu_server = wu_url.getHost();// server;
171: wu_retry = rt;
172: this .proxyHost = proxyHost;
173: this .proxyPort = proxyPort;
174: this .hostType = hostType;
175: } catch (MalformedURLException e) {
176: // logger.severe("watch_unavailable exception: " + e);
177: logger.log(Level.SEVERE, "PSSR_CSPU076", e);
178: }
179: }
180:
181: public void run() {
182:
183: boolean server_not_working = true;
184:
185: while (server_not_working) {
186: try {
187: sleep(wu_retry * 1000); // try again wu_retry seconds later
188: } catch (InterruptedException exception) {
189: }
190:
191: try {
192: PingServiceRequest psr = null;
193:
194: if (proxyHost == null) {
195: psr = new PingServiceRequest(wu_url);
196: } else {
197: if (proxyPassword == null) {
198: psr = new PingServiceRequest(wu_url, proxyHost,
199: proxyPort);
200: } else {
201: psr = new PingServiceRequest(wu_url, proxyHost,
202: proxyPort, proxyUser, proxyPassword);
203: }
204: }
205: boolean isAlive = psr.isServiceAlive();
206: if (isAlive) {
207: HostAvailabilityEventImpl event = new HostAvailabilityEventImpl();
208: event.setHost(wu_url.toString());
209: event
210: .setHostStatus(HostAvailabilityEvent.HOST_AVAILABLE);
211: event.setHostType(hostType);
212: HostAvailabilityMediator
213: .getHostAvailabilityMediator()
214: .notifyListeners(event);
215: server_not_working = false; // quit watching
216: }
217: } catch (Exception e) {
218: // Should not reach here as only hosts with valid proxy are
219: // being watched
220: // logger.log(Level.SEVERE, "Service Watch dog error", e);
221: logger.log(Level.SEVERE, "PSSR_CSPU077", e);
222: }
223: }
224: }
225: }
|