package com.perforce.config; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.perforce.common.ExitCode; import com.perforce.common.asset.ContentType; public class Configuration { private Logger logger = LoggerFactory.getLogger(Configuration.class); private Properties cfg = new Properties(); public Configuration() throws ConfigException { super(); setHidden(); setDefault(); } public Configuration(String filename) throws ConfigException { super(); setHidden(); load(filename); } /** * Loads a default set of configurations * * @throws Exception */ private void setDefault() throws ConfigException { String pwd = System.getProperty("user.dir") + File.separator; // General Configuration: Version ver = new Version(); set(CFG.TEST, false); set(CFG.VERSION, ver.getVersion()); set(CFG.SCHEMA, "5." + CFG.values().length); set(CFG.SCM_TYPE, ScmType.P4); // General audit and logging set(CFG.AUDIT_ENABLED, true); set(CFG.AUDIT_FILE, "audit.log"); set(CFG.CHANGE_MAP, "changeMap.txt"); // Perforce connection set(CFG.P4_MODE, "IMPORT"); set(CFG.P4_ROOT, pwd + "p4_root" + File.separator); set(CFG.P4_PORT, "localhost:4444"); set(CFG.P4_USER, "p4-user"); set(CFG.P4_PASSWD, ""); set(CFG.P4_CLIENT, "p4-client"); set(CFG.P4_CLIENT_ROOT, pwd + "ws" + File.separator); set(CFG.P4_UNICODE, false); set(CFG.P4_TRANSLATE, true); set(CFG.P4_CHARSET, "<none>"); set(CFG.P4_DEPOT_PATH, "import"); set(CFG.P4_DEPOT_SUB, File.separator); set(CFG.P4_JNL_PREFIX, "jnl."); set(CFG.P4_JNL_INDEX, 0); set(CFG.P4_LOG_ID, "<description>"); set(CFG.P4_OFFSET, 0L); set(CFG.P4_LINEEND, true); set(CFG.P4_SKIP_EMPTY, false); set(CFG.P4_DOWNGRADE, false); set(CFG.P4_START, 1L); set(CFG.P4_END, 0L); // Check system for case sensitivity if (Config.isCaseSensitive()) { set(CFG.P4_CASE, CaseSensitivity.NONE); set(CFG.P4_C1_MODE, false); } else { set(CFG.P4_CASE, CaseSensitivity.FIRST); set(CFG.P4_C1_MODE, true); } // Subversion specific modes set(CFG.SVN_DUMPFILE, "<unset>"); set(CFG.SVN_PROP_NAME, ".svn.properties"); set(CFG.SVN_PROP_ENCODE, "ini"); set(CFG.SVN_PROP_ENABLED, false); set(CFG.SVN_PROP_TYPE, ContentType.UNKNOWN); set(CFG.SVN_DIR_NAME, ".svn.empty"); set(CFG.SVN_DIR_ENABLED, false); set(CFG.SVN_KEEP_KEYWORD, true); set(CFG.SVN_MERGEINFO, false); set(CFG.SVN_LABELS, false); set(CFG.SVN_LABEL_DEPTH, 2); set(CFG.SVN_LABEL_FORMAT, "svn_label:{depth}"); // CVS specific modes set(CFG.CVS_ROOT, "<unset>"); set(CFG.CVS_MODULE, ""); set(CFG.CVS_WINDOW, 20 * 1000L); // milliseconds set(CFG.CVS_TMPDIR, "tmp"); set(CFG.CVS_LABELS, false); set(CFG.CVS_LABEL_FORMAT, "{symbol}"); set(CFG.CVS_TMPLOG_ENABLED, true); // UTF8 path normalisation String os = System.getProperty("os.name").toLowerCase(); if (os.contains("win")) set(CFG.P4_NORMALISATION, Normaliser.NFC); else if (os.contains("mac")) set(CFG.P4_NORMALISATION, Normaliser.NFD); else if (os.contains("nix")) set(CFG.P4_NORMALISATION, Normaliser.NFC); else if (os.contains("nux")) set(CFG.P4_NORMALISATION, Normaliser.NFC); else if (os.contains("sun")) set(CFG.P4_NORMALISATION, Normaliser.NFC); else set(CFG.P4_NORMALISATION, Normaliser.NFC); } public void setHidden() throws ConfigException { // Set hidden properties set(CFG.EXCLUDE_MAP, "exclude.map"); set(CFG.INCLUDE_MAP, "include.map"); set(CFG.ISSUE_MAP, "issue.map"); set(CFG.USER_MAP, "users.map"); set(CFG.TYPE_MAP, "types.map"); set(CFG.PATH_MAP, "path.map"); set(CFG.P4_LARGE_FILE, 10L * 1024L * 1024L); set(CFG.CVS_MAXLINE, 1024L * 1024L * 1024L); } private static <T> boolean check(CFG id, Class<T> type) { if (id.getType() == type) { return true; } return false; } public <T> void set(CFG id, T value) throws ConfigException { if (check(id, value.getClass())) { cfg.setProperty(id.toString(), String.valueOf(value)); } else { throw new ConfigException("unknown " + value.getClass().getName() + " option '" + id.toString() + "'"); } } public String getString(CFG id) { return cfg.getProperty(id.toString()); } public Object get(CFG id) throws ConfigException { String key = id.toString(); String value = cfg.getProperty(key); if (value == null) { StringBuffer sb = new StringBuffer(); sb.append("Cannot find '" + key + "' in the configuration file.\n"); sb.append("Please check or regnerate your configuration.\n"); throw new ConfigException(sb.toString()); } Class<?> type = id.getType(); try { if (type.isEnum()) { Method method = type.getMethod("parse", String.class); return method.invoke(null, value); } try { Constructor<?> cons = type.getConstructor(String.class); return cons.newInstance(value); } catch (NoSuchMethodException e) { Method method = type.getMethod("parse", String.class); return method.invoke(null, value); } } catch (Exception e) { throw new ConfigException("unknown type in option '" + type.toString() + "'", e); } } public String summary() { StringBuffer sb = new StringBuffer(); sb.append("Configuration settings:\n"); for (Map.Entry<Object, Object> c : cfg.entrySet()) { String key = (String) c.getKey(); String value = (String) c.getValue(); sb.append(" " + key + ": " + value + "\n"); } return sb.toString(); } private void load(String file) throws ConfigException { BufferedReader in = null; try { // Load User configuration options from file in = new BufferedReader(new FileReader(file)); cfg.load(in); in.close(); } catch (IOException e) { logger.error("Unable to load configuration file '" + file + "'\n", e); System.exit(ExitCode.USAGE.value()); } } public void store(String file, ScmType type) throws ConfigException { BufferedWriter out = null; try { out = new BufferedWriter(new FileWriter(file)); Date d = new Date(); out.write("# P4Convert configuration (" + d.toString() + ")\n\n"); out.write("# Please note that all paths should be absolute and must end with a path\n"); out.write("# delimiter e.g. '/' or '\\\\'. For example:\n"); out.write("# com.p4convert.p4.clientRoot=C:\\\\perforce\\\\client_ws\\\\ \n"); out.write("# com.p4convert.p4.clientRoot=/Users/perforce/client_ws/ \n"); switch (type) { case SVN: set(CFG.SCM_TYPE, ScmType.SVN); out.write("\n# Core converter settings\n"); storeGroup(out, "com.p4convert.core."); out.write("\n# Subversion import options\n"); storeGroup(out, "com.p4convert.svn."); break; case CVS: set(CFG.SCM_TYPE, ScmType.CVS); out.write("\n# Core converter settings\n"); storeGroup(out, "com.p4convert.core."); out.write("\n# CVS import options\n"); storeGroup(out, "com.p4convert.cvs."); break; default: out.close(); throw new ConfigException("SCM type not recognised."); } out.write("\n# Perforce Environment\n"); storeGroup(out, "com.p4convert.p4."); out.write("\n# Logging options\n"); storeGroup(out, "com.p4convert.log."); out.close(); } catch (IOException e) { throw new ConfigException("Cannot store configuration file", e); } } private void storeGroup(BufferedWriter out, String group) throws IOException { List<String> keys = new ArrayList<String>(cfg.stringPropertyNames()); Collections.sort(keys); for (String key : keys) { String value = (String) cfg.get(key); if (key.startsWith(group)) out.write(key + "=" + value + "\n"); } } public Collection<Entry<Object, Object>> getEntrySet() { return cfg.entrySet(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 13876 | Paul Allen | Rename/move file(s) | ||
//guest/paul_allen/p4convert-maven/src/com/perforce/config/Configuration.java | |||||
#1 | 13873 | Paul Allen | Branching using p4convert-maven | ||
//guest/perforce_software/p4convert/src/com/perforce/config/Configuration.java | |||||
#13 | 12890 | Paul Allen |
Extended start/stop change limits to CVS. - Moved start and stop configuration options out of SVN name space to the generic P4 space. - Abstracted shared range logic to ProcessChange #review-12880 |
||
#12 | 12521 | Paul Allen |
CVS: Audit logging when generating Tmp files from RCS. Creates a file "tmpFile.log" under the com.perforce.cvs.tmpDir and logging is enabled by default. com.perforce.cvs.audit.tmp=true |
||
#11 | 12319 | Paul Allen |
CVS: Format label names using com.p4convert.cvs.labelFormat. Default is: '{symbol}' the string is replaced by the CVS symbol used for that label. |
||
#10 | 12222 | Paul Allen |
New non-translate mode for high-ascii files. If the P4_TRANSLATE mode is disabled then text files with high-ascii characters are given the new type RAW-TEXT. Raw types are not translated and the content is used as-is. (exception UTF16/32) The default translation configuration is enabled: com.p4convert.p4.translate=true The com.p4convert.p4.translate=false mode is intended for use with non-unicode Perforce servers in a Windows only client environment. |
||
#9 | 11901 | Paul Allen |
SVN: Added command-line flags to support incremental conversions. Users can now use --start and --end flags to control the SVN revision range imported. The existing --repo flag if specified will override the configuration file. - Updates to Doc - Minor fix to default file header |
||
#8 | 11762 | Paul Allen |
Modified the --default flag to generate default entries for the client root and server root. - Moved Main.java out of SVN package. |
||
#7 | 11094 | Paul Allen |
Minor fixes. - Increase CVS_MAXLINE from 10MB to 1GB - More tolerant SVN header key : values |
||
#6 | 11070 | Paul Allen | Path Translation configuration options. | ||
#5 | 11017 | Paul Allen | SVN: Configuration options to support labels | ||
#4 | 11005 | Paul Allen |
Changed default user/client to 'p4-user' and 'p4-client'. Test cases still use the original 'svn-user' and 'svn-client' to avoid updating all the test results. |
||
#3 | 10719 | Paul Allen |
CVS: Activate old label code and add config option. disabled by default -- for the moment until it works (set log level from trace -> debug) |
||
#2 | 10654 | Paul Allen |
Added hidden configuration option for the max line buffer for reading a binary file. hidden.maxLineBuffer = (default) 10*1024*1024 |
||
#1 | 9807 | Paul Allen | Initial import of p4-convert (from change 894340) |