001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.weather;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.plugin.Plugin;
037: import org.blojsom.plugin.PluginException;
038: import org.blojsom.plugin.weather.beans.NWSInformation;
039: import org.blojsom.plugin.weather.beans.WeatherInformation;
040: import org.blojsom.util.BlojsomUtils;
041:
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044: import java.io.IOException;
045: import java.util.Map;
046: import java.util.WeakHashMap;
047:
048: /**
049: * WeatherPlugin
050: *
051: * @author David Czarnecki
052: * @author Mark Lussier
053: * @version $Id: WeatherPlugin.java,v 1.4 2007/01/17 02:35:15 czarneckid Exp $
054: * @since blojsom 3.0
055: */
056: public class WeatherPlugin implements Plugin {
057:
058: private Log _logger = LogFactory.getLog(WeatherPlugin.class);
059:
060: /**
061: * Weather station code initialization parameter
062: */
063: public static final String PROPERTY_WEATHER_CODE = "weather-station-code";
064:
065: /**
066: * Weather provider initialization parameter
067: */
068: public static final String PROPERTY_WEATHER_PROVIDER = "weather-provider";
069:
070: /**
071: * Default weather provider from National Weather Service
072: */
073: public static final String DEFAULT_WEATHER_PROVIDER = "org.blojsom.plugin.weather.beans.NWSInformation";
074:
075: /**
076: * Default Weather Station Code - Albany International Airport - Albany, NY USA
077: */
078: public static final String DEFAULT_WEATHER_CODE = "ALB";
079:
080: private Map _weatherInformation;
081:
082: /**
083: * Initialize this plugin. This method only called when the plugin is instantiated.
084: *
085: * @throws org.blojsom.plugin.PluginException
086: * If there is an error initializing the plugin
087: */
088: public void init() throws PluginException {
089: _weatherInformation = new WeakHashMap();
090: }
091:
092: /**
093: * Read the {@link Weather} settings for a blog
094: *
095: * @param blog {@link Blog}
096: * @return {@link Weather} settings for the blog
097: */
098: protected Weather readWeatherSettingsForBlog(Blog blog) {
099: Weather weather = new Weather();
100:
101: String stationCode = blog
102: .getProperty(WeatherPlugin.PROPERTY_WEATHER_CODE);
103: if (BlojsomUtils.checkNullOrBlank(stationCode)) {
104: stationCode = WeatherPlugin.DEFAULT_WEATHER_CODE;
105: }
106:
107: String providerClass = blog
108: .getProperty(WeatherPlugin.PROPERTY_WEATHER_PROVIDER);
109: if (BlojsomUtils.checkNullOrBlank(providerClass)) {
110: providerClass = WeatherPlugin.DEFAULT_WEATHER_PROVIDER;
111: }
112:
113: weather.setStationCode(stationCode);
114: weather.setProviderClass(providerClass);
115:
116: return weather;
117: }
118:
119: /**
120: * Process the blog entries
121: *
122: * @param httpServletRequest Request
123: * @param httpServletResponse Response
124: * @param blog {@link Blog} instance
125: * @param context Context
126: * @param entries Blog entries retrieved for the particular request
127: * @return Modified set of blog entries
128: * @throws PluginException If there is an error processing the blog entries
129: */
130: public Entry[] process(HttpServletRequest httpServletRequest,
131: HttpServletResponse httpServletResponse, Blog blog,
132: Map context, Entry[] entries) throws PluginException {
133: try {
134: WeatherInformation weatherInformation = (WeatherInformation) _weatherInformation
135: .get(blog.getBlogId());
136:
137: if (weatherInformation == null) {
138: Weather weather = readWeatherSettingsForBlog(blog);
139: WeatherFetcher weatherFetcher = new WeatherFetcher();
140: WeatherInformation weatherInformationForBlog = new NWSInformation(
141: weather.getStationCode());
142:
143: weatherFetcher
144: .retrieveForecast(weatherInformationForBlog);
145:
146: _weatherInformation.put(blog.getBlogId(),
147: weatherInformationForBlog);
148: if (_logger.isDebugEnabled()) {
149: _logger.debug("Put weather information for "
150: + blog.getBlogId() + " in cache");
151: }
152: } else {
153: if (_logger.isDebugEnabled()) {
154: _logger.debug("Retrieved weather information for "
155: + blog.getBlogId() + " from cache");
156: }
157: }
158:
159: context.put(WeatherConstants.BLOJSOM_WEATHER_INFORMATION,
160: weatherInformation);
161: } catch (IOException e) {
162: if (_logger.isErrorEnabled()) {
163: _logger.error(e);
164: }
165: }
166:
167: return entries;
168: }
169:
170: /**
171: * Perform any cleanup for the plugin. Called after {@link #process}.
172: *
173: * @throws org.blojsom.plugin.PluginException
174: * If there is an error performing cleanup for this plugin
175: */
176: public void cleanup() throws PluginException {
177: }
178:
179: /**
180: * Called when BlojsomServlet is taken out of service
181: *
182: * @throws org.blojsom.plugin.PluginException
183: * If there is an error in finalizing this plugin
184: */
185: public void destroy() throws PluginException {
186: }
187: }
|