001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.harness;
007:
008: import java.util.HashMap;
009:
010: import java.io.File;
011: import java.io.Serializable;
012:
013: import javax.servlet.http.HttpSession;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpSessionBindingListener;
016: import javax.servlet.http.HttpSessionBindingEvent;
017:
018: import com.sun.portal.desktop.context.PropertiesClientContext;
019: import com.sun.portal.desktop.context.PropertiesServiceContext;
020: import com.sun.portal.desktop.context.PropertiesConfigContext;
021:
022: // RedirectorContext is used in the top level JSP's to obtain redirection information
023: // on where to find the actual content producing jsp's located in the desktop directory.
024: // Basically, it produces just enough of the context to be able to get template paths.
025: // The top level jsp's do not produce any actual content, and thus do not have to be
026: // translated.
027:
028: // Like ProviderHarness we store our real context in a private cache because we do not want
029: // to try to store them on HTTP session.
030:
031: public class HarnessRedirector implements HttpSessionBindingListener,
032: Serializable {
033:
034: static public HarnessRedirector getRedirector(HttpServletRequest req)
035: throws ProviderHarnessException {
036:
037: HarnessRedirector red = null;
038: String cfgdir = req.getParameter(ProviderHarness.ARG_CONFIGDIR);
039: HttpSession sess = req.getSession();
040:
041: if (cfgdir == null) {
042: red = (HarnessRedirector) sess.getAttribute(SESSION_KEY);
043: if (red == null) {
044: throw new ProviderHarnessException(
045: "Cannot find HarnessRedirector in Session");
046: }
047: synchronized (m_RedCache) {
048: red.m_RedCtx = (RedirectorContext) m_RedCache
049: .get(red.m_CacheKey);
050: }
051: if (red.m_RedCtx == null) {
052: throw new ProviderHarnessException(
053: "Unexpected lookup failure in HarnessRedirector cache");
054: }
055: red.m_IgnoreUnbind = true;
056: sess.setAttribute(SESSION_KEY, red);
057: red.m_IgnoreUnbind = false;
058: return red;
059: }
060:
061: red = new HarnessRedirector();
062:
063: synchronized (m_RedCache) {
064: ++m_RedCounter;
065: red.m_CacheKey = "RED#" + m_RedCounter;
066: red.m_RedCtx = new RedirectorContext(cfgdir, req
067: .getLocale().toString());
068: sess.setAttribute(SESSION_KEY, red);
069: m_RedCache.put(red.m_CacheKey, red.m_RedCtx);
070: }
071:
072: return red;
073: }
074:
075: public String getRelativePath(String jspname)
076: throws ProviderHarnessException {
077: return m_RedCtx.getRelativePath(jspname);
078: }
079:
080: public void valueUnbound(
081: HttpSessionBindingEvent httpSessionBindingEvent) {
082: if (!m_IgnoreUnbind) {
083: synchronized (m_RedCache) {
084: m_RedCache.remove(m_CacheKey);
085: }
086: }
087: }
088:
089: public void valueBound(
090: HttpSessionBindingEvent httpSessionBindingEvent) {
091: }
092:
093: private String m_CacheKey;
094: private boolean m_IgnoreUnbind = false;
095:
096: private transient RedirectorContext m_RedCtx;
097:
098: private static int m_RedCounter = 0;
099: private static HashMap m_RedCache = new HashMap();
100:
101: private static final String SESSION_KEY = "desktop.Simulator.HarnessRedirector";
102: }
103:
104: class RedirectorContext {
105: RedirectorContext(String configdir, String locale) {
106: try {
107: m_ConfigDir = new File(configdir).getCanonicalPath();
108: } catch (Exception ex) {
109: m_ConfigDir = configdir;
110: }
111: m_Locale = locale;
112: m_DesktopProperties = new PropertiesConfigContext();
113: m_DesktopProperties.init(configdir
114: + "/desktop/desktopconfig.properties");
115: m_TplContext = new HarnessTemplateContext();
116: m_ServiceContext = new PropertiesServiceContext(configdir
117: + "/service-context.properties");
118: m_ClientContext = new PropertiesClientContext(configdir
119: + "/client-context.properties");
120: }
121:
122: public String getRelativePath(String jspname)
123: throws ProviderHarnessException {
124:
125: // This mimics the providercontext lookup with both provider name and
126: // channel name = HARNESS_TEMPLATES and a locale = the user's "real" locale,
127: // not the locale they are testing.
128:
129: File f = m_TplContext.getTemplatePath(m_DesktopProperties
130: .getTemplateBaseDir(), m_ServiceContext
131: .getDesktopType(), m_Locale, HARNESS_TEMPLATES,
132: HARNESS_TEMPLATES, m_ClientContext
133: .getClientPath(m_ClientContext
134: .getClientType(null)), jspname);
135:
136: if (f == null) {
137: throw new ProviderHarnessException(
138: "Cannot find template for " + HARNESS_TEMPLATES
139: + "/" + jspname);
140: }
141:
142: String rp = null;
143: try {
144: rp = f.getCanonicalPath();
145: } catch (Exception ex) {
146: rp = f.getPath();
147: }
148:
149: if (rp.startsWith(m_ConfigDir)) {
150: return rp.substring(m_ConfigDir.length() + 1);
151: }
152:
153: throw new ProviderHarnessException(
154: "Template file did not begin with config directory.");
155: }
156:
157: private PropertiesConfigContext m_DesktopProperties;
158: private HarnessTemplateContext m_TplContext;
159: private PropertiesServiceContext m_ServiceContext;
160: private PropertiesClientContext m_ClientContext;
161: private String m_ConfigDir;
162: private String m_Locale;
163:
164: private static final String HARNESS_TEMPLATES = "harness";
165: }
|