001: /**
002: * $Id: ContextTag.java,v 1.22 2005/10/18 22:28:22 js143762 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.base;
014:
015: import java.util.*;
016: import java.util.logging.Logger;
017: import java.util.logging.Level;
018:
019: import javax.servlet.jsp.*;
020: import javax.servlet.http.*;
021: import javax.servlet.jsp.tagext.*;
022:
023: import com.sun.ssoadapter.*;
024: import com.sun.ssoadapter.config.*;
025: import com.sun.portal.wireless.taglibs.util.UtilContext;
026: import com.sun.portal.wireless.util.SSOAdapterPrivate;
027: import com.sun.portal.wireless.util.AppConfigConstants;
028: import com.sun.portal.log.common.PortalLogger;
029:
030: /**
031: * The ContextTag abstract class supports tags which
032: * represent a context for a service.
033: *
034: * Subclasses must implement the findContext() method.
035: *
036: * Cooperating classes either look for the context
037: * in the pageContext with the appropriate name
038: * or use the getContext() method if this is
039: * a parent tag.
040: *
041: * There are no default attributes for this tag.
042: *
043: * @version 1.0
044: * @see Context
045: */
046: public abstract class ContextTag extends BeanTag {
047:
048: private static final String CONFIG_NAME_VECTOR_KEY = "configNameVector";
049:
050: private String configName = null;
051: private String testString = null;
052: private boolean testBoolean = false;
053: private String reset = null;
054: //When true, clean up (reset) context, closing connections, and don't reestablish
055: private boolean close = false;
056:
057: // Create a logger for this class
058: private static Logger debugLogger = PortalLogger
059: .getLogger(ContextTag.class);
060:
061: /**
062: * Find the context this tag represents.
063: *
064: * Subclasses must implement this method which
065: * should return the appropriate context for
066: * this session.
067: *
068: * @return the context
069: */
070: public abstract Context findContext() throws Exception;
071:
072: /**
073: * Remove the context this tag represents from all places
074: * it is referred from.
075: *
076: * Subclasses must implement this method which
077: * should remove the appropriate context.
078: *
079: */
080: public abstract void reset();
081:
082: /**
083: * Return the context as a bean
084: *
085: * @return the context
086: */
087: public Object newBean() throws Exception {
088: return findContext();
089: }
090:
091: /**
092: * Return the context as a bean
093: *
094: * @return the context
095: */
096: public Object findBean() throws Exception {
097: return findContext();
098: }
099:
100: /**
101: * Make the context available.
102: * If no context is found then throw JspException.
103: *
104: * @return EVAL_BODY_INCLUDE
105: * @exception JspException
106: */
107: public int doStartTag() throws JspException {
108: Context context = null;
109:
110: try {
111: context = findContext();
112: //If we are closing the context, reset (close all connections),
113: //and don't re-establish a new context(find)
114: if (close) {
115: if (context != null) {
116: context.closeConnection();
117: reset();
118: }
119: return EVAL_BODY_INCLUDE;
120: }
121:
122: if ((reset != null) && (context != null)) {
123: reset();
124: context = findContext();
125: }
126:
127: } catch (JspException e) {
128: throw e;
129: } catch (Exception e) {
130: if (testString == null) {
131: debugLogger.log(Level.FINE, "PSMA_CSPWTB0001", e);
132: throw new JspException(this .getClass().getName()
133: + ".doStartTag(): " + e);
134: }
135: }
136:
137: if (context == null) {
138: if (testString == null) {
139: debugLogger.fine("PSMA_CSPWTB0014");
140: throw new JspException(this .getClass().getName()
141: + ".doStartTag(): No context available");
142: } else {
143: return testBoolean ? SKIP_BODY : EVAL_BODY_INCLUDE;
144: }
145: }
146:
147: if (testString != null) {
148: boolean available = false;
149: try {
150: SSOAdapter ssoAdapter = context.getSSOAdapter();
151: available = (ssoAdapter.getConnection() != null);
152: } catch (Exception e) {
153: available = false;
154: }
155: if (available) {
156: return testBoolean ? EVAL_BODY_INCLUDE : SKIP_BODY;
157: } else {
158: return testBoolean ? SKIP_BODY : EVAL_BODY_INCLUDE;
159: }
160: }
161:
162: return EVAL_BODY_INCLUDE;
163: }
164:
165: /*
166: * Try to ensure that a useable configName is chosen.
167: */
168: protected void computeConfigName(PageContext pageContext,
169: String configNameKey, String ssoAdapterKey,
170: String configType) {
171: ConfigurationFactory factory = ConfigurationFactory
172: .getInstance(SSOAdapterPrivate.SSO_SERVICE_NAME,
173: SSOAdapterPrivate.SSO_ATTR_CONFIGURATIONS_NAME);
174:
175: if (configName == null) {
176:
177: //get the configName from Page
178: configName = getConfigNameFromPage(pageContext,
179: configNameKey);
180:
181: if (configName == null) {
182:
183: //get the configName from session
184: configName = getConfigNameFromSession(pageContext,
185: configNameKey);
186:
187: if (configName == null) {
188:
189: Vector configNameVector = (Vector) pageContext
190: .getAttribute(CONFIG_NAME_VECTOR_KEY);
191:
192: if (configNameVector == null) {
193: Enumeration configEnum = factory
194: .getConfigurationNames(new SSOAdapterSession(
195: (HttpServletRequest) pageContext
196: .getRequest()));
197: if (configEnum == null) {
198: return;
199: }
200: configNameVector = new Vector(3, 3);
201: while (configEnum.hasMoreElements()) {
202: configNameVector.add(configEnum
203: .nextElement());
204: }
205: pageContext.setAttribute(
206: CONFIG_NAME_VECTOR_KEY,
207: configNameVector);
208: }
209:
210: // Pick 1st config of matching type...
211: // ... and which is useable...
212:
213: SSOAdapterFactory ssoFactory = SSOAdapterFactory
214: .getInstance();
215: HttpServletRequest request = (HttpServletRequest) pageContext
216: .getRequest();
217:
218: for (int i = 0; i < configNameVector.size(); i++) {
219: Configuration conf;
220: try {
221: configName = (String) configNameVector
222: .get(i);
223: conf = factory.readConfiguration(
224: configName, true,
225: new SSOAdapterSession(request));
226: String type = conf.getProperty("type");
227:
228: if (type != null && type.length() != 0
229: && type.equals(configType)) {
230: //
231: // Test usability of candidate...
232: //
233: SSOAdapter ssoAdapter = ssoFactory
234: .getSSOAdapter(
235: configName,
236: new SSOAdapterSession(
237: request,
238: null,
239: null,
240: AppConfigConstants.MAP_INSTANCE_NAME));
241: Object conn = ssoAdapter
242: .getConnection();
243: if (conn != null) {
244: // DANGER: The context impl had better pick this up, or we've
245: // just created an orphaned connection of unknown duration.
246: pageContext.setAttribute(
247: ssoAdapterKey, ssoAdapter);
248: break;
249: }
250: }
251:
252: } catch (Exception e) {
253: }
254: configName = null;
255: }
256:
257: if (configName != null) {
258: setConfigNameOnSession(pageContext,
259: configNameKey, configName);
260: }
261: pageContext.setAttribute(configNameKey, configName);
262: } else {
263: pageContext.setAttribute(configNameKey, configName);
264: }
265: }
266:
267: } else {
268: pageContext.setAttribute(configNameKey, configName);
269: setConfigNameOnSession(pageContext, configNameKey,
270: configName);
271: }
272:
273: return;
274: }
275:
276: private String getConfigNameFromPage(PageContext pageContext,
277: String configNameKey) {
278: return (String) pageContext.getAttribute(configNameKey);
279: }
280:
281: private String getConfigNameFromSession(PageContext pageContext,
282: String configNameKey) {
283: String configName = null;
284: try {
285: UtilContext uc = UtilContext.getContext(pageContext);
286: configName = (String) uc.getAttribute(configNameKey);
287: } catch (Exception ex) {
288: debugLogger.fine("PSMA_CSPWTB0015");
289: }
290: return configName;
291: }
292:
293: private void setConfigNameOnSession(PageContext pageContext,
294: String configNameKey, String configName) {
295: try {
296: UtilContext uc = UtilContext.getContext(pageContext);
297: uc.setAttribute(configNameKey, configName);
298: } catch (Exception ex) {
299: debugLogger.fine("PSMA_CSPWTB0015");
300: }
301: }
302:
303: /*
304: * Process "config" attribute...
305: */
306: public void setConfig(String s) {
307: configName = Util.evalAttribute(pageContext, null, s);
308: if (configName.length() == 0) {
309: configName = null;
310: }
311: }
312:
313: /*
314: * Process "test" attribute...
315: */
316: public void setTest(String s) {
317: testString = Util.evalAttribute(pageContext, null, s);
318: testBoolean = Boolean.valueOf(testString).booleanValue();
319: }
320:
321: /**
322: * Process "reset" attribute...
323: */
324: public void setReset(String s) {
325: reset = Util.evalAttribute(pageContext, null, s);
326: if (reset.length() == 0) {
327: reset = null;
328: }
329: }
330:
331: /**
332: * Cleanup
333: */
334: public void release() {
335: configName = null;
336: testString = null;
337: testBoolean = false;
338: super .release();
339: }
340:
341: /**
342: * Process "close" attribute
343: */
344: public void setClose(String s) {
345: String closeS = Util.evalAttribute(pageContext, null, s);
346: if ((closeS != null) && (closeS.length() != 0)
347: && (!closeS.equalsIgnoreCase("false"))) {
348: close = true;
349: }
350: }
351:
352: }
|