KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > log > LoggerLoader


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47
48 package org.mr.core.log;
49
50
51 import java.io.File JavaDoc;
52 import java.util.Collection JavaDoc;
53 import java.util.HashSet JavaDoc;
54
55 import javax.xml.parsers.DocumentBuilder JavaDoc;
56 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
57
58 import org.apache.log4j.xml.DOMConfigurator;
59 import org.mr.MantaAgent;
60 import org.w3c.dom.Document JavaDoc;
61 import org.w3c.dom.Element JavaDoc;
62 import org.w3c.dom.NodeList JavaDoc;
63
64
65 /**
66  * Loads the configuration.
67  * This is done automatically without user intervention.
68  *
69  */

70 public class LoggerLoader {
71     
72   private static boolean loaded = false;
73   
74   private static Collection JavaDoc listeners = new HashSet JavaDoc(1);
75   
76     public static void init(String JavaDoc mantaName) {
77         // added by Shirley Sasson - 07/06/06
78
// Check if configuration is found in ConfigManager as a DOM element.
79
// If yes, use it as the configuration. If not, look for a configuration file.
80
Element JavaDoc configurationElement = MantaAgent.getInstance().getSingletonRepository().getConfigManager().getConfigurationDOMElement();
81         if (configurationElement != null)
82             parseAndLoadLogger(configurationElement, mantaName);
83         else {
84             String JavaDoc propertyFile = MantaAgent.getInstance().getSingletonRepository().getConfigManager().getConfigFileName();
85             File JavaDoc f = new File JavaDoc(propertyFile);
86             Element JavaDoc rootElement;
87             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
88             factory.setIgnoringElementContentWhitespace(true);
89
90             try {
91                 DocumentBuilder JavaDoc xmlBuilder= factory.newDocumentBuilder();
92                 Document JavaDoc xmlDoc = xmlBuilder.parse(f);
93
94                 rootElement = xmlDoc.getDocumentElement();
95                 parseAndLoadLogger(rootElement, mantaName);
96             }
97             catch(Exception JavaDoc e){
98                 StartupLogger.log.error("Problem loading logger", "LoggerLoader");
99                 e.printStackTrace();
100             }
101         }
102     }
103
104     private static void parseAndLoadLogger(Element JavaDoc configurationElement, String JavaDoc mantaName){
105         // log4j configuration is now at the same format as the rest of the config
106
NodeList JavaDoc logConfig = configurationElement.getElementsByTagName("log4j:configuration");
107         Element JavaDoc logConfigElement = (Element JavaDoc) logConfig.item(0);
108
109         if (logConfigElement != null){
110             MantaLogAppender.setMantaName(mantaName);
111             init(logConfigElement);
112         }
113     }
114
115   public static void init(Element JavaDoc config){
116     // configures logger
117
DOMConfigurator.configure(config);
118     loaded = true;
119   }
120   
121   
122   
123
124   /**
125    * if config file does not exist, a default file will be created.
126    */

127   static void ensureConfigFile( String JavaDoc configFile){
128
129     File JavaDoc logConfigFile = new File JavaDoc(configFile);
130     if( !logConfigFile.exists() ){
131       //System.out.println("Logging Module: DID NOT FIND CONFIG FILE FOR LOGGER at "+configFile);
132
StartupLogger.log.error("Did not find configuration file for logger at "+configFile, "LoggerLoader");
133      }
134   }
135
136   /**
137    * Ensures that the directory 'dir' exists.
138    * if it does not exist it will be created.
139    * @param dirName
140    */

141   static void ensureDirectory(String JavaDoc dirName){
142     
143     File JavaDoc dir = new File JavaDoc(dirName);
144     if(!dir.exists()) {
145
146       boolean createDir = dir.mkdirs();
147       if( !createDir ){
148         //System.err.println(
149
//"Logging Module: "+dirName+" directory does not exist and cannot be created !!");
150
//System.err.println("Logging Module cannot work properly.");
151
StartupLogger.log.error(dirName+" directory does not exist and cannot be created !!", "LoggerLoader");
152           StartupLogger.log.error("Logging Module cannot work properly.", "LoggerLoader");
153         return;
154       }
155       //System.out.println("Logging Module: "+dirName+" directory was created");
156
StartupLogger.log.info(dirName+" directory was created", "LoggerLoader");
157     }
158   }
159   
160   
161 /**
162  * @return Returns the loaded.
163  */

164 public static boolean isLoaded() {
165     return loaded;
166 }
167
168 /**
169  * @param loaded The loaded to set.
170  */

171 public static void setLoaded(boolean loaded) {
172     LoggerLoader.loaded = loaded;
173 }
174
175
176
177 }
178
Popular Tags