📄 blog.java
字号:
/* * Copyright (c) 2003-2006, Simon Brown * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * - Neither the name of Pebble nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */package net.sourceforge.pebble.domain;import net.sf.ehcache.Cache;import net.sourceforge.pebble.Constants;import net.sourceforge.pebble.PebbleContext;import net.sourceforge.pebble.PluginProperties;import net.sourceforge.pebble.Configuration;import net.sourceforge.pebble.aggregator.NewsFeedEntry;import net.sourceforge.pebble.aggregator.NewsFeedCache;import net.sourceforge.pebble.api.confirmation.CommentConfirmationStrategy;import net.sourceforge.pebble.api.confirmation.TrackBackConfirmationStrategy;import net.sourceforge.pebble.api.decorator.ContentDecorator;import net.sourceforge.pebble.api.event.EventDispatcher;import net.sourceforge.pebble.api.event.blog.BlogEvent;import net.sourceforge.pebble.api.event.blog.BlogListener;import net.sourceforge.pebble.api.event.blogentry.BlogEntryListener;import net.sourceforge.pebble.api.event.comment.CommentListener;import net.sourceforge.pebble.api.event.trackback.TrackBackListener;import net.sourceforge.pebble.api.permalink.PermalinkProvider;import net.sourceforge.pebble.confirmation.DefaultConfirmationStrategy;import net.sourceforge.pebble.dao.CategoryDAO;import net.sourceforge.pebble.dao.DAOFactory;import net.sourceforge.pebble.dao.PersistenceException;import net.sourceforge.pebble.decorator.ContentDecoratorChain;import net.sourceforge.pebble.decorator.HideUnapprovedResponsesDecorator;import net.sourceforge.pebble.event.DefaultEventDispatcher;import net.sourceforge.pebble.event.EventListenerList;import net.sourceforge.pebble.event.AuditListener;import net.sourceforge.pebble.event.blogentry.EmailSubscriptionListener;import net.sourceforge.pebble.event.blog.CacheListener;import net.sourceforge.pebble.index.*;import net.sourceforge.pebble.logging.AbstractLogger;import net.sourceforge.pebble.logging.CombinedLogFormatLogger;import net.sourceforge.pebble.permalink.DefaultPermalinkProvider;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import javax.servlet.http.HttpServletRequest;import java.io.*;import java.lang.reflect.Constructor;import java.util.*;/** * Represents a blog. * * @author Simon Brown */public class Blog extends AbstractBlog { private static final Log log = LogFactory.getLog(Blog.class); public static final String ABOUT_KEY = "about"; public static final String EMAIL_KEY = "email"; public static final String BLOG_OWNERS_KEY = "blogOwners"; public static final String BLOG_PUBLISHERS_KEY = "blogPublishers"; public static final String BLOG_CONTRIBUTORS_KEY = "blogContributors"; public static final String BLOG_READERS_KEY = "blogReaders"; public static final String PRIVATE_KEY = "private"; public static final String LUCENE_ANALYZER_KEY = "luceneAnalyzer"; public static final String CONTENT_DECORATORS_KEY = "decorators"; public static final String BLOG_LISTENERS_KEY = "blogListeners"; public static final String BLOG_ENTRY_LISTENERS_KEY = "blogEntryListeners"; public static final String COMMENT_LISTENERS_KEY = "commentListeners"; public static final String TRACKBACK_LISTENERS_KEY = "trackBackListeners"; public static final String EVENT_DISPATCHER_KEY = "eventDispatcher"; public static final String LOGGER_KEY = "logger"; public static final String PERMALINK_PROVIDER_KEY = "permalinkProviderName"; public static final String COMMENT_CONFIRMATION_STRATEGY_KEY = "commentConfirmationStrategy"; public static final String TRACKBACK_CONFIRMATION_STRATEGY_KEY = "trackBackConfirmationStrategy"; public static final String RICH_TEXT_EDITOR_FOR_COMMENTS_ENABLED_KEY = "richTextEditorForCommentsEnabled"; public static final String HOME_PAGE_KEY = "homePage"; /** the ID of this blog */ private String id = "default"; /** the collection of Year instance that this root blog is managing */ private List<Year> years; /** the root category associated with this blog */ private Category rootCategory; /** the referer filter associated with this blog */ private RefererFilterManager refererFilterManager; /** the editable theme belonging to this blog */ private Theme editableTheme; /** the permalink provider in use */ private PermalinkProvider permalinkProvider; /** the log used to log referers, requests, etc */ private AbstractLogger logger; /** the decorator chain associated with this blog */ private ContentDecoratorChain decoratorChain; private CommentConfirmationStrategy commentConfirmationStrategy; private TrackBackConfirmationStrategy trackBackConfirmationStrategy; /** the event dispatcher */ private EventDispatcher eventDispatcher; /** the event listener list */ private EventListenerList eventListenerList; /** the plugin properties */ private PluginProperties pluginProperties; private SearchIndex searchIndex; private BlogEntryIndex blogEntryIndex; private ResponseIndex responseIndex; private TagIndex tagIndex; private CategoryIndex categoryIndex; private AuthorIndex authorIndex; private StaticPageIndex staticPageIndex; private Cache blogEntryCache; private EmailSubscriptionList emailSubscriptionList; /** * Creates a new Blog instance, based at the specified location. * * @param root an absolute path pointing to the root directory of the blog */ public Blog(String root) { super(root); } protected void init() { super.init(); try { Class c = Class.forName(getPermalinkProviderName()); setPermalinkProvider((PermalinkProvider)c.newInstance()); } catch (Exception e) { error("Could not load permalink provider \"" + getPermalinkProviderName() + "\""); e.printStackTrace(); setPermalinkProvider(new DefaultPermalinkProvider()); } // load categories try { DAOFactory factory = DAOFactory.getConfiguredFactory(); CategoryDAO dao = factory.getCategoryDAO(); rootCategory = dao.getCategories(this); } catch (PersistenceException pe) { pe.printStackTrace(); } refererFilterManager = new RefererFilterManager(this); pluginProperties = new PluginProperties(this); years = new ArrayList(); // create the various indexes for this blog searchIndex = new SearchIndex(this); blogEntryIndex = new BlogEntryIndex(this); responseIndex = new ResponseIndex(this); tagIndex = new TagIndex(this); categoryIndex = new CategoryIndex(this); authorIndex = new AuthorIndex(this); staticPageIndex = new StaticPageIndex(this); decoratorChain = new ContentDecoratorChain(this); try { Class c = Class.forName(getCommentConfirmationStrategyName()); commentConfirmationStrategy = (CommentConfirmationStrategy)c.newInstance(); } catch (Exception e) { error("Could not load comment confirmation strategy \"" + getCommentConfirmationStrategyName() + "\""); e.printStackTrace(); commentConfirmationStrategy = new DefaultConfirmationStrategy(); } try { Class c = Class.forName(getTrackBackConfirmationStrategyName()); trackBackConfirmationStrategy = (TrackBackConfirmationStrategy)c.newInstance(); } catch (Exception e) { error("Could not load TrackBack confirmation strategy \"" + getTrackBackConfirmationStrategyName() + "\""); e.printStackTrace(); trackBackConfirmationStrategy = new DefaultConfirmationStrategy(); } emailSubscriptionList = new EmailSubscriptionList(this); initLogger(); initEventDispatcher(); initBlogListeners(); initBlogEntryListeners(); initCommentListeners(); initTrackBackListeners(); initDecorators(); } /** * Initialises the logger for this blog. */ private void initLogger() { log.debug("Initializing logger"); try { Class c = Class.forName(getLoggerName()); Constructor cons = c.getConstructor(new Class[] {Blog.class}); this.logger = (AbstractLogger)cons.newInstance(new Object[] {this}); } catch (Exception e) { error("Could not start logger \"" + getLoggerName() + "\""); e.printStackTrace(); this.logger = new CombinedLogFormatLogger(this); } } /** * Initialises the event dispatcher for this blog. */ private void initEventDispatcher() { log.debug("Initializing event dispatcher"); eventListenerList = new EventListenerList(); try { Class c = Class.forName(getEventDispatcherName()); this.eventDispatcher = (EventDispatcher)c.newInstance(); } catch (Exception e) { e.printStackTrace(); this.eventDispatcher = new DefaultEventDispatcher(); } eventDispatcher.setEventListenerList(eventListenerList); } /** * Initialises any blog listeners configured for this blog. */ private void initBlogListeners() { log.debug("Registering blog listeners"); String classNames = getBlogListeners(); if (classNames != null && classNames.length() > 0) { String classes[] = classNames.split("\\s+"); for (int i = 0; i < classes.length; i++) { if (!classes[i].startsWith("#")) { try { Class c = Class.forName(classes[i].trim()); BlogListener listener = (BlogListener)c.newInstance(); eventListenerList.addBlogListener(listener); } catch (Exception e) { error("Could not start blog listener \"" + classes[i] + "\""); log.error("Blog listener " + classes[i] + " could not be registered", e); } } } } eventListenerList.addBlogListener(new CacheListener()); } /** * Initialises any blog entry listeners configured for this blog. */ private void initBlogEntryListeners() { log.debug("Registering blog entry listeners"); String classNames = getBlogEntryListeners(); if (classNames != null && classNames.length() > 0) { String classes[] = classNames.split("\\s+"); for (int i = 0; i < classes.length; i++) { if (!classes[i].startsWith("#")) { try { Class c = Class.forName(classes[i].trim()); BlogEntryListener listener = (BlogEntryListener)c.newInstance(); eventListenerList.addBlogEntryListener(listener); } catch (Exception e) { error("Could not start blog entry listener \"" + classes[i] + "\""); log.error("Blog entry listener " + classes[i] + " could not be registered", e); } } } } // these are required to keep the various indexes up to date eventListenerList.addBlogEntryListener(new BlogEntryIndexListener()); eventListenerList.addBlogEntryListener(new TagIndexListener()); eventListenerList.addBlogEntryListener(new CategoryIndexListener()); eventListenerList.addBlogEntryListener(new AuthorIndexListener()); eventListenerList.addBlogEntryListener(new SearchIndexListener()); eventListenerList.addBlogEntryListener(new AuditListener()); try { eventListenerList.addBlogEntryListener(new EmailSubscriptionListener()); } catch (Throwable t) { log.warn("Error while starting e-mail subscription listener - add mail.jar and activation.jar to the server classpath if you want to enable this listener.", t); } } /** * Initialises any comment listeners configured for this blog. */ private void initCommentListeners() { log.debug("Registering comment listeners"); String classNames = getCommentListeners(); if (classNames != null && classNames.length() > 0) { String classes[] = classNames.split("\\s+"); for (int i = 0; i < classes.length; i++) { if (!classes[i].startsWith("#")) { try { Class c = Class.forName(classes[i].trim()); CommentListener listener = (CommentListener)c.newInstance(); eventListenerList.addCommentListener(listener); } catch (Exception e) { error("Could not start comment listener \"" + classes[i] + "\""); log.error("Comment listener " + classes[i] + " could not be registered", e); } } } } eventListenerList.addCommentListener(new ResponseIndexListener()); eventListenerList.addCommentListener(new AuditListener()); } /** * Initialises any TrackBack listeners configured for this blog. */ private void initTrackBackListeners() { log.debug("Registering TrackBack listeners"); String classNames = getTrackBackListeners(); if (classNames != null && classNames.length() > 0) { String classes[] = classNames.split("\\s+"); for (int i = 0; i < classes.length; i++) { if (!classes[i].startsWith("#")) { try { Class c = Class.forName(classes[i].trim()); TrackBackListener listener = (TrackBackListener)c.newInstance(); eventListenerList.addTrackBackListener(listener); } catch (Exception e) { error("Could not start TrackBack listener \"" + classes[i] + "\""); log.error("TrackBack listener " + classes[i] + " could not be registered", e); } } } } eventListenerList.addTrackBackListener(new ResponseIndexListener()); eventListenerList.addTrackBackListener(new AuditListener()); } /** * Initialises any content decorators configufred for this blog. */ private void initDecorators() { log.debug("Registering decorators"); decoratorChain.add(new HideUnapprovedResponsesDecorator()); String classNames = getContentDecorators(); if (classNames != null && classNames.length() > 0) { String classes[] = classNames.split("\\s+"); for (int i = 0; i < classes.length; i++) { if (!classes[i].startsWith("#")) { try { Class c = Class.forName(classes[i].trim()); ContentDecorator decorator = (ContentDecorator)c.newInstance(); decorator.setBlog(this); decoratorChain.add(decorator); } catch (Exception e) { error("Could not start decorator \"" + classes[i] + "\""); e.printStackTrace(); log.error(classes[i] + " could not be started", e); } } } } } /** * Gets the default properties for a Blog. * * @return a Properties instance */ protected Properties getDefaultProperties() { Properties defaultProperties = new Properties(); defaultProperties.setProperty(NAME_KEY, "My blog"); defaultProperties.setProperty(DESCRIPTION_KEY, ""); defaultProperties.setProperty(IMAGE_KEY, ""); defaultProperties.setProperty(AUTHOR_KEY, "Blog Owner"); defaultProperties.setProperty(EMAIL_KEY, "blog@yourdomain.com"); defaultProperties.setProperty(TIMEZONE_KEY, "Europe/London"); defaultProperties.setProperty(LANGUAGE_KEY, "en"); defaultProperties.setProperty(COUNTRY_KEY, "GB"); defaultProperties.setProperty(CHARACTER_ENCODING_KEY, "UTF-8"); defaultProperties.setProperty(RECENT_BLOG_ENTRIES_ON_HOME_PAGE_KEY, "3"); defaultProperties.setProperty(RECENT_RESPONSES_ON_HOME_PAGE_KEY, "3"); defaultProperties.setProperty(THEME_KEY, "default"); defaultProperties.setProperty(PRIVATE_KEY, FALSE); defaultProperties.setProperty(LUCENE_ANALYZER_KEY, "org.apache.lucene.analysis.SimpleAnalyzer"); defaultProperties.setProperty(CONTENT_DECORATORS_KEY, "net.sourceforge.pebble.decorator.RadeoxDecorator\n" + "net.sourceforge.pebble.decorator.HtmlDecorator\n" + "net.sourceforge.pebble.decorator.EscapeMarkupDecorator\n" + "net.sourceforge.pebble.decorator.RelativeUriDecorator\n" + "net.sourceforge.pebble.decorator.ReadMoreDecorator\n" + "net.sourceforge.pebble.decorator.BlogTagsDecorator"); defaultProperties.setProperty(BLOG_ENTRY_LISTENERS_KEY, "net.sourceforge.pebble.event.blogentry.XmlRpcNotificationListener"); defaultProperties.setProperty(COMMENT_LISTENERS_KEY, "net.sourceforge.pebble.event.response.IpAddressListener\r\n" + "net.sourceforge.pebble.event.response.LinkSpamListener\r\n" + "net.sourceforge.pebble.event.response.ContentSpamListener\r\n" + "net.sourceforge.pebble.event.response.SpamScoreListener\r\n" + "net.sourceforge.pebble.event.response.MarkApprovedWhenAuthenticatedListener\r\n" + "#net.sourceforge.pebble.event.response.DeleteRejectedListener\r\n" + "#net.sourceforge.pebble.event.comment.EmailAuthorNotificationListener"); defaultProperties.setProperty(TRACKBACK_LISTENERS_KEY, "net.sourceforge.pebble.event.response.IpAddressListener\r\n" + "net.sourceforge.pebble.event.response.LinkSpamListener\r\n" + "net.sourceforge.pebble.event.response.ContentSpamListener\r\n" + "net.sourceforge.pebble.event.response.SpamScoreListener\r\n" + "net.sourceforge.pebble.event.response.MarkApprovedWhenAuthenticatedListener\r\n" + "#net.sourceforge.pebble.event.response.DeleteRejectedListener\r\n" + "#net.sourceforge.pebble.event.trackback.EmailAuthorNotificationListener"); defaultProperties.setProperty(PERMALINK_PROVIDER_KEY, "net.sourceforge.pebble.permalink.DefaultPermalinkProvider"); defaultProperties.setProperty(EVENT_DISPATCHER_KEY, "net.sourceforge.pebble.event.DefaultEventDispatcher"); defaultProperties.setProperty(LOGGER_KEY, "net.sourceforge.pebble.logging.CombinedLogFormatLogger"); defaultProperties.setProperty(COMMENT_CONFIRMATION_STRATEGY_KEY, "net.sourceforge.pebble.confirmation.DefaultConfirmationStrategy"); defaultProperties.setProperty(TRACKBACK_CONFIRMATION_STRATEGY_KEY, "net.sourceforge.pebble.confirmation.DefaultConfirmationStrategy"); defaultProperties.setProperty(RICH_TEXT_EDITOR_FOR_COMMENTS_ENABLED_KEY, "true");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -