/* * [Config.java] * * Summary: Configuration constants for BrokenLinks. Read config from a properties file specified on the command line. * * Copyright: (c) 2008-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2008-07-30 initial version, created by refactoring */ package com.mindprod.brokenlinks; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import java.util.concurrent.TimeUnit; import static java.lang.System.*; /** * Configuration constants for BrokenLinks. Read config from a properties file specified on the command line. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2008-07-30 initial version, created by refactoring * @since 2008-07-30 */ class Config { static final boolean DEBUGGING = false; /** * file to store broken links, and presumed good links for website display */ static final File brokenlinksHTMLFile = new File( "include.html" ); /** * file to store broken links, to CSV format */ static final File exportBrokenLinksToCSVFile = new File( "brokenlinks.csv" ); /** * where we store our history of link status */ static final File historyBinFile = new File( "history.bin" ); /** * Known broken, but we are leaving as is for now. */ static final File leaveCSVFile = new File( "leave.csv" ); /** * where we store CSV of permanent redirects */ static final File permanentRedirectsCSVFile = new File( "permanentredirects.csv" ); /** * where we import presumed good URLs, overrides. */ static final File presumedGoodCSVFile = new File( "presumedgood.csv" ); /** * text file to contain a summary report */ static final File summaryReportFile = new File( "report.txt" ); /** * where we store CSV of temporary redirects */ static final File temporaryRedirectsCSVFile = new File( "temporaryredirects.csv" ); /** * where we import latest status of links from XENU */ static final File xenuPageCSVFile = new File( "xenupage.csv" ); /** * where we store configuring properties */ private static final File propertiesFile = new File( "brokenlinks.properties" ); /** * time in milliseconds a link must remain broken since being seen good to be considered broken. * Site might be down temporarily. If you reduce it, and get too many broken links, you can crank it up again. * It has no effect on the database, just how it is interpreted. */ static long brokenForgivenessMillis; /** * local website URLs start like this in Xenu files, e.g. file:///E:/mindprod/ or file://localhost/E:/mindprod * or https://richarddawkins.net/ */ static String localWebsiteURL; /** * time in milliseconds a link must remain broken since being seen good to be considered suspect. * Site might be down temporarily. If you reduce it, and get too many suspects, you can crank it up again. * It has no effect on the database, just how it is interpreted. * Suspect links are links going bad. We retest them. * Making this smaller retests links sooner to make sure they are still good. */ static long suspectForgivenessMillis; /** * prefix to convert a trimmed from back to a file reference or Html if not local. * e.g. E:/mindprod or https://richarddawkins.net/ */ static String toFileWebsitePrefix; /** * calculate the string to prefix on stripped froms to turn them back into ordinary file references */ private static void calcToFileWebsitePrefix() { if ( localWebsiteURL.startsWith( "file://localhost/" ) ) { toFileWebsitePrefix = ST.chopLeadingString( localWebsiteURL, "file://localhost/" ); } else if ( localWebsiteURL.startsWith( "file:///" ) ) { toFileWebsitePrefix = ST.chopLeadingString( localWebsiteURL, "file:///" ); } else if ( localWebsiteURL.startsWith( "http://" ) ) { toFileWebsitePrefix = localWebsiteURL; } else { err.println( "Warning: localWebsiteURL: " + localWebsiteURL + " does not have a standard pattern. Output" + " files " + "may look strange." ); toFileWebsitePrefix = localWebsiteURL; } } /** * Echo the configuration info to console. */ static void echoConfiguration() { BrokenLinks.spacedTitle( "CONFIGURATION" ); out.println( "brokenForgivenessDays = " + TimeUnit.MILLISECONDS.toDays( Config.brokenForgivenessMillis ) ); out.println( "suspectForgivenessDays = " + TimeUnit.MILLISECONDS.toDays( Config.suspectForgivenessMillis ) ); out.println( "localWebsiteURL = " + localWebsiteURL ); out.println(); BrokenLinks.spacedTitle( "FILES" ); out.println( "exported: brokenlinksHTMLFile = " + EIO.getCanOrAbsPath( brokenlinksHTMLFile ) ); out.println( "store: historyFile = " + EIO.getCanOrAbsPath( historyBinFile ) ); out.println( "config: leaveCSVFile = " + EIO.getCanOrAbsPath( leaveCSVFile ) ); out.println( "config: presumedGoodCSVFile = " + EIO.getCanOrAbsPath( presumedGoodCSVFile ) ); out.println( "exported: permanentRedirectsCSVFile = " + EIO.getCanOrAbsPath( permanentRedirectsCSVFile ) ); out.println( "config: propertiesFile = " + EIO.getCanOrAbsPath( propertiesFile ) ); out.println( "exported: summaryReportFile = " + EIO.getCanOrAbsPath( summaryReportFile ) ); out.println( "exported: temporaryRedirectsCSVFile = " + EIO.getCanOrAbsPath( temporaryRedirectsCSVFile ) ); out.println( "imported: xenuPageCSVFile = " + EIO.getCanOrAbsPath( xenuPageCSVFile ) ); } /** * Read the configuration properties file. * * @throws IOException if problems finding or reading the properties file. */ static void getConfiguration() throws IOException { BrokenLinks.spacedTitle( "reading " + Config.propertiesFile + " property file..." ); try { Properties props = new Properties(); FileInputStream fis = new FileInputStream( propertiesFile ); props.load( fis ); fis.close(); // now loaded, fetch from in-RAM collection. brokenForgivenessMillis = TimeUnit.DAYS.toMillis( Long.parseLong( props.getProperty( "brokenForgivenessDays", "6" ) ) ); suspectForgivenessMillis = TimeUnit.DAYS.toMillis( Long.parseLong( props.getProperty( "suspectForgivenessDays", "3" ) ) ); localWebsiteURL = props.getProperty( "localWebsiteURL", "file:///E:/mindprod/" ); // not // file://localhost/E:/mindprod/ unless that is what Xenu used calcToFileWebsitePrefix(); } catch ( IOException e ) { throw new IOException( "Fatal error: Problem reading properties file " + propertiesFile + " : " + e .getMessage() ); } } }