Java tutorial
/* * Copyright 2007 Pentaho Corporation. All rights reserved. * * This software was developed by Pentaho Corporation and is provided under the terms * of the Mozilla Public License, Version 1.1, or any later version. You may not use * this file except in compliance with the license. If you need a copy of the license, * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho * BI Platform. The Initial Developer is Pentaho Corporation. * * Software distributed under the Mozilla Public License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to * the license for the specific language governing your rights and limitations. * * Created * @author * */ package org.pentaho.supportutility.config.retriever; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.io.FileUtils; import org.pentaho.supportutility.config.constant.SupportUtilConstant; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class LogRetriever extends ConfigRetreiver { /* * (non-Javadoc) * * @see * org.pentaho.supportutility.config.retriever.ConfigRetreiver#readConfiguration * (java.util.Properties) */ @Override protected void readConfiguration(Properties props) { String dstDir = SupportUtilConstant.LOG_DIR; String logCount = props.getProperty(SupportUtilConstant.LOG_COUNT); String dstPath = props.getProperty(SupportUtilConstant.SUPP_INFO_DEST_PATH) + File.separator + props.getProperty(SupportUtilConstant.SUPP_INF_DIR); if (createDestDirectory(dstPath + File.separator + dstDir, null)) { if (getInstallType().equalsIgnoreCase(SupportUtilConstant.INSTALLER)) { File lPath = new File(logPath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; try { dBuilder = dbFactory.newDocumentBuilder(); Document documnet = dBuilder.parse(lPath); documnet.getDocumentElement().normalize(); NodeList paramList = documnet.getElementsByTagName(SupportUtilConstant.XML_PARAM); // parses log4j.xml to read log path for (int i = 0; i < paramList.getLength(); i++) { Node paramNode = paramList.item(i); if (paramNode.getNodeType() == Node.ELEMENT_NODE) { Element paramElement = (Element) paramNode; if (paramElement.getAttribute(SupportUtilConstant.XML_NAME) .equals(SupportUtilConstant.XML_FILE)) { File pentahoLogfile = new File( paramElement.getAttribute(SupportUtilConstant.XML_VALUE)); saveConfiguration(pentahoLogfile.getAbsolutePath(), dstPath + File.separator + dstDir, logCount); } } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else if (getInstallType().equalsIgnoreCase(SupportUtilConstant.ARCHIVE) || getInstallType().equalsIgnoreCase(SupportUtilConstant.MANUAL)) { saveConfiguration(logPath, dstPath + File.separator + dstDir, logCount); } } } /** * copies file from source to destination * * @param srcPath * @param dstPath * @param logCount */ protected void saveConfiguration(String srcPath, String dstPath, String logCount) { try { Integer noItem = Integer.parseInt(logCount); File dir = new File(srcPath.substring(0, srcPath.lastIndexOf(File.separator))); File[] files = dir.listFiles(); Arrays.sort(files, new Comparator<File>() { public int compare(File f1, File f2) { return new Long((f1).lastModified()).compareTo(new Long((f2).lastModified())); } }); for (int m = files.length - 1; m > files.length - noItem - 1; m--) { File pentahologfiles = new File(srcPath.substring(0, srcPath.lastIndexOf(File.separator)) + File.separator + files[m].getName()); File supportlogfiles = new File(dstPath + File.separator + files[m].getName()); FileUtils.copyFile(pentahologfiles, supportlogfiles); } } catch (IOException e) { e.printStackTrace(); } } }