001: /*
002: * Copyright 2001 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.providers.userinfo.tag;
007:
008: import java.util.Map;
009: import java.util.Hashtable;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.logging.Logger;
013: import java.util.logging.Level;
014:
015: import javax.servlet.http.HttpServletRequest;
016:
017: import com.sun.portal.providers.context.ProviderContext;
018: import com.sun.portal.providers.context.ProviderContextException;
019: import com.sun.portal.log.common.PortalLogger;
020:
021: /**
022: * this class manages tag modules for a particular provider
023: */
024:
025: public class TagModuleManager {
026: private ProviderContext context = null;
027: private Map modules = null;
028: private Map tags = null;
029: private String channel = null;
030: private static Logger logger = PortalLogger
031: .getLogger(TagModuleManager.class);
032:
033: public TagModuleManager(String channel, ProviderContext context,
034: HttpServletRequest req) throws TagException {
035: init(channel, context, req);
036: }
037:
038: private void init(String channel, ProviderContext context,
039: HttpServletRequest req) throws TagException {
040: this .channel = channel;
041: if (context == null) {
042: throw new TagException(
043: "TagModuleManager.init(): provider context was null");
044: }
045: // set provider context
046: this .context = context;
047:
048: tags = null;
049: modules = new HashMap();
050: Map moduleClassnames = null;
051:
052: try {
053: tags = context.getCollectionProperty(channel, "tags");
054: moduleClassnames = context.getCollectionProperty(channel,
055: "tagModules");
056: } catch (ProviderContextException pce) {
057: throw new TagException(
058: "TagModuleManager.init(): Failed to get properties");
059: }
060:
061: for (Iterator i = moduleClassnames.keySet().iterator(); i
062: .hasNext();) {
063: String module = (String) i.next();
064: String moduleClass = (String) moduleClassnames.get(module);
065:
066: logger.log(Level.FINEST, "PSCR_CSPPUIT0003", new Object[] {
067: module, moduleClass });
068:
069: //
070: // cast to the common parent. we don't know if it's read or write
071: //
072: try {
073: TagModule tm = (TagModule) (Class.forName(moduleClass)
074: .newInstance());
075:
076: try {
077: tm.init(channel, context, req);
078: } catch (TagException ta) {
079: logger.log(Level.INFO, "PSCR_CSPPUIT0004", ta);
080: continue;
081: }
082: modules.put(module, tm);
083: logger.log(Level.FINER, "PSCR_CSPPUIT0005");
084: } catch (ClassNotFoundException cnfe) {
085: logger.log(Level.INFO, "PSCR_CSPPUIT0004", cnfe);
086: continue;
087: } catch (IllegalAccessException iae) {
088: logger.log(Level.INFO, "PSCR_CSPPUIT0004", iae);
089: continue;
090: } catch (InstantiationException ie) {
091: logger.log(Level.INFO, "PSCR_CSPPUIT0004", ie);
092: continue;
093: }
094: }
095:
096: }
097:
098: public WriteTag getWriteTag(String tag) throws TagException {
099: String module = (String) tags.get(tag);
100: WriteTag wt = null;
101:
102: try {
103: wt = (WriteTag) modules.get(module);
104: } catch (ClassCastException cce) {
105: throw new TagCastException("error casting to write tag",
106: cce);
107: }
108:
109: if (wt == null) {
110: throw new UndefinedTagException("undefined tag=" + tag);
111: }
112:
113: return wt;
114: }
115:
116: public ReadTag getReadTag(String tag) throws TagException {
117: String module = (String) tags.get(tag);
118: ReadTag rt = null;
119:
120: try {
121: rt = (ReadTag) modules.get(module);
122: } catch (ClassCastException cce) {
123: throw new TagCastException("error casting to read tag", cce);
124: }
125:
126: if (rt == null) {
127: throw new UndefinedTagException(
128: "no module defined for tag=" + tag);
129: }
130:
131: return rt;
132: }
133:
134: public Hashtable getTable() {
135: Hashtable table = new Hashtable();
136: Map escapeTags = null;
137: try {
138: //escapeTags collection part of UserInfoProvider version 2 and up
139: if (context.getProviderVersion(channel) >= 2) {
140: escapeTags = context.getCollectionProperty(channel,
141: "escapeTags");
142: }
143: } catch (ProviderContextException pce) {
144: // Cannot read escapeTags collection. Not a fatal error. Proceed.
145: logger.log(Level.FINE, "PSCR_CSPPUIT0006", pce);
146: }
147: for (Iterator i = tags.keySet().iterator(); i.hasNext();) {
148: String tag = (String) i.next();
149:
150: ReadTag rt = null;
151: try {
152: rt = getReadTag(tag);
153: } catch (TagCastException tce) {
154: //
155: // this is not an error. it just means one of the defined tags
156: // is write-only, and therefore just not be stuck in the
157: // tag table for reading
158: //
159: logger.log(Level.FINE, "PSCR_CSPPUIT0007", tag);
160: continue;
161: } catch (TagException te) {
162: //
163: // don't die on this. we just failed for one tag
164: //
165: logger.log(Level.FINE, "PSCR_CSPPUIT0008", te);
166: continue;
167: }
168:
169: if (rt == null) {
170: logger.log(Level.FINE, "PSCR_CSPPUIT0009", tag);
171: continue;
172: }
173: TagModuleElement tme = null;
174: //
175: // Get the escape boolean value for the tag from escapeTags collection.
176: //
177: if ((escapeTags == null) || (!escapeTags.containsKey(tag))) {
178: //
179: // The tag was not found in the escapeTags collection. Dont escape this tag.
180: //
181: tme = new TagModuleElement(rt, tag, false, context);
182: } else {
183: tme = new TagModuleElement(rt, tag,
184: ((Boolean) escapeTags.get(tag)).booleanValue(),
185: context);
186: }
187:
188: //
189: // to lazy evaluation of tags
190: //
191: table.put(tag, tme);
192: }
193:
194: return table;
195: }
196: }
|