summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/main/java/org/apache/qpid/server/logging/management/LoggingManagementMBean.java
blob: a823fb7cb19d2328b24de2d38d00341e0e675a8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.    
 *
 * 
 */
package org.apache.qpid.server.logging.management;

import static org.apache.log4j.xml.QpidLog4JConfigurator.LOCK;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.qpid.management.common.mbeans.LoggingManagement;
import org.apache.qpid.management.common.mbeans.annotations.MBeanDescription;
import org.apache.qpid.server.management.AMQManagedObject;
import org.apache.qpid.util.FileUtils;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.Log4jEntityResolver;
import org.apache.log4j.xml.QpidLog4JConfigurator;
import org.apache.log4j.xml.QpidLog4JConfigurator.QpidLog4JSaxErrorHandler;
import org.apache.log4j.xml.QpidLog4JConfigurator.IllegalLoggerLevelException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.management.JMException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


/** MBean class for BrokerLoggingManagerMBean. It implements all the management features exposed for managing logging. */
@MBeanDescription("Logging Management Interface")
public class LoggingManagementMBean extends AMQManagedObject implements LoggingManagement
{

    private static final Logger _logger = Logger.getLogger(LoggingManagementMBean.class);
    private String _log4jConfigFileName;
    private int _log4jLogWatchInterval;
    private static final String INHERITED = "INHERITED";
    private static final String[] LEVELS = new String[]{Level.ALL.toString(), Level.TRACE.toString(), 
                                                        Level.DEBUG.toString(), Level.INFO.toString(), 
                                                        Level.WARN.toString(), Level.ERROR.toString(), 
                                                        Level.FATAL.toString(),Level.OFF.toString(),
                                                        INHERITED};   
    static TabularType _loggerLevelTabularType;
    static CompositeType _loggerLevelCompositeType;

    static
    {
        try
        {
            OpenType[] loggerLevelItemTypes = new OpenType[]{SimpleType.STRING, SimpleType.STRING};

            _loggerLevelCompositeType = new CompositeType("LoggerLevelList", "Logger Level Data", 
                                                         COMPOSITE_ITEM_NAMES.toArray(new String[COMPOSITE_ITEM_NAMES.size()]),
                                                         COMPOSITE_ITEM_DESCRIPTIONS.toArray(new String[COMPOSITE_ITEM_DESCRIPTIONS.size()]),
                                                         loggerLevelItemTypes);

            _loggerLevelTabularType = new TabularType("LoggerLevel", "List of loggers with levels",
                                                       _loggerLevelCompositeType, 
                                                       TABULAR_UNIQUE_INDEX.toArray(new String[TABULAR_UNIQUE_INDEX.size()]));
        }
        catch (OpenDataException e)
        {
            _logger.error("Tabular data setup for viewing logger levels was incorrect.");
            _loggerLevelTabularType = null;
        }
    }
    
    public LoggingManagementMBean(String log4jConfigFileName, int log4jLogWatchInterval) throws JMException
    {
        super(LoggingManagement.class, LoggingManagement.TYPE);
        _log4jConfigFileName = log4jConfigFileName;
        _log4jLogWatchInterval = log4jLogWatchInterval;
    }

    public String getObjectInstanceName()
    {
        return LoggingManagement.TYPE;
    }
    
    public Integer getLog4jLogWatchInterval()
    {
        return _log4jLogWatchInterval;
    }
    
    public String[] getAvailableLoggerLevels()
    {
        return LEVELS;
    }
    @SuppressWarnings("unchecked")
    public synchronized boolean setRuntimeLoggerLevel(String logger, String level)
    {   
        //check specified level is valid
        Level newLevel;
        try
        {
            newLevel = getLevel(level);
        }
        catch (Exception e)
        {
            return false;
        }
        
        //check specified logger exists
        Enumeration loggers = LogManager.getCurrentLoggers();
        Boolean loggerExists = false;
        
        while(loggers.hasMoreElements())
        {
            Logger log = (Logger) loggers.nextElement();
            if (log.getName().equals(logger))
            {
                loggerExists = true;
                break;
            }
        }
        
        if(!loggerExists)
        {
            return false;
        }
        
        //set the logger to the new level
        _logger.info("Setting level to " + level + " for logger: " + logger);
        
        Logger log = Logger.getLogger(logger);
        log.setLevel(newLevel);
        
        return true;
    }
    
    @SuppressWarnings("unchecked")
    public synchronized TabularData viewEffectiveRuntimeLoggerLevels()
    {
        if (_loggerLevelTabularType == null)
        {
            _logger.warn("TabluarData type not set up correctly");
            return null;
        }

        _logger.info("Getting levels for currently active log4j loggers");
        
        Enumeration loggers = LogManager.getCurrentLoggers();

        TabularData loggerLevelList = new TabularDataSupport(_loggerLevelTabularType);

        Logger logger;
        String loggerName;
        String level;
        
        try
        {
            while(loggers.hasMoreElements()){
                logger = (Logger) loggers.nextElement();

                loggerName = logger.getName();
                level = logger.getEffectiveLevel().toString();

                Object[] itemData = {loggerName, level};
                CompositeData loggerData = new CompositeDataSupport(_loggerLevelCompositeType, 
                        COMPOSITE_ITEM_NAMES.toArray(new String[COMPOSITE_ITEM_NAMES.size()]), itemData);
                loggerLevelList.put(loggerData);
            }
        }
        catch (OpenDataException e)
        {
            _logger.warn("Unable to create logger level list due to :" + e);
            return null;
        }

        return loggerLevelList;
        
    }
    
    public synchronized String getRuntimeRootLoggerLevel()
    {
        Logger rootLogger = Logger.getRootLogger();

        return rootLogger.getLevel().toString();
    }

    public synchronized boolean setRuntimeRootLoggerLevel(String level)
    {
        Level newLevel;
        try
        {
            newLevel = getLevel(level);
        }
        catch (Exception e)
        {
            return false;
        }
        
        if(newLevel == null)
        {
            //A null Level reference implies inheritance. Setting the runtime RootLogger 
            //to null is catastrophic (and prevented by Log4J at startup and runtime anyway).
            return false;
        }

        _logger.info("Setting RootLogger level to " + level);

        Logger log = Logger.getRootLogger();
        log.setLevel(newLevel);

        return true;
    }
    
    //method to convert from a string to a log4j Level, throws exception if the given value is invalid
    private Level getLevel(String level) throws Exception
    {
        if("null".equalsIgnoreCase(level) || INHERITED.equalsIgnoreCase(level))
        {
            //the string "null" or "inherited" signals to inherit from a parent logger,
            //using a null Level reference for the logger.
            return null;
        }
        
        Level newLevel = Level.toLevel(level);
        
        //above Level.toLevel call returns a DEBUG Level if the request fails. Check the result.
        if (newLevel.equals(Level.DEBUG) && !(level.equalsIgnoreCase("debug")))
        {
            //received DEBUG but we did not ask for it, the Level request failed.
            throw new Exception("Invalid level name");
        }
        
        return newLevel;
    }
    
    //method to parse the XML configuration file, validating it in the process, and returning a DOM Document of the content.
    private static synchronized Document parseConfigFile(String fileName) throws IOException
    {
        try
        {
            LOCK.lock();

            //check file was specified, exists, and is readable
            if(fileName == null)
            {
                _logger.warn("Provided log4j XML configuration filename is null");
                throw new IOException("Provided log4j XML configuration filename is null");
            }

            File configFile = new File(fileName);

            if (!configFile.exists())
            {
                _logger.warn("The log4j XML configuration file could not be found: " + fileName);
                throw new IOException("The log4j XML configuration file could not be found");
            }
            else if (!configFile.canRead())
            {
                _logger.warn("The log4j XML configuration file is not readable: " + fileName);
                throw new IOException("The log4j XML configuration file is not readable");
            }

            //parse it
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            Document doc;

            ErrorHandler errHandler = new QpidLog4JSaxErrorHandler();
            try
            {
                docFactory.setValidating(true);
                docBuilder = docFactory.newDocumentBuilder();
                docBuilder.setErrorHandler(errHandler);
                docBuilder.setEntityResolver(new Log4jEntityResolver());
                doc = docBuilder.parse(fileName);
            }
            catch (ParserConfigurationException e)
            {
                _logger.warn("Unable to parse the log4j XML file due to possible configuration error: " + e);
                //recommended that MBeans should use java.* and javax.* exceptions only
                throw new IOException("Unable to parse the log4j XML file due to possible configuration error: " + e.getMessage());
            }
            catch (SAXException e)
            {
                _logger.warn("The specified log4j XML file is invalid: " + e);
                //recommended that MBeans should use standard java.* and javax.* exceptions only
                throw new IOException("The specified log4j XML file is invalid: " + e.getMessage());
            }
            catch (IOException e)
            {
                _logger.warn("Unable to parse the specified log4j XML file" + e);
                throw new IOException("Unable to parse the specified log4j XML file: " + e.getMessage());
            }

            return doc;
        }
        finally
        {
            LOCK.unlock();
        }
    }

    
    private static synchronized boolean writeUpdatedConfigFile(String log4jConfigFileName, Document doc) throws IOException
    {
        try
        {
            LOCK.lock();

            File log4jConfigFile = new File(log4jConfigFileName);

            if (!log4jConfigFile.canWrite())
            {
                _logger.warn("Specified log4j XML configuration file is not writable: " + log4jConfigFile);
                throw new IOException("Specified log4j XML configuration file is not writable");
            }

            Transformer transformer = null;
            try
            {
                transformer = TransformerFactory.newInstance().newTransformer();
            }
            catch (Exception e)
            {
                _logger.warn("Could not create an XML transformer: " +e);
                return false;
            }

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "log4j.dtd");
            DOMSource source = new DOMSource(doc);

            File tmp;
            Random r = new Random();
            do
            {
                tmp = new File(log4jConfigFile.getPath() + r.nextInt() + ".tmp");
            }
            while(tmp.exists());
            
            tmp.deleteOnExit();
            
            try
            {
                StreamResult result = new StreamResult(tmp);
                transformer.transform(source, result);
            }
            catch (TransformerException e)
            {
                _logger.warn("Could not transform the XML into new file: " +e);
                throw new IOException("Could not transform the XML into new file: " +e);
            }

            // Swap temp file in to replace existing configuration file.
            File old = new File(log4jConfigFile.getAbsoluteFile() + ".old");
            if (old.exists())
            {
                old.delete();
            }
            
            if(!log4jConfigFile.renameTo(old))
            {
                //unable to rename the existing file to the backup name 
                _logger.error("Could not backup the existing log4j XML file");
                throw new IOException("Could not backup the existing log4j XML file");
            }

            if(!tmp.renameTo(log4jConfigFile))
            {
                //failed to rename the new file to the required filename
                
                if(!old.renameTo(log4jConfigFile))
                {
                    //unable to return the backup to required filename
                    _logger.error("Could not rename the new log4j configuration file into place, and unable to restore original file");
                    throw new IOException("Could not rename the new log4j configuration file into place, and unable to restore original file");
                }
                
                _logger.error("Could not rename the new log4j configuration file into place");
                throw new IOException("Could not rename the new log4j configuration file into place");
            }
            
            return true;
        }
        finally
        {
            LOCK.unlock();
        }
    }


    /* The log4j XML configuration file DTD defines three possible element
     * combinations for specifying optional logger+level settings.
     * Must account for the following:
     * 
     * <category name="x"> <priority value="y"/> </category>    OR
     * <category name="x"> <level value="y"/> </category>    OR
     * <logger name="x"> <level value="y"/> </logger>
     *
     * Noting also that the level/priority child element is optional too,
     * and not the only possible child element.
     */
    
    public static synchronized Map<String,String> retrieveConfigFileLoggersLevels(String fileName) throws IOException
    {
        try
        {
            LOCK.lock();

            Document doc = parseConfigFile(fileName);

            HashMap<String,String> loggerLevelList = new HashMap<String,String>();

            //retrieve the 'category' element nodes
            NodeList categoryElements = doc.getElementsByTagName("category");

            String categoryName;
            String priority = null;

            for (int i = 0; i < categoryElements.getLength(); i++)
            {
                Element categoryElement = (Element) categoryElements.item(i);
                categoryName = categoryElement.getAttribute("name");

                //retrieve the category's mandatory 'priority' or 'level' element's value.
                //It may not be the only child node, so request by tag name.
                NodeList priorityElements = categoryElement.getElementsByTagName("priority");
                NodeList levelElements = categoryElement.getElementsByTagName("level");

                if (priorityElements.getLength() != 0)
                {
                    Element priorityElement = (Element) priorityElements.item(0);
                    priority = priorityElement.getAttribute("value");
                }
                else if (levelElements.getLength() != 0)
                {
                    Element levelElement = (Element) levelElements.item(0);
                    priority = levelElement.getAttribute("value");
                }
                else
                {
                    //there is no exiting priority or level to view, move onto next category/logger
                    continue;
                }

                loggerLevelList.put(categoryName, priority);
            }

            //retrieve the 'logger' element nodes
            NodeList loggerElements = doc.getElementsByTagName("logger");

            String loggerName;
            String level;

            for (int i = 0; i < loggerElements.getLength(); i++)
            {
                Element loggerElement = (Element) loggerElements.item(i);
                loggerName = loggerElement.getAttribute("name");

                //retrieve the logger's mandatory 'level' element's value
                //It may not be the only child node, so request by tag name.
                NodeList levelElements = loggerElement.getElementsByTagName("level");

                Element levelElement = (Element) levelElements.item(0);
                level = levelElement.getAttribute("value");

                loggerLevelList.put(loggerName, level);
            }

            return loggerLevelList;
        }
        finally
        {
            LOCK.unlock();
        }
    }

    public synchronized TabularData viewConfigFileLoggerLevels() throws IOException
    {
        try
        {
            LOCK.lock();    

            if (_loggerLevelTabularType == null)
            {
                _logger.warn("TabluarData type not set up correctly");
                return null;
            }

            _logger.info("Getting logger levels from log4j configuration file");

            TabularData loggerLevelList = new TabularDataSupport(_loggerLevelTabularType);

            Map<String,String> levels = retrieveConfigFileLoggersLevels(_log4jConfigFileName);

            for (Map.Entry<String,String> entry : levels.entrySet())
            {
                String loggerName = entry.getKey();
                String level = entry.getValue();

                try
                {
                    Object[] itemData = {loggerName, level.toUpperCase()};
                    CompositeData loggerData = new CompositeDataSupport(_loggerLevelCompositeType, 
                            COMPOSITE_ITEM_NAMES.toArray(new String[COMPOSITE_ITEM_NAMES.size()]), itemData);
                    loggerLevelList.put(loggerData);
                }
                catch (OpenDataException e)
                {
                    _logger.warn("Unable to create logger level list due to :" + e);
                    return null;
                }
            }

            return loggerLevelList;
        }
        finally
        {
            LOCK.unlock();
        }
    }

    public synchronized boolean setConfigFileLoggerLevel(String logger, String level) throws IOException
    {
        try
        {
            LOCK.lock();

            //check that the specified level is a valid log4j Level
            try
            {
                getLevel(level);
            }
            catch (Exception e)
            {
                //it isnt a valid level
                return false;
            }

            _logger.info("Setting level to " + level + " for logger '" + logger
                    + "' in log4j xml configuration file: " + _log4jConfigFileName);

            Document doc = parseConfigFile(_log4jConfigFileName);

            //retrieve the 'category' and 'logger' element nodes
            NodeList categoryElements = doc.getElementsByTagName("category");
            NodeList loggerElements = doc.getElementsByTagName("logger");

            //collect them into a single elements list
            List<Element> logElements = new ArrayList<Element>();

            for (int i = 0; i < categoryElements.getLength(); i++)
            {
                logElements.add((Element) categoryElements.item(i));
            }
            for (int i = 0; i < loggerElements.getLength(); i++)
            {
                logElements.add((Element) loggerElements.item(i));
            }

            //try to locate the specified logger/category in the elements retrieved
            Element logElement = null;
            for (Element e : logElements)
            {
                if (e.getAttribute("name").equals(logger))
                {
                    logElement = e;
                    break;
                }
            }

            if (logElement == null)
            {
                //no loggers/categories with given name found, does not exist to update
                _logger.warn("Specified logger does not exist in the configuration file: " +logger);
                return false;
            }

            //retrieve the optional 'priority' or 'level' sub-element value.
            //It may not be the only child node, so request by tag name.
            NodeList priorityElements = logElement.getElementsByTagName("priority");
            NodeList levelElements = logElement.getElementsByTagName("level");

            Element levelElement = null;
            if (priorityElements.getLength() != 0)
            {
                levelElement = (Element) priorityElements.item(0);
            }
            else if (levelElements.getLength() != 0)
            {
                levelElement = (Element) levelElements.item(0);
            }
            else
            {
                //there is no exiting priority or level element to update
                return false;
            }

            //update the element with the new level/priority
            levelElement.setAttribute("value", level.toLowerCase());

            //output the new file
            return writeUpdatedConfigFile(_log4jConfigFileName, doc);
        }
        finally
        {
            LOCK.unlock();
        }
    }

    
    /* The log4j XML configuration file DTD defines 2 possible element
     * combinations for specifying the optional root logger level settings
     * Must account for the following:
     * 
     * <root> <priority value="y"/> </root>    OR
     * <root> <level value="y"/> </root> 
     *
     * Noting also that the level/priority child element is optional too,
     * and not the only possible child element.
     */
    
    public static synchronized String retrieveConfigFileRootLoggerLevel(String fileName) throws IOException
    {
        try
        {
            LOCK.lock();

            Document doc = parseConfigFile(fileName);

            //retrieve the optional 'root' element node
            NodeList rootElements = doc.getElementsByTagName("root");

            if (rootElements.getLength() == 0)
            {
                //there is no root logger definition
                return "N/A";
            }

            Element rootElement = (Element) rootElements.item(0);

            //retrieve the optional 'priority' or 'level' element value.
            //It may not be the only child node, so request by tag name.
            NodeList priorityElements = rootElement.getElementsByTagName("priority");
            NodeList levelElements = rootElement.getElementsByTagName("level");
            String priority = null;

            if (priorityElements.getLength() != 0)
            {
                Element priorityElement = (Element) priorityElements.item(0);
                priority = priorityElement.getAttribute("value");
            }
            else if(levelElements.getLength() != 0)
            {
                Element levelElement = (Element) levelElements.item(0);
                priority = levelElement.getAttribute("value");
            }

            if(priority != null)
            {
                return priority;
            }
            else
            {
                return "N/A";
            }
        }
        finally
        {
            LOCK.unlock();
        }
    }
    
    public synchronized String getConfigFileRootLoggerLevel() throws IOException
    {
        return retrieveConfigFileRootLoggerLevel(_log4jConfigFileName).toUpperCase();
    }
    
    public synchronized boolean setConfigFileRootLoggerLevel(String level) throws IOException
    {
        try
        {
            LOCK.lock();

            //check that the specified level is a valid log4j Level
            try
            {
                Level newLevel = getLevel(level);
                if(newLevel == null)
                {
                    //A null Level reference implies inheritance. Setting the config file RootLogger 
                    //to "null" or "inherited" just ensures it defaults to DEBUG at startup as Log4J 
                    //prevents this catastrophic situation at startup and runtime anyway.
                    return false;
                }
            }
            catch (Exception e)
            {
                //it isnt a valid level
                return false;
            }

            _logger.info("Setting level to " + level + " for the Root logger in " +
                    "log4j xml configuration file: " + _log4jConfigFileName);

            Document doc = parseConfigFile(_log4jConfigFileName);

            //retrieve the optional 'root' element node
            NodeList rootElements = doc.getElementsByTagName("root");

            if (rootElements.getLength() == 0)
            {
                return false;
            }

            Element rootElement = (Element) rootElements.item(0);

            //retrieve the optional 'priority' or 'level' sub-element value.
            //It may not be the only child node, so request by tag name.
            NodeList priorityElements = rootElement.getElementsByTagName("priority");
            NodeList levelElements = rootElement.getElementsByTagName("level");

            Element levelElement = null;
            if (priorityElements.getLength() != 0)
            {
                levelElement = (Element) priorityElements.item(0);
            }
            else if (levelElements.getLength() != 0)
            {
                levelElement = (Element) levelElements.item(0);
            }
            else
            {
                //there is no exiting priority/level to update
                return false;
            }

            //update the element with the new level/priority
            levelElement.setAttribute("value", level);

            //output the new file
            return writeUpdatedConfigFile(_log4jConfigFileName, doc);
        }
        finally
        {
            LOCK.unlock();
        }
    }

    public synchronized void reloadConfigFile() throws IOException
    {
        try
        {
            LOCK.lock();

            QpidLog4JConfigurator.configure(_log4jConfigFileName);
            _logger.info("Applied log4j configuration from: " + _log4jConfigFileName);
        }
        catch (IllegalLoggerLevelException e)
        {
            _logger.warn("The log4j configuration reload request was aborted: " + e);
            //recommended that MBeans should use standard java.* and javax.* exceptions only
            throw new IOException("The log4j configuration reload request was aborted: " + e.getMessage());
        }
        catch (ParserConfigurationException e)
        {
            _logger.warn("The log4j configuration reload request was aborted: " + e);
            throw new IOException("The log4j configuration reload request was aborted: " + e.getMessage());
        }
        catch (SAXException e)
        {
            _logger.warn("The log4j configuration reload request was aborted: " + e);
            //recommended that MBeans should use standard java.* and javax.* exceptions only
            throw new IOException("The log4j configuration reload request was aborted: " + e.getMessage());
        }
        catch (IOException e)
        {
            _logger.warn("The log4j configuration reload request was aborted: " + e);
            throw new IOException("The log4j configuration reload request was aborted: " + e.getMessage());
        }
        finally
        {
            LOCK.unlock();
        }
    }
}