001: /*
002: * ApplicationLoginHostChooser.java
003: *
004: * Created on August 29, 2003, 11:29 PM
005: */
006:
007: package com.sun.portal.util;
008:
009: import java.net.MalformedURLException;
010: import java.net.URL;
011: import java.util.logging.Level;
012: import java.util.logging.Logger;
013:
014: import com.sun.portal.log.common.PortalLogger;
015: import com.sun.portal.rproxy.configservlet.client.PlatformProfile;
016:
017: /**
018: *
019: * @author Mridul Muralidharan
020: * @version
021: */
022: public class ApplicationLoginHostChooser extends
023: HostAvailabilityListener {
024:
025: private URL currentHostInUse = null;
026:
027: private boolean isInitialised = false;
028:
029: private static final String bootupISUrl;
030:
031: // private static Logger logger =
032: // Logger.getLogger("com.sun.portal.sra.rproxy");
033: private static Logger logger = PortalLogger
034: .getLogger(ApplicationLoginHostChooser.class);
035:
036: static {
037: String url = null;
038: String protocol = com.iplanet.am.util.SystemProperties
039: .get("com.iplanet.am.server.protocol");
040: String host = com.iplanet.am.util.SystemProperties
041: .get("com.iplanet.am.server.host");
042: String port = com.iplanet.am.util.SystemProperties
043: .get("com.iplanet.am.server.port");
044:
045: try {
046: URL _url = new URL(protocol, host, Integer.parseInt(port),
047: "/");
048: url = _url.toString();
049: } catch (MalformedURLException mfEx) {
050: // logger.log(Level.SEVERE, "Unable to create IS server url from
051: // AMConfig.properties", mfEx);
052: logger.log(Level.SEVERE, "PSSR_CSPU017", mfEx);
053: // logger.severe("protocol : " + protocol + " host : " + host + "
054: // port : " + port);
055: Object[] params = { protocol, host, port };
056: logger.log(Level.SEVERE, "PSSR_CSPU018", params);
057:
058: } catch (NumberFormatException nfEx) {
059: // logger.log(Level.SEVERE, "Unable to create IS server url from
060: // AMConfig.properties", nfEx);
061: logger.log(Level.SEVERE, "PSSR_CSPU019", nfEx);
062: // logger.severe("protocol : " + protocol + " host : " + host + "
063: // port : " + port);
064: Object[] params = { protocol, host, port };
065: logger.log(Level.SEVERE, "PSSR_CSPU020", params);
066: }
067: bootupISUrl = url;
068: }
069:
070: private static ApplicationLoginHostChooser instance = new ApplicationLoginHostChooser();
071:
072: private ApplicationLoginHostChooser() {
073: isInitialised = false;
074: addHostAvailabilityListener(this );
075: }
076:
077: private synchronized void setCurrentHostInUse(URL url) {
078: currentHostInUse = url;
079: }
080:
081: /*
082: * ApplicationLogin is required to handle the case when ServersList is not
083: * initialised - this happens when the first request is going to be made to
084: * fetch the gateway profile. At this time , we dont have a list of servers
085: * to choose from , but will have to rely on the server , port and protocol
086: * for IS server that is specified in AMConfig.properties
087: */
088: public static synchronized URL getAlivePlatFormServerURL() {
089: String url = null;
090: String igString = com.sun.portal.util.SystemProperties
091: .get("gateway.ignoreServerList");
092:
093: if (igString != null && igString.equalsIgnoreCase("true")) {
094: GWDebug.debug
095: .message("ApplicationLoginHostChooser::getAlivePlatFormServerURL: "
096: + "ignoring ServerList, using "
097: + bootupISUrl);
098: url = bootupISUrl;
099:
100: } else {
101: if (PlatformProfile.isInitialised()) {
102: url = ServersList.getInstance().getServeraLive();
103: } else {
104: url = bootupISUrl;
105: }
106: }
107: URL retUrl = null;
108: if (url != null) {
109: try {
110: retUrl = new URL(url);
111: instance.setCurrentHostInUse(retUrl);
112: } catch (MalformedURLException mfEx) {
113: retUrl = null;
114: }
115: }
116: return retUrl;
117: }
118:
119: public void hostAvailabilityChanged(HostAvailabilityEvent hae) {
120: boolean needRecreate = false;
121:
122: // logger.info("Host avaliability changed : " + hae.getHostStatus() + "
123: // for " + hae.getHost());
124: Object[] params = { new Integer(hae.getHostStatus()),
125: hae.getHost() };
126: logger.log(Level.INFO, "PSSR_CSPU021", params);
127: // System.out.println("Host avaliability changed : " +
128: // hae.getHostStatus() + " for " + hae.getHost());
129: if (!isInitialised || currentHostInUse == null) {
130: needRecreate = true;
131: }
132: if (hae.getHostType() == hae.IS_HOST) {
133: if (hae.getHostStatus() == HostAvailabilityEvent.HOST_UNAVAILABLE) {
134: try {
135: URL tmpURL = new URL(hae.getHost());
136: if (tmpURL.getHost().equalsIgnoreCase(
137: currentHostInUse.getHost())
138: && tmpURL.getPort() == currentHostInUse
139: .getPort()) {
140: needRecreate = true;
141: }
142: } catch (Exception ex) {
143: needRecreate = true;
144: }
145: }
146: }
147:
148: // logger.info("Host avaliability changed -> Host : " + hae.getHost() +
149: // " needRecreate app session : " + needRecreate);
150: Object[] params1 = { hae.getHost(), new Boolean(needRecreate) };
151: logger.log(Level.INFO, "PSSR_CSPU022", params1);
152: if (needRecreate) {
153: recreateLoginSession();
154: }
155: }
156:
157: private void recreateLoginSession() {
158: GWLogManager.createNewAppSession();
159: }
160: }
|