001package org.unix4j.context; 002 003import java.io.File; 004import java.util.Locale; 005import java.util.Map; 006import java.util.Properties; 007 008import org.unix4j.convert.ConverterRegistry; 009import org.unix4j.convert.DefaultConverterRegistry; 010import org.unix4j.convert.ValueConverter; 011import org.unix4j.util.FileUtil; 012import org.unix4j.variable.DefaultVariableContext; 013import org.unix4j.variable.ExecutionContextVariableResolver; 014import org.unix4j.variable.MapVariableResolver; 015import org.unix4j.variable.VariableContext; 016 017/** 018 * Default execution context implementation with setters for some values and 019 * meaningful defaults for instance taken from system properties. 020 */ 021public class DefaultExecutionContext implements ExecutionContext { 022 023 /** 024 * Factory returning a new instance of {@link DefaultExecutionContext} using 025 * the default constructor. 026 */ 027 public static final ExecutionContextFactory FACTORY = new ExecutionContextFactory() { 028 @Override 029 public DefaultExecutionContext createExecutionContext() { 030 return new DefaultExecutionContext(); 031 } 032 }; 033 034 private String user; 035 private File userHome; 036 private File tempDirectory; 037 private File currentDirectory; 038 private Locale locale; 039 private VariableContext variableContext = null;//lazy init 040 private ConverterRegistry converterRegistry = null;//lazy init 041 042 public DefaultExecutionContext() { 043 this.currentDirectory = null;//default 044 } 045 public DefaultExecutionContext(File currentDirectory) { 046 this.currentDirectory = currentDirectory; 047 } 048 public void setCurrentDirectory(File currentDirectory) { 049 this.currentDirectory = currentDirectory; 050 } 051 public void setCurrentDirectory(String currentDirectory) { 052 setCurrentDirectory(new File(currentDirectory)); 053 } 054 @Override 055 public File getRelativeToCurrentDirectory(File file) { 056 return FileUtil.toAbsoluteFile(getCurrentDirectory(), file); 057 } 058 @Override 059 public File getCurrentDirectory() { 060 if (currentDirectory == null) { 061 currentDirectory = new File(System.getProperty("user.dir")); 062 } 063 return currentDirectory; 064 } 065 @Override 066 public String getUser() { 067 if (user == null) { 068 user = System.getProperty("user.name"); 069 } 070 return user; 071 } 072 @Override 073 public File getUserHome() { 074 if (userHome == null) { 075 userHome = new File(System.getProperty("user.home")); 076 } 077 return userHome; 078 } 079 @Override 080 public File getTempDirectory() { 081 if (tempDirectory == null) { 082 tempDirectory = new File(System.getProperty("java.io.tmpdir")); 083 } 084 return tempDirectory; 085 } 086 public void setLocale(Locale locale) { 087 this.locale = locale; 088 } 089 @Override 090 public Locale getLocale() { 091 if (locale == null) { 092 locale = Locale.getDefault(); 093 } 094 return locale; 095 } 096 @Override 097 public Map<String, String> getEnv() { 098 return System.getenv(); 099 } 100 @Override 101 public Properties getSys() { 102 return System.getProperties(); 103 } 104 @Override 105 public VariableContext getVariableContext() { 106 if (variableContext == null) { 107 variableContext = new DefaultVariableContext(); 108 variableContext.addVariableResolver(MapVariableResolver.getEnv()); 109 variableContext.addVariableResolver(MapVariableResolver.getSystemProperties()); 110 variableContext.addVariableResolver(new ExecutionContextVariableResolver(this)); 111 } 112 return variableContext; 113 } 114 @Override 115 public ConverterRegistry getConverterRegistry() { 116 if (converterRegistry == null) { 117 converterRegistry = new DefaultConverterRegistry(); 118 } 119 return converterRegistry; 120 } 121 @Override 122 public <V> ValueConverter<V> getValueConverterFor(Class<V> type) { 123 return getConverterRegistry().getValueConverterFor(type); 124 } 125}