KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > logging > log4j > Log4jService


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
18 package org.apache.geronimo.system.logging.log4j;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.RandomAccessFile JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.nio.CharBuffer JavaDoc;
30 import java.nio.MappedByteBuffer JavaDoc;
31 import java.nio.channels.FileChannel JavaDoc;
32 import java.nio.charset.Charset JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.LinkedList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Set JavaDoc;
39 import java.util.Timer JavaDoc;
40 import java.util.TimerTask JavaDoc;
41 import java.util.regex.Matcher JavaDoc;
42 import java.util.regex.Pattern JavaDoc;
43 import java.util.regex.PatternSyntaxException JavaDoc;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogConfigurationException;
47 import org.apache.commons.logging.LogFactory;
48 import org.apache.geronimo.gbean.GBeanInfo;
49 import org.apache.geronimo.gbean.GBeanInfoBuilder;
50 import org.apache.geronimo.gbean.GBeanLifecycle;
51 import org.apache.geronimo.kernel.log.GeronimoLogFactory;
52 import org.apache.geronimo.kernel.log.GeronimoLogging;
53 import org.apache.geronimo.system.logging.SystemLog;
54 import org.apache.geronimo.system.serverinfo.DirectoryUtils;
55 import org.apache.geronimo.system.serverinfo.ServerConstants;
56 import org.apache.geronimo.system.serverinfo.ServerInfo;
57 import org.apache.log4j.FileAppender;
58 import org.apache.log4j.Level;
59 import org.apache.log4j.LogManager;
60 import org.apache.log4j.Logger;
61
62 /**
63  * A Log4j logging service.
64  *
65  * @version $Rev: 485321 $ $Date: 2006-12-10 19:14:46 -0500 (Sun, 10 Dec 2006) $
66  */

67 public class Log4jService implements GBeanLifecycle, SystemLog {
68     // A substitution variable in the file path in the config file
69
private final static Pattern JavaDoc VARIABLE_PATTERN = Pattern.compile("\\$\\{.*?\\}");
70     // Next 6 are patterns that identify log messages in our default format
71
private final static Pattern JavaDoc DEFAULT_ANY_START = Pattern.compile("^\\d\\d\\:\\d\\d\\:\\d\\d\\,\\d\\d\\d (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) .*");
72     private final static Pattern JavaDoc DEFAULT_FATAL_START = Pattern.compile("^\\d\\d\\:\\d\\d\\:\\d\\d\\,\\d\\d\\d FATAL .*");
73     private final static Pattern JavaDoc DEFAULT_ERROR_START = Pattern.compile("^\\d\\d\\:\\d\\d\\:\\d\\d\\,\\d\\d\\d (ERROR|FATAL) .*");
74     private final static Pattern JavaDoc DEFAULT_WARN_START = Pattern.compile("^\\d\\d\\:\\d\\d\\:\\d\\d\\,\\d\\d\\d (WARN|ERROR|FATAL) .*");
75     private final static Pattern JavaDoc DEFAULT_INFO_START = Pattern.compile("^\\d\\d\\:\\d\\d\\:\\d\\d\\,\\d\\d\\d (INFO|WARN|ERROR|FATAL) .*");
76     private final static Pattern JavaDoc DEFAULT_DEBUG_START = Pattern.compile("^\\d\\d\\:\\d\\d\\:\\d\\d\\,\\d\\d\\d (DEBUG|INFO|WARN|ERROR|FATAL) .*");
77     // Next 6 are patterns that identify log messages if the user changed the format -- but we assume the log level is in there somewhere
78
private final static Pattern JavaDoc UNKNOWN_ANY_START = Pattern.compile("(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)");
79     private final static Pattern JavaDoc UNKNOWN_FATAL_START = Pattern.compile("FATAL");
80     private final static Pattern JavaDoc UNKNOWN_ERROR_START = Pattern.compile("(ERROR|FATAL)");
81     private final static Pattern JavaDoc UNKNOWN_WARN_START = Pattern.compile("(WARN|ERROR|FATAL)");
82     private final static Pattern JavaDoc UNKNOWN_INFO_START = Pattern.compile("(INFO|WARN|ERROR|FATAL)");
83     private final static Pattern JavaDoc UNKNOWN_DEBUG_START = Pattern.compile("(DEBUG|INFO|WARN|ERROR|FATAL)");
84     // Pattern that matches a single line (used to calculate line numbers and check for follow-on stack traces)
85
private final static Pattern JavaDoc FULL_LINE_PATTERN = Pattern.compile("^.*", Pattern.MULTILINE);
86
87
88     /**
89      * The URL to the configuration file.
90      */

91     private String JavaDoc configurationFile;
92
93     /**
94      * The time (in seconds) between checking for new config.
95      */

96     private int refreshPeriod;
97
98     /**
99      * The properties service
100      */

101     private final ServerInfo serverInfo;
102
103     /**
104      * The URL watch timer (in daemon mode).
105      */

106     private Timer JavaDoc timer = new Timer JavaDoc(true);
107
108     /**
109      * A monitor to check when the config URL changes.
110      */

111     private TimerTask JavaDoc monitor;
112
113     /**
114      * Last time the file was changed.
115      */

116     private long lastChanged = -1;
117
118     /**
119      * Is this service running?
120      */

121     private boolean running = false;
122
123     /**
124      * Construct a <code>Log4jService</code>.
125      *
126      * @param configurationFile The log4j configuration file.
127      * @param refreshPeriod The refresh refreshPeriod (in seconds).
128      */

129     public Log4jService(final String JavaDoc configurationFile, final int refreshPeriod, ServerInfo serverInfo) {
130         this.refreshPeriod = refreshPeriod;
131         this.configurationFile = configurationFile;
132         this.serverInfo = serverInfo;
133     }
134
135     /**
136      * Gets the level of the root logger.
137      */

138     public synchronized String JavaDoc getRootLoggerLevel() {
139         Level level = LogManager.getRootLogger().getLevel();
140
141         if (level != null) {
142             return level.toString();
143         }
144
145         return null;
146     }
147
148     /**
149      * Sets the level of the root logger.
150      *
151      * @param level The level to change the logger to.
152      */

153     public synchronized void setRootLoggerLevel(final String JavaDoc level) {
154
155         String JavaDoc currentLevel = this.getRootLoggerLevel();
156
157         // ensure that the level has really been changed
158
if (!currentLevel.equals(level)) {
159             LogManager.getRootLogger().setLevel(XLevel.toLevel(level));
160         }
161     }
162
163     /**
164      * Gets the level of the logger of the give name.
165      *
166      * @param logger The logger to inspect.
167      */

168     public String JavaDoc getLoggerEffectiveLevel(final String JavaDoc logger) {
169         if (logger == null) {
170             throw new IllegalArgumentException JavaDoc("logger is null");
171         }
172
173         Level level = LogManager.getLogger(logger).getEffectiveLevel();
174
175         if (level != null) {
176             return level.toString();
177         }
178
179         return null;
180     }
181
182     /**
183      * Gets the level of the logger of the give name.
184      *
185      * @param logger The logger to inspect.
186      */

187     public String JavaDoc getLoggerLevel(final String JavaDoc logger) {
188         if (logger == null) {
189             throw new IllegalArgumentException JavaDoc("logger is null");
190         }
191
192         Level level = LogManager.getLogger(logger).getLevel();
193
194         if (level != null) {
195             return level.toString();
196         }
197
198         return null;
199     }
200
201     /**
202      * Sets the level for a logger of the give name.
203      *
204      * @param logger The logger to change level
205      * @param level The level to change the logger to.
206      */

207     public void setLoggerLevel(final String JavaDoc logger, final String JavaDoc level) {
208         if (logger == null) {
209             throw new IllegalArgumentException JavaDoc("logger is null");
210         }
211         if (level == null) {
212             throw new IllegalArgumentException JavaDoc("level is null");
213         }
214
215         Logger.getLogger(logger).setLevel(XLevel.toLevel(level));
216     }
217
218     /**
219      * Get the refresh period.
220      *
221      * @return the refresh period (in seconds)
222      */

223     public synchronized int getRefreshPeriodSeconds() {
224         return refreshPeriod;
225     }
226
227     /**
228      * Set the refresh period.
229      *
230      * @param period the refresh period (in seconds)
231      * @throws IllegalArgumentException if refresh period is <= 0
232      */

233     public synchronized void setRefreshPeriodSeconds(final int period) {
234         if (period < 1) {
235             throw new IllegalArgumentException JavaDoc("Refresh period must be > 0");
236         }
237
238         if (this.refreshPeriod != period) {
239             this.refreshPeriod = period;
240             schedule();
241         }
242     }
243
244     /**
245      * Get the logging configuration URL.
246      *
247      * @return the logging configuration URL
248      */

249     public synchronized String JavaDoc getConfigFileName() {
250         return configurationFile;
251     }
252
253     /**
254      * Set the logging configuration URL.
255      *
256      * @param configurationFile the logging configuration file
257      */

258     public synchronized void setConfigFileName(final String JavaDoc configurationFile) {
259         if (configurationFile == null) {
260             throw new IllegalArgumentException JavaDoc("configurationFile is null");
261         }
262
263         // ensure that the file name has really been updated
264
if (!this.configurationFile.equals(configurationFile)) {
265             this.configurationFile = configurationFile;
266             lastChanged = -1;
267         }
268     }
269
270     /**
271      * Get the content of logging configuration file.
272      *
273      * @return the content of logging configuration file
274      */

275     public synchronized String JavaDoc getConfiguration() {
276         File JavaDoc file = resolveConfigurationFile();
277         if (file == null || !file.canRead()) {
278             return null;
279         }
280         Reader JavaDoc in = null;
281         try {
282             StringBuffer JavaDoc configuration = new StringBuffer JavaDoc();
283             in = new InputStreamReader JavaDoc(new FileInputStream JavaDoc(file));
284             char[] buffer = new char[4096];
285             for (int size = in.read(buffer); size >= 0; size = in.read(buffer)) {
286                 configuration.append(buffer, 0, size);
287             }
288             return configuration.toString();
289         } catch (IOException JavaDoc e) {
290             e.printStackTrace();
291         } finally {
292             if (in != null) {
293                 try {
294                     in.close();
295                 } catch (IOException JavaDoc e1) {
296                     e1.printStackTrace();
297                 }
298             }
299         }
300         return null;
301     }
302
303     /**
304      * Overwrites the content of logging configuration file.
305      *
306      * @param configuration the new content of logging configuration file
307      */

308     public synchronized void setConfiguration(final String JavaDoc configuration) throws IOException JavaDoc {
309         if (configuration == null || configuration.length() == 0) {
310             throw new IllegalArgumentException JavaDoc("configuration is null or an empty string");
311         }
312
313         File JavaDoc file = resolveConfigurationFile();
314         if (file == null) {
315             throw new IllegalStateException JavaDoc("Configuration file is null");
316         }
317
318         // make parent directory if necessary
319
if (!file.getParentFile().exists()) {
320             if (!file.getParentFile().mkdirs()) {
321                 throw new IllegalStateException JavaDoc("Could not create parent directory of log configuration file: " + file.getParent());
322             }
323         }
324
325         // verify that the file is writable or does not exist
326
if (file.exists() && !file.canWrite()) {
327             throw new IllegalStateException JavaDoc("Configuration file is not writable: " + file.getAbsolutePath());
328         }
329
330         OutputStream JavaDoc out = null;
331         try {
332             out = new FileOutputStream JavaDoc(file);
333             out.write(configuration.getBytes());
334         } finally {
335             if (out != null) {
336                 try {
337                     out.close();
338                 } catch (IOException JavaDoc e) {
339                     e.printStackTrace();
340                 }
341             }
342         }
343     }
344
345     public synchronized String JavaDoc[] getLogFileNames() {
346         List JavaDoc list = new ArrayList JavaDoc();
347         for (Enumeration JavaDoc e = Logger.getRootLogger().getAllAppenders(); e.hasMoreElements();) {
348             Object JavaDoc appender = e.nextElement();
349             if (appender instanceof FileAppender) {
350                 list.add(((FileAppender) appender).getFile());
351             }
352         }
353         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
354     }
355
356     private static SearchResults searchFile(File JavaDoc file, String JavaDoc targetLevel, Pattern JavaDoc textSearch, Integer JavaDoc start, Integer JavaDoc stop, int max, boolean stacks) {
357         List JavaDoc list = new LinkedList JavaDoc();
358         boolean capped = false;
359         int lineCount = 0;
360         try {
361             RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(file, "r");
362             FileChannel JavaDoc fc = raf.getChannel();
363             MappedByteBuffer JavaDoc bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
364             CharBuffer JavaDoc cb = Charset.forName("US-ASCII").decode(bb); //todo: does Log4J use a different charset on a foreign PC?
365
Matcher JavaDoc target = null;
366             Matcher JavaDoc any = null;
367             Matcher JavaDoc lines = FULL_LINE_PATTERN.matcher(cb);
368             Matcher JavaDoc text = textSearch == null ? null : textSearch.matcher("");
369             boolean hit = false;
370             max = Math.min(max, MAX_SEARCH_RESULTS);
371             while(lines.find()) {
372                 ++lineCount;
373                 if(target == null) {
374                     if(DEFAULT_ANY_START.matcher(cb.subSequence(lines.start(), lines.end())).find()) {
375                         target = getDefaultPatternForLevel(targetLevel).matcher("");
376                         any = DEFAULT_ANY_START.matcher("");
377                     } else {
378                         target = getUnknownPatternForLevel(targetLevel).matcher("");
379                         any = UNKNOWN_ANY_START.matcher("");
380                     }
381                 }
382                 if(start != null && start.intValue() > lineCount) {
383                     continue;
384                 }
385                 if(stop != null && stop.intValue() < lineCount) {
386                     continue;
387                 }
388                 CharSequence JavaDoc line = cb.subSequence(lines.start(), lines.end());
389                 target.reset(line);
390                 if(target.find()) {
391                     if(text != null) {
392                         text.reset(line);
393                         if(!text.find()) {
394                             hit = false;
395                             continue;
396                         }
397                     }
398                     list.add(new LogMessage(lineCount,line.toString()));
399                     if(list.size() > max) {
400                         list.remove(0);
401                         capped = true;
402                     }
403                     hit = true;
404                 } else if(stacks && hit) {
405                     any.reset(line);
406                     if(!any.find()) {
407                         list.add(new LogMessage(lineCount,line.toString()));
408                         if(list.size() > max) {
409                             list.remove(0);
410                             capped = true;
411                         }
412                     } else {
413                         hit = false;
414                     }
415                 }
416             }
417             fc.close();
418             raf.close();
419         } catch (Exception JavaDoc e) {} // TODO: improve exception handling
420
return new SearchResults(lineCount, (LogMessage[]) list.toArray(new LogMessage[list.size()]), capped);
421     }
422
423     private static String JavaDoc substituteSystemProps(String JavaDoc source) {
424         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
425         int last = 0;
426         Matcher JavaDoc m = VARIABLE_PATTERN.matcher(source);
427         while(m.find()) {
428             buf.append(source.substring(last, m.start()));
429             String JavaDoc prop = source.substring(m.start()+2, m.end()-1);
430             buf.append(System.getProperty(prop));
431             last = m.end();
432         }
433         buf.append(source.substring(last));
434         return buf.toString();
435     }
436
437     private static Pattern JavaDoc getDefaultPatternForLevel(String JavaDoc targetLevel) {
438         if(targetLevel.equals("FATAL")) {
439             return DEFAULT_FATAL_START;
440         } else if(targetLevel.equals("ERROR")) {
441             return DEFAULT_ERROR_START;
442         } else if(targetLevel.equals("WARN")) {
443             return DEFAULT_WARN_START;
444         } else if(targetLevel.equals("INFO")) {
445             return DEFAULT_INFO_START;
446         } else if(targetLevel.equals("DEBUG")) {
447             return DEFAULT_DEBUG_START;
448         } else {
449             return DEFAULT_ANY_START;
450         }
451     }
452
453     private static Pattern JavaDoc getUnknownPatternForLevel(String JavaDoc targetLevel) {
454         if(targetLevel.equals("FATAL")) {
455             return UNKNOWN_FATAL_START;
456         } else if(targetLevel.equals("ERROR")) {
457             return UNKNOWN_ERROR_START;
458         } else if(targetLevel.equals("WARN")) {
459             return UNKNOWN_WARN_START;
460         } else if(targetLevel.equals("INFO")) {
461             return UNKNOWN_INFO_START;
462         } else if(targetLevel.equals("DEBUG")) {
463             return UNKNOWN_DEBUG_START;
464         } else {
465             return UNKNOWN_ANY_START;
466         }
467     }
468
469     public SearchResults getMatchingItems(String JavaDoc logFile, Integer JavaDoc firstLine, Integer JavaDoc lastLine, String JavaDoc minLevel, String JavaDoc text, int maxResults, boolean includeStackTraces) {
470         // Ensure the file argument is really a log file!
471
if(logFile == null) {
472             throw new IllegalArgumentException JavaDoc("Must specify a log file");
473         }
474         String JavaDoc[] files = getLogFileNames();
475         boolean found = false;
476         for (int i = 0; i < files.length; i++) {
477             if(files[i].equals(logFile)) {
478                 found = true;
479                 break;
480             }
481         }
482         if(!found) {
483             throw new IllegalArgumentException JavaDoc("Not a log file!");
484         }
485         // Check for valid log level
486
if(minLevel == null) {
487             minLevel = "TRACE";
488         } else if(!minLevel.equals("FATAL") && !minLevel.equals("ERROR") && !minLevel.equals("WARN") &&
489                 !minLevel.equals("INFO") && !minLevel.equals("DEBUG") && !minLevel.equals("TRACE")) {
490             throw new IllegalArgumentException JavaDoc("Not a valid log level");
491         }
492         // Check that the text pattern is valid
493
Pattern JavaDoc textPattern = null;
494         try {
495             textPattern = text == null || text.equals("") ? null : Pattern.compile(text);
496         } catch (PatternSyntaxException JavaDoc e) {
497             throw new IllegalArgumentException JavaDoc("Bad regular expression '"+text+"'");
498         }
499         // Make sure we can find the log file
500
File JavaDoc log = new File JavaDoc(substituteSystemProps(logFile));
501         if(!log.exists()) {
502             throw new IllegalArgumentException JavaDoc("Log file "+log.getAbsolutePath()+" does not exist");
503         }
504         // Run the search
505
return searchFile(log, minLevel, textPattern, firstLine, lastLine, maxResults, includeStackTraces);
506     }
507
508     /**
509      * Force the logging system to reconfigure.
510      */

511     public void reconfigure() {
512         File JavaDoc file = resolveConfigurationFile();
513         if (file == null || !file.exists()) {
514             return;
515         }
516
517         // Record the default console log level
518
System.setProperty("org.apache.geronimo.log.ConsoleLogLevel", GeronimoLogging.getConsoleLogLevel().toString());
519
520         try {
521             URLConfigurator.configure(file.toURL());
522         } catch (MalformedURLException JavaDoc e) {
523             e.printStackTrace();
524         }
525
526         // refresh the level info for every log
527
GeronimoLogFactory logFactory = (GeronimoLogFactory) LogFactory.getFactory();
528         Set JavaDoc instances = logFactory.getInstances();
529         for (Iterator JavaDoc iterator = instances.iterator(); iterator.hasNext();) {
530             Object JavaDoc log = iterator.next();
531             if (log instanceof CachingLog4jLog) {
532                 ((CachingLog4jLog)log).updateLevelInfo();
533             }
534         }
535     }
536
537     private synchronized void schedule() {
538         if (timer != null) {
539             // kill the old monitor
540
if (monitor != null) {
541                 monitor.cancel();
542             }
543
544             // start the new one
545
monitor = new URLMonitorTask();
546             TimerTask JavaDoc task = monitor;
547             timer.schedule(monitor, 1000 * refreshPeriod, 1000 * refreshPeriod);
548             task.run();
549         }
550     }
551
552     public void doStart() {
553         LogFactory logFactory = LogFactory.getFactory();
554         if (logFactory instanceof GeronimoLogFactory) {
555             synchronized (this) {
556                 timer = new Timer JavaDoc(true);
557
558                 // Periodically check the configuration file
559
schedule();
560
561                 // Make sure the root Logger has loaded
562
Logger logger = LogManager.getRootLogger();
563
564                 reconfigure();
565
566                 File JavaDoc file = resolveConfigurationFile();
567                 if (file != null) {
568                     lastChanged = file.lastModified();
569                 }
570                 logEnvInfo(logger);
571             }
572
573             // Change all of the loggers over to use log4j
574
GeronimoLogFactory geronimoLogFactory = (GeronimoLogFactory) logFactory;
575             synchronized (geronimoLogFactory) {
576                 if (!(geronimoLogFactory.getLogFactory() instanceof CachingLog4jLogFactory)) {
577                     geronimoLogFactory.setLogFactory(new CachingLog4jLogFactory());
578                 }
579             }
580         }
581
582         synchronized (this) {
583             running = true;
584         }
585     }
586
587     public synchronized void doStop() {
588         running = false;
589         if (monitor != null) {
590             monitor.cancel();
591             monitor = null;
592         }
593         if (timer != null) {
594             timer.cancel();
595             timer = null;
596         }
597     }
598
599     public void doFail() {
600         doStop();
601     }
602
603     private synchronized File JavaDoc resolveConfigurationFile() {
604         try {
605             return serverInfo.resolveServer(configurationFile);
606         } catch (Exception JavaDoc e) {
607             return null;
608         }
609     }
610
611     private void logEnvInfo(Logger log) {
612        try {
613           log.info("----------------------------------------------");
614           log.info("Started Logging Service");
615           log.debug("Log4jService created with configFileName=" + this.configurationFile +
616                     ", refreshPeriodSeconds=" + this.refreshPeriod);
617           log.info("Runtime Information:");
618           log.info(" Install Directory = " + DirectoryUtils.getGeronimoInstallDirectory().toString());
619           log.info(" JVM in use = " + System.getProperty("java.vendor") + " Java " + System.getProperty("java.version"));
620           log.info("Java Information:");
621           log.info(" System property [java.runtime.name] = " + System.getProperty("java.runtime.name"));
622           log.info(" System property [java.runtime.version] = " + System.getProperty("java.runtime.version"));
623           log.info(" System property [os.name] = " + System.getProperty("os.name"));
624           log.info(" System property [os.version] = " + System.getProperty("os.version"));
625           log.info(" System property [sun.os.patch.level] = " + System.getProperty("sun.os.patch.level"));
626           log.info(" System property [os.arch] = " + System.getProperty("os.arch"));
627           log.info(" System property [java.class.version] = " + System.getProperty("java.class.version"));
628           log.info(" System property [locale] = " + System.getProperty("user.language") + "_" + System.getProperty("user.country"));
629           log.info(" System property [unicode.encoding] = " + System.getProperty("sun.io.unicode.encoding"));
630           log.info(" System property [file.encoding] = " + System.getProperty("file.encoding"));
631           log.info(" System property [java.vm.name] = " + System.getProperty("java.vm.name"));
632           log.info(" System property [java.vm.vendor] = " + System.getProperty("java.vm.vendor"));
633           log.info(" System property [java.vm.version] = " + System.getProperty("java.vm.version"));
634           log.info(" System property [java.vm.info] = " + System.getProperty("java.vm.info"));
635           log.info(" System property [java.home] = " + System.getProperty("java.home"));
636           log.info(" System property [java.classpath] = " + System.getProperty("java.classpath"));
637           log.info(" System property [java.library.path] = " + System.getProperty("java.library.path"));
638           log.info(" System property [java.endorsed.dirs] = " + System.getProperty("java.endorsed.dirs"));
639           log.info(" System property [java.ext.dirs] = " + System.getProperty("java.ext.dirs"));
640           log.info(" System property [sun.boot.class.path] = " + System.getProperty("sun.boot.class.path"));
641           log.info("----------------------------------------------");
642        } catch (Exception JavaDoc e) {
643           String JavaDoc msg = "Exception caught during logging of Runtime Information. Exception=" + e.toString();
644           log.error(msg);
645           System.err.println(msg);
646        }
647     }
648
649
650     private class URLMonitorTask extends TimerTask JavaDoc {
651         public void run() {
652             try {
653                 long lastModified;
654                 synchronized (this) {
655                     if (running == false) {
656                         return;
657                     }
658
659                     File JavaDoc file = resolveConfigurationFile();
660                     if (file == null) {
661                         return;
662                     }
663
664                     lastModified = file.lastModified();
665                 }
666
667                 if (lastChanged < lastModified) {
668                     lastChanged = lastModified;
669                     reconfigure();
670                 }
671             } catch (Exception JavaDoc e) {
672             }
673         }
674     }
675
676     private static class CachingLog4jLogFactory extends LogFactory {
677         public Log getInstance(Class JavaDoc clazz) throws LogConfigurationException {
678             return getInstance(clazz.getName());
679         }
680
681         public Log getInstance(String JavaDoc name) throws LogConfigurationException {
682             return new CachingLog4jLog(name);
683         }
684
685         public Object JavaDoc getAttribute(String JavaDoc name) {
686             return null;
687         }
688
689         public String JavaDoc[] getAttributeNames() {
690             return new String JavaDoc[0];
691         }
692
693         public void release() {
694         }
695
696         public void removeAttribute(String JavaDoc name) {
697         }
698
699         public void setAttribute(String JavaDoc name, Object JavaDoc value) {
700         }
701     }
702
703     public static final GBeanInfo GBEAN_INFO;
704
705     static {
706         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Log4jService.class, "SystemLog");
707
708         infoFactory.addAttribute("configFileName", String JavaDoc.class, true);
709         infoFactory.addAttribute("refreshPeriodSeconds", int.class, true);
710         infoFactory.addAttribute("configuration", String JavaDoc.class, false);
711         infoFactory.addAttribute("rootLoggerLevel", String JavaDoc.class, false);
712
713         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
714
715         infoFactory.addOperation("reconfigure", "void");
716         infoFactory.addOperation("setLoggerLevel", new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class}, "void");
717         infoFactory.addOperation("getLoggerLevel", new Class JavaDoc[]{String JavaDoc.class}, "java.lang.String");
718         infoFactory.addOperation("getLoggerEffectiveLevel", new Class JavaDoc[]{String JavaDoc.class}, "java.lang.String");
719         infoFactory.addInterface(SystemLog.class);
720
721         infoFactory.setConstructor(new String JavaDoc[]{"configFileName", "refreshPeriodSeconds", "ServerInfo"});
722
723         GBEAN_INFO = infoFactory.getBeanInfo();
724     }
725
726     public static GBeanInfo getGBeanInfo() {
727         return GBEAN_INFO;
728     }
729 }
730
Popular Tags