File Source: pebblecontextlistener.java
/*
P/P * Method: net.sourceforge.pebble.web.listener.PebbleContextListener__static_init
*
* Postconditions:
* init'ed(log)
*/
1 /*
2 * Copyright (c) 2003-2006, Simon Brown
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
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * - Neither the name of Pebble nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
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;
/*
P/P * Method: void net.sourceforge.pebble.web.listener.PebbleContextListener()
*/
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 * Allows the blog to be loaded when this web application is started up.
49 *
50 * @author Simon Brown
51 */
52 public class PebbleContextListener implements ServletContextListener {
53
54 /** the log used by this class */
55 private static Log log = LogFactory.getLog(PebbleContextListener.class);
56
57 /**
58 * Called when the web application is started.
59 *
60 * @param event a ServletContextEvent instance
61 */
62 public void contextInitialized(ServletContextEvent event) {
/*
P/P * Method: void contextInitialized(ServletContextEvent)
*
* Preconditions:
* event != null
* log != null
*
* Presumptions:
* java.lang.System:currentTimeMillis(...)@114 - java.lang.System:currentTimeMillis(...)@63 in -263..264-1
* java.util.Iterator:next(...)@79 != null
* javax.servlet.ServletContextEvent:getServletContext(...)@72 != null
* net.sourceforge.pebble.domain.BlogManager:getBlogs(...)@78 != null
* net.sourceforge.pebble.domain.BlogManager:getInstance(...)@74 != null
* ...
*
* Postconditions:
* (soft) net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
* init'ed(net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).webApplicationRoot)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@79: {1}, {0}
* net.sourceforge.pebble.domain.Blog:getNumberOfBlogEntries(...)@82: {-231..-1, 1..232-1}, {0}
*/
63 long startTime = System.currentTimeMillis();
64 log.info("Starting Pebble");
65
66 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
67 Configuration config = (Configuration)applicationContext.getBean("pebbleConfiguration");
68
69 DAOFactory.setConfiguredFactory(config.getDaoFactory());
70 PebbleContext ctx = PebbleContext.getInstance();
71 ctx.setConfiguration(config);
72 ctx.setWebApplicationRoot(event.getServletContext().getRealPath("/"));
73
74 BlogManager.getInstance().setMultiBlog(config.isMultiBlog());
75 BlogManager.getInstance().startBlogs();
76
77 // find those blogs with no entries and add a welcome note
78 Collection<Blog> blogs = (Collection<Blog>)BlogManager.getInstance().getBlogs();
79 for (Blog blog : blogs) {
80 try {
81 // and add a default entry, if one doesn't exist
82 if (blog.getNumberOfBlogEntries() == 0) {
83 log.info("Creating 'welcome note' blog entry for " + blog.getId());
84 BlogEntry blogEntry = new BlogEntry(blog);
85 blogEntry.setTitle("Welcome");
86 blogEntry.setBody(
87 "<p>\n" +
88 "Welcome to your new Pebble powered blog. Here are a few suggestions for getting started.\n" +
89 "</p>\n" +
90 "\n" +
91 "<ul>\n" +
92 "<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" +
93 "<li>Modify your <a href=\"viewBlogProperties.secureaction\">blog properties</a></li>\n" +
94 "<li><a href=\"addBlogEntry.secureaction\">Create a new blog entry</a>.</li>\n" +
95 "<li>Give out a link to your <a href=\"./rss.xml\">RSS</a> feed.</li>\n" +
96 "<li>Remove the default user and create your own user on the <a href=\"viewUsers.secureaction\">users page</a>.</li>\n" +
97 "<li>Take a look at the <a href=\"./help/index.html\">online help</a>.</li>\n" +
98 "<li>Delete this blog entry when you're finished with it.</li>\n" +
99 "</ul>\n" +
100 "\n" +
101 "<p>\n" +
102 "Have fun!\n" +
103 "</p>");
104 blogEntry.setAuthor("username");
105 blogEntry.setPublished(true);
106 BlogService service = new BlogService();
107 service.putBlogEntry(blogEntry);
108 }
109 } catch (BlogServiceException e) {
110 log.warn("Could not store 'welcome note' blog entry for " + blog.getId());
111 }
112 }
113
114 long endTime = System.currentTimeMillis();
115 log.info("Pebble started in " + (endTime-startTime) + "ms");
116 }
117
118 /**
119 * Called when the web application is shutdown.
120 *
121 * @param event a ServletContextEvent instance
122 */
123 public void contextDestroyed(ServletContextEvent event) {
/*
P/P * Method: void contextDestroyed(ServletContextEvent)
*
* Preconditions:
* log != null
*
* Presumptions:
* net.sourceforge.pebble.domain.BlogManager:getInstance(...)@125 != null
*/
124 log.info("Stopping Pebble");
125 BlogManager.getInstance().stopBlogs();
126
127 log.info("Pebble stopped");
128 }
129
130 }
SofCheck Inspector Build Version : 2.22510
| pebblecontextlistener.java |
2010-Jun-25 19:40:32 |
| pebblecontextlistener.class |
2010-Jul-19 20:23:38 |