/* * [Config.java] * * Summary: Configuration variables for the BulkFTP. * * Copyright: (c) 2011-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 2011-12-05 initial version. */ package com.mindprod.bulkftp; import com.mindprod.common18.Build; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.fastcat.FastCat; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import static java.lang.System.*; /** * Configuration variables for the BulkFTP. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-12-05 initial version. * @since 2011-12-05 */ class Config { /** * Which individual directories in the base directory will be distributed. The directory but none of its * subdirectories will be distributed. The name may not begin or end with a \, though it may contain embedded \. */ @SuppressWarnings( { "NonConstantFieldWithUpperCaseName" } ) public static String[] DIRS_TO_DISTRIBUTE; /** * Which individual directories in the base directory will be withheld from distribution. The directory but none of * its subdirectories will be withheld. The name must not begin or end with a \, though it may contain embedded \. * You only need mention dirs that were previously included. */ @SuppressWarnings( { "NonConstantFieldWithUpperCaseName" } ) public static String[] DIRS_TO_WITHHOLD; /** * e.g. chk,class,cnt,log,zip,digest,nlx,tmp,ion * Which file extensions should be excluded. These are case INsensitive. Don't include the dot. */ @SuppressWarnings( { "NonConstantFieldWithUpperCaseName" } ) public static String[] EXTENSIONS_TO_WITHHOLD; /** * Which files in the base directory will be distributed. The filename must include the directory, but not the base. * The filename must not begin or end with a \, though it may contain embedded \. */ @SuppressWarnings( { "NonConstantFieldWithUpperCaseName" } ) public static String[] FILES_TO_DISTRIBUTE; /** * e.g. zips,replicator,renney,jgloss\snippet\vslick,projects\snippet\vslick,applets\snippet\vslick * Which individual files in the base directory will be withheld from distribution. The filename must include the * directory, but not the base. The filename must not begin or end with a \, though it may contain embedded \. You * only need mention files that were previously included. */ @SuppressWarnings( { "NonConstantFieldWithUpperCaseName" } ) public static String[] FILES_TO_WITHHOLD; /** * how many times to try connectiong.) */ public static int MAX_UPLOAD_TRIES = 3; /** * e.g. * Which entire directory trees in the base directory will be distributed. The directory and all its * subdirectories * will be distributed. The name must not begin or end with a \, though it may contain embedded \. */ @SuppressWarnings( { "NonConstantFieldWithUpperCaseName" } ) public static String[] TREES_TO_DISTRIBUTE; /** * Which entire directory trees in the base directory will be withheld from distribution. The directory and all its * subdirectories will be withheld, even if they appear in the list to be distributed. The name must not begin or * end with a \, though it may contain embedded \. You only need mention trees that were previously included. */ @SuppressWarnings( { "NonConstantFieldWithUpperCaseName" } ) public static String[] TREES_TO_WITHHOLD; /** * do we need to verify what files are already on the server */ public static boolean VERIFY = false; /** * how many char is the CLIENT_ROOT_DIR including trailing \. */ static int CLIENT_PREFIX_LENGTH; /** * root directory of files os server */ static File CLIENT_ROOT_DIR; /** * higheist value to use for secondary FTP data port */ static int FTP_PORT_HIGH = 53950; /** * lowest value to use for secondary FTP data port */ static int FTP_PORT_LOW = 53940; /** * host of the FTP server */ static String HOST = "mindprod.com"; // 65.110.21.43 /** * password to login */ static String PASSWORD = System.getenv( "FTP_PASSWORD" ); /** * true if want PASV, false if ACTIVE */ static boolean PASV = false; /** * where the properties are stored, e.g. E:\env\bulkftp\mindprod.properties */ static File PROPERTIES_FILE; /** * rnot directory of files os server, e.g. "/com/mindprod/www" */ static String SERVER_ROOT_DIR; /** * where to save the state of the files between sessions e.g. E:\env\bulkftp/mindprod.ser */ static File STATE_SAVE_FILE; /** * login account name, e.g. "roedy.mindprod.com" */ static String USER_NAME; /** * root directory of files os server */ private static String CLIENT_ROOT_DIR_STRING; static void analyseCommandLine( final String[] args ) { for ( String arg : args ) { if ( arg.equals( "-verify" ) ) { VERIFY = true; } else { if ( !arg.endsWith( ".properties" ) ) { throw new IllegalArgumentException( arg + " needs a .properties extension" ); } PROPERTIES_FILE = new File( arg ); final File parent = PROPERTIES_FILE.getParentFile(); String name = ST.chopTrailingString( PROPERTIES_FILE.getName(), ".properties" ) + ".ser"; STATE_SAVE_FILE = new File( parent, name ); } } } /** * Check that parameters specified are reasonable. */ static void ensureConfigValid() { if ( HOST.length() == 0 ) { throw new IllegalArgumentException( "HOST missing." ); } if ( USER_NAME.length() == 0 ) { throw new IllegalArgumentException( "USER_NAME missing." ); } if ( PASSWORD.length() == 0 ) { throw new IllegalArgumentException( "PASSWORD missing." ); } if ( CLIENT_ROOT_DIR_STRING.length() == 0 ) { throw new IllegalArgumentException( "CLIENT_ROOT_DIR missing." ); } CLIENT_ROOT_DIR = new File( CLIENT_ROOT_DIR_STRING ); CLIENT_PREFIX_LENGTH = new File( CLIENT_ROOT_DIR, "x" ).getAbsolutePath().length() - 1; if ( SERVER_ROOT_DIR.length() == 0 ) { throw new IllegalArgumentException( "SERVER_ROOT_DIR missing." ); } for ( String aTREES_TO_DISTRIBUTE : TREES_TO_DISTRIBUTE ) { if ( aTREES_TO_DISTRIBUTE.endsWith( File.separator ) ) { throw new IllegalArgumentException( "TREES_TO_DISTRIBUTE has trailing separator." ); } if ( aTREES_TO_DISTRIBUTE.startsWith( File.separator ) ) { throw new IllegalArgumentException( "TREES_TO_DISTRIBUTE has leading separator." ); } } for ( String aDIRS_TO_DISTRIBUTE : DIRS_TO_DISTRIBUTE ) { if ( aDIRS_TO_DISTRIBUTE.endsWith( File.separator ) ) { throw new IllegalArgumentException( "DIRS_TO_DISTRIBUTE has trailing separator." ); } if ( aDIRS_TO_DISTRIBUTE.startsWith( File.separator ) ) { throw new IllegalArgumentException( "DIRS_TO_DISTRIBUTE has leading separator." ); } } for ( String aFILES_TO_DISTRIBUTE : FILES_TO_DISTRIBUTE ) { if ( aFILES_TO_DISTRIBUTE.endsWith( File.separator ) ) { throw new IllegalArgumentException( "FILES_TO_DISTRIBUTE has trailing separator." ); } if ( aFILES_TO_DISTRIBUTE.startsWith( File.separator ) ) { throw new IllegalArgumentException( "FILES_TO_DISTRIBUTE has leading separator." ); } } for ( String aTREES_TO_WITHHOLD : TREES_TO_WITHHOLD ) { if ( aTREES_TO_WITHHOLD.endsWith( File.separator ) ) { throw new IllegalArgumentException( "TREES_TO_WITHHOLD has trailing separator." ); } if ( aTREES_TO_WITHHOLD.startsWith( File.separator ) ) { throw new IllegalArgumentException( "TREES_TO_WITHHOLD has leading separator." ); } } for ( String aDIRS_TO_WITHHOLD : DIRS_TO_WITHHOLD ) { if ( aDIRS_TO_WITHHOLD.endsWith( File.separator ) ) { throw new IllegalArgumentException( "DIRS_TO_WITHHOLD has trailing separator." ); } if ( aDIRS_TO_WITHHOLD.startsWith( File.separator ) ) { throw new IllegalArgumentException( "DIRS_TO_WITHHOLD has leading separator." ); } } for ( String aFILES_TO_WITHHOLD : FILES_TO_WITHHOLD ) { if ( aFILES_TO_WITHHOLD.endsWith( File.separator ) ) { throw new IllegalArgumentException( "FILES_TO_WITHHOLD has trailing separator." ); } if ( aFILES_TO_WITHHOLD.startsWith( File.separator ) ) { throw new IllegalArgumentException( "FILES_TO_WITHHOLD has leading separator." ); } } if ( !( 1 <= MAX_UPLOAD_TRIES && MAX_UPLOAD_TRIES <= 99 ) ) { throw new IllegalArgumentException( "MAX_UPLOAD_TRIES should be in range 1 .. 99." ); } for ( String aEXTENSIONS_TO_WITHHOLD : EXTENSIONS_TO_WITHHOLD ) { if ( aEXTENSIONS_TO_WITHHOLD.indexOf( '.' ) >= 0 ) { throw new IllegalArgumentException( "EXTENSIONS_TO_WITHHOLD contains a ." ); } } } /** * Display a parameter that has several values, separated by commas. * * @param name Name of the parameter * @param values array of Strings values of the parameter * * @return display string, without \n. */ static String show( String name, String[] values ) { final FastCat sb = new FastCat( ( values.length + 1 ) * 2 ); sb.append( name ); sb.append( ": " ); for ( int i = 0; i < values.length; i++ ) { if ( i != 0 ) { sb.append( "," ); } sb.append( values[ i ] ); } return sb.toString(); } /** * Display Sender Config information * * @return string representation of the entire Sender config. */ public static String dumpConfig() { final FastCat sb = new FastCat( 51 ); sb.append( "\nC O N F I G U R A T I O N\n" ); sb.append( "HOST: " ); sb.append( HOST ); sb.append( "\nUSER_NAME: " ); sb.append( USER_NAME ); sb.append( "\nPASSWORD: " ); sb.append( PASSWORD ); sb.append( "\nFTP_PORT_LOW: " ); sb.append( FTP_PORT_LOW ); sb.append( "\nFTP_PORT_HIGH: " ); sb.append( FTP_PORT_HIGH ); sb.append( "\nCLIENT_ROOT_DIR: " ); sb.append( EIO.getCanOrAbsPath( CLIENT_ROOT_DIR ) ); sb.append( "\nSERVER_ROOT_DIR: " ); sb.append( SERVER_ROOT_DIR ); sb.append( "\nPASV: " ); sb.append( PASV ); sb.append( "\n" ); sb.append( show( "TREES_TO_DISTRIBUTE", TREES_TO_DISTRIBUTE ) ); sb.append( "\n" ); sb.append( show( "TREES_TO_WITHHOLD", TREES_TO_WITHHOLD ) ); sb.append( "\n" ); sb.append( show( "DIRS_TO_DISTRIBUTE", DIRS_TO_DISTRIBUTE ) ); sb.append( "\n" ); sb.append( show( "DIRS_TO_WITHHOLD", DIRS_TO_WITHHOLD ) ); sb.append( "\n" ); sb.append( show( "FILES_TO_DISTRIBUTE", FILES_TO_DISTRIBUTE ) ); sb.append( "\n" ); sb.append( show( "FILES_TO_WITHHOLD", FILES_TO_WITHHOLD ) ); sb.append( "\n" ); sb.append( show( "EXTENSIONS_TO_WITHHOLD", EXTENSIONS_TO_WITHHOLD ) ); sb.append( "\nMAX_UPLOAD_TRIES: " ); sb.append( MAX_UPLOAD_TRIES ); sb.append( "\n\nC O M M A N D L I N E\n" ); if ( VERIFY ) { sb.append( "-verify\n" ); } sb.append( "PROPERTIES_FILE: " ); sb.append( EIO.getCanOrAbsPath( PROPERTIES_FILE ) ); sb.append( "\n\nP R O G R A M\nVERSION: " ); sb.append( BulkFTP.VERSION_STRING ); sb.append( "\nBUILD: " ); sb.append( Build.BUILD_NUMBER ); sb.append( "\nJAVA_VERSION: " ); sb.append( System.getProperty( "java.version", "unknown" ) ); sb.append( "\nSTATE_SAVE_FILE: " ); sb.append( EIO.getCanOrAbsPath( STATE_SAVE_FILE ) ); sb.append( "\n" ); return sb.toString(); } /** * Get values for properties from the xxxreplicator.properties file. */ public static void getConfigFromPropertiesFile() { /** * Key=value pairs, used to populate constants from a properties file. */ MultiProperties m = new MultiProperties( 40, .75f ); try { m.load( new FileInputStream( PROPERTIES_FILE ) ); } catch ( IOException e ) { err.println( "Unable to read properties file: " ); } HOST = m.get( "HOST", "" ); USER_NAME = m.get( "USER_NAME", "" ); PASSWORD = m.get( "PASSWORD", "" ); FTP_PORT_LOW = m.getInt( "FTP_PORT_LOW", 53940 ); FTP_PORT_HIGH = m.getInt( "FTP_PORT_HIGH", 53950 ); CLIENT_ROOT_DIR_STRING = m.get( "CLIENT_ROOT_DIR", "" ); SERVER_ROOT_DIR = m.get( "SERVER_ROOT_DIR", "" ); PASV = m.getBoolean( "PASV", false ); TREES_TO_DISTRIBUTE = m.getMultiple( "TREES_TO_DISTRIBUTE" ); DIRS_TO_DISTRIBUTE = m.getMultiple( "DIRS_TO_DISTRIBUTE" ); FILES_TO_DISTRIBUTE = m.getMultiple( "FILES_TO_DISTRIBUTE" ); TREES_TO_WITHHOLD = m.getMultiple( "TREES_TO_WITHHOLD" ); DIRS_TO_WITHHOLD = m.getMultiple( "DIRS_TO_WITHHOLD" ); FILES_TO_WITHHOLD = m.getMultiple( "FILES_TO_WITHHOLD" ); EXTENSIONS_TO_WITHHOLD = m.getMultiple( "EXTENSIONS_TO_WITHHOLD" ); MAX_UPLOAD_TRIES = m.getInt( "MAX_UPLOAD_TRIES", 3 ); } }