001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rproxy.rewriter;
006:
007: import java.lang.reflect.Constructor;
008: import java.net.MalformedURLException;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import com.sun.portal.rewriter.Translator;
013: import com.sun.portal.rewriter.TranslatorChain;
014: import com.sun.portal.rewriter.TranslatorHook;
015: import com.sun.portal.rewriter.util.ConfigManager;
016: import com.sun.portal.rewriter.util.Debug;
017: import com.sun.portal.rewriter.util.StringHelper;
018: import com.sun.portal.rewriter.util.uri.PageSpec;
019: import com.sun.portal.rproxy.connectionhandler.Request;
020: import com.sun.portal.rproxy.rewriter.obscure.ObscureTranslatorHook;
021: import com.sun.portal.rproxy.rewriter.util.SRAPConfigManager;
022: import com.sun.portal.rproxy.rewriter.util.uri.SRAPGatewayURI;
023: import com.sun.portal.rproxy.rewriter.yahoo.YahooConfigManager;
024: import com.sun.portal.rproxy.rewriter.yahoo.YahooTranslatorHook;
025:
026: public class SRAPTranslatorBuilder {
027: public static final String PROPERTY_CUSTOM_TRANSLATOR_HOOKS = "CUSTOM_TRANSLATOR_HOOKS";
028:
029: private static final Constructor[] CUSTOM_TRANSLATOR_HOOKS;
030:
031: static {
032: String[] classNames = StringHelper
033: .tokenize(ConfigManager.getProperty(
034: PROPERTY_CUSTOM_TRANSLATOR_HOOKS, ""), ";");
035: Class[] hookConstructors = new Class[] { Translator.class,
036: SRAPGatewayURI.class };
037: List bList = new ArrayList();
038: for (int i = 0; i < classNames.length; i++) {
039: try {
040: Class bClass = Class.forName(classNames[i]);
041: bList.add(bClass.getConstructor(hookConstructors));
042: } catch (Exception e) {
043: Debug.warning("Unable to add custom translator:", e);
044: }
045: }
046: CUSTOM_TRANSLATOR_HOOKS = (Constructor[]) bList
047: .toArray(new Constructor[0]);
048: }// static
049:
050: public static final Translator create(final Request aRequest,
051: final PageSpec aPageSpec) {
052: return create(getGatewayURI(aRequest), getPageSpec(aRequest,
053: aPageSpec));
054: }// create()
055:
056: private static final Translator create(
057: final SRAPGatewayURI aGatewayURI, final PageSpec aPageSpec) {
058: SRAPTranslator lSRAPTranslator = new SRAPTranslator(
059: aGatewayURI, aPageSpec);
060:
061: if (YahooConfigManager.isYahooEnabled()
062: || SRAPConfigManager.isObscureURIs()
063: || CUSTOM_TRANSLATOR_HOOKS.length != 0) {
064: TranslatorChain chainTranslator;
065: TranslatorHook obscureTranslatorHook = null;
066: if (SRAPConfigManager.isObscureURIs()) {
067: obscureTranslatorHook = new ObscureTranslatorHook(
068: lSRAPTranslator, lSRAPTranslator
069: .getGatewayURI());
070: chainTranslator = new TranslatorChain(lSRAPTranslator,
071: obscureTranslatorHook.getJSFunctionSpec());
072: } else {
073: chainTranslator = new TranslatorChain(lSRAPTranslator);
074: }
075:
076: if (YahooConfigManager.isYahooEnabled()) {
077: chainTranslator
078: .addTranslatorHook(new YahooTranslatorHook(
079: lSRAPTranslator, lSRAPTranslator
080: .getGatewayURI()));
081: }
082:
083: addCustomHooks(chainTranslator, lSRAPTranslator
084: .getGatewayURI());
085: // Add obscureTranslator at last..
086: if (obscureTranslatorHook != null) {
087: chainTranslator
088: .addTranslatorHook(obscureTranslatorHook);
089: }
090: // BugNo:4323953
091: chainTranslator.enableSmartChaining();
092: return chainTranslator;
093: } else {
094: return lSRAPTranslator;
095: }
096: }// create()
097:
098: private static final PageSpec getPageSpec(final Request aRequest,
099: final PageSpec aPageSpec) {
100: PageSpec lResult;
101: if (aPageSpec != null) {
102: lResult = aPageSpec;
103: } else {
104: lResult = new PageSpec(aRequest.getURL(), null);
105: }
106:
107: return lResult;
108: }// getPageSpec()
109:
110: private static final SRAPGatewayURI getGatewayURI(
111: final Request aRequest) {
112: try {
113: return new SRAPGatewayURI(aRequest.getGatewayURL());
114: } catch (MalformedURLException me) {
115: }
116:
117: return null;
118: }// getGatewayURI()
119:
120: private static final void addCustomHooks(
121: final TranslatorChain aTranslatorChain,
122: final SRAPGatewayURI aGatewayURI) {
123: for (int i = 0; i < CUSTOM_TRANSLATOR_HOOKS.length; i++) {
124: try {
125: TranslatorHook bTranslatorHook = (TranslatorHook) (CUSTOM_TRANSLATOR_HOOKS[i]
126: .newInstance(new Object[] {
127: aTranslatorChain.getRootTranslator(),
128: aGatewayURI }));
129: aTranslatorChain.addTranslatorHook(bTranslatorHook);
130: } catch (Exception e) {
131: Debug
132: .warning(
133: "Unable to create instance of custom translator:",
134: e);
135: }
136: }
137: }// addCustomHooks()
138:
139: }// class SRAPTranslatorBuilder
|