1 3 27 28 30 package de.qfs.apps.qflog; 31 32 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.WindowAdapter ; 37 import java.awt.event.WindowEvent ; 38 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.FileOutputStream ; 42 import java.io.IOException ; 43 44 import java.util.Hashtable ; 45 46 import javax.swing.JButton ; 47 import javax.swing.JFrame ; 48 49 import de.qfs.lib.config.Configuration; 50 import de.qfs.lib.config.ConfigWrapper; 51 import de.qfs.lib.gui.SwingUtil; 52 import de.qfs.lib.log.Log; 53 import de.qfs.lib.log.Logger; 54 import de.qfs.lib.log.StreamFilter; 55 import de.qfs.lib.logrmi.RemoteLogFilter; 56 import de.qfs.lib.util.ArgsParser; 57 import de.qfs.lib.util.MalformedArgumentListException; 58 59 import de.qfs.apps.qflog.logview.LogView; 60 61 63 69 public class TestClient 70 { 71 73 76 private final static Logger logger = new Logger (TestClient.class); 77 78 81 private final static String CLIENT_NAME = "testclient"; 82 83 86 private static Hashtable options; 87 88 90 92 97 public static void main (String [] args) 98 { 99 getOpts(args); 100 Logger.setLogLevels(options); 101 102 int outputlevel = 2; 103 if (options.containsKey("outputlevel")) { 104 try { 105 outputlevel = 106 Integer.parseInt((String ) options.get("outputlevel")); 107 } catch (NumberFormatException ex) { 108 } 109 } 110 Log.setOutputLevel(outputlevel); 111 112 String clientname = CLIENT_NAME; 113 if (options.containsKey("clientname")) { 114 clientname = (String ) options.get("clientname"); 115 } 116 117 String logserver = (String ) options.get("logserver"); 119 boolean waitfor = options.containsKey("waitforserver"); 120 121 try { 122 if (waitfor) { 123 int port = -1; 124 if (options.containsKey("port")) { 125 try { 126 port = Integer.parseInt((String ) options.get("port")); 127 } catch (NumberFormatException ex) { 128 } 129 } 130 boolean local = ! options.containsKey("allownonlocal"); 131 boolean create = options.containsKey("createregistry"); 132 if (logserver != null) { 133 RemoteLogFilter.logRemote(logserver, clientname, port, 134 create, local); 135 } else { 136 RemoteLogFilter.logRemote(clientname, port, 137 create, local); 138 } 139 } else if (logserver != null) { 140 RemoteLogFilter.logRemote(logserver, clientname); 141 } 142 } catch (Exception ex) { 143 if (logger.level >= Log.ERR) { 144 logger.log("main(String[])", ex); 145 } 146 } 147 148 if (options.containsKey("logfile")) { 150 try { 151 StreamFilter.logToFile 152 (clientname, new File ((String ) options.get("logfile")), 153 StreamFilter.MODE_CREATE); 154 } catch (IOException ex) { 155 if (logger.level >= Log.ERR) { 156 logger.log("main(String[])", ex); 157 } 158 } 159 } 160 161 if (options.containsKey("configfile")) { 163 try { 164 FileInputStream in = 165 new FileInputStream ((String ) options.get("configfile")); 166 Configuration.instance().restore(in); 167 in.close(); 168 } catch (IOException ex) { 169 } 171 } 172 173 JFrame frame = new JFrame ("TestClient"); 174 frame.setName("TestClient.frame"); 175 Configuration.instance().register(ConfigWrapper.makeWrapper 176 (frame, "TestClient.frame")); 177 frame.addWindowListener(new WindowAdapter () { 178 public void windowClosing(WindowEvent e) { 179 exit(); 180 } 181 }); 182 183 JButton button = new JButton ("quit"); 184 button.setName("quit"); 185 186 button.addActionListener(new ActionListener () { 187 public void actionPerformed(ActionEvent e) { 188 exit(); 189 } 190 }); 191 frame.getContentPane().add(button); 192 frame.pack(); 193 frame.show(); 194 195 if (options.containsKey("internal")) { 197 final String cn = clientname; 198 SwingUtil.invokeAndWait(new Runnable () { 199 public void run() { 200 LogView lv = LogView.logToView(cn); 201 lv.setLoggingEnabled(true); 202 lv.getFrame().show(); 203 } 204 }); 205 } 206 207 int numloggers = 3; 208 if (options.containsKey("numloggers")) { 209 try { 210 numloggers = 211 Integer.parseInt((String ) options.get("numloggers")); 212 } catch (NumberFormatException ex) { 213 } 214 } 215 for (int i = 1; i <= numloggers; i++) { 217 int pkgnum = i / 2 + 1; 218 int clnum = (i - 1) % 3 + 1; 219 new LoggerThread("pkg_" + pkgnum + 220 ".Logger" + (char) ('A' + pkgnum - 1) + 221 "_" + clnum, i * 1000).start(); 222 } 223 224 } 225 226 228 233 236 private static void exit() 237 { 238 Log.setQueueing(false); 239 RemoteLogFilter.stopLogging(); 240 StreamFilter.stopLogging(); 241 if (options.containsKey("configfile")) { 243 try { 244 FileOutputStream out = 245 new FileOutputStream ((String ) options.get("configfile")); 246 Configuration.instance().save(out); 247 out.close(); 248 } catch (IOException ex) { 249 } 251 } 252 253 System.exit(0); 254 255 } 256 257 260 265 private static void getOpts (String [] args) 266 { 267 if (logger.level >= Log.MTD) { 268 logger.log(Log.MTD, "getOpts(String[])", 269 logger.level < Log.MTDDETAIL ? "" : 270 "args: " + args); 271 } 272 273 ArgsParser ap = new ArgsParser (new String [] { 274 "-allownonlocal", 275 "+clientname", 276 "+configfile", 277 "-createregistry", 278 "-internal", 279 "+log-*", 280 "+logfile", 281 "+logserver", 282 "+numloggers", 283 "+outputlevel", 284 "+port", 285 "-waitforserver", 286 }); 287 288 try { 289 ap.parse(args); 290 options = ap.getOptions(); 291 } catch (MalformedArgumentListException ex) { 292 if (logger.level >= Log.ERR) { 293 logger.log("getOpts(String[])", ex); 294 } 295 options = new Hashtable (); 296 } 297 } 298 299 301 303 306 private static class LoggerThread 307 extends Thread 308 { 309 311 314 private Logger logger; 315 316 319 private int delay; 320 321 323 325 331 public LoggerThread (String name, int delay) 332 { 333 super ("LoggerThread-" + name); 334 setDaemon(true); 335 logger = new Logger (name); 336 this.delay = delay; 337 } 338 339 341 343 346 public void run() 347 { 348 while (true) { 349 try { 350 sleep(delay); 351 } catch (InterruptedException ex) { 352 } 353 int lvl = (int) Math.floor(Math.random() * 10.0) + 1; 354 int mtdnum = (int) Math.floor(Math.random() * 5.0) + 1; 355 int msgnum = (int) Math.floor(Math.random() * 8.0) + 1; 356 if (logger.level >= lvl) { 357 logger.log(lvl, "method_" + mtdnum, "message-" + msgnum); 358 } 359 } 360 } 361 362 } 364 365 } 367 | Popular Tags |