KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.geronimo.system.serverinfo.ServerConstants;
24
25 /**
26  * A startup monitor that shows the progress of loading and starting
27  * modules using a text based progress bar and the use of line
28  * feeds to update the progress display, therefore minimizing the
29  * number of lines output to the terminal.
30  * <p/>
31  * A summary will also be produced containing a list of ports
32  * Geronimo is listening on, the configIds of application modules
33  * that were started and the URLs of Web applications that were started.
34  *
35  * @version $Revision: 1.0$
36  */

37 public class ProgressBarStartupMonitor implements StartupMonitor {
38     private final static char STATUS_NOT_READY = ' ';
39     private final static char STATUS_LOADING = '-';
40     private final static char STATUS_LOADED = '>';
41     private final static char STATUS_STARTED = '*';
42     private final static char STATUS_FAILED = 'x';
43     private final static int MAX_WIDTH = 70;
44     private PrintStream JavaDoc out;
45     private String JavaDoc currentOperation;
46     private Artifact[] modules;
47     private char[] moduleStatus = new char[0];
48     private long started;
49     private int percent = 0;
50     private Kernel kernel;
51     private int operationLimit = 50;
52     private boolean finished = false;
53     private UpdateThread thread;
54
55     public synchronized void systemStarting(long startTime) {
56         out = System.out;
57         started = startTime;
58     }
59
60     public synchronized void systemStarted(Kernel kernel) {
61         out.println("Starting Geronimo Application Server v" + ServerConstants.getVersion());
62         this.kernel = kernel;
63         currentOperation = "Loading";
64     }
65
66     public synchronized void foundModules(Artifact[] modules) {
67         this.modules = modules;
68         moduleStatus = new char[modules.length];
69         for (int i = 0; i < moduleStatus.length; i++) {
70             moduleStatus[i] = STATUS_NOT_READY;
71         }
72         operationLimit = MAX_WIDTH
73                 - 5 // two brackets, start and stop tokens, space afterward
74
- modules.length // module tokens
75
- 4 // 2 digits of percent plus % plus space afterward
76
- 5;// 3 digits of time plus s plus space afterward
77
repaint();
78         thread = new UpdateThread();
79         thread.start();
80     }
81
82     public synchronized void calculatePercent() {
83         if (finished) {
84             this.percent = 100;
85             return;
86         }
87         int percent = 0;
88         if (kernel != null) percent += 5;
89         int total = moduleStatus.length * 2;
90         int progress = 0;
91         for (int i = 0; i < moduleStatus.length; i++) {
92             char c = moduleStatus[i];
93             switch (c) {
94                 case STATUS_LOADED:
95                     progress += 1;
96                     break;
97                 case STATUS_STARTED:
98                 case STATUS_FAILED:
99                     progress += 2;
100                     break;
101             }
102         }
103         percent += Math.round(90f * (float) progress / (float) total);
104         this.percent = percent;
105     }
106
107     public synchronized void moduleLoading(Artifact module) {
108         currentOperation = " Loading " + module;
109         for (int i = 0; i < modules.length; i++) {
110             if (modules[i].equals(module)) {
111                 moduleStatus[i] = STATUS_LOADING;
112             }
113         }
114         repaint();
115     }
116
117     public synchronized void moduleLoaded(Artifact module) {
118         for (int i = 0; i < modules.length; i++) {
119             if (modules[i].equals(module)) {
120                 moduleStatus[i] = STATUS_LOADED;
121             }
122         }
123         calculatePercent();
124         repaint();
125     }
126
127     public synchronized void moduleStarting(Artifact module) {
128         currentOperation = "Starting " + module;
129     }
130
131     public synchronized void moduleStarted(Artifact module) {
132         for (int i = 0; i < modules.length; i++) {
133             if (modules[i].equals(module)) {
134                 moduleStatus[i] = STATUS_STARTED;
135             }
136         }
137         calculatePercent();
138         repaint();
139     }
140
141     public synchronized void startupFinished() {
142         finished = true;
143         currentOperation = "Startup complete";
144         calculatePercent();
145         thread.done = true;
146         thread.interrupt();
147     }
148
149     public synchronized void serverStartFailed(Exception JavaDoc problem) {
150         currentOperation = "Startup failed";
151         repaint();
152         out.println();
153         problem.printStackTrace(out);
154     }
155
156     private synchronized void repaint() {
157         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
158         buf.append("\r[");
159         buf.append(kernel == null ? STATUS_NOT_READY : STATUS_STARTED);
160         for (int i = 0; i < moduleStatus.length; i++) {
161             buf.append(moduleStatus[i]);
162         }
163         buf.append(finished ? STATUS_STARTED : STATUS_NOT_READY);
164         buf.append("] ");
165         if (percent < 10) {
166             buf.append(' ');
167         }
168         buf.append(percent).append("% ");
169         int time = Math.round((float) (System.currentTimeMillis() - started) / 1000f);
170         if (time < 10) {
171             buf.append(' ');
172         }
173         if (time < 100) {
174             buf.append(' ');
175         }
176         buf.append(time).append("s ");
177         if (currentOperation.length() > operationLimit) {
178             int space = currentOperation.indexOf(' ', 5);
179             buf.append(currentOperation.substring(0, space + 1));
180             // "Foo BarBarBar" limit 9 = "Foo ...ar" = 13 - 9 + 3 + 1 + 3
181
// buf.append("...").append(currentOperation.substring(currentOperation.length()-operationLimit+space+4));
182
// "FooBar BarBarBar" limit 12 = "FooBar Ba..." = (7, 12-3)
183
buf.append(currentOperation.substring(space + 1, operationLimit - 3)).append("...");
184         } else {
185             buf.append(currentOperation);
186             for (int i = currentOperation.length(); i < operationLimit; i++) {
187                 buf.append(' ');
188             }
189         }
190         out.print(buf.toString());
191         out.flush();
192     }
193
194     private class UpdateThread extends Thread JavaDoc {
195         private volatile boolean done = false;
196
197         public UpdateThread() {
198             super("Progress Display Update Thread");
199             setDaemon(true);
200         }
201
202         public void run() {
203             while (!done) {
204                 try {
205                     Thread.sleep(500);
206                 } catch (InterruptedException JavaDoc e) {
207                     continue;
208                 }
209                 repaint();
210             }
211
212             repaint();
213             out.println();
214             StartupMonitorUtil.wrapUp(ProgressBarStartupMonitor.this.out, ProgressBarStartupMonitor.this.kernel);
215         }
216     }
217 }
218
Popular Tags