001package org.unix4j.variable;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005
006import org.unix4j.context.ExecutionContext;
007
008/**
009 * Variable resolver for values defined by the {@link ExecutionContext}. The
010 * {@link Variable} constants define variable names for execution context
011 * values.
012 */
013public class ExecutionContextVariableResolver implements VariableResolver {
014        /**
015         * Variable name constants for {@link ExecutionContext} values.
016         */
017        public static enum Variable {
018                /**
019                 * Variable {@code $pwd} to access
020                 * {@link ExecutionContext#getCurrentDirectory()}.
021                 */
022                CurrentDirectory("$pwd") {
023                        @Override
024                        public Object getValue(ExecutionContext executionContext) {
025                                return executionContext.getCurrentDirectory();
026                        }
027                },
028                /**
029                 * Variable {@code $whoami} to access {@link ExecutionContext#getUser()}
030                 * .
031                 */
032                User("$whoami") {
033                        @Override
034                        public Object getValue(ExecutionContext executionContext) {
035                                return executionContext.getUser();
036                        }
037                },
038                /**
039                 * Variable {@code $home} to access
040                 * {@link ExecutionContext#getUserHome()}.
041                 */
042                UserHome("$home") {
043                        @Override
044                        public Object getValue(ExecutionContext executionContext) {
045                                return executionContext.getUserHome();
046                        }
047                },
048                /**
049                 * Variable {@code $temp} to access
050                 * {@link ExecutionContext#getTempDirectory()}.
051                 */
052                TempDirectory("$temp") {
053                        @Override
054                        public Object getValue(ExecutionContext executionContext) {
055                                return executionContext.getTempDirectory();
056                        }
057                },
058                /**
059                 * Variable {@code $locale} to access
060                 * {@link ExecutionContext#getLocale()}.
061                 */
062                Locale("$locale") {
063                        @Override
064                        public Object getValue(ExecutionContext executionContext) {
065                                return executionContext.getLocale();
066                        }
067                };
068                private final String unixName;
069
070                private Variable(String unixName) {
071                        this.unixName = unixName;
072                }
073
074                public String getUnixName() {
075                        return unixName;
076                }
077
078                public static Variable getByVariableName(String variableName) {
079                        for (final Variable v : values()) {
080                                if (v.getUnixName().equals(variableName)) {
081                                        return v;
082                                }
083                        }
084                        return null;
085                }
086
087                abstract public Object getValue(ExecutionContext executionContext);
088        }
089
090        private final ExecutionContext executionContext;
091
092        /**
093         * Constructor with execution context.
094         * 
095         * @param executionContext
096         *            the execution context with values resolved by this class
097         */
098        public ExecutionContextVariableResolver(ExecutionContext executionContext) {
099                this.executionContext = executionContext;
100        }
101
102        @Override
103        public Object getValue(String name) {
104                final Variable variable = Variable.getByVariableName(name);
105                if (variable != null) {
106                        return variable.getValue(executionContext);
107                }
108                return null;
109        }
110
111        @Override
112        public String toString() {
113                final Map<Object, Object> map = new LinkedHashMap<Object, Object>();
114                for (final Variable v : Variable.values()) {
115                        map.put(v, v.getValue(executionContext));
116                }
117                return map.toString();
118        }
119
120}