KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > main > LongStartupMonitor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.main;
18
19 import java.io.PrintStream JavaDoc;
20
21 import org.apache.geronimo.kernel.Kernel;
22 import org.apache.geronimo.kernel.repository.Artifact;
23 import java.text.DateFormat JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.TimeZone JavaDoc;
26 import java.util.Date JavaDoc;
27
28 /**
29  * A startup monitor that shows the progress of loading and starting
30  * modules, outputing a new line for each module started
31  * showing the time taken to start the module along with the
32  * moduleId.
33  * <p/>
34  * This startup monitor produces more lines of output than the
35  * ProgressBarStartupMonitor but its output is suitable for redirection
36  * to a file or for when Geronimo is running under an IDE or other tool.
37  * <p/>
38  * A summary will also be produced containing a list of ports
39  * Geronimo is listening on, the configIds of application modules
40  * that were started and the URLs of Web applications that were started.
41  *
42  * @version $Revision: 1.0$
43  */

44 public class LongStartupMonitor implements StartupMonitor {
45
46     /**
47      * PrintStream
48      */

49     private PrintStream JavaDoc out;
50
51     /**
52      * Number of modules to start
53      */

54     private int numModules;
55
56     /**
57      * Number of digits in number of modules to start
58      */

59     private int numModulesDigits;
60
61     /**
62      * Number of modules currently being started
63      */

64     private int moduleNum;
65
66     /**
67      * Length of longest module name
68      */

69     private int longestModuleNameLength;
70     
71     /**
72      * Time Geronimo was started
73      */

74     private long started;
75
76     /**
77      * Time the current module being processed was started
78      */

79     private long moduleStarted;
80
81     /**
82      * The Kernel of the system being started
83      */

84     private Kernel kernel;
85
86     public synchronized void systemStarting(long startTime) {
87         out = System.out;
88         started = startTime;
89     }
90
91     public synchronized void systemStarted(Kernel kernel) {
92         this.kernel = kernel;
93     }
94
95     public synchronized void foundModules(Artifact[] modules) {
96         numModules = modules.length;
97         numModulesDigits = Integer.toString(numModules).length();
98         
99         for (int i = 0, len= 0; i < modules.length; i++) {
100             len = modules[i].toString().length();
101             if (len > longestModuleNameLength)
102                 longestModuleNameLength = len;
103         }
104     }
105
106     public synchronized void moduleLoading(Artifact module) {
107         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Module ");
108         // pad module index
109
int configIndexDigits = Integer.toString(++moduleNum).length();
110         for (; configIndexDigits < numModulesDigits; configIndexDigits++) {
111             buf.append(' ');
112         }
113         // append module index / total configs
114
buf.append(moduleNum).append('/').append(numModules).append(' ');
115         // append module name
116
buf.append(module);
117         // pad end of module with spaces so trailing startup times will line up
118
int len = module.toString().length();
119         for (; len < longestModuleNameLength; len++) {
120             buf.append(' ');
121         }
122         out.print(buf);
123     }
124
125     public synchronized void moduleLoaded(Artifact module) {
126     }
127
128     public synchronized void moduleStarting(Artifact module) {
129         moduleStarted = System.currentTimeMillis();
130     }
131
132     public synchronized void moduleStarted(Artifact module) {
133         long time = System.currentTimeMillis() - moduleStarted;
134         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
135         buf.append(" started in ");
136         
137         String JavaDoc formattedTime = getFormattedTime(time);
138         if (formattedTime.startsWith("0.")) {
139             // don't display zero seconds
140
formattedTime = " " +formattedTime.substring(1);
141         }
142         
143         // if first number (e.g. seconds or minutes) is one digit,
144
// pad it with a leading space to get times to line up nicely
145
int index = formattedTime.indexOf(':'); // must look for colon first
146
if (index == -1)
147             index = formattedTime.indexOf('.');
148                 
149         if (index == 1)
150             buf.append(' ');
151             
152         buf.append(formattedTime);
153         
154         out.println(buf.toString());
155     }
156
157     public synchronized void startupFinished() {
158         int time = Math.round((float) (System.currentTimeMillis() - started) / 1000f);
159
160         out.println("Startup completed in " + time + " seconds");
161         StartupMonitorUtil.wrapUp(out, kernel);
162     }
163
164     public synchronized void serverStartFailed(Exception JavaDoc problem) {
165         out.println("Server Startup failed");
166         out.println();
167         problem.printStackTrace(out);
168     }
169
170     // time formatting method - thanks to Maven
171
private static String JavaDoc getFormattedTime( long time )
172     {
173         String JavaDoc pattern = "s.SSS's'";
174         if ( time / 60000L > 0 )
175         {
176             pattern = "m:s" + pattern;
177             if ( time / 3600000L > 0 )
178             {
179                 pattern = "H:m" + pattern;
180             }
181         }
182         DateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc( pattern );
183         fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
184         return fmt.format( new Date JavaDoc( time ) );
185     }
186 }
187
Popular Tags