001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.providers.containers.tck;
006:
007: import java.util.List;
008: import java.util.ArrayList;
009: import java.util.Arrays;
010: import java.util.Collections;
011: import java.util.Map;
012: import java.util.HashMap;
013: import java.util.Hashtable;
014: import java.util.ResourceBundle;
015: import java.util.Iterator;
016: import java.util.StringTokenizer;
017: import java.util.logging.Logger;
018: import java.util.logging.Level;
019:
020: import java.net.URL;
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import com.sun.portal.providers.Provider;
026: import com.sun.portal.providers.ProviderException;
027:
028: import com.sun.portal.providers.context.ProviderContext;
029: import com.sun.portal.providers.context.ContainerProviderContext;
030: import com.sun.portal.providers.context.ProviderContextException;
031:
032: import com.sun.portal.providers.util.PropertyUtil;
033: import com.sun.portal.providers.util.ProviderCommands;
034:
035: import com.sun.portal.providers.containers.ProviderWindowStates;
036: import com.sun.portal.providers.containers.jsp.table.JSPTableContainerProvider;
037: import com.sun.portal.providers.util.ProviderProperties;
038: import com.sun.portal.log.common.PortalLogger;
039:
040: /**
041: * TCKContainerProvider is a special ContainerProvider created
042: * to be used by JSR168's TCK. TCKContainerProvider allows TCK to
043: * test the portlets by specifying the selected channels from the
044: * query string. TCKContainerProvider uses the portletName parameter
045: * name to look for the channels in the query string.
046: * TCKContainerProvider then stores the portletNames in the session for
047: * future requests in the same session. The channels stored in the session
048: * will only be used if the portletName parameter is not present in the
049: * query string.
050: * TCKContainerProvider extends JSPTableContainerProvider.
051: */
052: public class TCKContainerProvider extends JSPTableContainerProvider {
053:
054: private static final String PIPE_STRING = "|";
055: private ChannelNameProcessor _channelNameProcessor = null;
056:
057: // Create a logger for this class
058: private static Logger debugLogger = PortalLogger
059: .getLogger(TCKContainerProvider.class);
060:
061: public void init(String n, HttpServletRequest httpreq)
062: throws ProviderException {
063: super .init(n, httpreq);
064:
065: ContainerProviderContext cpc = getContainerProviderContext();
066:
067: boolean parallelChannelsInit = false;
068:
069: try {
070: parallelChannelsInit = cpc.getBooleanProperty(getName(),
071: ProviderProperties.PARALLEL_CHANNELS_INIT);
072: } catch (ProviderContextException pce) {
073:
074: debugLogger.log(Level.INFO, "PSPL_TKCSPPCT0001",
075: ProviderProperties.PARALLEL_CHANNELS_INIT);
076: // error not fatal, keep going
077: }
078: if (parallelChannelsInit) {
079: cpc.initProviders(httpreq, n, getSelectedChannels(httpreq),
080: 0);
081: }
082: }
083:
084: public List getSelectedChannels(HttpServletRequest req)
085: throws ProviderException {
086: String[] channels = req.getParameterValues("portletName");
087: List selected = null;
088: List portletNames = null;
089: if (channels != null && channels.length != 0) {
090: portletNames = Arrays.asList(channels);
091: selected = processNames(portletNames);
092: if (getProviderContext().isAuthless(req)) {
093: getProviderContext().setClientProperty(
094: getName() + ".SelectedChannels",
095: getSelectedString(selected));
096: } else {
097: getProviderContext().setSessionProperty(
098: getName() + ".SelectedChannels", selected);
099: }
100: } else {
101: if (getProviderContext().isAuthless(req)) {
102: String selectedString = (String) getProviderContext()
103: .getClientProperty(
104: getName() + ".SelectedChannels");
105: selected = getSelectedList(selectedString);
106: } else {
107: selected = (List) getProviderContext()
108: .getSessionProperty(
109: getName() + ".SelectedChannels");
110: }
111: if (selected == null) {
112: selected = Collections.EMPTY_LIST;
113: }
114: }
115: return selected;
116: }
117:
118: private List processNames(List portletNames)
119: throws ProviderException {
120: return getChannelNameProcessor().processNames(
121: getProviderContext(), getName(), portletNames);
122: }
123:
124: private String getSelectedString(List selected) {
125: StringBuffer selectedBuffer = new StringBuffer("");
126: if (selected != null && !selected.isEmpty()) {
127: for (int i = 0; i < selected.size(); i++) {
128: String channel = (String) selected.get(i);
129: selectedBuffer.append(channel);
130: if ((i + 1) < selected.size()) {
131: selectedBuffer.append(PIPE_STRING);
132: }
133: }
134: }
135: return selectedBuffer.toString();
136: }
137:
138: private List getSelectedList(String selected) {
139: List selectedList = new ArrayList();
140: if (selected != null && selected.indexOf(PIPE_STRING) != -1) {
141: StringTokenizer st = new StringTokenizer(selected,
142: PIPE_STRING);
143: while (st.hasMoreTokens()) {
144: selectedList.add(st.nextToken());
145: }
146: } else {
147: if (selected == null || selected.length() == 0) {
148: selectedList = null;
149: } else {
150: selectedList.add(selected);
151: }
152: }
153: return selectedList;
154: }
155:
156: public URL processEdit(HttpServletRequest req,
157: HttpServletResponse res) throws ProviderException {
158: String channelAction = req.getParameter(getName()
159: + ".channelAction");
160: String targetProvider = req.getParameter(getName()
161: + ".targetProvider");
162:
163: if (channelAction.equals("minimize")) {
164: setWindowState(targetProvider,
165: ProviderWindowStates.MINIMIZE);
166: } else if (channelAction.equals("maximize")) {
167: setWindowState(targetProvider, ProviderWindowStates.NORMAL);
168: } else if (channelAction.equals("truemaximize")) {
169: setWindowState(targetProvider,
170: ProviderWindowStates.MAXIMIZE);
171: }
172:
173: return null;
174: }
175:
176: public Map getProviderCommands(HttpServletRequest req)
177: throws ProviderException {
178: Map providerCmdsMap = new HashMap();
179:
180: ContainerProviderContext cpc = getContainerProviderContext();
181: ProviderContext pc = getProviderContext();
182:
183: List selected = getSelectedChannels(req);
184:
185: String desktopURL = pc.getDesktopURL(req);
186: String sContext = pc.getStaticContentPath();
187: ResourceBundle bundle = getResourceBundle();
188:
189: Map channelsIsDetached = getMapProperty(ProviderProperties.CHANNELS_IS_DETACHED);
190: Map channelsIsDetachable = getMapProperty(ProviderProperties.CHANNELS_IS_DETACHABLE);
191: Map channelsIsMinimized = getMapProperty(ProviderProperties.CHANNELS_IS_MINIMIZED);
192: Map channelsIsMinimizable = getMapProperty(ProviderProperties.CHANNELS_IS_MINIMIZABLE);
193: Map channelsIsMaximizable = Collections.EMPTY_MAP;
194: try {
195: if (pc.existsCollectionProperty(getName(),
196: ProviderProperties.CHANNELS_IS_MAXIMIZABLE)) {
197: channelsIsMaximizable = getMapProperty(ProviderProperties.CHANNELS_IS_MAXIMIZABLE);
198: }
199: } catch (ProviderContextException pce) {
200: }
201: String maximizedChannel = "";
202: if (existsStringProperty(ProviderProperties.MAXIMIZED_CHANNEL)) {
203: maximizedChannel = getStringProperty(ProviderProperties.MAXIMIZED_CHANNEL);
204: }
205: Map channelsIsRemovable = getMapProperty(ProviderProperties.CHANNELS_IS_REMOVABLE);
206: boolean dIsMinimizable = getBooleanProperty(ProviderProperties.DEFAULT_CHANNEL_IS_MINIMIZABLE);
207: boolean dIsMaximizable = false;
208: if (existsBooleanProperty(ProviderProperties.DEFAULT_CHANNEL_IS_MAXIMIZABLE)) {
209: dIsMaximizable = getBooleanProperty(ProviderProperties.DEFAULT_CHANNEL_IS_MAXIMIZABLE);
210: }
211: boolean dIsMinimized = getBooleanProperty(ProviderProperties.DEFAULT_CHANNEL_IS_MINIMIZED);
212: boolean dIsDetachable = getBooleanProperty(ProviderProperties.DEFAULT_CHANNEL_IS_DETACHABLE);
213: boolean dIsDetached = getBooleanProperty(ProviderProperties.DEFAULT_CHANNEL_IS_DETACHED);
214: boolean dIsRemovable = getBooleanProperty(ProviderProperties.DEFAULT_CHANNEL_IS_REMOVABLE);
215:
216: String normalizeImage = "b_normal.gif";
217: String maximizeImage = "b_maximize.gif";
218: String minimizeImage = "b_minimize.gif";
219: String helpImage = "b_help.gif";
220: String editImage = "b_edit.gif";
221: String removeImage = "b_remove.gif";
222: String attachImage = "b_attach.gif";
223: String detachImage = "b_new_window.gif";
224:
225: for (int i = 0; i < selected.size(); i++) {
226: String channel = (String) selected.get(i);
227: Provider p = cpc.getProvider(req, getName(), channel);
228: Hashtable tagtable = ProviderCommands.getProviderCommands(
229: p, getName(), cpc, req, bundle, desktopURL,
230: sContext, channelsIsDetachable, channelsIsDetached,
231: channelsIsMinimizable, channelsIsMaximizable,
232: maximizedChannel, channelsIsMinimized,
233: channelsIsRemovable, dIsDetachable, dIsDetached,
234: dIsMinimizable, dIsMaximizable, dIsMinimized,
235: dIsRemovable, normalizeImage, maximizeImage,
236: minimizeImage, helpImage, editImage, removeImage,
237: attachImage, detachImage);
238: providerCmdsMap.put(channel, tagtable);
239: }
240: return providerCmdsMap;
241: }
242:
243: public Map getBooleanValuesFromMap(List channels, Map map,
244: boolean dValue) {
245: Map channelMap = new HashMap();
246: for (int i = 0; i < channels.size(); i++) {
247: String channel = (String) channels.get(i);
248: boolean value = PropertyUtil.getBooleanValueFromMap(map,
249: channel, dValue);
250: channelMap.put(channel, new Boolean(value));
251: }
252: return channelMap;
253: }
254:
255: private ChannelNameProcessor getChannelNameProcessor()
256: throws ProviderException {
257:
258: if (_channelNameProcessor == null) {
259:
260: try {
261: String channelNameProcessorClass = getProviderContext()
262: .getStringProperty(getName(),
263: "tckChannelNameProcessor");
264: if (channelNameProcessorClass == null) {
265: throw new ProviderException(
266: "tckChannelNameProcessor is null");
267: }
268: _channelNameProcessor = (ChannelNameProcessor) (Class
269: .forName(channelNameProcessorClass)
270: .newInstance());
271:
272: } catch (Exception ex) {
273: throw new ProviderException(ex.getMessage(), ex);
274: }
275: }
276:
277: return _channelNameProcessor;
278:
279: }
280: }
|