001: /**
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.fabric.util;
013:
014: import com.sun.portal.admin.common.PSConfigConstants;
015:
016: import java.io.BufferedReader;
017: import java.io.File;
018: import java.io.FileReader;
019: import java.io.FileWriter;
020: import java.io.IOException;
021: import java.io.PrintWriter;
022:
023: import com.sun.portal.util.Platform;
024:
025: /**
026: * This class provides methods to copy Log Config files from the templates
027: * directory to the appropriate directory and do the tag swapping in the
028: * config files.
029: *
030: */
031: public class LogConfigUtil {
032: /**
033: * File separator
034: */
035: public final static String fs = Platform.fs;
036:
037: // Tokens
038: private static final String LOG_DIR_TOKEN = "%LOG_DIR%";
039: private static final String ADMIN_LOG_DIR_TOKEN = "%ADMIN_LOG_DIR%";
040: private static final String ROOT_LOGGER_TOKEN = "%ROOT_LOGGER%";
041: private static final String INSTANCE_ID_TOKEN = "%instanceID";
042: private static final String SRA_COMPONENT_TYPE_TOKEN = "%sraComponentType.";
043: private static final boolean EXCLUDE_SEARCH = true;
044: private static final boolean EXCLUDE_CONSOLE = true;
045: private static final boolean EXCLUDE_AUDIT = true;
046: private static String logLevel = null;
047:
048: /**
049: * Returns the log config file.
050: * Copies the log config file from the templates directory(psBaseDir/template)
051: * to a temp file and returns it.
052: * The token %ROOT_LOGGER% will be replaced by loggerName
053: * The token %LOG_DIR% will be replaced by logDestDir
054: *
055: * @param psBaseDir Portal Server product installation directory path
056: * @param logDestDir destination directory path for the logs
057: * @param loggerName the logger name that will used for token replacement
058: */
059: public static File getAdminLogConfigFile(String psBaseDir,
060: String logDestDir, String loggerName, boolean excludeAudit)
061: throws IOException {
062:
063: // Copy the PSLogConfig properties from the template
064: // to a temp file
065: String logConfigTemplate = psBaseDir + fs + "template" + fs
066: + PSConfigConstants.PS_LOG_CONFIG_FILE;
067: File logConfigFile = File.createTempFile(loggerName, ".tmp");
068: logConfigFile.deleteOnExit();
069:
070: //System.out.println("template:" + logConfigTemplate + " dest:" + logConfigDest);
071: copyFileFromTemplate(logConfigTemplate, logConfigFile,
072: EXCLUDE_SEARCH, EXCLUDE_CONSOLE, excludeAudit);
073:
074: //System.out.println("LogDir:" + logDir);
075: FileUtil.replaceTokenInFile(logConfigFile, ROOT_LOGGER_TOKEN,
076: loggerName);
077: FileUtil.replaceTokenInFile(logConfigFile, LOG_DIR_TOKEN,
078: logDestDir);
079:
080: // Store the level in the local static variable, this value is used during reset loglevel.
081: if (logLevel == null) {
082: String logLevelLine = FileUtil.findTextInFile(
083: logConfigFile, loggerName + ".level");
084: int index = logLevelLine.indexOf("=");
085: if (index != -1) {
086: logLevel = logLevelLine.substring(index + 1).trim();
087: } else {
088: logLevel = "SEVERE";
089: }
090: }
091:
092: // For Admin Log Config Files, make the default level FINEST
093: FileUtil.replaceLineInFile(logConfigFile,
094: loggerName + ".level", loggerName + ".level=FINEST");
095:
096: // The token %sraComponentType and %instanceID is not relevant,
097: // hence do not include them
098: FileUtil.replaceTokenInFile(logConfigFile,
099: SRA_COMPONENT_TYPE_TOKEN, "");
100: FileUtil.replaceTokenInFile(logConfigFile, INSTANCE_ID_TOKEN,
101: "");
102:
103: // Create the logs directory
104: FileUtil.makeDir(logDestDir);
105:
106: return logConfigFile;
107: }
108:
109: /**
110: * Returns the default loglevel present in the log config file.
111: *
112: */
113: public static String getDefaultLogLevel() {
114: return logLevel;
115: }
116:
117: /**
118: * Returns the log config file template
119: * Copies the log config file from the templates directory(psBaseDir/template)
120: * to a temp file and returns it.
121: * The token %ROOT_LOGGER% will be replaced by debug.com.sun.portal
122: *
123: * @param psBaseDir Portal Server product installation directory path
124: */
125: public static File getLogConfigFileTemplate(String psBaseDir)
126: throws IOException {
127:
128: // Copy the PSLogConfig properties from the template
129: // to a temp file
130: String loggerName = "debug.com.sun.portal";
131: String logConfigTemplate = psBaseDir + fs + "template" + fs
132: + PSConfigConstants.PS_LOG_CONFIG_FILE;
133: File logConfigFile = File.createTempFile(loggerName, ".tmp");
134:
135: //System.out.println("template:" + logConfigTemplate + " dest:" + logConfigDest);
136: copyFileFromTemplate(logConfigTemplate, logConfigFile,
137: EXCLUDE_SEARCH, EXCLUDE_CONSOLE, EXCLUDE_AUDIT);
138:
139: //System.out.println("LogDir:" + logDir);
140: FileUtil.replaceTokenInFile(logConfigFile, ROOT_LOGGER_TOKEN,
141: loggerName);
142:
143: // The token %instanceID is not relevant,
144: // hence do not include it
145: FileUtil.replaceTokenInFile(logConfigFile, INSTANCE_ID_TOKEN,
146: "");
147:
148: return logConfigFile;
149: }
150:
151: /**
152: * Creates the log config file for portal
153: * Copies the log config file from the templates directory(psBaseDir/template)
154: * to the config directory of Portal ID(psDataDir/portals/portalID/config).
155: * The destination file is PSLogConfig.properties
156: * It also replaces the tag %LOG_DIR% with the the log
157: * directory(psDataDir/portals/portalID/logs).
158: *
159: * @param psBaseDir Portal Server product installation directory path
160: * @param psDataDir Portal Server software data directory path
161: * @param portalID the Portal ID
162: */
163: public static void createPortalLogConfigFile(String psBaseDir,
164: String psDataDir, String portalID) {
165:
166: String loggerName = "debug.com.sun.portal";
167: String portalDir = psDataDir + fs + "portals" + fs + portalID;
168: //Path of the portal specific config directory
169: String portalConfigDir = portalDir + fs + "config";
170:
171: // Copy the PSLogConfig properties from the template
172: // and place it in the configuration directory of the
173: // new portal
174: String logConfigTemplate = psBaseDir + fs + "template" + fs
175: + PSConfigConstants.PS_LOG_CONFIG_FILE;
176: String logConfigDest = portalConfigDir + fs
177: + PSConfigConstants.PS_LOG_CONFIG_FILE;
178: File logConfigFile = new File(logConfigDest);
179:
180: //System.out.println("template:" + logConfigTemplate + " dest:" + logConfigDest);
181: copyFileFromTemplate(logConfigTemplate, logConfigFile,
182: EXCLUDE_SEARCH, !EXCLUDE_CONSOLE, EXCLUDE_AUDIT);
183:
184: String logDir = portalDir + fs + "logs";
185: //System.out.println("LogDir:" + logDir);
186: String adminLogDir = psDataDir + fs + "logs" + fs + "admin";
187:
188: FileUtil.replaceTokenInFile(logConfigFile, ROOT_LOGGER_TOKEN,
189: loggerName);
190: FileUtil.replaceTokenInFile(logConfigFile, LOG_DIR_TOKEN,
191: logDir);
192: FileUtil.replaceTokenInFile(logConfigFile, ADMIN_LOG_DIR_TOKEN,
193: adminLogDir);
194:
195: // The token %sraComponentType is not relevant,
196: // hence do not include it
197: FileUtil.replaceTokenInFile(logConfigFile,
198: SRA_COMPONENT_TYPE_TOKEN, "");
199:
200: // Create the logs directory
201: FileUtil.makeDir(logDir);
202: }
203:
204: /**
205: * Creates the log config file for search.
206: * Copies the log config file from the templates directory(psBaseDir/template)
207: * to the config directory of Portal ID(searchDataDir/searchservers/searchServerID/config).
208: * The destination file is SearchLogConfig.properties
209: * It also replaces the tag %LOG_DIR% with the the log
210: * directory(searchDataDir/searchservers/searchServerID/logs).
211: *
212: * @param psBaseDir Portal Server product installation directory path
213: * @param searchDataDir Search Server software data directory path
214: * @param searchServerID the Search server ID
215: */
216: public static void createSearchLogConfigFile(String psBaseDir,
217: String searchDataDir, String searchServerID) {
218:
219: String loggerName = "debug.com.sun.portal.search";
220: String searchDir = searchDataDir + fs + "searchservers" + fs
221: + searchServerID;
222: //Path of the search specific config directory
223: String searchConfigDir = searchDir + fs + "config";
224:
225: // Copy the PSLogConfig properties from the template
226: // and place it in the configuration directory of the
227: // new searchserver
228: String logConfigTemplate = psBaseDir + fs + "template" + fs
229: + PSConfigConstants.PS_LOG_CONFIG_FILE;
230: String logConfigDest = searchConfigDir + fs
231: + PSConfigConstants.SEARCH_LOG_CONFIG_FILE;
232: File logConfigFile = new File(logConfigDest);
233:
234: //System.out.println("template:" + logConfigTemplate + " dest:" + logConfigDest);
235: copyFileFromTemplate(logConfigTemplate, logConfigFile,
236: !EXCLUDE_SEARCH, EXCLUDE_CONSOLE, EXCLUDE_AUDIT);
237:
238: String logDir = searchDir + fs + "logs";
239: //System.out.println("LogDir:" + logDir);
240:
241: FileUtil.replaceTokenInFile(logConfigFile, ROOT_LOGGER_TOKEN,
242: loggerName);
243: FileUtil.replaceTokenInFile(logConfigFile, LOG_DIR_TOKEN,
244: logDir);
245:
246: // The token %sraComponentType is not relevant,
247: // hence do not include it
248: FileUtil.replaceTokenInFile(logConfigFile,
249: SRA_COMPONENT_TYPE_TOKEN, "");
250:
251: // Create the logs directory
252: FileUtil.makeDir(logDir);
253: }
254:
255: /**
256: * Copies the log file from the template to the destination.
257: * If excludeSearch is true, it excludes lines containing rdm, rdmserver
258: * and rdmgr.
259: * @param logConfigTemplate the Log config file present in the template
260: * @param logConfigFile the full path file name of the config file
261: * @param excludeSearch true if search related properties needs to be excluded.
262: * @param excludeConsole true if admin console related properties needs to be excluded.
263: * @param excludeAudit true if audit related properties needs to be excluded.
264: */
265: private static void copyFileFromTemplate(String logConfigTemplate,
266: File logConfigFile, boolean excludeSearch,
267: boolean excludeConsole, boolean excludeAudit) {
268: String line;
269: BufferedReader inBuff = null;
270: PrintWriter outBuff = null;
271: try {
272: inBuff = new BufferedReader(new FileReader(
273: logConfigTemplate));
274: outBuff = new PrintWriter(new FileWriter(logConfigFile));
275: while ((line = inBuff.readLine()) != null) {
276: // For search include lines with rdm, rdmserver, rdmgr
277: if (excludeSearch) {
278: if (line.indexOf(".rdm.") >= 0
279: || line.indexOf(".rdmserver.") >= 0
280: || line.indexOf(".rdmgr.") >= 0
281: || line.indexOf(".autoclassify.") >= 0) {
282: continue;
283: }
284: }
285: // For admin console include lines with admin.console
286: if (excludeConsole) {
287: if (line.indexOf(".admin.console.") >= 0) {
288: continue;
289: }
290: }
291: // For audit include lines with audit.
292: if (excludeAudit) {
293: if (line.indexOf(".audit.") >= 0) {
294: continue;
295: }
296: }
297:
298: outBuff.println(line);
299: }
300: } catch (Exception e) {
301: System.out.println("Cannot copy the file from : "
302: + logConfigTemplate + " to "
303: + logConfigFile.getAbsoluteFile());
304: } finally {
305: try {
306: if (inBuff != null)
307: inBuff.close();
308: if (outBuff != null)
309: outBuff.close();
310: } catch (IOException ignored) {
311: }
312: }
313: }
314:
315: /*
316: public static void main(String[] args) throws IOException {
317: //LogConfigUtil.createPortalLogConfigFile("/opt/SUNWportal", "/var/opt/SUNWportal", "myPortal");
318: //LogConfigUtil.createSearchLogConfigFile("/opt/SUNWportal", "/var/opt/SUNWportal", "mySearch");
319: File inFile = LogConfigUtil.getAdminLogConfigFile("/opt/SUNWportal", "/tmp", "debug.com.sun.portal", true);
320: File inFile1 = LogConfigUtil.getAdminLogConfigFile("/opt/SUNWportal", "/tmp", "debug.com.sun.portal", false);
321: File outFile = new File("/tmp/test.properties");
322: File outFile1 = new File("/tmp/test1.properties");
323: FileUtil.copyFile(inFile, outFile);
324: FileUtil.copyFile(inFile1, outFile1);
325: System.out.println(LogConfigUtil.getDefaultLogLevel());
326: }
327: */
328: }
|