File Source: NewsfeedCache.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.rendering.velocity.deprecated;
    20  
    21  import com.sun.syndication.feed.synd.SyndFeed;
    22  import com.sun.syndication.io.SyndFeedInput;
    23  import java.io.InputStreamReader;
    24  import java.io.UnsupportedEncodingException;
    25  import java.net.URL;
    26  import java.net.URLConnection;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.roller.weblogger.config.WebloggerConfig;
    30  import org.apache.roller.weblogger.util.LRUCache2;
    31  
    32  
    33  /**
    34   * Returns parsed RSS feed by pulling one from a cache or by retrieving and
    35   * parging the specified feed using the Flock RSS parser.
    36   *
    37   * TODO: use PlanetRoller to implement NewsfeedCache instead.
    38   */
    39  public class NewsfeedCache {
    40      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.velocity.deprecated.NewsfeedCache__static_init
              * 
              *  Postconditions:
              *    instance == null
              *    init'ed(log)
              */
    41      private static Log log = LogFactory.getLog(NewsfeedCache.class);
    42      
    43      /** Static singleton * */
    44      private static NewsfeedCache instance = null;
    45      
    46      /** Instance vars * */
    47      private boolean aggregator_enabled = true;
    48      private boolean aggregator_cache_enabled = true;
    49      private int     aggregator_cache_timeout = 14400;
    50      
    51      /** LRU cache */
    52      LRUCache2 mCache = null;
    53      
    54      
    55      /** Constructor */
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.rendering.velocity.deprecated.NewsfeedCache()
              * 
              *  Preconditions:
              *    (soft) log != null
              * 
              *  Postconditions:
              *    this.aggregator_cache_enabled == 1
              *    init'ed(this.aggregator_cache_timeout)
              *    this.aggregator_enabled == 1
              *    this.mCache == &new LRUCache2(NewsfeedCache#2)
              *    new LRUCache2(NewsfeedCache#2) num objects == 1
              * 
              *  Test Vectors:
              *    java.lang.String:equalsIgnoreCase(...)@62: {0}, {1}
              *    java.lang.String:equalsIgnoreCase(...)@65: {0}, {1}
              *    org.apache.roller.weblogger.config.WebloggerConfig:getProperty(...)@60: Addr_Set{null}, Inverse{null}
              */
    56      private NewsfeedCache() {
    57          // lookup the props we need
    58          String enabled = WebloggerConfig.getProperty("aggregator.enabled");
    59          String usecache = WebloggerConfig.getProperty("aggregator.cache.enabled");
    60          String cachetime = WebloggerConfig.getProperty("aggregator.cache.timeout");
    61          
    62          if("true".equalsIgnoreCase(enabled))
    63              this.aggregator_enabled = true;
    64          
    65          if("true".equalsIgnoreCase(usecache))
    66              this.aggregator_cache_enabled = true;
    67          
    68          try {
    69              if (cachetime != null) {
    70                  this.aggregator_cache_timeout = Integer.parseInt(cachetime);
    71              }
    72          } catch(NumberFormatException e) { 
    73              log.error("ERROR 'aggregator.cache.timeout'not a valid integer: " + cachetime);
    74          }
    75          
    76          // finally ... create the cache
    77          this.mCache = new LRUCache2(100, 1000 * this.aggregator_cache_timeout);
    78      }
    79      
    80      
    81      /** static singleton retriever */
    82      public static NewsfeedCache getInstance() {
                 /* 
    P/P           *  Method: NewsfeedCache getInstance()
                  * 
                  *  Preconditions:
                  *    init'ed(instance)
                  *    (soft) log != null
                  * 
                  *  Postconditions:
                  *    instance == One-of{old instance, &new NewsfeedCache(getInstance#1)}
                  *    instance != null
                  *    return_value == One-of{old instance, &new NewsfeedCache(getInstance#1)}
                  *    return_value != null
                  *    new LRUCache2(NewsfeedCache#2) num objects <= 1
                  *    new NewsfeedCache(getInstance#1) num objects <= 1
                  *    new NewsfeedCache(getInstance#1).aggregator_cache_enabled == 1
                  *    init'ed(new NewsfeedCache(getInstance#1).aggregator_cache_timeout)
                  *    new NewsfeedCache(getInstance#1).aggregator_enabled == 1
                  *    new NewsfeedCache(getInstance#1).mCache == &new LRUCache2(NewsfeedCache#2)
                  * 
                  *  Test Vectors:
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@85: {0}, {1}
                  */
    83          synchronized (NewsfeedCache.class) {
    84              if (instance == null) {
    85                  if (log.isDebugEnabled()) {
    86                      log.debug("Instantiating new NewsfeedCache");
    87                  }
    88                  instance = new NewsfeedCache();
    89              }
    90          }
    91          return instance;
    92      }
    93      
    94      
    95      /**
    96       * Returns a Channel object for the supplied RSS newsfeed URL.
    97       *
    98       * @param feedUrl RSS newsfeed URL.
    99       * @return FlockFeedI for specified RSS newsfeed URL.
   100       */
   101      public SyndFeed getChannel(String feedUrl) {
   102          
                 /* 
    P/P           *  Method: SyndFeed getChannel(String)
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  *    (soft) init'ed(this.aggregator_cache_enabled)
                  *    (soft) init'ed(this.aggregator_enabled)
                  *    (soft) this.mCache != null
                  * 
                  *  Presumptions:
                  *    java.lang.String:indexOf(...)@147 + java.lang.String:length(...)@151 in -231..232-1
                  *    java.net.URL:openConnection(...)@141 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    this.aggregator_cache_enabled: {0}, {1}
                  *    this.aggregator_enabled: {1}, {0}
                  *    java.lang.String:indexOf(...)@147: {-231..-1}, {0..232-1}
                  *    java.lang.String:indexOf(...)@149: {-231..-2, 0..232-1}, {-1}
                  *    java.net.URLConnection:getContentType(...)@143: Addr_Set{null}, Inverse{null}
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@111: {0}, {1}
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@117: {0}, {1}
                  *    org.apache.commons.logging.Log:isDebugEnabled(...)@136: {0}, {1}
                  *    org.apache.roller.weblogger.util.LRUCache2:get(...)@116: Inverse{null}, Addr_Set{null}
                  */
   103          SyndFeed feed = null;
   104          try {
   105              // If aggregator has been disable return null
   106              if (!aggregator_enabled) {
   107                  return null;
   108              }
   109              
   110              if (aggregator_cache_enabled) {
   111                  if (log.isDebugEnabled()) {
   112                      log.debug("Newsfeed: use Cache for " + feedUrl);
   113                  }
   114                  
   115                  // Get pre-parsed feed from the cache
   116                  feed = (SyndFeed) mCache.get(feedUrl);
   117                  if (log.isDebugEnabled()) {
   118                      log.debug("Newsfeed: got from Cache");
   119                  }
   120                  
   121                  if (feed == null) {
   122                      try {
   123                          // Parse the feed
   124                          SyndFeedInput feedInput = new SyndFeedInput();
   125                          feed = feedInput.build(new InputStreamReader(
   126                                  new URL(feedUrl).openStream()));
   127                      } catch (Exception e1) {
   128                          log.info("Error parsing RSS: " + feedUrl);
   129                      }
   130                  }
   131                  // Store parsed feed in the cache
   132                  mCache.put(feedUrl, feed);
   133                  log.debug("Newsfeed: not in Cache");
   134                  
   135              } else {
   136                  if (log.isDebugEnabled()) {
   137                      log.debug("Newsfeed: not using Cache for " + feedUrl);
   138                  }
   139                  try {
   140                      // charset fix from Jason Rumney (see ROL-766)
   141                      URLConnection connection = new URL(feedUrl).openConnection();
   142                      connection.connect();
   143                      String contentType = connection.getContentType();
   144                      // Default charset to UTF-8, since we are expecting XML
   145                      String charset = "UTF-8";
   146                      if (contentType != null) {
   147                          int charsetStart = contentType.indexOf("charset=");
   148                          if (charsetStart >= 0) {
   149                              int charsetEnd = contentType.indexOf(";", charsetStart);
   150                              if (charsetEnd == -1) charsetEnd = contentType.length();
   151                              charsetStart += "charset=".length();
   152                              charset = contentType.substring(charsetStart, charsetEnd);
   153                              // Check that charset is recognized by Java
   154                              try {
+  155                                  byte[] test = "test".getBytes(charset);
   156                              } catch (UnsupportedEncodingException codingEx) {
   157                                  // default to UTF-8
   158                                  charset = "UTF-8";
   159                              }
   160                          }
   161                      }
   162                      // Parse the feed
   163                      SyndFeedInput feedInput = new SyndFeedInput();
   164                      feed = feedInput.build(new InputStreamReader(
   165                              connection.getInputStream(), charset));
   166                  } catch (Exception e1) {
   167                      log.info("Error parsing RSS: " + feedUrl);
   168                  }
   169              }
   170              
   171          } catch (Exception ioe) {
   172              if (log.isDebugEnabled()) {
   173                  log.debug("Newsfeed: Unexpected exception", ioe);
   174              }
   175          }
   176          
   177          return feed;
   178      }
   179      
   180  }








SofCheck Inspector Build Version : 2.18479
NewsfeedCache.java 2009-Jan-02 14:25:30
NewsfeedCache.class 2009-Sep-04 03:12:45