001: /*
002: * $Id: JSPNativeContainerProvider.java,v 1.7 2005/09/21 10:48:32 dg154973 Exp $
003: * Copyright 2003 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: */
014:
015: package com.sun.portal.wireless.providers.containers.jsp.list;
016:
017: import java.util.List;
018: import java.util.Map;
019: import java.util.HashMap;
020: import java.util.ArrayList;
021: import java.util.logging.Level;
022: import java.util.logging.LogRecord;
023: import java.util.logging.Logger;
024: import com.sun.portal.log.common.PortalLogger;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import com.sun.portal.providers.ProviderException;
029: import com.sun.portal.providers.context.ContainerProviderContext;
030: import com.sun.portal.providers.context.ProviderContextException;
031: import com.sun.portal.wireless.providers.containers.jsp.WirelessJSPContainerProviderAdapter;
032:
033: public class JSPNativeContainerProvider extends
034: WirelessJSPContainerProviderAdapter {
035:
036: public final static int MAXSIZE = 32000;
037: public final static String PARALLEL_CHANNELS_INIT = "parallelChannelsInit";
038: public final static String START_CHANNEL_PARAM = "startChannel";
039: public final static String MAXSIZE_KEY = "WmlDeckSize";
040: public final static String FRONT_PAGE_SIZE = "frontPageSize";
041: public final static String TIMEOUT = "timeout";
042:
043: private static Logger logger = PortalLogger
044: .getLogger(JSPNativeContainerProvider.class);
045:
046: public void init(String name, HttpServletRequest httpReq)
047: throws ProviderException {
048: super .init(name, httpReq);
049: boolean isParallel = false;
050: ContainerProviderContext cpc = getContainerProviderContext();
051: try {
052: isParallel = cpc.getBooleanProperty(getName(),
053: PARALLEL_CHANNELS_INIT);
054: } catch (ProviderContextException e) {
055: throw new ProviderException(
056: "JSPNativeContainerProvider.init(): ", e);
057: }
058:
059: if (isParallel) {
060: cpc.initProviders(httpReq, name, getSelectedChannels(), 0);
061: }
062: }
063:
064: /**
065: * Obtain the content map that is chunked according the device
066: * maxSize. The map contains the list of channel names, broken
067: * down into pages.
068: *
069: * @param httpReq The servlet request.
070: * @param httpRes The servlet response.
071: * @return Map Map of ordered content generated by providers.
072: **/
073:
074: public Map getChunkedContentMap(HttpServletRequest httpReq,
075: HttpServletResponse httpRes) throws ProviderException {
076:
077: // Get the container provider context
078: ContainerProviderContext cpc = getContainerProviderContext();
079:
080: // Get the ordered, presentable list
081: List orderedChannelList = getOrderedSelectedChannels(httpReq);
082:
083: // Get timeout
084: int timeout = 0;
085: try {
086: timeout = cpc.getIntegerProperty(getName(), TIMEOUT, 180);
087: } catch (ProviderContextException e) {
088: throw new ProviderException(
089: "JSPNativeContainerProvider.getChunkedContentMap(): ",
090: e);
091: }
092:
093: // Get all content
094: Map contentMap = new HashMap();
095: if (orderedChannelList != null) {
096: contentMap = cpc.getContent(httpReq, httpRes, getName(),
097: orderedChannelList, timeout);
098: }
099: // Chunking ...
100:
101: // Get maxSize
102: // Get the maximum channel page. Since we can't figure out the size
103: // of the current page, we will prob has to get it from a property?
104: // maxSize = client max - current page size
105: int maxSize = MAXSIZE;
106: String maxSizeStr = cpc.getClientTypeProperty(MAXSIZE_KEY);
107: try {
108: if (maxSizeStr != null) {
109: maxSize = Integer.parseInt(maxSizeStr);
110: if (maxSize < 0) {
111: maxSize = MAXSIZE;
112: }
113: }
114: } catch (NumberFormatException nfe) {
115: if (logger.isLoggable(Level.INFO)) {
116: LogRecord rec = new LogRecord(Level.INFO,
117: "PSMA_CSPWPCJL0002");
118: String[] param = { maxSizeStr };
119: rec.setThrown(nfe);
120: rec.setParameters(param);
121: rec.setLoggerName(logger.getName());
122: logger.log(rec);
123: }
124: }
125:
126: // Get Estimated front page size
127: int frontPageSize = 0;
128: try {
129: frontPageSize = cpc.getIntegerProperty(getName(),
130: FRONT_PAGE_SIZE, 1000);
131: } catch (ProviderContextException e) {
132: throw new ProviderException(
133: "JSPNativeContainerProvider.getChunkedContentMap(): ",
134: e);
135: }
136:
137: // Compute effective max size
138: if (maxSize < frontPageSize) {
139: if (logger.isLoggable(Level.WARNING)) {
140: String[] param = { "" + maxSize, "" + frontPageSize };
141: logger.log(Level.WARNING, "PSMA_CSPWPCJL0003", param);
142: }
143: } else {
144: if (logger.isLoggable(Level.FINEST)) {
145: String[] param = { "maxSize", "" + maxSize };
146: logger.log(Level.FINEST, "PSMA_CSPWPCJL0001", param);
147: param[0] = "frontPageSize";
148: param[1] = "" + frontPageSize;
149: logger.log(Level.FINEST, "PSMA_CSPWPCJL0001", param);
150: }
151: }
152:
153: // Break the content map into "pages"
154: // Go thru each content size and add to max size, put the channel names
155: // in a list
156:
157: Map chunkedChannelMap = new HashMap();
158: int numOrderedChannels = 0;
159: if (orderedChannelList != null) {
160: numOrderedChannels = orderedChannelList.size();
161: }
162: int currentSize = frontPageSize;
163: int currentPage = 1;
164: ArrayList currentPageList = new ArrayList();
165:
166: // Iterate according to this channel list order
167: for (int i = 0; i < numOrderedChannels; i++) {
168:
169: String channelName = (String) orderedChannelList.get(i);
170: Object channelContent = contentMap.get(channelName);
171: if (channelContent != null) {
172: int channelContentSize = ((StringBuffer) channelContent)
173: .length();
174:
175: // Will it put us over the edge?
176: if ((maxSize - currentSize) > channelContentSize) {
177:
178: // We are ok, add into list
179: currentPageList.add(channelName);
180:
181: if (logger.isLoggable(Level.FINEST)) {
182: String[] param = { "" + currentPage,
183: channelName };
184: logger.log(Level.FINEST, "PSMA_CSPWPCJL0004",
185: param);
186: }
187: // Increase size
188: currentSize += channelContentSize;
189:
190: } else {
191: // Adding this channel will put us over the limit,
192: // need to start a new page
193:
194: /**
195: * Fix for 4950078 DOC:NativeJSPDesktop: frontPageSize greater than maximum size not handled.
196: * When the frontpagesize is larger than maximum wmldecksize split the desktop content to mulitple pages.
197: */
198: if (currentPageList.size() > 0) {
199: // Copy into map
200: chunkedChannelMap.put(new Integer(currentPage),
201: currentPageList.clone());
202: // Turn a new page
203: currentPage++;
204: }
205:
206: // Reset the values
207: currentPageList.clear();
208: currentSize = frontPageSize;
209:
210: // Re-add the current page
211: currentPageList.add(channelName);
212: currentSize += channelContentSize;
213:
214: if (logger.isLoggable(Level.FINEST)) {
215: String[] param = { "" + currentPage,
216: channelName };
217: logger.log(Level.FINEST, "PSMA_CSPWPCJL0004",
218: param);
219: }
220:
221: }
222: }
223: }
224:
225: // Do we have remaining stuff?
226: if (currentPageList.size() > 0) {
227: // Dump everything into map
228: chunkedChannelMap.put(new Integer(currentPage),
229: currentPageList.clone());
230: }
231:
232: return chunkedChannelMap;
233: }
234:
235: }
|