KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > qfs > apps > qflog > TestClient


1 // {{{ copyright
2

3 /********************************************************************
4  *
5  * $Id: TestClient.java,v 1.5 2000/07/05 14:07:43 gs Exp $
6  *
7  * The contents of this file are subject to the Mozilla Public
8  * License Version 1.1 (the "License"); you may not use this file
9  * except in compliance with the License. You may obtain a copy of
10  * the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  * implied. See the License for the specific language governing
15  * rights and limitations under the License.
16  *
17  * The Original Code is qfs.de code.
18  *
19  * The Initial Developer of the Original Code is Gregor Schmid.
20  * Portions created by Gregor Schmid are
21  * Copyright (C) 2000 Quality First Software, Gregor Schmid.
22  * All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  *******************************************************************/

27
28 // }}}
29

30 package de.qfs.apps.qflog;
31
32 // {{{ imports
33

34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.WindowAdapter JavaDoc;
37 import java.awt.event.WindowEvent JavaDoc;
38
39 import java.io.File JavaDoc;
40 import java.io.FileInputStream JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import java.util.Hashtable JavaDoc;
45
46 import javax.swing.JButton JavaDoc;
47 import javax.swing.JFrame JavaDoc;
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 // }}}
62

63 /**
64  * This little tool is used to demonstrate and test qflog.
65  *
66  * @author Gregor Schmid
67  * @version $Revision: 1.5 $
68  */

69 public class TestClient
70 {
71     // {{{ variables
72

73     /**
74      * The Logger used for logging.
75      */

76     private final static Logger logger = new Logger (TestClient.class);
77
78     /**
79      * The default client name.
80      */

81     private final static String JavaDoc CLIENT_NAME = "testclient";
82
83     /**
84      * The command line options.
85      */

86     private static Hashtable JavaDoc options;
87
88     // }}}
89

90     // {{{ main
91

92     /**
93      * Main point of entry for the TestClient.
94      *
95      * @param args The command line arguments.
96      */

97     public static void main (String JavaDoc[] 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 JavaDoc) options.get("outputlevel"));
107             } catch (NumberFormatException JavaDoc ex) {
108             }
109         }
110         Log.setOutputLevel(outputlevel);
111
112         String JavaDoc clientname = CLIENT_NAME;
113         if (options.containsKey("clientname")) {
114             clientname = (String JavaDoc) options.get("clientname");
115         }
116
117         // create RemoteLogFilter
118
String JavaDoc logserver = (String JavaDoc) 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 JavaDoc) options.get("port"));
127                     } catch (NumberFormatException JavaDoc 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 JavaDoc ex) {
143             if (logger.level >= Log.ERR) {
144                 logger.log("main(String[])", ex);
145             }
146         }
147
148         // create logfile
149
if (options.containsKey("logfile")) {
150             try {
151                 StreamFilter.logToFile
152                     (clientname, new File JavaDoc ((String JavaDoc) options.get("logfile")),
153                      StreamFilter.MODE_CREATE);
154             } catch (IOException JavaDoc ex) {
155                 if (logger.level >= Log.ERR) {
156                     logger.log("main(String[])", ex);
157                 }
158             }
159         }
160
161         // restore settings from a configfile?
162
if (options.containsKey("configfile")) {
163             try {
164                 FileInputStream JavaDoc in =
165                     new FileInputStream JavaDoc ((String JavaDoc) options.get("configfile"));
166                 Configuration.instance().restore(in);
167                 in.close();
168             } catch (IOException JavaDoc ex) {
169                 // so be it
170
}
171         }
172
173         JFrame JavaDoc frame = new JFrame JavaDoc ("TestClient");
174         frame.setName("TestClient.frame");
175         Configuration.instance().register(ConfigWrapper.makeWrapper
176                                           (frame, "TestClient.frame"));
177         frame.addWindowListener(new WindowAdapter JavaDoc () {
178             public void windowClosing(WindowEvent JavaDoc e) {
179                 exit();
180             }
181         });
182
183         JButton JavaDoc button = new JButton JavaDoc ("quit");
184         button.setName("quit");
185
186         button.addActionListener(new ActionListener JavaDoc () {
187             public void actionPerformed(ActionEvent JavaDoc e) {
188                 exit();
189             }
190         });
191         frame.getContentPane().add(button);
192         frame.pack();
193         frame.show();
194
195         // bring up a LogFrame if internal logging is on.
196
if (options.containsKey("internal")) {
197             final String JavaDoc cn = clientname;
198             SwingUtil.invokeAndWait(new Runnable JavaDoc() {
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 JavaDoc) options.get("numloggers"));
212             } catch (NumberFormatException JavaDoc ex) {
213             }
214         }
215         // finally start the logging threads
216
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     // }}}
227

228     //----------------------------------------------------------------------
229
// helper methods
230
//----------------------------------------------------------------------
231
// {{{ exit
232

233     /**
234      * Cleanup and exit.
235      */

236     private static void exit()
237     {
238         Log.setQueueing(false);
239         RemoteLogFilter.stopLogging();
240         StreamFilter.stopLogging();
241         // restore settings from a configfile?
242
if (options.containsKey("configfile")) {
243             try {
244                 FileOutputStream JavaDoc out =
245                     new FileOutputStream JavaDoc ((String JavaDoc) options.get("configfile"));
246                 Configuration.instance().save(out);
247                 out.close();
248             } catch (IOException JavaDoc ex) {
249                 // so be it
250
}
251         }
252
253         System.exit(0);
254
255     }
256
257     // }}}
258
// {{{ getOpts
259

260     /**
261      * Parse the options from the command line arguments.
262      *
263      * @param args The arguments.
264      */

265     private static void getOpts (String JavaDoc[] 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 JavaDoc[] {
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 JavaDoc ();
296         }
297     }
298
299     // }}}
300

301     // {{{ class LoggerThread
302

303     /**
304      * Helper class that logs messages in a background thread.
305      */

306     private static class LoggerThread
307         extends Thread JavaDoc
308     {
309         // {{{ variables
310

311         /**
312          * The logger to log with.
313          */

314         private Logger logger;
315
316         /**
317          * Delay between messages.
318          */

319         private int delay;
320
321         // }}}
322

323         // {{{ constructor
324

325         /**
326          * Create a new LoggerThread.
327          *
328          * @param name Class name for the logger.
329          * @param delay Delay between messages.
330          */

331         public LoggerThread (String JavaDoc name, int delay)
332         {
333             super ("LoggerThread-" + name);
334             setDaemon(true);
335             logger = new Logger (name);
336             this.delay = delay;
337         }
338
339         // }}}
340

341         // {{{ run
342

343         /**
344          * Log messages.
345          */

346         public void run()
347         {
348             while (true) {
349                 try {
350                     sleep(delay);
351                 } catch (InterruptedException JavaDoc 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         // }}}
363
}
364
365     // }}}
366
}
367
Popular Tags