| 1 31 package org.blojsom.plugin.delicious; 32 33 import del.icio.us.Delicious; 34 import del.icio.us.DeliciousUtils; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.blojsom.blog.Blog; 38 import org.blojsom.blog.Entry; 39 import org.blojsom.event.EventBroadcaster; 40 import org.blojsom.fetcher.Fetcher; 41 import org.blojsom.fetcher.FetcherException; 42 import org.blojsom.plugin.PluginException; 43 import org.blojsom.plugin.admin.event.EntryAddedEvent; 44 import org.blojsom.plugin.velocity.StandaloneVelocityPlugin; 45 import org.blojsom.util.BlojsomMetaDataConstants; 46 import org.blojsom.util.BlojsomUtils; 47 48 import javax.servlet.ServletConfig ; 49 import javax.servlet.http.HttpServletRequest ; 50 import javax.servlet.http.HttpServletResponse ; 51 import java.text.MessageFormat ; 52 import java.util.*; 53 54 61 public class DailyPostingPlugin extends StandaloneVelocityPlugin { 62 63 private Log _logger = LogFactory.getLog(DailyPostingPlugin.class); 64 65 private static final String DAILY_POSTING_POLL_TIME_IP = "daily-posting-poll-time"; 67 private static final int DAILY_POSTING_POLL_TIME_DEFAULT = (1000 * 60 * 60); 68 private int _pollTime = DAILY_POSTING_POLL_TIME_DEFAULT; 69 70 private static final String DAILY_POSTING_TEMPLATE = "org/blojsom/plugin/delicious/daily-posting-template.vm"; 72 73 private static final String DAILY_POSTING_USERNAME = "DAILY_POSTING_USERNAME"; 75 private static final String DAILY_POSTING_POSTS = "DAILY_POSTING_POSTS"; 76 77 private static final String DAILY_POSTING_USERNAME_IP = "daily-posting-username"; 79 private static final String DAILY_POSTING_PASSWORD_IP = "daily-posting-password"; 80 private static final String DAILY_POSTING_CATEGORY_IP = "daily-posting-category"; 81 private static final String DAILY_POSTING_HOUR_IP = "daily-posting-hour"; 82 private static final String DAILY_POSTING_TITLE_IP = "daily-posting-title"; 83 private static final String DAILY_POSTING_AUTHOR_IP = "daily-posting-author"; 84 private static final String DAILY_POSTING_TITLE_DEFAULT = "del.icio.us links for {0}"; 85 86 private boolean _finished = false; 87 private DeliciousChecker _checker; 88 89 private String _proxyHost = null; 90 private String _proxyPort = null; 91 92 private Fetcher _fetcher; 93 private ServletConfig _servletConfig; 94 private EventBroadcaster _eventBroadcaster; 95 96 99 public DailyPostingPlugin() { 100 } 101 102 107 public void setFetcher(Fetcher fetcher) { 108 _fetcher = fetcher; 109 } 110 111 116 public void setServletConfig(ServletConfig servletConfig) { 117 _servletConfig = servletConfig; 118 } 119 120 125 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 126 _eventBroadcaster = eventBroadcaster; 127 } 128 129 135 public void init() throws PluginException { 136 super.init(); 137 138 String pollTime = _servletConfig.getInitParameter(DAILY_POSTING_POLL_TIME_IP); 139 if (BlojsomUtils.checkNullOrBlank(pollTime)) { 140 _pollTime = DAILY_POSTING_POLL_TIME_DEFAULT; 141 } else { 142 try { 143 _pollTime = Integer.parseInt(pollTime); 144 if (_pollTime < DAILY_POSTING_POLL_TIME_DEFAULT) { 145 _pollTime = DAILY_POSTING_POLL_TIME_DEFAULT; 146 } 147 } catch (NumberFormatException e) { 148 _pollTime = DAILY_POSTING_POLL_TIME_DEFAULT; 149 } 150 } 151 152 try { 153 _proxyHost = System.getProperty("http.proxyHost"); 154 _proxyPort = System.getProperty("http.proxyPort"); 155 } catch (Exception e) { 156 _logger.error(e); 157 } 158 159 _checker = new DeliciousChecker(); 160 _checker.setDaemon(true); 161 _checker.start(); 162 } 163 164 175 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 176 return entries; 177 } 178 179 185 public void cleanup() throws PluginException { 186 } 187 188 194 public void destroy() throws PluginException { 195 _finished = true; 196 } 197 198 201 private class DeliciousChecker extends Thread { 202 203 213 public DeliciousChecker() { 214 super(); 215 } 216 217 231 public void run() { 232 try { 233 while (!_finished) { 234 String [] blogIDs = _fetcher.loadBlogIDs(); 235 236 Blog blog; 237 String blogID; 238 239 for (int i = 0; i < blogIDs.length; i++) { 240 blogID = blogIDs[i]; 241 242 try { 243 blog = _fetcher.loadBlog(blogID); 244 245 String postingCategory = blog.getProperty(DAILY_POSTING_CATEGORY_IP); 246 String deliciousUsername = blog.getProperty(DAILY_POSTING_USERNAME_IP); 247 String deliciousPassword = blog.getProperty(DAILY_POSTING_PASSWORD_IP); 248 String postingHour = blog.getProperty(DAILY_POSTING_HOUR_IP); 249 String postTitle = blog.getProperty(DAILY_POSTING_TITLE_IP); 250 String postingAuthor = blog.getProperty(DAILY_POSTING_AUTHOR_IP); 251 252 if (BlojsomUtils.checkNullOrBlank(postTitle)) { 253 postTitle = DAILY_POSTING_TITLE_DEFAULT; 254 } 255 256 if (BlojsomUtils.checkNullOrBlank(postingCategory) || 257 BlojsomUtils.checkNullOrBlank(deliciousPassword) || 258 BlojsomUtils.checkNullOrBlank(deliciousUsername) || 259 BlojsomUtils.checkNullOrBlank(postingHour)) { 260 } else { 261 Date now = new Date(); 262 Calendar calendar = Calendar.getInstance(); 263 calendar.setTime(now); 264 int currentHour = calendar.get(Calendar.HOUR_OF_DAY); 265 266 try { 267 int hourToPost = Integer.parseInt(postingHour); 268 if (hourToPost == currentHour) { 269 Delicious delicious = new Delicious(deliciousUsername, deliciousPassword); 270 if (_proxyHost != null && _proxyPort != null) { 271 delicious.setProxyConfiguration(_proxyHost, Integer.parseInt(_proxyPort)); 272 } 273 274 List posts = delicious.getPostsForDate(null, now); 275 if (posts.size() > 0) { 276 HashMap deliciousContext = new HashMap(); 277 deliciousContext.put(DAILY_POSTING_USERNAME, deliciousUsername); 278 deliciousContext.put(DAILY_POSTING_POSTS, posts); 279 280 String renderedLinkTemplate = mergeTemplate(DAILY_POSTING_TEMPLATE, blog, deliciousContext); 281 282 String nowAsString = DeliciousUtils.getDeliciousDate(now); 284 postingCategory = BlojsomUtils.normalize(postingCategory); 285 286 Entry entry; 287 entry = _fetcher.newEntry(); 288 289 String title = MessageFormat.format(postTitle, new Object []{nowAsString, deliciousUsername}); 290 entry.setBlogId(blog.getId()); 291 entry.setTitle(title); 292 entry.setDescription(renderedLinkTemplate); 293 entry.setDate(new Date()); 294 entry.setStatus(BlojsomMetaDataConstants.NEW_STATUS); 295 entry.setBlogCategoryId(Integer.valueOf(postingCategory)); 296 try { 297 if (_fetcher.loadUser(blog, postingAuthor) != null) { 298 entry.setAuthor(postingAuthor); 299 } 300 } catch (FetcherException e) { 301 } 302 303 _fetcher.saveEntry(blog, entry); 304 305 _eventBroadcaster.broadcastEvent(new EntryAddedEvent(this, new Date(), entry, blog)); 306 if (_logger.isDebugEnabled()) { 307 _logger.debug("Posted del.icio.us links for: " + blog.getBlogId() + " using: " + deliciousUsername); 308 } 309 } 310 } 311 } catch (NumberFormatException e) { 312 } 313 } 314 } catch (FetcherException e) { 315 if (_logger.isErrorEnabled()) { 316 _logger.error(e); 317 } 318 } 319 } 320 321 sleep(_pollTime); 322 } 323 } catch (InterruptedException e) { 324 if (_logger.isErrorEnabled()) { 325 _logger.error(e); 326 } 327 } catch (FetcherException e) { 328 if (_logger.isErrorEnabled()) { 329 _logger.error(e); 330 } 331 } 332 } 333 } 334 } | Popular Tags |