001: /**
002: * $Id: UBTReport.java,v 1.12 2006/10/27 07:06:55 vk162573 Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.ubt.admin.mbeans;
014:
015: import net.sf.jasperreports.engine.JasperPrint;
016:
017: import java.io.File;
018: import java.io.FileInputStream;
019:
020: import java.util.*;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023: import java.util.logging.LogRecord;
024:
025: import com.sun.portal.log.common.PortalLogger;
026: import com.sun.portal.admin.server.mbeans.PSResource;
027: import com.sun.portal.admin.server.AdminServerUtil;
028: import com.sun.portal.admin.common.context.PSConfigContext;
029: import com.sun.portal.admin.common.context.PortalDomainContext;
030: import com.sun.portal.admin.common.PSMBeanException;
031: import com.sun.portal.ubt.report.server.UBTReportGenerator;
032:
033: public class UBTReport extends PSResource implements UBTReportMBean {
034:
035: private String psPortalID;
036: private static Logger logger = PortalLogger
037: .getLogger(UBTReport.class);
038: private static final String INSTANCE_PATTRN = "%instance";
039:
040: /**
041: * Initializes this UBT Report mbean with the given Portal
042: * Server configuration context, portal domain context, and path.
043: *
044: * @param cc the Portal Server configuration context.
045: * @param pdc the portal domain context.
046: * @param path the path to identify this resource instance.
047: */
048: public void init(PSConfigContext cc, PortalDomainContext pdc,
049: List path) {
050: super .init(cc, pdc, path);
051: psPortalID = (String) path.get(1);
052: System.setProperty("jasper.reports.compile.class.path",
053: getJasperClasspath(cc));
054: }
055:
056: private String getJasperClasspath(PSConfigContext cc) {
057: StringBuffer classpath = new StringBuffer();
058: String dir = cc.getPSBaseDir() + fs + "lib" + fs;
059: String seperator = File.pathSeparator;
060: classpath.append(dir).append("jasperreports.jar").append(
061: seperator);
062: classpath.append(dir).append("jfreechart.jar")
063: .append(seperator);
064: classpath.append(dir).append("itext.jar").append(seperator);
065: classpath.append(dir).append("jcommon.jar").append(seperator);
066: classpath.append(dir).append("commons-beanutils.jar").append(
067: seperator);
068: classpath.append(dir).append("commons-collections.jar").append(
069: seperator);
070: classpath.append(dir).append("commons-digester.jar").append(
071: seperator);
072: classpath.append(dir).append("commons-logging.jar").append(
073: seperator);
074:
075: return classpath.toString();
076:
077: }
078:
079: /**
080: * Generate UBT Report. If no reportName is given,
081: * it generates all the UBT reports
082: *
083: * @param reportName
084: * @throws PSMBeanException
085: */
086: public Object generateUBTReport(String reportName, Locale locale)
087: throws PSMBeanException {
088: return generateUBTReport(
089: reportName != null ? new String[] { reportName } : null,
090: locale);
091: }
092:
093: /**
094: * Generate UBT Report. If no reportName is given,
095: * it generates all the UBT reports, else all the reports that are specified are generated
096: * @param reportNames Array of reports to be generated
097: * @return
098: * @throws PSMBeanException
099: */
100: //Mainly used by the console
101: public Object generateUBTReport(String[] reportNames, Locale locale)
102: throws PSMBeanException {
103: Properties ubtProperties = getUBTConfigProperties();
104: validateReportGeneration(ubtProperties);
105: List logFile = getLogFileNames(ubtProperties);
106: if (logFile.size() == 0) {
107: logger.log(Level.SEVERE, "PSUB_CSPUAM0001", getID());
108:
109: throw new PSMBeanException(
110: "admin.error.ubt.no.log.file.found",
111: new Object[] { getID() });
112: }
113: JasperPrint[] jPrints = null;
114: try {
115: UBTReportGenerator repGenerator = new UBTReportGenerator(
116: getTemplateDir(), psPortalID, locale);
117: jPrints = repGenerator.generateReport(reportNames,
118: (File[]) logFile.toArray(new File[logFile.size()]));
119: } catch (Exception e) {
120: if (logger.isLoggable(Level.SEVERE)) {
121: LogRecord record = new LogRecord(Level.SEVERE,
122: "PSUB_CSPUAM0002");
123: record.setParameters(new String[] { getID() });
124: record.setThrown(e);
125: record.setLoggerName(logger.getName());
126: logger.log(record);
127: }
128:
129: throw new PSMBeanException(
130: "admin.error.ubt.generating.report",
131: e.getMessage(), e, new Object[] { getID() });
132: }
133: return jPrints;
134: }
135:
136: /**
137: * Returns UBTReportI[] mined report data, which an aggregator can use to aggreagate
138: * to generate actual report.
139: * This is derived data per report for a host(all instances of that portal)
140: * @param reportNames
141: * @return UBTReportI[]
142: * @throws PSMBeanException
143: */
144: public Object getReportData(String[] reportNames)
145: throws PSMBeanException {
146: Properties ubtProperties = getUBTConfigProperties();
147: validateReportGeneration(ubtProperties);
148: List logFile = getLogFileNames(ubtProperties);
149: if (logFile.size() == 0) {
150: logger.log(Level.SEVERE, "PSUB_CSPUAM0001", getID());
151:
152: throw new PSMBeanException(
153: "admin.error.ubt.no.log.file.found",
154: new Object[] { getID() });
155: }
156: TreeMap reportData = null;
157: try {
158: UBTReportGenerator repGenerator = new UBTReportGenerator(
159: getTemplateDir(), psPortalID, Locale.getDefault());
160: reportData = repGenerator.getUBTReportData(reportNames,
161: (File[]) logFile.toArray(new File[logFile.size()]));
162: } catch (Exception e) {
163: if (logger.isLoggable(Level.SEVERE)) {
164: LogRecord record = new LogRecord(Level.SEVERE,
165: "PSUB_CSPUAM0002");
166: record.setParameters(new String[] { getID() });
167: record.setThrown(e);
168: record.setLoggerName(logger.getName());
169: logger.log(record);
170: }
171:
172: throw new PSMBeanException(
173: "admin.error.ubt.generating.report",
174: e.getMessage(), e, new Object[] { getID() });
175: }
176: return reportData;
177: }
178:
179: /**
180: * To get the template folder path
181: * @return
182: */
183: private String getTemplateDir() {
184: return cc.getPSBaseDir() + fs + "template" + fs + "ubtreport";
185: }
186:
187: /**
188: *
189: * @return
190: * @throws PSMBeanException
191: */
192: private Properties getUBTConfigProperties() throws PSMBeanException {
193: String dataPath = cc.getPSDataDir();
194: String ubtPropertyFilePath = dataPath + fs + "portals" + fs
195: + psPortalID + fs + "config" + fs
196: + "UBTConfig.properties";
197:
198: File ubtPropertyFile = new File(ubtPropertyFilePath);
199: Properties ubtProperties = new Properties();
200:
201: try {
202: ubtProperties.load(new FileInputStream(ubtPropertyFile));
203:
204: } catch (Exception e) {
205: if (logger.isLoggable(Level.SEVERE)) {
206: LogRecord record = new LogRecord(Level.SEVERE,
207: "PSUB_CSPUAM0003");
208: record.setParameters(new String[] { getID() });
209: record.setThrown(e);
210: record.setLoggerName(logger.getName());
211: logger.log(record);
212: }
213:
214: throw new PSMBeanException(
215: "admin.error.ubt.reading.ubt.properties", e
216: .getMessage(), e, new Object[] { getID() });
217: }
218:
219: return ubtProperties;
220:
221: }
222:
223: /**
224: *
225: * @param ubtProperties
226: * @throws PSMBeanException
227: */
228: private void validateReportGeneration(Properties ubtProperties)
229: throws PSMBeanException {
230: String handler = (String) ubtProperties
231: .get("com.sun.portal.ubt.handler");
232: if (handler != null) {
233: StringTokenizer tokens = new StringTokenizer(handler, " ,");
234: boolean valid = false;
235: while (tokens.hasMoreTokens()) {
236: String token = tokens.nextToken();
237: if ("java.util.logging.FileHandler".equals(token)) {
238: valid = true;
239: break;
240: }
241: }
242: if (!valid) {
243: logger.log(Level.SEVERE, "PSUB_CSPUAM0004",
244: new String[] { handler, getID() });
245:
246: throw new PSMBeanException(
247: "admin.error.ubt.report.not.supported",
248: new Object[] { getID() });
249: }
250: } else {
251: logger.log(Level.SEVERE, "PSUB_CSPUAM0005", getID());
252: throw new PSMBeanException(
253: "admin.error.ubt.filehandler.missing",
254: new Object[] { getID() });
255: }
256: }
257:
258: /**
259: *
260: * @param ubtPropertyFile
261: * @return
262: * @throws PSMBeanException
263: */
264: //To get a list of all the ubt log files for a portal
265: private List getLogFileNames(Properties ubtPropertyFile)
266: throws PSMBeanException {
267: String filePattern = (String) ubtPropertyFile
268: .get("java.util.logging.FileHandler.pattern");
269:
270: List portalInstanceIds = null;
271: if (filePattern.indexOf(INSTANCE_PATTRN) != -1) {
272: //This means file pattern can be as follows
273: ///var/opt/SUNWportal/portals/engPortal/%instance/logs/ubt.%u.%g.%instance.log
274: //So, we need to get all the instance ids for replacing
275: // %instance with instance name
276: portalInstanceIds = getPortalInstances();
277: }
278:
279: List allFileList = getAllFilesInLogFolders(filePattern,
280: portalInstanceIds);
281: List ubtLogFileList = new ArrayList();
282: List regExp = getRegularExpressionList(portalInstanceIds,
283: filePattern);
284: Iterator iter = allFileList.iterator();
285: while (iter.hasNext()) {
286: File logFile = (File) iter.next();
287: if (matchRegExp(logFile, regExp)) {
288: ubtLogFileList.add(logFile);
289: }
290: }
291: return ubtLogFileList;
292: }
293:
294: /**
295: *
296: * @param logFile
297: * @param regExpList
298: * @return
299: */
300: private boolean matchRegExp(File logFile, List regExpList) {
301: String fileName = logFile.getName();
302: if (regExpList.size() > 1) {
303: Iterator iter = regExpList.iterator();
304: while (iter.hasNext()) {
305: String regExp = (String) iter.next();
306: if (fileName.matches(regExp)) {
307: return true;
308: }
309: }
310: } else {
311: String regExp = (String) regExpList.get(0);
312: return fileName.matches(regExp);
313: }
314:
315: return false;
316:
317: }
318:
319: /**
320: *
321: * @param filePattern
322: * @param instanceList
323: * @return Returns a list of all the files found in folders specified by
324: * filePattern
325: */
326: //To get a list of all the folders containing log files
327: private List getAllFilesInLogFolders(String filePattern,
328: List instanceList) {
329: //check whether log files are in their respective instance folders
330: List allFilesInLogFolder = new ArrayList();
331: if (instanceList != null) {
332: //This means file pattern can be as follows
333: ///var/opt/SUNWportal/portals/engPortal/%instance/logs/ubt.%u.%g.%instance.log
334: //So, we need to get all the folders by replacing %instance with instance name
335: Iterator iter = instanceList.iterator();
336: while (iter.hasNext()) {
337: String newfilePattern = filePattern.replaceAll(
338: INSTANCE_PATTRN, (String) iter.next());
339: String folder = newfilePattern.substring(0,
340: newfilePattern.lastIndexOf(fs));
341: File logFolder = new File(folder);
342: if (logFolder.exists() && logFolder.isDirectory()) {
343: File[] possibleLogFiles = logFolder.listFiles();
344: allFilesInLogFolder.addAll(Arrays
345: .asList(possibleLogFiles));
346: }
347: }//while
348: } else {
349: String folder = filePattern.substring(0, filePattern
350: .lastIndexOf(fs));
351: File logFolder = new File(folder);
352: if (logFolder.exists() && logFolder.isDirectory()) {
353: File[] possibleLogFiles = logFolder.listFiles();
354: allFilesInLogFolder.addAll(Arrays
355: .asList(possibleLogFiles));
356: }//if
357: }
358:
359: return allFilesInLogFolder;
360: }
361:
362: /**
363: *
364: * @param portalInstanceIds
365: * @param filePattern
366: * @return
367: */
368: private List getRegularExpressionList(List portalInstanceIds,
369: String filePattern) {
370: List regExpList = new ArrayList();
371: String actualFilePattern = filePattern.substring(filePattern
372: .lastIndexOf(fs) + 1);
373: int instanceIdx = actualFilePattern.indexOf(INSTANCE_PATTRN);
374: if (portalInstanceIds != null && instanceIdx != -1) {
375: Iterator iter = portalInstanceIds.iterator();
376: while (iter.hasNext()) {
377: regExpList.add(getRegularExpression(actualFilePattern,
378: (String) iter.next()));
379: }
380: } else {
381: regExpList
382: .add(getRegularExpression(actualFilePattern, null));
383: }
384:
385: return regExpList;
386: }
387:
388: /**
389: *
390: * @param pattern
391: * @param instanceId
392: * @return Regular expression with all the pattern replaced by respective string
393: */
394: private String getRegularExpression(String pattern,
395: String instanceId) {
396: String regExp = pattern.replaceAll("\\.", "\\\\.");
397: regExp = regExp.replaceAll("%u", ".*");
398: regExp = regExp.replaceAll("%g", ".*");
399: if (instanceId != null) {
400: regExp = regExp.replaceAll(INSTANCE_PATTRN, instanceId);
401: }
402: return regExp;
403: }
404:
405: /**
406: * Returns a list of portal instances. This is requied if file pattern
407: * is containing template place holder.
408: * @return java.util.List
409: * @throws PSMBeanException
410: */
411: private List getPortalInstances() throws PSMBeanException {
412: try {
413: return new ArrayList(
414: AdminServerUtil.getPortalServerInstances(
415: m_sPsDomainID, psPortalID));
416: } catch (Exception e) {
417: if (logger.isLoggable(Level.SEVERE)) {
418: LogRecord record = new LogRecord(Level.SEVERE,
419: "PSUB_CSPUAM0006");
420: record.setParameters(new String[] { getID() });
421: record.setThrown(e);
422: record.setLoggerName(logger.getName());
423: logger.log(record);
424: }
425:
426: throw new PSMBeanException("admin.error.ubt.getInstances",
427: e.getMessage(), e, new Object[] { getID() });
428: }
429: }
430:
431: }
|