| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.web.listener; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.Configuration; |
| 35 | |
import net.sourceforge.pebble.PebbleContext; |
| 36 | |
import net.sourceforge.pebble.dao.DAOFactory; |
| 37 | |
import net.sourceforge.pebble.domain.*; |
| 38 | |
import org.apache.commons.logging.Log; |
| 39 | |
import org.apache.commons.logging.LogFactory; |
| 40 | |
import org.springframework.context.ApplicationContext; |
| 41 | |
import org.springframework.web.context.support.WebApplicationContextUtils; |
| 42 | |
|
| 43 | |
import javax.servlet.ServletContextEvent; |
| 44 | |
import javax.servlet.ServletContextListener; |
| 45 | |
import java.util.Collection; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 0 | public class PebbleContextListener implements ServletContextListener { |
| 53 | |
|
| 54 | |
|
| 55 | 0 | private static Log log = LogFactory.getLog(PebbleContextListener.class); |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public void contextInitialized(ServletContextEvent event) { |
| 63 | 0 | long startTime = System.currentTimeMillis(); |
| 64 | 0 | log.info("Starting Pebble"); |
| 65 | |
|
| 66 | 0 | ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext()); |
| 67 | 0 | Configuration config = (Configuration)applicationContext.getBean("pebbleConfiguration"); |
| 68 | |
|
| 69 | 0 | DAOFactory.setConfiguredFactory(config.getDaoFactory()); |
| 70 | 0 | PebbleContext ctx = PebbleContext.getInstance(); |
| 71 | 0 | ctx.setConfiguration(config); |
| 72 | 0 | ctx.setWebApplicationRoot(event.getServletContext().getRealPath("/")); |
| 73 | 0 | ctx.setApplicationContext(applicationContext); |
| 74 | |
|
| 75 | 0 | BlogManager.getInstance().setMultiBlog(config.isMultiBlog()); |
| 76 | 0 | BlogManager.getInstance().startBlogs(); |
| 77 | |
|
| 78 | |
|
| 79 | 0 | Collection<Blog> blogs = (Collection<Blog>)BlogManager.getInstance().getBlogs(); |
| 80 | 0 | for (Blog blog : blogs) { |
| 81 | |
try { |
| 82 | |
|
| 83 | 0 | if (blog.getNumberOfBlogEntries() == 0) { |
| 84 | 0 | log.info("Creating 'welcome note' blog entry for " + blog.getId()); |
| 85 | 0 | BlogEntry blogEntry = new BlogEntry(blog); |
| 86 | 0 | blogEntry.setTitle("Welcome"); |
| 87 | 0 | blogEntry.setBody( |
| 88 | |
"<p>\n" + |
| 89 | |
"Welcome to your new Pebble powered blog. Here are a few suggestions for getting started.\n" + |
| 90 | |
"</p>\n" + |
| 91 | |
"\n" + |
| 92 | |
"<ul>\n" + |
| 93 | |
"<li>Login to see the admin features of your blog. The default username is <code>username</code> and the password is <code>password</code>.</li>\n" + |
| 94 | |
"<li>Modify your <a href=\"viewBlogProperties.secureaction\">blog properties</a></li>\n" + |
| 95 | |
"<li><a href=\"addBlogEntry.secureaction\">Create a new blog entry</a>.</li>\n" + |
| 96 | |
"<li>Give out a link to your <a href=\"./rss.xml\">RSS</a> feed.</li>\n" + |
| 97 | |
"<li>Remove the default user and create your own user on the <a href=\"viewUsers.secureaction\">users page</a>.</li>\n" + |
| 98 | |
"<li>Take a look at the <a href=\"./help/index.html\">online help</a>.</li>\n" + |
| 99 | |
"<li>Delete this blog entry when you're finished with it.</li>\n" + |
| 100 | |
"</ul>\n" + |
| 101 | |
"\n" + |
| 102 | |
"<p>\n" + |
| 103 | |
"Have fun!\n" + |
| 104 | |
"</p>"); |
| 105 | 0 | blogEntry.setAuthor("username"); |
| 106 | 0 | blogEntry.setPublished(true); |
| 107 | 0 | BlogService service = new BlogService(); |
| 108 | 0 | service.putBlogEntry(blogEntry); |
| 109 | |
} |
| 110 | 0 | } catch (BlogServiceException e) { |
| 111 | 0 | log.warn("Could not store 'welcome note' blog entry for " + blog.getId()); |
| 112 | 0 | } |
| 113 | |
} |
| 114 | |
|
| 115 | 0 | long endTime = System.currentTimeMillis(); |
| 116 | 0 | log.info("Pebble started in " + (endTime-startTime) + "ms"); |
| 117 | 0 | } |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public void contextDestroyed(ServletContextEvent event) { |
| 125 | 0 | log.info("Stopping Pebble"); |
| 126 | 0 | BlogManager.getInstance().stopBlogs(); |
| 127 | |
|
| 128 | 0 | log.info("Pebble stopped"); |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
} |