0001: /**
0002: * Copyright 2004 Sun Microsystems, Inc. All
0003: * rights reserved. Use of this product is subject
0004: * to license terms. Federal Acquisitions:
0005: * Commercial Software -- Government Users
0006: * Subject to Standard License Terms and
0007: * Conditions.
0008: *
0009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
0010: * are trademarks or registered trademarks of Sun Microsystems,
0011: * Inc. in the United States and other countries.
0012: */package com.sun.portal.log.common;
0013:
0014: import com.sun.portal.log.monitor.FileChangeListener;
0015: import com.sun.portal.log.monitor.FileMonitor;
0016:
0017: import java.io.BufferedInputStream;
0018: import java.io.File;
0019: import java.io.FileInputStream;
0020: import java.io.FileNotFoundException;
0021: import java.io.IOException;
0022: import java.io.InputStream;
0023: import java.util.Collections;
0024: import java.util.Comparator;
0025: import java.util.Enumeration;
0026: import java.util.HashMap;
0027: import java.util.Iterator;
0028: import java.util.Map;
0029: import java.util.Properties;
0030: import java.util.Set;
0031: import java.util.StringTokenizer;
0032: import java.util.TreeMap;
0033: import java.util.TreeSet;
0034: import java.util.Vector;
0035: import java.util.logging.ConsoleHandler;
0036: import java.util.logging.FileHandler;
0037: import java.util.logging.Filter;
0038: import java.util.logging.Formatter;
0039: import java.util.logging.Handler;
0040: import java.util.logging.Level;
0041: import java.util.logging.LogManager;
0042: import java.util.logging.Logger;
0043:
0044: /**
0045: * This is class is responsible for the configuration. It gets the default values
0046: * from PSLogConfig.properties.
0047: * Any independent (web)app, must call <code>new PortalLogManager().init(rootLoggerName);<code>
0048: * in the init of the (web)app.
0049: * By default, rootlogger passed to init is configured with the default values from
0050: * PSLogConfig.properties.
0051: */
0052: public class PortalLogManager implements FileChangeListener {
0053:
0054: // Maintains a list of root loggers (used for File monitoring)
0055: // Key = Config file name
0056: // Value = Vector of root logger names
0057: static Map rootLoggerMap = Collections
0058: .synchronizedMap(new HashMap());
0059:
0060: // Maintains a list of PortalLoggerDetail (used in list-loggers --detail CLI command)
0061: // Key = loggerName
0062: // Value = PortalLoggerDetail object
0063: static Map loggerDetailMap = Collections
0064: .synchronizedMap(new HashMap());
0065:
0066: // Loads the content of PortalLogConfig.properties
0067: static Properties configProperties;
0068:
0069: // Maintains a list of Properties object(used in reloading)
0070: // Key = Config file name
0071: // Value = Properties object
0072: static Map configPropertiesMap = Collections
0073: .synchronizedMap(new HashMap());;
0074:
0075: static {
0076: // initial config for common classes in the server classpath
0077: PortalLogManager manager = new PortalLogManager();
0078: manager.init(PortalLogManager.DEBUG_ROOT_LOGGER);
0079: }
0080:
0081: public static final String LOG_CONFIG_FILENAME = "PSLogConfig.properties";
0082: public static final String SEARCH_LOG_CONFIG_FILENAME = "SearchLogConfig.properties";
0083: public static final String DEFAULT_LOG_CONFIG_FILE = "PSLogConfigDefault.properties";
0084: public static final String LOG_MSG_FILE = "PortalLogMessages.properties";
0085: public static final String SRA_LOG_CONFIG_FILENAME_PREFIX = "platform.conf.";
0086: public static final String ADMIN_LOG_CONFIG_FILENAME = "PSAdminLogConfig.properties";
0087:
0088: public static final String LOG_CONFIG_FILENAME_PROPERTY = "com.sun.portal.log.config.file";
0089: public static final String INSTANCE_ID_PROPERTY = "com.sun.portal.instance.id";
0090: public static final String PORTAL_ID_PROPERTY = "com.sun.portal.portal.id";
0091: public static final String SRA_COMPONENT_TYPE_PROPERTY = "com.sun.portal.sra.component.type";
0092:
0093: public static final String DEBUG_ROOT_LOGGER = "debug.com.sun.portal";
0094: public static final String DEBUG_ROOT_LOGGER_SEARCH = "debug.com.sun.portal.search";
0095: public static final String DEBUG_COM_SUN_PREFIX = "debug.com.sun.";
0096:
0097: // All default values will be picked from default properties file
0098:
0099: // Attributes of the logger
0100: public static final String LOG_LEVEL_ATTRIBUTE = ".level";
0101: public static final String LOG_HANDLER_ATTRIBUTE = ".handler";
0102: public static final String LOG_SEPARATE_FILE_ATTRIBUTE = ".separatefile";
0103: public static final String LOG_TO_SERVER_ATTRIBUTE = ".setserverlogs";
0104: public static final String LOG_USE_PARENT_HANDLER = ".useparenthandler";
0105: public static final String LOG_STACK_TRACE_ATTRIBUTE = ".stacktrace";
0106:
0107: // Attributes of the File Handler
0108: public static final String DEFAULT_LOG_HANDLER = "java.util.logging.FileHandler";
0109: public static final String LOG_HANDLER_PATTERN_ATTRIBUTE = ".pattern";
0110: public static final String LOG_HANDLER_LIMIT_ATTRIBUTE = ".limit";
0111: public static final String LOG_HANDLER_COUNT_ATTRIBUTE = ".count";
0112: public static final String LOG_HANDLER_APPEND_ATTRIBUTE = ".append";
0113: public static final String LOG_HANDLER_FILTER_ATTRIBUTE = ".filter";
0114: public static final String LOG_HANDLER_FORMATTER_ATTRIBUTE = ".formatter";
0115:
0116: // Attributes of the PortalLogFormatter
0117: public static final String DEFAULT_LOG_FORMATTER = "com.sun.portal.log.common.PortalLogFormatter";
0118: public static final String STACK_TRACE_UNKNOWN = "unknown";
0119:
0120: // Patterns in the config file
0121: public static final String LOGGER_PATTERN = "%logger";
0122: public static final String INSTANCE_ID_PATTERN = "%instanceID";
0123: public static final String PORTAL_ID_PATTERN = "%portalID";
0124: public static final String SRA_COMPONENT_TYPE_PATTERN = "%sraComponentType";
0125:
0126: // Parameters for File monitoring
0127: public static final String LOG_CONFIG_CHECK_PERIOD_KEY = "log.config.check.period";
0128:
0129: private static Object syncObject = new Object();
0130: private static boolean dirPermissionsModified = false;
0131:
0132: /**
0133: * Sets up initial configuration for the logger.
0134: *
0135: * @param rootLoggerName
0136: */
0137: public void init(String rootLoggerName) {
0138:
0139: String fullPathConfigFile = System
0140: .getProperty(LOG_CONFIG_FILENAME_PROPERTY);
0141: PortalLogDebug.info("Config File:" + fullPathConfigFile);
0142:
0143: // Read the config file as specified in the system property
0144: if (fullPathConfigFile != null) {
0145: readConfigFile(fullPathConfigFile);
0146: //init Loggers list
0147: initLoggersList(rootLoggerName, fullPathConfigFile, false);
0148: }
0149:
0150: // Load all the configuration files
0151: loadConfigFiles();
0152:
0153: // configure the root logger and other loggers if separate file for the logger is mentioned
0154: configure(rootLoggerName);
0155:
0156: // Add the root logger with config file name as the key, if the key entry
0157: // for config file is new, set up a file monitoring for that file. But if the
0158: // config file is already added , it means file monitoring has already been
0159: // added for that file, hence do not add file monitoring
0160: if (fullPathConfigFile != null) {
0161: if (addRootLogger(fullPathConfigFile, rootLoggerName))
0162: configureFileMonitor(fullPathConfigFile);
0163: }
0164: }
0165:
0166: // This will be called by Search webapp & psconsole webapp
0167: /**
0168: * Sets up the initial configuration for the logger based on the
0169: * fullPathConfigFile
0170: *
0171: * @param rootLoggerName the name of the rootLogger
0172: * @param fullPathConfigFile the config filename along with the full path
0173: */
0174: public void init(String rootLoggerName, String fullPathConfigFile) {
0175:
0176: PortalLogDebug.info("Config File:" + fullPathConfigFile);
0177: //init Loggers list
0178: initLoggersList(rootLoggerName, fullPathConfigFile, true);
0179:
0180: // Read the configuration file
0181: readConfigFile(fullPathConfigFile);
0182:
0183: // Load all the configuration files
0184: loadConfigFiles();
0185:
0186: // configure the root logger and other loggers if separate file for the logger is mentioned
0187: configure(rootLoggerName);
0188:
0189: // Add the root logger with config file name as the key, if the key entry
0190: // for config file is new, set up a file monitoring for that file. But if the
0191: // config file is already added , it means file monitoring has already been
0192: // added for that file, hence do not add file monitoring
0193: if (addRootLogger(fullPathConfigFile, rootLoggerName))
0194: configureFileMonitor(fullPathConfigFile);
0195: }
0196:
0197: /**
0198: * If the attribute serverlogs is set to true, returns true, else
0199: * returns false.
0200: * If true, the messages are logged to the webcontainer logs.
0201: *
0202: * @return true if messages are logged to the webcontainer logs.
0203: */
0204: boolean useServerLogs(String loggerName) {
0205: String value = getProperty(loggerName, LOG_TO_SERVER_ATTRIBUTE);
0206: return "true".equals(value);
0207: }
0208:
0209: /**
0210: * Add the root logger to the map with config name as the key.
0211: * If its a new entry, add it and return true, else return false.
0212: *
0213: * @param fullPathConfigFile full path file name of the config file
0214: * @param rootLoggerName name of the root logger
0215: * @return true if the key fullPathConfigFile is new
0216: */
0217: private boolean addRootLogger(String fullPathConfigFile,
0218: String rootLoggerName) {
0219: boolean isNewEntry;
0220: Vector rootLoggerNames = (Vector) rootLoggerMap
0221: .get(fullPathConfigFile);
0222: if (rootLoggerNames == null) {
0223: rootLoggerNames = new Vector();
0224: rootLoggerNames.add(rootLoggerName);
0225: isNewEntry = true;
0226: } else {
0227: rootLoggerNames.add(rootLoggerName);
0228: isNewEntry = false;
0229: }
0230: rootLoggerMap.put(fullPathConfigFile, rootLoggerNames);
0231: return isNewEntry;
0232: }
0233:
0234: /**
0235: * Returns PortalLoggerDetail object for the logger.
0236: * The details for each logger is maintained in a HashMap.
0237: *
0238: * @param loggerName name of the logger
0239: * @return the PortalLoggerDetail object which contains the detail info of the logger
0240: */
0241: private PortalLoggerDetail getPortalLoggerDetail(String loggerName) {
0242: PortalLoggerDetail portalLoggerDetail = (PortalLoggerDetail) loggerDetailMap
0243: .get(loggerName);
0244: if (portalLoggerDetail == null) {
0245: portalLoggerDetail = new PortalLoggerDetail();
0246: loggerDetailMap.put(loggerName, portalLoggerDetail);
0247: }
0248: return portalLoggerDetail;
0249: }
0250:
0251: /**
0252: * Configures the root logger and other loggers if .separatefile attribute
0253: * for the loggers is in the config file.
0254: * If the attribute .setserverlogs is set to true, no configuration is done.
0255: *
0256: * @param rootLoggerName
0257: */
0258: private void configure(String rootLoggerName) {
0259:
0260: PortalLogDebug.info("Use server logs: "
0261: + useServerLogs(rootLoggerName));
0262: if (useServerLogs(rootLoggerName)) {
0263: // If logs need to be directed to server logs, load resource bundle
0264: Logger rootLogger = getLogger(rootLoggerName);
0265: /*
0266: // Uncomment if desired to have PortalLogFormatter for logs that got to
0267: // the web container
0268: Handler[] handlers = rootLogger.getHandlers();
0269: if(handlers.length > 0) {
0270: handlers[0].setFormatter(new PortalLogFormatter());
0271: }
0272: */
0273: return;
0274: }
0275:
0276: // configure the root logger
0277: configureLogger(rootLoggerName);
0278:
0279: // Configure the loggers that have to be moved to a separate file,
0280: // that have the level set and the handler specified
0281: configureLoggers(rootLoggerName);
0282: }
0283:
0284: /**
0285: * Configures the loggers if the .separatefile attribute is set to
0286: * true in the config file and if .level attribute is set in the
0287: * config file.
0288: */
0289: private void configureLoggers(String rootLoggerName) {
0290:
0291: // Get a list of loggers to be moved to a separate file
0292: Set separateFiles = Collections.synchronizedSet(new TreeSet());
0293: // Get a list of loggers on which Levels should be set
0294: Map levels = Collections.synchronizedMap(new TreeMap());
0295: // Get a list of loggers on which Handlers should be set
0296: Set handlers = Collections.synchronizedSet(new TreeSet());
0297:
0298: Enumeration e = configProperties.propertyNames();
0299: String propertyName, loggerName;
0300: while (e.hasMoreElements()) {
0301: propertyName = (String) e.nextElement();
0302:
0303: if (propertyName.endsWith(LOG_SEPARATE_FILE_ATTRIBUTE)) {
0304: if (getProperty(propertyName).equals("true")) {
0305: int index = propertyName
0306: .indexOf(LOG_SEPARATE_FILE_ATTRIBUTE);
0307: loggerName = propertyName.substring(0, index);
0308: if (loggerName.startsWith(DEBUG_ROOT_LOGGER)
0309: && !loggerName.equals(rootLoggerName)) {
0310: separateFiles.add(loggerName);
0311: }
0312: }
0313: } else if (propertyName.endsWith(LOG_LEVEL_ATTRIBUTE)) {
0314: int index = propertyName.indexOf(LOG_LEVEL_ATTRIBUTE);
0315: loggerName = propertyName.substring(0, index);
0316: if (loggerName.startsWith(DEBUG_ROOT_LOGGER)) {
0317: levels.put(loggerName, getProperty(propertyName));
0318: }
0319: } else if (propertyName.endsWith(LOG_HANDLER_ATTRIBUTE)) {
0320: int index = propertyName.indexOf(LOG_HANDLER_ATTRIBUTE);
0321: loggerName = propertyName.substring(0, index);
0322: if (loggerName.startsWith(DEBUG_ROOT_LOGGER)
0323: && !loggerName.equals(rootLoggerName)) {
0324: handlers.add(loggerName);
0325: }
0326: }
0327: }
0328:
0329: Iterator itr = null;
0330: // Configure the loggers in the list
0331: itr = separateFiles.iterator();
0332: while (itr.hasNext()) {
0333: try {
0334: configureLogger((String) itr.next());
0335: } catch (Exception ex) {
0336: PortalLogDebug.error(ex.getMessage());
0337: }
0338: }
0339:
0340: // Set the level for the loggers in the list
0341: Set mappings = levels.entrySet();
0342: itr = mappings.iterator();
0343: Map.Entry mapEntry;
0344: while (itr.hasNext()) {
0345: mapEntry = (Map.Entry) itr.next();
0346: setLogLevel((String) mapEntry.getKey(), (String) mapEntry
0347: .getValue());
0348: }
0349:
0350: /*
0351: Find a new way if handling handlers
0352: itr = handlers.iterator();
0353: while(itr.hasNext()) {
0354: loggerName = (String)itr.next();
0355: removeHandlers(loggerName);
0356: addHandlers(loggerName);
0357: }
0358: */
0359: }
0360:
0361: /**
0362: * Searches for the property with the specified key in the config property list
0363: * and returns the value.
0364: *
0365: * @param key the property key.
0366: * @return the value in the config property list with the specified key value.
0367: */
0368: private String getProperty(String key) {
0369: String value = configProperties.getProperty(key);
0370: return value;
0371: }
0372:
0373: /**
0374: * Searches for the default property with the specified key in the config property list
0375: * and returns the value. The default property is the key defined in default config file
0376: *
0377: * @param key the property key.
0378: * @return the value in the config property list with the specified key value.
0379: */
0380: private String getDefaultProperty(String key) {
0381: return getProperty(key);
0382: }
0383:
0384: /**
0385: * Searches for the default property with the specified logger name and attribute
0386: * in the config property list and returns the value.
0387: * The default property is the attribute defined in default config file
0388: *
0389: * @param loggerName the name of the logger
0390: * @param attribute the name of the attribute
0391: * @return the value in the config property list with the specified logger name and
0392: * attribute.
0393: */
0394: private String getDefaultProperty(String loggerName,
0395: String attribute) {
0396: String value = getProperty(loggerName, attribute);
0397: return value;
0398: }
0399:
0400: /**
0401: * Returns the default value for log.config.check.period property from PSLogConfigDefault.properties
0402: *
0403: * @return default value for log.config.check.period property
0404: */
0405: private long getDefaultLogConfigCheckPeriodValue() {
0406: String value = getDefaultProperty(LOG_CONFIG_CHECK_PERIOD_KEY);
0407: long configCheckPeriod;
0408: try {
0409: configCheckPeriod = Long.parseLong(value);
0410: } catch (NumberFormatException nfe) {
0411: configCheckPeriod = 2000;
0412: // Should never happen. printed to check the cause
0413: nfe.printStackTrace();
0414: }
0415: return configCheckPeriod;
0416: }
0417:
0418: /**
0419: * Searches for the property with the specified logger name and attribute
0420: * in the config property list and returns the value.
0421: *
0422: * @param loggerName the name of the logger
0423: * @param attribute the name of the attribute
0424: * @return the value in the config property list with the specified logger name and
0425: * attribute.
0426: */
0427: private String getProperty(String loggerName, String attribute) {
0428: String value = getProperty(loggerName + attribute);
0429: while (value == null && !loggerName.equals(DEBUG_ROOT_LOGGER)) {
0430: loggerName = getParent(loggerName);
0431: value = getProperty(loggerName + attribute);
0432: }
0433: return value;
0434: }
0435:
0436: /**
0437: * Searches for the property with the specified logger name and attribute
0438: * in the config property list and returns the value. But if the value is
0439: * null returns the specified default value.
0440: *
0441: * @param loggerName the name of the logger
0442: * @param attribute the name of the attribute
0443: * @param defaultValue the default value
0444: * @return the value in the config property list with the specified logger name and
0445: * attribute, if null defaultValue is returned.
0446: */
0447: private String getProperty(String loggerName, String attribute,
0448: String defaultValue) {
0449: String value = getProperty(loggerName, attribute);
0450: return ((value == null) ? defaultValue : value);
0451: }
0452:
0453: /**
0454: * Returns the logger name space.
0455: *
0456: * @param loggerName the name of the logger
0457: * @return the logger name space.
0458: */
0459: private String getParent(String loggerName) {
0460: int lastIndex = loggerName.lastIndexOf(".");
0461: if (lastIndex == -1)
0462: return DEBUG_ROOT_LOGGER;
0463: return (loggerName.substring(0, lastIndex));
0464: }
0465:
0466: /**
0467: * Returns the log level for the logger from the log config file.
0468: * If the level is not found, returns the default level.
0469: *
0470: * @param loggerName the name of the logger.
0471: * @return the log level for the logger from the log config file.
0472: */
0473: public Level getLevel(String loggerName) {
0474: String levelValue = getProperty(loggerName, LOG_LEVEL_ATTRIBUTE);
0475: return parseLevel(levelValue);
0476: }
0477:
0478: private String getUseParentHandler(String loggerName) {
0479: return getProperty(loggerName, LOG_USE_PARENT_HANDLER, "false");
0480: }
0481:
0482: /**
0483: * Configure the File monitoring for the log config file
0484: *
0485: * @param fullPathConfigFile the full path filename for the
0486: * log config file.
0487: */
0488: private void configureFileMonitor(String fullPathConfigFile) {
0489: long periodicity = 0;
0490: String val = getProperty(LOG_CONFIG_CHECK_PERIOD_KEY);
0491: try {
0492: if (val != null) {
0493: periodicity = Long.parseLong(val);
0494: }
0495: } catch (Exception e) {
0496: //not a proper long value, drop through
0497: }
0498: if (periodicity <= 0)
0499: periodicity = getDefaultLogConfigCheckPeriodValue();
0500: try {
0501: FileMonitor.getInstance().addFileChangeListener(this ,
0502: fullPathConfigFile, periodicity);
0503: } catch (Exception e) {
0504: PortalLogDebug.error(e);
0505: }
0506: }
0507:
0508: /**
0509: * Read the configuration file and save it the HashMap
0510: */
0511: private void readConfigFile(String fullPathConfigFile) {
0512: Properties tmpConfigProperties = new Properties();
0513: InputStream in = null;
0514: try {
0515: in = new FileInputStream(fullPathConfigFile);
0516: tmpConfigProperties.load(in);
0517: } catch (FileNotFoundException e) {
0518: PortalLogDebug.error(e.getMessage());
0519: } catch (IOException e) {
0520: PortalLogDebug.error(e.getMessage());
0521: } finally {
0522: if (in != null) {
0523: try {
0524: in.close();
0525: } catch (IOException ignored) {
0526: }
0527: }
0528: }
0529: configPropertiesMap
0530: .put(fullPathConfigFile, tmpConfigProperties);
0531: }
0532:
0533: /**
0534: * Read the configuration file and load it in the Properties
0535: */
0536: private void loadConfigFiles() {
0537:
0538: // Always read the default configuration file
0539: loadDefaultConfigFile();
0540:
0541: Set mappings = configPropertiesMap.entrySet();
0542: Iterator itr = mappings.iterator();
0543: Map.Entry mapEntry;
0544: Properties tmpConfigProperties;
0545: while (itr.hasNext()) {
0546: mapEntry = (Map.Entry) itr.next();
0547: tmpConfigProperties = (Properties) mapEntry.getValue();
0548: loadConfigProperties(tmpConfigProperties);
0549: }
0550: }
0551:
0552: /**
0553: * Read the input Properties into the configProperties.
0554: *
0555: * @param tmpConfigProperties
0556: */
0557: private void loadConfigProperties(Properties tmpConfigProperties) {
0558: Enumeration e = tmpConfigProperties.propertyNames();
0559: String key;
0560: while (e.hasMoreElements()) {
0561: key = (String) e.nextElement();
0562: configProperties.setProperty(key, tmpConfigProperties
0563: .getProperty(key));
0564: }
0565: }
0566:
0567: /**
0568: * Read the default configuration file and load it in the Properties
0569: */
0570: private void loadDefaultConfigFile() {
0571: InputStream defaultConfigBundle = null;
0572: Properties defaultProperties = new Properties();
0573: try {
0574: defaultConfigBundle = this .getClass().getClassLoader()
0575: .getResourceAsStream(
0576: PortalLogManager.DEFAULT_LOG_CONFIG_FILE);
0577: defaultProperties.load(defaultConfigBundle);
0578: } catch (Exception e) {
0579: PortalLogDebug.error(e.getMessage());
0580: } finally {
0581: if (defaultConfigBundle != null) {
0582: try {
0583: defaultConfigBundle.close();
0584: } catch (IOException e) {
0585: //drop through
0586: }
0587: }
0588: }
0589: configProperties = new Properties(defaultProperties);
0590: }
0591:
0592: /**
0593: * Configure the logger for the given logger name
0594: *
0595: * @param rootLoggerName the name of the root logger
0596: */
0597: private void configureLogger(String rootLoggerName) {
0598:
0599: // get the logger from the container.
0600: Logger rootLogger = getLogger(rootLoggerName);
0601:
0602: PortalLogDebug
0603: .info("Configuring Root Logger:" + rootLoggerName);
0604:
0605: // set parent handler false
0606: rootLogger.setUseParentHandlers(Boolean.valueOf(
0607: getUseParentHandler(rootLoggerName)).booleanValue());
0608:
0609: try {
0610: // set level
0611: rootLogger.setLevel(getLevel(rootLoggerName));
0612: PortalLogDebug.info("Root Logger Level:"
0613: + rootLogger.getLevel());
0614: } catch (Exception ex) {
0615: PortalLogDebug.error("Can't set level for "
0616: + rootLoggerName + LOG_LEVEL_ATTRIBUTE);
0617: // Probably a bad level. Drop through.
0618: }
0619:
0620: // If different handler is added, remove it
0621: // different handler means a handler that is different from the one mentioned in the
0622: // properties file.
0623: removeHandlers(rootLoggerName);
0624:
0625: /*
0626: // Add a delay of 1 second between removing/closing handlers and adding new ones
0627: try {
0628: Thread.sleep(1000);
0629: } catch(InterruptedException ignored) {}
0630: */
0631:
0632: // add the handlers mentioned in the properties file
0633: addHandlers(rootLoggerName);
0634:
0635: // add the root logger to the collections in PortalLogger and also to the LoggersList
0636: PortalLogger.addRootLogger(rootLoggerName);
0637: }
0638:
0639: private Logger getLogger(String rootLoggerName) {
0640: //set resource bundle on the root logger
0641: Logger rootLogger = null;
0642: try {
0643: //Logger API suffixes the default locale string to the LOG_MSG_FILE with a preceeding '_'
0644: rootLogger = Logger.getLogger(rootLoggerName, LOG_MSG_FILE
0645: .substring(0, LOG_MSG_FILE.indexOf(".")));
0646: } catch (Exception e) {
0647: PortalLogDebug
0648: .error("Can't get resource bundle corresponding to "
0649: + LOG_MSG_FILE + " " + e.getMessage());
0650: }
0651: return rootLogger;
0652: }
0653:
0654: /**
0655: * Add the handlers that is specified in the properties file.
0656: * If the handler is FileHandler, set its attributes,
0657: * but if the handler is not FileHandler, only load it.
0658: *
0659: * @param rootLoggerName
0660: */
0661: private void addHandlers(String rootLoggerName) {
0662: Logger rootLogger = Logger.getLogger(rootLoggerName);
0663: rootLogger.setUseParentHandlers(false);
0664:
0665: String handlerNames[] = parseClassNames(getProperty(
0666: rootLoggerName, LOG_HANDLER_ATTRIBUTE));
0667: for (int i = 0; i < handlerNames.length; i++) {
0668: String handlerName = handlerNames[i].trim();
0669: Handler handler = null;
0670:
0671: PortalLogDebug.info("Adding Handler:" + handlerName);
0672: if (handlerName.equals(DEFAULT_LOG_HANDLER)) {
0673: try {
0674: handler = getDefaultHandler(rootLoggerName);
0675: } catch (Exception e) {
0676: handler = new ConsoleHandler();
0677: PortalLogDebug
0678: .error("Could not add file handler to PS logger...adding ConsoleHandler."
0679: + e.getMessage());
0680: }
0681: } else {
0682: try {
0683: handler = (Handler) getClassInstance(handlerName);
0684: } catch (Exception ex) {
0685: PortalLogDebug.error(ex.getMessage());
0686: }
0687: }
0688:
0689: if (handler != null) {
0690: addFilterFormatter(rootLoggerName, handler);
0691: //add handler to the root logger
0692: rootLogger.addHandler(handler);
0693: }
0694: }
0695:
0696: //add the default file handler with all the properties set
0697: // only if there is no handler already
0698: if (rootLogger.getHandlers().length == 0) {
0699: // set file handler and default formatter
0700: addDefaultHandler(DEBUG_ROOT_LOGGER);
0701: }
0702: }
0703:
0704: /**
0705: * Adds the Filter and Formatter to the Handler
0706: *
0707: * @param loggerName the name of the logger
0708: * @param handler the handler associated with the logger
0709: */
0710: private void addFilterFormatter(String loggerName, Handler handler) {
0711:
0712: String suffix = LOG_HANDLER_ATTRIBUTE + "."
0713: + DEFAULT_LOG_HANDLER;
0714:
0715: //set handler's level to FINEST as we want to control through Logger's level
0716: handler.setLevel(Level.FINEST);
0717: //set handler's filter
0718: Filter filter = getFilter(loggerName,
0719: (suffix + LOG_HANDLER_FILTER_ATTRIBUTE), null);
0720: handler.setFilter(filter);
0721: PortalLogDebug.info("Adding Filter:" + filter);
0722: //set handler's formatter
0723: Formatter formatter = getFormatter(loggerName,
0724: (suffix + LOG_HANDLER_FORMATTER_ATTRIBUTE),
0725: new PortalLogFormatter());
0726: handler.setFormatter(formatter);
0727: PortalLogDebug.info("Adding Formatter:" + formatter);
0728: if (formatter instanceof PortalLogFormatter) {
0729: // Set the resource bundle
0730: Logger rootLogger = Logger.getLogger(DEBUG_ROOT_LOGGER);
0731: ((PortalLogFormatter) formatter)
0732: .setResourceBundle(rootLogger.getResourceBundle());
0733: ((PortalLogFormatter) formatter)
0734: .setResourceBundleName(rootLogger
0735: .getResourceBundleName());
0736:
0737: String stacktraceValue;
0738: // If the level is SEVERE or WARNING, the stacktrace will always will be true
0739: if (Logger.getLogger(loggerName).getLevel().intValue() >= Level.WARNING
0740: .intValue()) {
0741: stacktraceValue = "true";
0742: } else {
0743: stacktraceValue = getProperty(loggerName,
0744: LOG_STACK_TRACE_ATTRIBUTE, "false");
0745:
0746: // if useParentHandler is true, do not log stack trace for all levels
0747: if (getUseParentHandler(loggerName).equals("true"))
0748: stacktraceValue = "false";
0749: }
0750:
0751: ((PortalLogFormatter) formatter).setPrintStackTrace(Boolean
0752: .valueOf(stacktraceValue).booleanValue());
0753: getPortalLoggerDetail(loggerName).setStackTraceValue(
0754: stacktraceValue);
0755: } else {
0756: getPortalLoggerDetail(loggerName).setStackTraceValue(
0757: STACK_TRACE_UNKNOWN);
0758: }
0759: }
0760:
0761: /**
0762: * Add the handler with the default values from the properties file
0763: *
0764: * @param loggerName
0765: */
0766: private void addDefaultHandler(String loggerName) {
0767: //add file handler if pattern is mentioned or add console handler
0768: Logger logger = Logger.getLogger(loggerName);
0769: logger.setUseParentHandlers(false);
0770: Handler handler = null;
0771: try {
0772: handler = getDefaultHandler(loggerName);
0773: } catch (Exception e) {
0774: handler = new ConsoleHandler();
0775: PortalLogDebug
0776: .error("Could not add file handler to PS logger...adding ConsoleHandler."
0777: + e.getMessage());
0778: }
0779:
0780: if (handler != null) {
0781: addFilterFormatter(loggerName, handler);
0782:
0783: //add only if there is no handler already
0784: if (logger.getHandlers().length == 0)
0785: logger.addHandler(handler);
0786: }
0787: }
0788:
0789: /**
0790: * Gets the default handler which is a file handler.
0791: *
0792: * @param loggerName
0793: * @return
0794: * @throws Exception
0795: */
0796: private Handler getDefaultHandler(String loggerName)
0797: throws Exception {
0798: Handler fileHandler = null;
0799: //pattern is essential to be mentioned in the properties file
0800: //otherwise FileHandler is configured using LogManager properties
0801: //which in this pattern's case is NOT appropriate
0802: //'%logger' is an additional pattern supported to distinguish different root logger files.
0803: // %u, %g are supported by FileHandler
0804: String suffix = LOG_HANDLER_ATTRIBUTE + "."
0805: + DEFAULT_LOG_HANDLER;
0806:
0807: String pattern = getProperty(loggerName,
0808: (suffix + LOG_HANDLER_PATTERN_ATTRIBUTE));
0809: if (pattern == null || pattern.equals(""))
0810: throw new Exception("Pattern is either null or empty");
0811:
0812: StringBuffer buffer = new StringBuffer(pattern);
0813:
0814: // Replace the tag %instanceID with instance id if present
0815: // and create a directory by that name
0816: replaceInstanceId(buffer);
0817:
0818: // Replace the tag %portalID with portal id if present
0819: replacePortalId(buffer);
0820:
0821: // Replace the tag %sraComponentType with SRA component type if present
0822: replaceSRAComponentType(buffer);
0823:
0824: // Replace the tag %logger with loggername
0825: replaceLoggerPattern(buffer, loggerName);
0826:
0827: // If the pattern has debug.com.sun. remove it so that the logger name starts with portal.XXX
0828: replacePrefix(buffer);
0829:
0830: int limit = 0;
0831: int count = 2;
0832: try {
0833: limit = Integer.parseInt(getProperty(loggerName,
0834: (suffix + LOG_HANDLER_LIMIT_ATTRIBUTE)));
0835: count = Integer.parseInt(getProperty(loggerName,
0836: (suffix + LOG_HANDLER_COUNT_ATTRIBUTE)));
0837: } catch (Exception e) {
0838: PortalLogDebug
0839: .error("Could not get the property .limit and .count");
0840: }
0841:
0842: // Create directories if not present
0843: int lastIndex = buffer.lastIndexOf("/");
0844: if (lastIndex != -1) {
0845: String logDir = buffer.substring(0, lastIndex);
0846: boolean makeDirValue;
0847: if (pattern.indexOf(SRA_COMPONENT_TYPE_PATTERN) != -1
0848: && !dirPermissionsModified) {
0849: // If SRA, after creating directory, change file permissions
0850: makeDirValue = makeDir(logDir);
0851: lastIndex = logDir.lastIndexOf("/");
0852: if (lastIndex != -1) {
0853: String sraLogDir = logDir.substring(0, lastIndex);
0854: // Modify directory permissions
0855: chmod(sraLogDir);
0856: dirPermissionsModified = true;
0857: }
0858: } else {
0859: makeDirValue = makeDir(logDir);
0860: }
0861: if (!makeDirValue)
0862: throw new Exception("Cannot create the directory: "
0863: + logDir);
0864: }
0865:
0866: //append is set to true by default, this is againt the fileHandler default
0867: boolean append = Boolean.valueOf(
0868: getProperty(loggerName, suffix
0869: + LOG_HANDLER_APPEND_ATTRIBUTE, "true"))
0870: .booleanValue();
0871: if (limit != 0) {
0872: fileHandler = new FileHandler(buffer.toString(), limit,
0873: count, append);
0874: } else {
0875: // in this case limit is unlimited, with count set to 1
0876: fileHandler = new FileHandler(buffer.toString(), append);
0877: }
0878: if (fileHandler == null)
0879: throw new Exception("FileHandler is null");
0880:
0881: // If SRA, after creating file, change the file permissions
0882: if (pattern.indexOf(SRA_COMPONENT_TYPE_PATTERN) != -1) {
0883: int index = buffer.lastIndexOf("/");
0884: if (index != -1) {
0885: String logDir = buffer.substring(0, index);
0886: // Modify directory permissions
0887: chmod(logDir);
0888: }
0889: }
0890:
0891: // Attach a error manager
0892: fileHandler.setErrorManager(new PortalLogErrorManager());
0893:
0894: getPortalLoggerDetail(loggerName).setPattern(buffer.toString());
0895: return fileHandler;
0896: }
0897:
0898: private void chmod(String logDir) {
0899: if (System.getProperty("os.name").indexOf("indows") == -1) {
0900: Runtime rt = Runtime.getRuntime();
0901: try {
0902: Process process = rt.exec("/bin/chmod -R a+rwx "
0903: + logDir);
0904: process.waitFor();
0905: } catch (Exception e) {
0906: PortalLogDebug.error(e);
0907: }
0908: }
0909: }
0910:
0911: private void replaceInstanceId(StringBuffer buffer) {
0912: String instanceId = getInstanceId();
0913: if (instanceId == null)
0914: instanceId = "";
0915: int index = buffer.indexOf(INSTANCE_ID_PATTERN);
0916: if (index != -1) {
0917: buffer.replace(index, index + INSTANCE_ID_PATTERN.length(),
0918: instanceId);
0919: }
0920: PortalLogDebug.info("Instance Id:" + instanceId);
0921: }
0922:
0923: private void replacePortalId(StringBuffer buffer) {
0924: String portalId = getPortalId();
0925: int index = buffer.indexOf(PORTAL_ID_PATTERN);
0926: if (index != -1) {
0927: if (portalId == null)
0928: buffer.replace(index, index
0929: + PORTAL_ID_PATTERN.length() + 1, ""); // +1 is for "."
0930: else
0931: buffer.replace(index, index
0932: + PORTAL_ID_PATTERN.length(), portalId);
0933: }
0934: PortalLogDebug.info("Portal Id:" + portalId);
0935: }
0936:
0937: private void replaceSRAComponentType(StringBuffer buffer) {
0938: String sraComponentType = getSRAComponentType();
0939: int index = buffer.indexOf(SRA_COMPONENT_TYPE_PATTERN);
0940: if (index != -1) {
0941: if (sraComponentType == null)
0942: buffer.replace(index, index
0943: + SRA_COMPONENT_TYPE_PATTERN.length() + 1, ""); // +1 is for "."
0944: else
0945: buffer.replace(index, index
0946: + SRA_COMPONENT_TYPE_PATTERN.length(),
0947: sraComponentType);
0948: }
0949: PortalLogDebug.info("SRA Component Type:" + sraComponentType);
0950: }
0951:
0952: private void replaceLoggerPattern(StringBuffer buffer,
0953: String loggerName) {
0954: int index = buffer.indexOf(LOGGER_PATTERN);
0955: if (index != -1) {
0956: buffer.replace(index, index + LOGGER_PATTERN.length(),
0957: loggerName);
0958: }
0959: }
0960:
0961: private void replacePrefix(StringBuffer buffer) {
0962: int index = buffer.indexOf(DEBUG_COM_SUN_PREFIX);
0963: if (index != -1) {
0964: buffer.replace(index,
0965: index + DEBUG_COM_SUN_PREFIX.length(), "");
0966: }
0967: }
0968:
0969: private String getInstanceId() {
0970: return System.getProperty(INSTANCE_ID_PROPERTY);
0971: }
0972:
0973: private String getPortalId() {
0974: return System.getProperty(PORTAL_ID_PROPERTY);
0975: }
0976:
0977: private String getSRAComponentType() {
0978: return System.getProperty(SRA_COMPONENT_TYPE_PROPERTY);
0979: }
0980:
0981: /**
0982: * Returns the instance of the Filter associated with the filter
0983: * attribute of FileHandler. If the property is not defined or has problems
0984: * the defaultValue is returned.
0985: *
0986: * @param loggerName the name of the logger
0987: * @param attribute the filter attribute
0988: * @param defaultValue the default Filter
0989: * @return the instance of the Filter
0990: */
0991: private Filter getFilter(String loggerName, String attribute,
0992: Filter defaultValue) {
0993: String value = getProperty(loggerName, attribute);
0994: try {
0995: if (value != null) {
0996: return (Filter) getClassInstance(value);
0997: }
0998: } catch (Exception ex) {
0999: // We got one of a variety of exceptions in creating the
1000: // class or creating an instance.
1001: // Drop through.
1002: }
1003: // We got an exception. Return the defaultValue.
1004: return defaultValue;
1005: }
1006:
1007: // check if this class is loaded by bootstrap classloader,
1008: // some implementation may retun null
1009: // in that case get the system classloader and load the class
1010: // else use it.
1011: // this check is added to avoid any problems in having the generic classes
1012: // in the server classpath
1013: private Object getClassInstance(String value) throws Exception {
1014: Class cls;
1015: ClassLoader clsLoader = this .getClass().getClassLoader();
1016: if (clsLoader == null) {
1017: cls = ClassLoader.getSystemClassLoader().loadClass(value);
1018: } else {
1019: cls = clsLoader.loadClass(value);
1020: }
1021: return cls.newInstance();
1022:
1023: }
1024:
1025: /**
1026: * Returns the instance of the Formatter associated with the formatter
1027: * attribute of FileHandler. If the property is not defined or has problems
1028: * the defaultValue is returned.
1029: *
1030: * @param loggerName the name of the logger
1031: * @param attribute the formatter attribute
1032: * @param defaultValue the default Formatter
1033: * @return the instance of the Formatter
1034: */
1035: private Formatter getFormatter(String loggerName, String attribute,
1036: Formatter defaultValue) {
1037: String value = getProperty(loggerName, attribute);
1038: try {
1039: if (value != null) {
1040: return (Formatter) getClassInstance(value);
1041: }
1042: } catch (Exception ex) {
1043: // We got one of a variety of exceptions in creating the
1044: // class or creating an instance.
1045: // Drop through.
1046: }
1047: // We got an exception. Return the defaultValue.
1048: return defaultValue;
1049: }
1050:
1051: /**
1052: * Returns a list of whitespace or comma separated classnames from a property.
1053: *
1054: * @param names whitespace or comma separated classnames
1055: * @return a array of classnames
1056: */
1057: private String[] parseClassNames(String names) {
1058: StringTokenizer tokens = new StringTokenizer(names, " \t,");
1059: String[] tokenValues = new String[tokens.countTokens()];
1060: int i = 0;
1061: while (tokens.hasMoreTokens())
1062: tokenValues[i++] = tokens.nextToken();
1063: return tokenValues;
1064: }
1065:
1066: /**
1067: * Returns a list of loggers.
1068: *
1069: * @return a list of loggers.
1070: */
1071: private Vector listPSLoggers() {
1072: Enumeration e = LogManager.getLogManager().getLoggerNames();
1073: Vector loggerNames = new Vector();
1074: String loggerName;
1075: while (e.hasMoreElements()) {
1076: loggerName = (String) e.nextElement();
1077: if (loggerName.startsWith(DEBUG_ROOT_LOGGER)) {
1078: loggerNames.add(loggerName);
1079: }
1080: }
1081: return loggerNames;
1082: }
1083:
1084: /**
1085: * Returns a list of child loggers from the list of PortalLoggers
1086: * for the given logger.
1087: * The list includes the logger.
1088: *
1089: * @param loggerName the name of the logger
1090: * @return list of child loggers.
1091: */
1092: private Vector getChildLoggers(String loggerName) {
1093: Vector loggerNames = listPSLoggers();
1094: return getChildLoggers(loggerName, loggerNames);
1095: }
1096:
1097: /**
1098: * Returns a list of child loggers from the list of given loggerNames
1099: * for the given logger.
1100: * The list includes the logger.
1101: *
1102: * @param loggerName the name of the logger
1103: * @return list of child loggers.
1104: */
1105: private Vector getChildLoggers(String loggerName, Vector loggerNames) {
1106: String loggerNamespace = loggerName + ".";
1107: int numberOfLoggers = loggerNames.size();
1108: String childLoggerName;
1109: Vector childLoggers = new Vector();
1110: for (int i = 0; i < numberOfLoggers; i++) {
1111: childLoggerName = (String) loggerNames.get(i);
1112: if (childLoggerName.equals(loggerName)
1113: || childLoggerName.startsWith(loggerNamespace)) {
1114: childLoggers.add(childLoggerName);
1115: }
1116: }
1117: return childLoggers;
1118: }
1119:
1120: /**
1121: * Sets the level for the logger.
1122: * All the child logger of the logger inherit the same level.
1123: *
1124: * @param loggerName the name of the logger.
1125: * @param level the value for the level.
1126: */
1127: private void setLogLevel(String loggerName, String level) {
1128:
1129: if (level != null) {
1130: Vector childLoggers = this .getChildLoggers(loggerName);
1131: int numberOfLoggers = childLoggers.size();
1132: String childLoggerName;
1133: for (int i = 0; i < numberOfLoggers; i++) {
1134: childLoggerName = (String) childLoggers.get(i);
1135: Logger.getLogger(childLoggerName).setLevel(
1136: parseLevel(level));
1137: }
1138: }
1139: }
1140:
1141: /**
1142: * Remove all the handlers associated with the logger and all its children.
1143: * If the root logger is "debug.com.sun.portal" do not remove the child
1144: * handlers of "debug.com.sun.portal.search".
1145: *
1146: * @param loggerName
1147: */
1148: private void removeHandlers(String loggerName) {
1149: // Get other root loggers
1150: Vector childLoggers = this .getChildLoggers(loggerName);
1151: int numberOfLoggers = childLoggers.size();
1152: String childLoggerName;
1153: for (int i = 0; i < numberOfLoggers; i++) {
1154: childLoggerName = (String) childLoggers.get(i);
1155: if (loggerName.equals(DEBUG_ROOT_LOGGER)
1156: && childLoggerName
1157: .startsWith(DEBUG_ROOT_LOGGER_SEARCH))
1158: continue;
1159: Logger tempLogger = Logger.getLogger(childLoggerName);
1160: Handler[] handlers = tempLogger.getHandlers();
1161: for (int j = 0; j < handlers.length; j++) {
1162: handlers[j].close();
1163: tempLogger.removeHandler(handlers[j]);
1164: }
1165: }
1166: }
1167:
1168: /**
1169: * Parse a level name string into a Level. If the level name is invalid,
1170: * use the default value specified in the default config properties.
1171: *
1172: * @param value the level name to be parsed.
1173: * @return parsed value
1174: */
1175: private Level parseLevel(String value) {
1176: Level level;
1177: try {
1178: level = Level.parse(value);
1179: } catch (IllegalArgumentException iae) {
1180: String levelValue = getDefaultProperty(DEBUG_ROOT_LOGGER,
1181: LOG_LEVEL_ATTRIBUTE);
1182: try {
1183: level = Level.parse(levelValue);
1184: } catch (IllegalArgumentException iae1) {
1185: level = Level.SEVERE;
1186: PortalLogDebug.info("Invalid level:" + levelValue
1187: + " for " + DEBUG_ROOT_LOGGER
1188: + " defaulting to SEVERE.");
1189: }
1190: }
1191: return level;
1192: }
1193:
1194: /**
1195: * Reads the configuration file and loads the content
1196: * and configure the loggers associated with the filename.
1197: *
1198: * @param fileName
1199: */
1200: public void reloadConfiguration(String fileName) {
1201:
1202: // Get the list of root loggers to be configured.
1203: Vector rootLoggerNames = (Vector) rootLoggerMap.get(fileName);
1204:
1205: // Re-read the config file
1206: readConfigFile(fileName);
1207:
1208: // Load all the configuration files
1209: loadConfigFiles();
1210:
1211: // configure the root logger and other loggers if separate file for the logger is mentioned
1212: int size = rootLoggerNames.size();
1213: for (int i = 0; i < size; i++)
1214: configure((String) rootLoggerNames.get(i));
1215: }
1216:
1217: public void fileChanged(String fileName) {
1218: synchronized (syncObject) {
1219: reloadConfiguration(fileName);
1220: }
1221: }
1222:
1223: private static boolean makeDir(String dirName) {
1224: File dir = new File(dirName);
1225: if (dir.exists()) {
1226: return true;
1227: } else {
1228: return dir.mkdirs();
1229: }
1230: }
1231:
1232: public String toString() {
1233: StringBuffer sb = new StringBuffer();
1234: sb.append(configProperties);
1235: return sb.toString();
1236: }
1237:
1238: private void initLoggersList(String loggerName, String fileName,
1239: boolean special) {
1240: LoggersList instance = LoggersList.init(fileName, special);
1241: if (!special)
1242: LoggersList.add(loggerName);
1243: }
1244:
1245: /**
1246: * Returns the log filename associated with the loggername.
1247: * @param loggerName the name of the logger.
1248: * @param logConfigFileName the configuration file for the logger
1249: * @return the name of the logfile associated with the logger.
1250: * @throws FileNotFoundException if the log configuration file is not found.
1251: * @throws IOException if an I/O error occurs.
1252: */
1253: public String getLogFileName(String loggerName,
1254: String logConfigFileName) throws FileNotFoundException,
1255: IOException {
1256: String loggerNameBak = loggerName;
1257: String logFileName = null;
1258: Properties props = new Properties();
1259: BufferedInputStream iStream = new BufferedInputStream(
1260: new FileInputStream(new File(logConfigFileName)));
1261: props.load(iStream);
1262: iStream.close();
1263:
1264: // Check whether the handler for the loggerName has .pattern attribute
1265: // If yes, return its pattern
1266: StringBuffer attributeBuffer = new StringBuffer();
1267: attributeBuffer.append(PortalLogManager.LOG_HANDLER_ATTRIBUTE)
1268: .append(".");
1269: String handlerName;
1270: if (loggerName.indexOf(".search.") != -1) {
1271: handlerName = (String) props
1272: .get(PortalLogManager.DEBUG_ROOT_LOGGER_SEARCH
1273: + PortalLogManager.LOG_HANDLER_ATTRIBUTE);
1274: } else {
1275: handlerName = (String) props
1276: .get(PortalLogManager.DEBUG_ROOT_LOGGER
1277: + PortalLogManager.LOG_HANDLER_ATTRIBUTE);
1278: }
1279: attributeBuffer.append(handlerName).append(
1280: PortalLogManager.LOG_HANDLER_PATTERN_ATTRIBUTE);
1281: String attribute = attributeBuffer.toString();
1282: String value = props.getProperty(loggerName + attribute);
1283: while (value == null
1284: && !loggerName
1285: .equals(PortalLogManager.DEBUG_ROOT_LOGGER)) {
1286: loggerName = getParent(loggerName);
1287: value = props.getProperty(loggerName + attribute);
1288: }
1289: logFileName = value;
1290:
1291: // Check whether the loggerName has .separatefile attribute
1292: // If yes, return the pattern of its parent
1293: if (props.containsKey(loggerName
1294: + PortalLogManager.LOG_SEPARATE_FILE_ATTRIBUTE)) {
1295: attribute = PortalLogManager.LOG_HANDLER_ATTRIBUTE + "."
1296: + DEFAULT_LOG_HANDLER
1297: + PortalLogManager.LOG_HANDLER_PATTERN_ATTRIBUTE;
1298: value = props.getProperty(loggerName + attribute);
1299: while (value == null
1300: && !loggerName
1301: .equals(PortalLogManager.DEBUG_ROOT_LOGGER)) {
1302: loggerName = getParent(loggerName);
1303: value = props.getProperty(loggerName + attribute);
1304: }
1305: logFileName = value;
1306: }
1307:
1308: StringBuffer buffer = new StringBuffer();
1309: if (logFileName != null) {
1310: buffer.append(logFileName);
1311:
1312: // Replace the tag %instanceID with instance id if present
1313: // and create a directory by that name
1314: replaceInstanceId(buffer);
1315:
1316: // Replace the tag %portalID with portal id if present
1317: replacePortalId(buffer);
1318:
1319: // Replace the tag %sraComponentType with SRA component type if present
1320: replaceSRAComponentType(buffer);
1321:
1322: // Replace the tag %logger with loggername
1323: replaceLoggerPattern(buffer, loggerNameBak);
1324:
1325: // If the pattern has debug.com.sun. remove it so that the logger name starts with portal.XXX
1326: replacePrefix(buffer);
1327: }
1328: return buffer.toString();
1329: }
1330:
1331: /**
1332: * Returns the log filename associated with the loggername.
1333: * Here the log configuration filename is obtained from the system property.
1334: * @param loggerName the name of the logger.
1335: * @throws FileNotFoundException if the log configuration file is not found.
1336: * @throws IOException if an I/O error occurs.
1337: */
1338: public String getLogFileName(String loggerName)
1339: throws FileNotFoundException, IOException {
1340:
1341: String logConfigFileName = System
1342: .getProperty(LOG_CONFIG_FILENAME_PROPERTY);
1343: return getLogFileName(loggerName, logConfigFileName);
1344: }
1345:
1346: public static void deleteSRALoggersList(String fullPathConfigFile,
1347: String sraComponentType) {
1348: LoggersList.delete(fullPathConfigFile, sraComponentType);
1349: }
1350:
1351: /*
1352: public static void main(String[] args) throws Exception {
1353: PortalLogManager manager = new PortalLogManager();
1354: String name = manager.getLogFileName(args[0], args[1]);
1355: System.out.println(name);
1356: }
1357: */
1358: }
1359:
1360: /**
1361: * Used to Sort the logger names in the descending order.
1362: */
1363: class LoggerComparator implements Comparator {
1364:
1365: public int compare(Object o1, Object o2) {
1366: String s1 = (String) o1;
1367: String s2 = (String) o2;
1368: return s2.compareTo(s1);
1369: }
1370:
1371: }
|