| 1 31 package org.blojsom.plugin.weather; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Entry; 37 import org.blojsom.plugin.Plugin; 38 import org.blojsom.plugin.PluginException; 39 import org.blojsom.plugin.weather.beans.NWSInformation; 40 import org.blojsom.plugin.weather.beans.WeatherInformation; 41 import org.blojsom.util.BlojsomUtils; 42 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.io.IOException ; 46 import java.util.Map ; 47 import java.util.WeakHashMap ; 48 49 57 public class WeatherPlugin implements Plugin { 58 59 private Log _logger = LogFactory.getLog(WeatherPlugin.class); 60 61 64 public static final String PROPERTY_WEATHER_CODE = "weather-station-code"; 65 66 69 public static final String PROPERTY_WEATHER_PROVIDER = "weather-provider"; 70 71 74 public static final String DEFAULT_WEATHER_PROVIDER = "org.blojsom.plugin.weather.beans.NWSInformation"; 75 76 79 public static final String DEFAULT_WEATHER_CODE = "ALB"; 80 81 private Map _weatherInformation; 82 83 89 public void init() throws PluginException { 90 _weatherInformation = new WeakHashMap (); 91 } 92 93 99 protected Weather readWeatherSettingsForBlog(Blog blog) { 100 Weather weather = new Weather(); 101 102 String stationCode = blog.getProperty(WeatherPlugin.PROPERTY_WEATHER_CODE); 103 if (BlojsomUtils.checkNullOrBlank(stationCode)) { 104 stationCode = WeatherPlugin.DEFAULT_WEATHER_CODE; 105 } 106 107 String providerClass = blog.getProperty(WeatherPlugin.PROPERTY_WEATHER_PROVIDER); 108 if (BlojsomUtils.checkNullOrBlank(providerClass)) { 109 providerClass = WeatherPlugin.DEFAULT_WEATHER_PROVIDER; 110 } 111 112 weather.setStationCode(stationCode); 113 weather.setProviderClass(providerClass); 114 115 return weather; 116 } 117 118 129 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 130 try { 131 WeatherInformation weatherInformation = (WeatherInformation) _weatherInformation.get(blog.getBlogId()); 132 133 if (weatherInformation == null) { 134 Weather weather = readWeatherSettingsForBlog(blog); 135 WeatherFetcher weatherFetcher = new WeatherFetcher(); 136 WeatherInformation weatherInformationForBlog = new NWSInformation(weather.getStationCode()); 137 138 weatherFetcher.retrieveForecast(weatherInformationForBlog); 139 140 _weatherInformation.put(blog.getBlogId(), weatherInformationForBlog); 141 if (_logger.isDebugEnabled()) { 142 _logger.debug("Put weather information for " + blog.getBlogId() + " in cache"); 143 } 144 } else { 145 if (_logger.isDebugEnabled()) { 146 _logger.debug("Retrieved weather information for " + blog.getBlogId() + " from cache"); 147 } 148 } 149 150 context.put(WeatherConstants.BLOJSOM_WEATHER_INFORMATION, weatherInformation); 151 } catch (IOException e) { 152 if (_logger.isErrorEnabled()) { 153 _logger.error(e); 154 } 155 } 156 157 return entries; 158 } 159 160 166 public void cleanup() throws PluginException { 167 } 168 169 175 public void destroy() throws PluginException { 176 } 177 } 178 | Popular Tags |