001: /*
002: * ClientDetector.java
003: *
004: * Created on November 13, 2002, 4:38 PM
005: */
006:
007: package com.sun.portal.rproxy.configservlet.client;
008:
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.Set;
012: import java.util.logging.Level;
013: import java.util.logging.Logger;
014:
015: import javax.servlet.http.HttpServletRequest;
016:
017: import com.sun.portal.log.common.PortalLogger;
018: import com.sun.portal.util.SystemProperties;
019:
020: /**
021: *
022: * @author mm132998
023: * @version
024: */
025: public class ClientDetector implements Runnable {
026:
027: static private ClientDetector cd;
028:
029: private HashMap table;
030:
031: private static Logger logger = PortalLogger
032: .getLogger(ClientDetector.class);
033:
034: static {
035: cd = new ClientDetector();
036: new Thread(cd).start();
037: }
038:
039: private ClientDetector() {
040: table = new HashMap();
041: }
042:
043: public static String isCookieSupported(HttpServletRequest req) {
044:
045: String userAgent = req.getHeader("User-Agent");
046: if (userAgent == null) {
047: return "autodetect";
048: }
049:
050: userAgent = userAgent.substring(userAgent.indexOf(":") + 1)
051: .trim();
052: return cd.isCookieSupportedImpl(userAgent);
053:
054: // return false;
055: }
056:
057: // For now.
058: private String isCookieSupportedImpl(String userAgent) {
059:
060: CDCacheEntry result = retrieveValue(userAgent);
061:
062: if (result == null) {
063: // Get new value from RemoteConfigServlet.
064: result = fetchCookieSupport(userAgent);
065: }
066:
067: return result.toString();
068: // return false;
069: }
070:
071: private static final String CDM_GET_CLIENT_PROPERTY = "CDM_GET_CLIENT_PROPERTY";
072:
073: private CDCacheEntry fetchCookieSupport(String userAgent) {
074: CDCacheEntry retval = null;
075:
076: com.sun.portal.rproxy.configservlet.Request request = new com.sun.portal.rproxy.configservlet.Request(
077: null, null, CDM_GET_CLIENT_PROPERTY, userAgent);
078: com.sun.portal.rproxy.configservlet.Response response;
079:
080: try {
081: response = SrapClient.execute(request);
082: } catch (Exception ex) {
083: response = null;
084: }
085:
086: if (response != null) {
087: String result = response.getReturnedObject().toString();
088: if (result != null) {
089: retval = new CDCacheEntry(result);
090: storeValue(userAgent, retval);
091: }
092: }
093: return retval;
094: }
095:
096: private synchronized void storeValue(Object key, Object value) {
097: table.put(key, value);
098: }
099:
100: private synchronized CDCacheEntry retrieveValue(Object key) {
101: return (CDCacheEntry) table.get(key);
102: }
103:
104: private static int CACHE_SLEEP_TIME;
105:
106: private static int CLEANUP_TIME;
107: static {
108: try {
109: CACHE_SLEEP_TIME = Integer.parseInt(SystemProperties.get(
110: "gateway.cdm.cacheSleepTime", "60000"));
111: } catch (Exception ex) {
112: CACHE_SLEEP_TIME = 60000;
113: }
114: try {
115: CLEANUP_TIME = Integer.parseInt(SystemProperties.get(
116: "gateway.cdm.cacheCleanupTime", "300000"));
117: } catch (Exception ex) {
118: CLEANUP_TIME = 300000;
119: }
120: }
121:
122: public void run() {
123:
124: boolean done = false;
125: while (!done) {
126: try {
127: try {
128: Thread.currentThread().sleep(
129: (long) CACHE_SLEEP_TIME);
130: } catch (InterruptedException iEx) {
131: // logger.log(Level.SEVERE, "Exception in ClientDetector
132: // cache cleaner thread ", iEx);
133: Object[] params = { iEx };
134: logger.log(Level.SEVERE,
135: "PSSRRPROXY_CSPRCSERVLETC000", iEx);
136: }
137: cleanupCache();
138: } catch (Throwable th) {
139: // logger.log(Level.SEVERE, "Throwable thrown in ClientDetector
140: // cache cleaner thread ", th);
141: Object[] params = { th };
142: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCSERVLETC001",
143: th);
144: }
145: }
146: }
147:
148: private synchronized void cleanupCache() {
149: Set set = table.keySet();
150: Iterator iter = set.iterator();
151: CDCacheEntry entry;
152: long currtime = System.currentTimeMillis();
153: Object key;
154:
155: while (iter.hasNext()) {
156: key = iter.next();
157: entry = (CDCacheEntry) table.get(key);
158:
159: if (currtime - entry.getTimestamp() > CLEANUP_TIME) {
160: iter.remove();
161: }
162: }
163: }
164: }
165:
166: class CDCacheEntry {
167:
168: private String value;
169:
170: private long timestamp;
171:
172: CDCacheEntry(String value) {
173: this .value = value;
174: this .timestamp = System.currentTimeMillis();
175: }
176:
177: String getValue() {
178: return value;
179: }
180:
181: public String toString() {
182: return value;
183: }
184:
185: long getTimestamp() {
186: return timestamp;
187: }
188: }
|