Coverage Report - net.sourceforge.pebble.web.listener.PebbleContextListener
 
Classes in this File Line Coverage Branch Coverage Complexity
PebbleContextListener
0%
0/34
0%
0/4
2.5
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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;
 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  0
 public class PebbleContextListener implements ServletContextListener {
 53  
 
 54  
   /** the log used by this class */
 55  0
   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) {
 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  
     // find those blogs with no entries and add a welcome note
 79  0
     Collection<Blog> blogs = (Collection<Blog>)BlogManager.getInstance().getBlogs();
 80  0
     for (Blog blog : blogs) {
 81  
       try {
 82  
         // and add a default entry, if one doesn't exist
 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  
    * Called when the web application is shutdown.
 121  
    *
 122  
    * @param event   a ServletContextEvent instance
 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  
 }