1 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 21 public class UIRss extends UIExoCommand { 22 private static long TIME_TO_LIVE = 1000 * 60 * 30; 23 24 private String rssURL_; 25 private int itemToShow_; 26 private Channel channel_; 27 private ExoCache cache_ ; 28 29 public UIRss(CacheService cservice) throws Exception { 30 FacesContext context = FacesContext.getCurrentInstance(); 31 PortletRequest request = (PortletRequest) context.getExternalContext().getRequest(); 32 rssURL_ = request.getPreferences().getValue("url", "/exo-news.xml"); 33 String 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 ex) { 39 itemToShow_ = 5; 40 } 41 setId("UIRss"); 42 setRendererType("RssRenderer"); 43 44 addActionListener(UpdateActionListener.class, UPDATE_ACTION); 45 } 46 47 public String 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 url) throws Exception { 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 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 { 87 UIRss uiRss = (UIRss) event.getComponent(); 88 uiRss.cache_.remove(uiRss.rssURL_); 89 uiRss.channel_ = uiRss.loadChannel(uiRss.rssURL_); 90 } 91 } 92 } | Popular Tags |