View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.aggregator.impl;
18  
19  import java.io.CharArrayWriter;
20  import java.io.PrintWriter;
21  
22  import org.apache.jetspeed.aggregator.PortletContent;
23  import org.apache.jetspeed.aggregator.PortletRenderer;
24  import org.apache.jetspeed.cache.ContentCacheKey;
25  
26  
27  public class PortletContentImpl implements PortletContent
28  {
29      private CharArrayWriter cw;
30      private PrintWriter writer;
31      private boolean complete = false;
32      private ContentCacheKey cacheKey;
33      private int expiration = 0;
34      private String title;
35      private PortletRenderer renderer = null;
36      
37      PortletContentImpl()
38      {
39          init();
40      }
41      
42      PortletContentImpl(PortletRenderer renderer, ContentCacheKey cacheKey, int expiration, String title)
43      {
44          this.renderer = renderer;
45          this.cacheKey = cacheKey;
46          this.expiration = expiration;
47          this.title = title;
48          init();
49      }
50  
51      PortletContentImpl(PortletRenderer renderer, ContentCacheKey cacheKey, int expiration)
52      {
53          this(renderer, cacheKey, expiration,"no title");
54      }
55     
56      public PrintWriter getWriter()
57      {
58          return writer;
59      }
60  
61      public void init()
62      {
63          cw = new CharArrayWriter();
64          writer = new PrintWriter(cw);
65      }
66  
67      public void release()
68      {
69          writer.close();
70      }
71  
72      public String toString()
73      {
74          writer.flush();
75          return cw.toString();
76      }
77  
78      public void writeTo( java.io.Writer out ) throws java.io.IOException
79      {
80          writer.flush();
81          cw.writeTo(out);
82      }
83  
84      public char[] toCharArray()
85      {
86          writer.flush();
87          return cw.toCharArray();
88      }
89  
90      public boolean isComplete()
91      {
92          return complete;
93      }
94  
95      void setComplete(boolean state, boolean notify)
96      {
97          if (renderer != null && notify)
98              renderer.notifyContentComplete(this);
99          this.complete = state;
100     }
101     
102     public String getContent()
103     {
104         return toString();
105     }
106     /***
107      * <p>
108      * complete
109      * </p>
110      *
111      * @see org.apache.jetspeed.aggregator.PortletContent#complete()
112      * 
113      */
114     public void complete()
115     {
116        setComplete(true, true);
117     }
118     
119     // error case, don't notify 
120     public void completeWithError()
121     {
122         setComplete(true, false);
123     }
124     
125     public ContentCacheKey getCacheKey()
126     {
127         return cacheKey;
128     }
129    
130     public int getExpiration()
131     {
132         return expiration;
133     }
134     
135     public void setExpiration(int expiration)
136     {
137         this.expiration = expiration;
138     }
139     
140     public String getTitle()
141     {
142         return title;
143     }
144     
145     public void setTitle(String title)
146     {
147         this.title = title;
148     }
149         
150 }