comparison src/org/tmatesoft/hg/internal/BasicSessionContext.java @ 456:909306e412e2

Refactor LogFacility and SessionContext, better API for both
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 18 Jun 2012 16:54:00 +0200
parents 12f668401613
children dde18bc7053b
comparison
equal deleted inserted replaced
454:36fd1fd06492 456:909306e412e2
19 import java.util.Collections; 19 import java.util.Collections;
20 import java.util.Map; 20 import java.util.Map;
21 21
22 import org.tmatesoft.hg.core.SessionContext; 22 import org.tmatesoft.hg.core.SessionContext;
23 import org.tmatesoft.hg.util.LogFacility; 23 import org.tmatesoft.hg.util.LogFacility;
24 import org.tmatesoft.hg.util.LogFacility.Severity;
24 25
25 /** 26 /**
26 * 27 *
27 * @author Artem Tikhomirov 28 * @author Artem Tikhomirov
28 * @author TMate Software Ltd. 29 * @author TMate Software Ltd.
29 */ 30 */
30 public class BasicSessionContext implements SessionContext { 31 public class BasicSessionContext extends SessionContext {
31 32
32 private LogFacility logFacility; 33 private LogFacility logFacility;
33 private final Map<String, Object> properties; 34 private final Map<String, Object> properties;
34 35
35 public BasicSessionContext(LogFacility log) { 36 public BasicSessionContext(LogFacility log) {
40 public BasicSessionContext(Map<String,?> propertyOverrides, LogFacility log) { 41 public BasicSessionContext(Map<String,?> propertyOverrides, LogFacility log) {
41 logFacility = log; 42 logFacility = log;
42 properties = propertyOverrides == null ? Collections.<String,Object>emptyMap() : (Map<String, Object>) propertyOverrides; 43 properties = propertyOverrides == null ? Collections.<String,Object>emptyMap() : (Map<String, Object>) propertyOverrides;
43 } 44 }
44 45
46 @Override
45 public LogFacility getLog() { 47 public LogFacility getLog() {
46 // e.g. for exceptions that we can't handle but log (e.g. FileNotFoundException when we've checked beforehand file.canRead() 48 // e.g. for exceptions that we can't handle but log (e.g. FileNotFoundException when we've checked beforehand file.canRead()
47 if (logFacility == null) { 49 if (logFacility == null) {
48 boolean needDebug = _getBooleanProperty("hg.consolelog.debug", false); 50 PropertyMarshal pm = new PropertyMarshal(this);
49 boolean needInfo = needDebug || _getBooleanProperty("hg.consolelog.info", false); 51 boolean needDebug = pm.getBoolean("hg4j.consolelog.debug", false);
50 logFacility = new StreamLogFacility(needDebug, needInfo, true, System.out); 52 boolean needInfo = pm.getBoolean("hg4j.consolelog.info", false);
53 boolean needTime = pm.getBoolean("hg4j.consolelog.tstamp", true);
54 Severity l = needDebug ? Severity.Debug : (needInfo ? Severity.Info : Severity.Warn);
55 logFacility = new StreamLogFacility(l, needTime, System.out);
51 } 56 }
52 return logFacility; 57 return logFacility;
53 } 58 }
54 59
55 private boolean _getBooleanProperty(String name, boolean defaultValue) { 60 // specific helpers for boolean and int values are available from PropertyMarshal
56 // can't use <T> and unchecked cast because got no confidence passed properties are strictly of the kind of my default values, 61 @Override
57 // i.e. if boolean from outside comes as "true", while I pass default as Boolean or vice versa. 62 public Object getConfigurationProperty(String name, Object defaultValue) {
58 Object p = getProperty(name, defaultValue);
59 return p instanceof Boolean ? ((Boolean) p).booleanValue() : Boolean.parseBoolean(String.valueOf(p));
60 }
61
62 // TODO specific helpers for boolean and int values
63 public Object getProperty(String name, Object defaultValue) {
64 // NOTE, this method is invoked from getLog(), hence do not call getLog from here unless changed appropriately 63 // NOTE, this method is invoked from getLog(), hence do not call getLog from here unless changed appropriately
65 Object value = properties.get(name); 64 Object value = properties.get(name);
66 if (value != null) { 65 if (value != null) {
67 return value; 66 return value;
68 } 67 }