01: package com.sun.portal.util;
02:
03: import java.util.regex.Matcher;
04: import java.util.regex.Pattern;
05: import java.util.Properties;
06: import java.io.IOException;
07: import java.io.File;
08:
09: import org.apache.log4j.FileAppender;
10:
11: /**
12: * Portal Specific File Appender that token replaces the PORTAL_ID and
13: * INSTANCE_ID of the filename.
14: *
15: */
16:
17: public class PortalFileAppender extends FileAppender {
18:
19: // PORTAL_DATA_DIR token is different from other two tokens(i.e has @). This is because this
20: // token is replaced during install time for portlet-apps. But for non-portlet apps its being replaced
21: // during run time.
22: private static Pattern PORTAL_DATA_DIR_PAT = Pattern
23: .compile("@PORTAL_DATA_DIR@");
24: private static Pattern PORTAL_ID_PAT = Pattern
25: .compile("%PORTAL_ID%");
26: private static Pattern INSTANCE_ID_PAT = Pattern
27: .compile("%INSTANCE_ID%");
28: public static final String PORTAL_ID = "com.sun.portal.portal.id";
29: public static final String INSTANCE_ID = "com.sun.portal.instance.id";
30: private static String portalId;
31: private static String instanceId;
32: private static String portalDataDir;
33:
34: static {
35: try {
36: Properties psprops = new Properties();
37: psprops.load(ClassLoader
38: .getSystemResourceAsStream("PSConfig.properties"));
39: portalDataDir = psprops.getProperty("ps.data.location");
40: portalId = System.getProperty(PORTAL_ID);
41: instanceId = System.getProperty(INSTANCE_ID);
42: } catch (Exception e) {
43: // Ignore, as nothing can be done.
44: }
45: }
46:
47: public synchronized void setFile(String fileName, boolean append,
48: boolean bufferedIO, int bufferSize) throws IOException {
49: String tmpFileName = fileName;
50: Matcher m;
51: if (portalDataDir == null)
52: return; // No use continuing the tag swap
53: m = PORTAL_DATA_DIR_PAT.matcher(tmpFileName);
54: tmpFileName = m.replaceAll(portalDataDir);
55:
56: if (portalId == null)
57: return; // No use continuing the tag swap
58: m = PORTAL_ID_PAT.matcher(tmpFileName);
59: tmpFileName = m.replaceAll(portalId);
60:
61: if (instanceId == null)
62: return; // No use continuing the tag swap
63: m = INSTANCE_ID_PAT.matcher(tmpFileName);
64: tmpFileName = m.replaceAll(instanceId);
65:
66: // Create directory if not present
67: int lastIndex = tmpFileName.lastIndexOf("/");
68: if (lastIndex != -1) {
69: String logDir = tmpFileName.substring(0, lastIndex);
70: makeDir(logDir);
71: }
72:
73: super .setFile(tmpFileName, append, bufferedIO, bufferSize);
74: }
75:
76: private static boolean makeDir(String dirName) {
77: File dir = new File(dirName);
78: if (dir.exists()) {
79: return true;
80: } else {
81: return dir.mkdirs();
82: }
83: }
84: }
|