1 17 package org.apache.geronimo.system.main; 18 19 import java.io.PrintStream ; 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 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 out; 45 private String 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 - modules.length - 4 - 5; 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 problem) { 150 currentOperation = "Startup failed"; 151 repaint(); 152 out.println(); 153 problem.printStackTrace(out); 154 } 155 156 private synchronized void repaint() { 157 StringBuffer buf = new StringBuffer (); 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 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 { 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 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 |