View Javadoc

1   package org.apache.geronimo.system.main;
2   
3   import java.io.PrintStream;
4   
5   import org.apache.geronimo.kernel.Kernel;
6   import org.apache.geronimo.kernel.repository.Artifact;
7   import java.text.DateFormat;
8   import java.text.SimpleDateFormat;
9   import java.util.TimeZone;
10  import java.util.Date;
11  
12  /**
13   * A startup monitor that shows the progress of loading and starting
14   * modules, outputing a new line for each module started
15   * showing the time taken to start the module along with the
16   * moduleId.
17   * <p/>
18   * This startup monitor produces more lines of output than the
19   * ProgressBarStartupMonitor but its output is suitable for redirection
20   * to a file or for when Geronimo is running under an IDE or other tool.
21   * <p/>
22   * A summary will also be produced containing a list of ports
23   * Geronimo is listening on, the configIds of application modules
24   * that were started and the URLs of Web applications that were started.
25   *
26   * @version $Revision: 1.0$
27   */
28  public class LongStartupMonitor implements StartupMonitor {
29  
30      /**
31       * PrintStream
32       */
33      private PrintStream out;
34  
35      /**
36       * Number of modules to start
37       */
38      private int numModules;
39  
40      /**
41       * Number of digits in number of modules to start
42       */
43      private int numModulesDigits;
44  
45      /**
46       * Number of modules currently being started
47       */
48      private int moduleNum;
49  
50      /**
51       * Length of longest module name
52       */
53      private int longestModuleNameLength;
54      
55      /**
56       * Time Geronimo was started
57       */
58      private long started;
59  
60      /**
61       * Time the current module being processed was started
62       */
63      private long moduleStarted;
64  
65      /**
66       * The Kernel of the system being started
67       */
68      private Kernel kernel;
69  
70      public synchronized void systemStarting(long startTime) {
71          out = System.out;
72          started = startTime;
73      }
74  
75      public synchronized void systemStarted(Kernel kernel) {
76          this.kernel = kernel;
77      }
78  
79      public synchronized void foundModules(Artifact[] modules) {
80          numModules = modules.length;
81          numModulesDigits = Integer.toString(numModules).length();
82          
83          for (int i = 0, len= 0; i < modules.length; i++) {
84              len = modules[i].toString().length();
85              if (len > longestModuleNameLength)
86                  longestModuleNameLength = len;
87          }
88      }
89  
90      public synchronized void moduleLoading(Artifact module) {
91          StringBuffer buf = new StringBuffer("Module ");
92          // pad module index
93          int configIndexDigits = Integer.toString(++moduleNum).length();
94          for (; configIndexDigits < numModulesDigits; configIndexDigits++) {
95              buf.append(' ');
96          }
97          // append module index / total configs
98          buf.append(moduleNum).append('/').append(numModules).append(' ');
99          // append module name
100         buf.append(module);
101         // pad end of module with spaces so trailing startup times will line up
102         int len = module.toString().length();
103         for (; len < longestModuleNameLength; len++) {
104             buf.append(' ');
105         }
106         out.print(buf);
107     }
108 
109     public synchronized void moduleLoaded(Artifact module) {
110     }
111 
112     public synchronized void moduleStarting(Artifact module) {
113         moduleStarted = System.currentTimeMillis();
114     }
115 
116     public synchronized void moduleStarted(Artifact module) {
117         long time = System.currentTimeMillis() - moduleStarted;        
118         StringBuffer buf = new StringBuffer();
119         buf.append(" started in ");
120         
121         String formattedTime = getFormattedTime(time);
122         if (formattedTime.startsWith("0.")) {
123             // don't display zero seconds
124             formattedTime = " " +formattedTime.substring(1);
125         }
126         
127         // if first number (e.g. seconds or minutes) is one digit,
128         // pad it with a leading space to get times to line up nicely
129         int index = formattedTime.indexOf(':'); // must look for colon first
130         if (index == -1)
131             index = formattedTime.indexOf('.');
132                 
133         if (index == 1)
134             buf.append(' ');
135             
136         buf.append(formattedTime);
137         
138         out.println(buf.toString());
139     }
140 
141     public synchronized void startupFinished() {
142         int time = Math.round((float) (System.currentTimeMillis() - started) / 1000f);
143 
144         out.println("Startup completed in " + time + " seconds");
145         StartupMonitorUtil.wrapUp(out, kernel);
146     }
147 
148     public synchronized void serverStartFailed(Exception problem) {
149         out.println("Server Startup failed");
150         out.println();
151         problem.printStackTrace(out);
152     }
153 
154     // time formatting method - thanks to Maven 
155     private static String getFormattedTime( long time )
156     {
157         String pattern = "s.SSS's'";
158         if ( time / 60000L > 0 )
159         {
160             pattern = "m:s" + pattern;
161             if ( time / 3600000L > 0 )
162             {
163                 pattern = "H:m" + pattern;
164             }
165         }
166         DateFormat fmt = new SimpleDateFormat( pattern );
167         fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
168         return fmt.format( new Date( time ) );
169     }
170 }