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.ValueConverter;
010import org.unix4j.util.FileUtil;
011import org.unix4j.variable.ExecutionContextVariableResolver;
012import org.unix4j.variable.VariableContext;
013
014/**
015 * A derived execution context allows overriding of some values while forwarding
016 * other getter calls to an original delegate context.
017 */
018public class DerivedExecutionContext implements ExecutionContext {
019
020        private final ExecutionContext delegate;
021
022        private String user;
023        private File userHome;
024        private File tempDirectory;
025        private File currentDirectory;
026        private Locale locale;
027        private Map<String, String> env = null;
028        private Properties sys;
029        private VariableContext variableContext = null;
030        private ConverterRegistry converterRegistry = null;
031
032        /**
033         * Constructor for new derived execution context with a new instance of
034         * {@link DefaultExecutionContext} as delegate context.
035         */
036        public DerivedExecutionContext() {
037                this(new DefaultExecutionContext());
038        }
039
040        /**
041         * Constructor for new derived execution context with the specified delegate
042         * context.
043         * 
044         * @param delegate
045         *            the delegate context to which all getter calls are forwarded
046         *            by default
047         */
048        public DerivedExecutionContext(ExecutionContext delegate) {
049                this.delegate = delegate;
050                init();
051        }
052
053        /**
054         * Initialisation method called from the constructor. The default
055         * implementation adds a {@link ExecutionContextVariableResolver} for this
056         * derived context to the {@link VariableContext}.
057         * 
058         * @see #getVariableContext()
059         */
060        protected void init() {
061                getVariableContext().addVariableResolver(new ExecutionContextVariableResolver(this));
062        }
063
064        public void setCurrentDirectory(File currentDirectory) {
065                this.currentDirectory = currentDirectory;
066        }
067
068        public void setCurrentDirectory(String currentDirectory) {
069                setCurrentDirectory(currentDirectory == null ? null : new File(currentDirectory));
070        }
071
072        @Override
073        public File getCurrentDirectory() {
074                if (currentDirectory != null) {
075                        return currentDirectory;
076                }
077                return delegate.getCurrentDirectory();
078        }
079
080        public void setUser(String user) {
081                this.user = user;
082        }
083
084        @Override
085        public File getRelativeToCurrentDirectory(File file) {
086                return FileUtil.toAbsoluteFile(getCurrentDirectory(), file);
087        }
088
089        @Override
090        public String getUser() {
091                if (user != null) {
092                        return user;
093                }
094                return delegate.getUser();
095        }
096
097        public void setUserHome(File userHome) {
098                this.userHome = userHome;
099        }
100
101        public void setUserHome(String userHome) {
102                setUserHome(userHome == null ? null : new File(userHome));
103        }
104
105        @Override
106        public File getUserHome() {
107                if (userHome != null) {
108                        return userHome;
109                }
110                return delegate.getUserHome();
111        }
112
113        public void setTempDirectory(File tempDirectory) {
114                this.tempDirectory = tempDirectory;
115        }
116
117        public void setTempDirectory(String tempDirectory) {
118                setTempDirectory(tempDirectory == null ? null : new File(tempDirectory));
119        }
120
121        @Override
122        public File getTempDirectory() {
123                if (tempDirectory != null) {
124                        return tempDirectory;
125                }
126                return delegate.getTempDirectory();
127        }
128
129        public void setLocale(Locale locale) {
130                this.locale = locale;
131        }
132
133        @Override
134        public Locale getLocale() {
135                if (locale != null) {
136                        return locale;
137                }
138                return delegate.getLocale();
139        }
140
141        public void setEnv(Map<String, String> env) {
142                this.env = env;
143        }
144
145        @Override
146        public Map<String, String> getEnv() {
147                if (env != null) {
148                        return env;
149                }
150                return delegate.getEnv();
151        }
152
153        public void setSys(Properties sys) {
154                this.sys = sys;
155        }
156
157        @Override
158        public Properties getSys() {
159                if (sys != null) {
160                        return sys;
161                }
162                return delegate.getSys();
163        }
164
165        public void setVariableContext(VariableContext variableContext) {
166                this.variableContext = variableContext;
167        }
168
169        @Override
170        public VariableContext getVariableContext() {
171                if (variableContext != null) {
172                        return variableContext;
173                }
174                return delegate.getVariableContext();
175        }
176
177        public void setConverterRegistry(ConverterRegistry converterRegistry) {
178                this.converterRegistry = converterRegistry;
179        }
180
181        @Override
182        public ConverterRegistry getConverterRegistry() {
183                if (converterRegistry != null) {
184                        return converterRegistry;
185                }
186                return delegate.getConverterRegistry();
187        }
188
189        @Override
190        public <V> ValueConverter<V> getValueConverterFor(Class<V> type) {
191                return getConverterRegistry().getValueConverterFor(type);
192        }
193}