001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.execute.impl;
017
018import static java.lang.String.format;
019import static org.apache.commons.lang3.StringUtils.isBlank;
020import static org.kuali.common.util.base.Precondition.checkNotBlank;
021import static org.kuali.common.util.base.Precondition.checkNotNull;
022import static org.kuali.common.util.log.Loggers.newLogger;
023
024import java.nio.charset.Charset;
025import java.util.List;
026import java.util.Locale;
027
028import org.kuali.common.util.execute.Executable;
029import org.kuali.common.util.file.CanonicalFile;
030import org.slf4j.Logger;
031
032import com.google.common.collect.ImmutableList;
033
034public final class ShowEnvExec implements Executable {
035
036        private static final Logger logger = newLogger();
037        private static final Object[] EMPTY_OBJECT_ARRAY = {};
038
039        public ShowEnvExec() {
040                this(false);
041        }
042
043        public ShowEnvExec(boolean skip) {
044                this.skip = skip;
045        }
046
047        private final boolean skip;
048
049        @Override
050        public void execute() {
051                if (skip) {
052                        return;
053                }
054                List<Object> java = copyOf(System.getProperty("java.runtime.version"), System.getProperty("java.vm.name"), System.getProperty("java.vm.vendor"));
055                List<Object> javaHome = copyOf(new CanonicalFile(System.getProperty("java.home")));
056                List<Object> JAVA_HOME = getJavaHomeEnvironmentVariable();
057                List<Object> other = copyOf(Locale.getDefault().toString(), Charset.defaultCharset().displayName());
058                List<Object> os = copyOf(System.getProperty("os.name"), System.getProperty("os.version"), System.getProperty("os.arch"));
059                info("Java version: %s, name: %s, vendor: %s", java);
060                info("java.home: %s", javaHome);
061                info("JAVA_HOME: %s", JAVA_HOME);
062                info("Default locale: %s, platform encoding: %s", other);
063                info("OS name: %s, version: %s, arch: %s", os);
064        }
065
066        private static List<Object> getJavaHomeEnvironmentVariable() {
067                String javaHome = System.getenv("JAVA_HOME");
068                if (isBlank(javaHome)) {
069                        return copyOf("-- Not set --");
070                } else {
071                        return copyOf(new CanonicalFile(javaHome));
072                }
073        }
074
075        private static List<Object> copyOf(Object... args) {
076                return ImmutableList.copyOf(args);
077        }
078
079        private static void info(String msg, List<Object> args) {
080                logger.info(format(checkNotBlank(msg, "msg"), checkNotNull(args, "args").toArray(EMPTY_OBJECT_ARRAY)));
081        }
082
083        public boolean isSkip() {
084                return skip;
085        }
086
087}