annotate src/org/tmatesoft/hg/internal/BasicSessionContext.java @ 435:ab5a11a61732

Added tag v0.9.0 for changeset 7e1912b4ce99
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 30 Mar 2012 18:16:43 +0200
parents 12f668401613
children 909306e412e2
rev   line source
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
388
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
2 * Copyright (c) 2011-2012 TMate Software Ltd
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
388
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
19 import java.util.Collections;
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
20 import java.util.Map;
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
21
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import org.tmatesoft.hg.core.SessionContext;
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import org.tmatesoft.hg.util.LogFacility;
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 /**
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 *
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 * @author Artem Tikhomirov
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 * @author TMate Software Ltd.
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 */
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 public class BasicSessionContext implements SessionContext {
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31
407
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
32 private LogFacility logFacility;
388
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
33 private final Map<String, Object> properties;
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34
431
12f668401613 FIXMEs: awkward API refactored, what need to be internal got hidden; public aspects got captured in slim interfaces
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 407
diff changeset
35 public BasicSessionContext(LogFacility log) {
12f668401613 FIXMEs: awkward API refactored, what need to be internal got hidden; public aspects got captured in slim interfaces
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 407
diff changeset
36 this(null, log);
388
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
37 }
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
38
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
39 @SuppressWarnings("unchecked")
431
12f668401613 FIXMEs: awkward API refactored, what need to be internal got hidden; public aspects got captured in slim interfaces
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 407
diff changeset
40 public BasicSessionContext(Map<String,?> propertyOverrides, LogFacility log) {
407
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
41 logFacility = log;
388
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
42 properties = propertyOverrides == null ? Collections.<String,Object>emptyMap() : (Map<String, Object>) propertyOverrides;
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 }
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 public LogFacility getLog() {
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 // e.g. for exceptions that we can't handle but log (e.g. FileNotFoundException when we've checked beforehand file.canRead()
407
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
47 if (logFacility == null) {
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
48 boolean needDebug = _getBooleanProperty("hg.consolelog.debug", false);
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
49 boolean needInfo = needDebug || _getBooleanProperty("hg.consolelog.info", false);
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
50 logFacility = new StreamLogFacility(needDebug, needInfo, true, System.out);
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
51 }
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 return logFacility;
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
407
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
54
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
55 private boolean _getBooleanProperty(String name, boolean defaultValue) {
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
56 // can't use <T> and unchecked cast because got no confidence passed properties are strictly of the kind of my default values,
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
57 // i.e. if boolean from outside comes as "true", while I pass default as Boolean or vice versa.
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
58 Object p = getProperty(name, defaultValue);
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
59 return p instanceof Boolean ? ((Boolean) p).booleanValue() : Boolean.parseBoolean(String.valueOf(p));
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
60 }
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61
407
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
62 // TODO specific helpers for boolean and int values
338
3cfa4d908fc9 Add options to control DataAccessProvider, allow to turn off use of file memory mapping in particular to solve potential sharing violation (os file handle gets released on MappedByteByffer being GC'd, not on FileChannel.close())
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
63 public Object getProperty(String name, Object defaultValue) {
407
30922c728341 Better multiline log printout; options to tune default log output
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 388
diff changeset
64 // NOTE, this method is invoked from getLog(), hence do not call getLog from here unless changed appropriately
388
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
65 Object value = properties.get(name);
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
66 if (value != null) {
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
67 return value;
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
68 }
b015f3918120 Work on FIXME: correct HgDataFile#workingCopy with tests; BasicSessionContext with property override; platform-specific options to internals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 338
diff changeset
69 value = System.getProperty(name);
338
3cfa4d908fc9 Add options to control DataAccessProvider, allow to turn off use of file memory mapping in particular to solve potential sharing violation (os file handle gets released on MappedByteByffer being GC'd, not on FileChannel.close())
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
70 return value == null ? defaultValue : value;
3cfa4d908fc9 Add options to control DataAccessProvider, allow to turn off use of file memory mapping in particular to solve potential sharing violation (os file handle gets released on MappedByteByffer being GC'd, not on FileChannel.close())
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
71 }
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 }