View Javadoc
1   /*
2    * Copyright (C) 2010 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 2.1 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  
20  package org.exoplatform.services.portletcache;
21  
22  import org.exoplatform.commons.cache.future.Loader;
23  import org.exoplatform.services.log.Log;
24  
25  /**
26   * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
27   * @version $Revision$
28   */
29  class PortletRenderer implements Loader<WindowKey, MarkupFragment, PortletRenderContext>
30  {
31  
32    /** . */
33    private final Log log;
34  
35    PortletRenderer(Log log)
36    {
37      this.log = log;
38    }
39  
40    public MarkupFragment retrieve(PortletRenderContext context, WindowKey key) throws Exception
41    {
42      BufferedRenderResponse bufferedResp = new BufferedRenderResponse(context.resp);
43      context.chain.doFilter(context.req, bufferedResp);
44  
45      //
46      long now = System.currentTimeMillis();
47  
48      //
49      String expirationCache = bufferedResp.getExpirationCache();
50      long expirationCacheMillis = now;
51      if (expirationCache != null)
52      {
53        try
54        {
55          int expirationCacheSec = Integer.parseInt(expirationCache);
56          if (expirationCacheSec == -1)
57          {
58            expirationCacheMillis = Long.MAX_VALUE;
59          }
60          else if (expirationCacheSec > 0)
61          {
62            expirationCacheMillis += 1000 * expirationCacheSec;
63          }
64        }
65        catch (NumberFormatException e)
66        {
67          if (log.isWarnEnabled()) {
68            log.warn("Incorrect expiration cache value " + expirationCache);
69          }
70        }
71      }
72  
73      //
74      return new MarkupFragment(expirationCacheMillis, bufferedResp.getBytes());
75    }
76  }