View Javadoc

1   /*
2    * $Id: SplashScreen.java 12150 2008-06-24 22:42:52Z aguenther $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import org.mule.api.MuleContext;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * Implements singleton pattern to allow different splash-screen implementations
25   * following the concept of header, body, and footer. Header and footer are
26   * reserved internally to Mule but body can be used to customize splash-screen
27   * output. External code can e.g. hook into the start-up splash-screen as follows:
28   * <pre><code>
29   *   SplashScreen splashScreen = SplashScreen.getInstance(ServerStartupSplashScreen.class);
30   *   splashScreen.addBody("Some extra text");
31   * </code></pre>
32   */
33  public abstract class SplashScreen
34  {
35      protected List header = new ArrayList();
36      protected List body = new ArrayList();
37      protected List footer = new ArrayList();
38      
39      private static Log logger = LogFactory.getLog(SplashScreen.class);
40      
41      private static final Map instances = new HashMap();    
42      
43      public static synchronized SplashScreen getInstance(Class clazz)
44      {
45          SplashScreen splashScreen = (SplashScreen) instances.get(clazz);
46          
47          if (splashScreen == null)
48          {
49              try
50              {
51                  splashScreen = (SplashScreen) clazz.newInstance();
52                  instances.put(clazz, splashScreen);
53              }
54              catch (Exception ignore)
55              {
56                  logger.debug(ignore);
57              }
58          }
59          
60          return splashScreen;
61      }
62  
63      /**
64       * Setting the header clears body and footer assuming a new
65       * splash-screen is built.
66       * 
67       * @param context
68       */
69      final public void setHeader(MuleContext context)
70      {
71          header.clear();
72          doHeader(context);
73      }
74      
75      final public void addBody(String line)
76      {
77          doBody(line);
78      }
79      
80      final public void setFooter(MuleContext context)
81      {
82          footer.clear();
83          doFooter(context);
84      }
85      
86      protected void doHeader(MuleContext context)
87      {
88          // default reserved for mule core info
89      }   
90      
91      protected void doBody(String line)
92      {
93          body.add(line);
94      }
95  
96      protected void doFooter(MuleContext context)
97      {
98          // default reserved for mule core info
99      }    
100     
101     public String toString()
102     {
103         List boilerPlate = new ArrayList(header);
104         boilerPlate.addAll(body);
105         boilerPlate.addAll(footer);
106         return StringMessageUtils.getBoilerPlate(boilerPlate, '*', 70);
107     }
108     
109     protected SplashScreen()
110     {
111         // make sure no one else creates an instance
112     }
113 }