KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > weather > WeatherPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

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 JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.WeakHashMap JavaDoc;
48
49 /**
50  * WeatherPlugin
51  *
52  * @author David Czarnecki
53  * @author Mark Lussier
54  * @version $Id: WeatherPlugin.java,v 1.3 2006/03/26 18:57:29 czarneckid Exp $
55  * @since blojsom 3.0
56  */

57 public class WeatherPlugin implements Plugin {
58
59     private Log _logger = LogFactory.getLog(WeatherPlugin.class);
60
61     /**
62      * Weather station code initialization parameter
63      */

64     public static final String JavaDoc PROPERTY_WEATHER_CODE = "weather-station-code";
65
66     /**
67      * Weather provider initialization parameter
68      */

69     public static final String JavaDoc PROPERTY_WEATHER_PROVIDER = "weather-provider";
70
71     /**
72      * Default weather provider from National Weather Service
73      */

74     public static final String JavaDoc DEFAULT_WEATHER_PROVIDER = "org.blojsom.plugin.weather.beans.NWSInformation";
75
76     /**
77      * Default Weather Station Code - Albany International Airport - Albany, NY USA
78      */

79     public static final String JavaDoc DEFAULT_WEATHER_CODE = "ALB";
80
81     private Map JavaDoc _weatherInformation;
82
83     /**
84      * Initialize this plugin. This method only called when the plugin is instantiated.
85      *
86      * @throws org.blojsom.plugin.PluginException
87      * If there is an error initializing the plugin
88      */

89     public void init() throws PluginException {
90         _weatherInformation = new WeakHashMap JavaDoc();
91     }
92
93     /**
94      * Read the {@link Weather} settings for a blog
95      *
96      * @param blog {@link Blog}
97      * @return {@link Weather} settings for the blog
98      */

99     protected Weather readWeatherSettingsForBlog(Blog blog) {
100         Weather weather = new Weather();
101
102         String JavaDoc stationCode = blog.getProperty(WeatherPlugin.PROPERTY_WEATHER_CODE);
103         if (BlojsomUtils.checkNullOrBlank(stationCode)) {
104             stationCode = WeatherPlugin.DEFAULT_WEATHER_CODE;
105         }
106
107         String JavaDoc 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     /**
119      * Process the blog entries
120      *
121      * @param httpServletRequest Request
122      * @param httpServletResponse Response
123      * @param blog {@link Blog} instance
124      * @param context Context
125      * @param entries Blog entries retrieved for the particular request
126      * @return Modified set of blog entries
127      * @throws PluginException If there is an error processing the blog entries
128      */

129     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc 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 JavaDoc e) {
152             if (_logger.isErrorEnabled()) {
153                 _logger.error(e);
154             }
155         }
156
157         return entries;
158     }
159
160     /**
161      * Perform any cleanup for the plugin. Called after {@link #process}.
162      *
163      * @throws org.blojsom.plugin.PluginException
164      * If there is an error performing cleanup for this plugin
165      */

166     public void cleanup() throws PluginException {
167     }
168
169     /**
170      * Called when BlojsomServlet is taken out of service
171      *
172      * @throws org.blojsom.plugin.PluginException
173      * If there is an error in finalizing this plugin
174      */

175     public void destroy() throws PluginException {
176     }
177 }
178
Popular Tags