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.desktop.taglib;
006:
007: import java.io.IOException;
008: import javax.servlet.jsp.tagext.TagSupport;
009: import javax.servlet.jsp.PageContext;
010: import javax.servlet.jsp.tagext.Tag;
011:
012: import com.sun.portal.providers.Provider;
013: import com.sun.portal.providers.containers.ContainerProvider;
014:
015: public class BaseDesktopTagSupport extends TagSupport {
016:
017: private static final String VARIABLE_PREFIX = "$";
018: protected DesktopTaglibContext mdttlContext = null;
019: protected String mId = null;
020: protected int mScope = PageContext.PAGE_SCOPE;
021: protected boolean m_silentException = false;
022:
023: static public final int UNDEFINED = DesktopTaglibContext.UNDEFINED;
024: static public final int PROVIDER = DesktopTaglibContext.PROVIDER;
025: static public final int CONTAINER = DesktopTaglibContext.CONTAINER;
026:
027: static protected boolean toBoolean(String name) {
028: return ((name != null) && name.toLowerCase().equals("true"));
029: }
030:
031: public void setId(String id) {
032: if (id != null && id.length() > 0)
033: this .mId = id;
034: }
035:
036: public void setSilentException(String silentException) {
037: m_silentException = toBoolean(silentException);
038: }
039:
040: public void setScope(String scope) {
041: if (scope != null) {
042: if (scope.equalsIgnoreCase("request")) {
043: mScope = PageContext.REQUEST_SCOPE;
044: } else if (scope.equalsIgnoreCase("session")) {
045: mScope = PageContext.SESSION_SCOPE;
046: } else if (scope.equalsIgnoreCase("application")) {
047: mScope = PageContext.APPLICATION_SCOPE;
048: }
049: }
050: }
051:
052: public void setParent(Tag t) {
053: super .setParent(t);
054: mdttlContext = null;
055: mId = null;
056: int mScope = PageContext.PAGE_SCOPE;
057: boolean m_silentException = false;
058: }
059:
060: protected void processResult(Object result)
061: throws DesktopTaglibException {
062: if (mId != null) {
063: pageContext.setAttribute(mId, result, mScope);
064: } else {
065: try {
066: pageContext.getOut().print(result);
067: } catch (IOException e) {
068: throw new DesktopTaglibException(e);
069: }
070: }
071: }
072:
073: protected int processException(DesktopTaglibException t)
074: throws DesktopTaglibException {
075: return processException(t, SKIP_BODY, null);
076: }
077:
078: protected int processException(DesktopTaglibException t,
079: Object retObj) throws DesktopTaglibException {
080: return processException(t, SKIP_BODY, retObj);
081: }
082:
083: protected int processException(DesktopTaglibException t,
084: int returnVal, Object retObj) throws DesktopTaglibException {
085:
086: if (m_silentException) {
087: // not printing any error message to the desktop
088: //pageContext.getOut().print( t.getMessage() );
089:
090: if (mId != null && retObj != null) {
091: pageContext.setAttribute(mId, retObj, mScope);
092: }
093: return returnVal;
094: } else {
095: throw t;
096: }
097: }
098:
099: protected boolean isContextVariable(String parameter) {
100: if (parameter != null) {
101: return parameter.startsWith(VARIABLE_PREFIX);
102: } else {
103: return false;
104: }
105: }
106:
107: protected Object resolveParameter(String parameter)
108: throws DesktopTaglibException {
109: if (isContextVariable(parameter)) {
110: String var = getVariable(parameter);
111: Object obj = pageContext.findAttribute(var);
112: if (obj == null) {
113: throw new DesktopTaglibException(
114: DesktopTaglibException.UNDEFINED_PARAMETER, var);
115: }
116: return obj;
117: } else {
118: return parameter;
119: }
120: }
121:
122: protected String getVariable(String parameter) {
123: if (isContextVariable(parameter)) {
124: return parameter.substring(VARIABLE_PREFIX.length());
125: } else {
126: return parameter;
127: }
128: }
129:
130: protected void loadDesktopTaglibContext() {
131:
132: if (mdttlContext == null) {
133: mdttlContext = (DesktopTaglibContext) pageContext
134: .getAttribute(DesktopTaglibContext.PROVIDER_STACK);
135: if (mdttlContext == null) {
136: // throw empty provider stack
137: }
138: }
139: }
140:
141: protected void saveDesktopTaglibContext() {
142: pageContext.setAttribute(DesktopTaglibContext.PROVIDER_STACK,
143: mdttlContext);
144: }
145:
146: protected Provider getProvider() throws DesktopTaglibException {
147: loadDesktopTaglibContext();
148: Provider p = mdttlContext.getCurrentProvider();
149: if (p == null) {
150: throw new DesktopTaglibException(
151: DesktopTaglibException.EMPTY_CONTEXT);
152: } else {
153: return p;
154: }
155: }
156:
157: protected void setCurrentProvider(Provider p)
158: throws DesktopTaglibException {
159: loadDesktopTaglibContext();
160: mdttlContext.setCurrentProvider(p);
161: }
162:
163: protected ContainerProvider getContainer()
164: throws DesktopTaglibException {
165: loadDesktopTaglibContext();
166: ContainerProvider c = mdttlContext.getContainer();
167: if (c == null) {
168: throw new DesktopTaglibException(
169: DesktopTaglibException.EMPTY_CONTEXT);
170: } else {
171: return c;
172: }
173: }
174:
175: protected void setCurrentContainer(ContainerProvider cp)
176: throws DesktopTaglibException {
177: loadDesktopTaglibContext();
178: mdttlContext.setContainer(cp);
179: }
180:
181: protected void setParentCurrent() throws DesktopTaglibException {
182: loadDesktopTaglibContext();
183: mdttlContext.setParentCurrent();
184: }
185:
186: protected void unsetParentCurrent() throws DesktopTaglibException {
187: loadDesktopTaglibContext();
188: mdttlContext.unsetParentCurrent();
189: }
190:
191: protected int getCurrentObjType() {
192: loadDesktopTaglibContext();
193:
194: if (mdttlContext == null)
195: return UNDEFINED;
196:
197: return mdttlContext.getCurrentObjType();
198: }
199:
200: protected Provider getCurrentObj() throws DesktopTaglibException {
201: switch (getCurrentObjType()) {
202: case PROVIDER:
203: return getProvider();
204: case CONTAINER:
205: return (Provider) getContainer();
206: default:
207: throw new DesktopTaglibException(
208: DesktopTaglibException.EMPTY_CONTEXT);
209: }
210: }
211:
212: // Cache Stuff, it is pretty primitive now.
213: // May need to add for feature in the future. But it works for now!
214: // - davechiu
215:
216: protected void cacheObj(String key, Object obj) {
217: pageContext.setAttribute(key, obj, PageContext.REQUEST_SCOPE);
218: }
219:
220: protected Object getCachedObj(String key) {
221: return pageContext.findAttribute(key);
222:
223: }
224:
225: protected void clearCache(String key) {
226: //pageContext.removeAttribute( key );
227: }
228:
229: protected void clearCache() {
230: }
231: }
|