KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > rss > component > UIRss


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.rss.component;
6
7
8 import javax.faces.context.ExternalContext;
9 import javax.faces.context.FacesContext;
10 import javax.portlet.PortletRequest;
11 import org.exoplatform.faces.core.component.UIExoCommand;
12 import org.exoplatform.faces.core.event.ExoActionEvent;
13 import org.exoplatform.faces.core.event.ExoActionListener;
14 import org.exoplatform.services.cache.*;
15 /**
16  * Sat, Jan 03, 2004 @ 11:16
17  * @author: Tuan Nguyen
18  * @email: tuan08@users.sourceforge.net
19  * @version: $Id: UIRss.java,v 1.10 2004/09/21 20:16:22 tuan08 Exp $
20  */

21 public class UIRss extends UIExoCommand {
22   private static long TIME_TO_LIVE = 1000 * 60 * 30;
23
24   private String JavaDoc rssURL_;
25   private int itemToShow_;
26   private Channel channel_;
27   private ExoCache cache_ ;
28
29   public UIRss(CacheService cservice) throws Exception JavaDoc {
30     FacesContext context = FacesContext.getCurrentInstance();
31     PortletRequest request = (PortletRequest) context.getExternalContext().getRequest();
32     rssURL_ = request.getPreferences().getValue("url", "/exo-news.xml");
33     String JavaDoc tmp = request.getPreferences().getValue("item-to-show", "5");
34     cache_ = cservice.getCacheInstance(getClass().getName()) ;
35     channel_ = loadChannel(rssURL_);
36     try {
37       itemToShow_ = Integer.parseInt(tmp);
38     } catch (Exception JavaDoc ex) {
39       itemToShow_ = 5;
40     }
41     setId("UIRss");
42     setRendererType("RssRenderer");
43
44     addActionListener(UpdateActionListener.class, UPDATE_ACTION);
45   }
46
47   public String JavaDoc getRssURL() {
48     return rssURL_;
49   }
50
51   public int getItemToShow() {
52     return itemToShow_;
53   }
54
55   public Channel getChannel() {
56     return channel_;
57   }
58
59   private Channel loadChannel(String JavaDoc url) throws Exception JavaDoc {
60     Channel channel = (Channel) cache_.get(url);
61     long currentTime = System.currentTimeMillis();
62     if (channel != null &&
63         currentTime < (channel.getUpdateTime() + TIME_TO_LIVE)) {
64       cache_.remove(url);
65       channel = null;
66     }
67
68     if (channel == null) {
69       synchronized (cache_) {
70         String JavaDoc realUrl = null;
71         if (url.startsWith("http:") || url.startsWith("file:")) {
72           realUrl = url;
73         } else {
74           FacesContext context = FacesContext.getCurrentInstance();
75           ExternalContext econtext = context.getExternalContext();
76           realUrl = econtext.getResource(url).toString();
77         }
78         channel = Channel.parse(realUrl);
79         cache_.put(url, channel);
80       }
81     }
82     return channel;
83   }
84   
85   public static class UpdateActionListener extends ExoActionListener {
86     public void execute(ExoActionEvent event) throws Exception JavaDoc {
87       UIRss uiRss = (UIRss) event.getComponent();
88       uiRss.cache_.remove(uiRss.rssURL_);
89       uiRss.channel_ = uiRss.loadChannel(uiRss.rssURL_);
90     }
91   }
92 }
Popular Tags