001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.aggregator.impl;
018:
019: import java.io.CharArrayWriter;
020: import java.io.PrintWriter;
021:
022: import org.apache.jetspeed.aggregator.PortletContent;
023: import org.apache.jetspeed.aggregator.PortletRenderer;
024: import org.apache.jetspeed.cache.ContentCacheKey;
025:
026: public class PortletContentImpl implements PortletContent {
027: private CharArrayWriter cw;
028: private PrintWriter writer;
029: private boolean complete = false;
030: private ContentCacheKey cacheKey;
031: private int expiration = 0;
032: private String title;
033: private PortletRenderer renderer = null;
034:
035: PortletContentImpl() {
036: init();
037: }
038:
039: PortletContentImpl(PortletRenderer renderer,
040: ContentCacheKey cacheKey, int expiration, String title) {
041: this .renderer = renderer;
042: this .cacheKey = cacheKey;
043: this .expiration = expiration;
044: this .title = title;
045: init();
046: }
047:
048: PortletContentImpl(PortletRenderer renderer,
049: ContentCacheKey cacheKey, int expiration) {
050: this (renderer, cacheKey, expiration, "no title");
051: }
052:
053: public PrintWriter getWriter() {
054: return writer;
055: }
056:
057: public void init() {
058: cw = new CharArrayWriter();
059: writer = new PrintWriter(cw);
060: }
061:
062: public void release() {
063: writer.close();
064: }
065:
066: public String toString() {
067: writer.flush();
068: return cw.toString();
069: }
070:
071: public void writeTo(java.io.Writer out) throws java.io.IOException {
072: writer.flush();
073: cw.writeTo(out);
074: }
075:
076: public char[] toCharArray() {
077: writer.flush();
078: return cw.toCharArray();
079: }
080:
081: public boolean isComplete() {
082: return complete;
083: }
084:
085: void setComplete(boolean state, boolean notify) {
086: if (renderer != null && notify)
087: renderer.notifyContentComplete(this );
088: this .complete = state;
089: }
090:
091: public String getContent() {
092: return toString();
093: }
094:
095: /**
096: * <p>
097: * complete
098: * </p>
099: *
100: * @see org.apache.jetspeed.aggregator.PortletContent#complete()
101: *
102: */
103: public void complete() {
104: setComplete(true, true);
105: }
106:
107: // error case, don't notify
108: public void completeWithError() {
109: setComplete(true, false);
110: }
111:
112: public ContentCacheKey getCacheKey() {
113: return cacheKey;
114: }
115:
116: public int getExpiration() {
117: return expiration;
118: }
119:
120: public void setExpiration(int expiration) {
121: this .expiration = expiration;
122: }
123:
124: public String getTitle() {
125: return title;
126: }
127:
128: public void setTitle(String title) {
129: this.title = title;
130: }
131:
132: }
|