01: package com.sun.portal.util;
02:
03: import java.util.Iterator;
04: import java.util.Properties;
05: import java.util.regex.Matcher;
06: import java.util.regex.Pattern;
07: import java.io.IOException;
08: import java.net.URL;
09: import org.apache.log4j.PropertyConfigurator;
10: import org.apache.log4j.spi.LoggerRepository;
11: import org.apache.log4j.spi.Configurator;
12:
13: /**
14: * Initialises the Log4J based on the configuration file
15: * <PS_DATA_DIR>/portals/<PORTAL_ID>/config/portal-log4j.properties
16: *
17: */
18:
19: public class Log4jInit implements Configurator {
20:
21: private static ResourceLoader resourceLoader = ResourceLoader
22: .getInstance(System.getProperties());
23: private static Pattern PORTAL_ID_PAT = Pattern
24: .compile("%PORTAL_ID%");
25: private static Pattern INSTANCE_ID_PAT = Pattern
26: .compile("%INSTANCE_ID%");
27: private static String PORTAL_ID = resourceLoader.getPortalId();
28: private static String INSTANCE_ID = resourceLoader.getInstanceId();
29:
30: public void doConfigure(URL url, LoggerRepository repository) {
31: try {
32: Properties p = resourceLoader
33: .getProperties("portal-log4j.properties");
34: tokenizeProperties(p);
35: PropertyConfigurator.configure(p);
36: } catch (IOException ex) {
37: ex.printStackTrace();
38: throw new RuntimeException(
39: "Portal Log4j Initialization Failed: "
40: + ex.getMessage());
41: }
42: }
43:
44: private static void tokenizeProperties(Properties p) {
45: for (Iterator i = p.keySet().iterator(); i.hasNext();) {
46: String key = (String) i.next();
47: String val = p.getProperty(key);
48:
49: Matcher m;
50: m = PORTAL_ID_PAT.matcher(val);
51: val = m.replaceAll(PORTAL_ID);
52: m = INSTANCE_ID_PAT.matcher(val);
53: val = m.replaceAll(INSTANCE_ID);
54:
55: p.put(key, val);
56: }
57: }
58: }
|