001/**
002 * Copyright 2014-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.jute.system;
017
018import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH;
019import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_VERSION;
020import static com.google.common.base.StandardSystemProperty.JAVA_EXT_DIRS;
021import static com.google.common.base.StandardSystemProperty.JAVA_HOME;
022import static com.google.common.base.StandardSystemProperty.JAVA_IO_TMPDIR;
023import static com.google.common.base.StandardSystemProperty.JAVA_LIBRARY_PATH;
024import static org.kuali.common.jute.reflect.Reflection.checkNoNulls;
025import static org.kuali.common.jute.system.SystemFiles.fromSystemProperty;
026
027import java.io.File;
028import java.util.List;
029
030import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
031import com.google.common.collect.ImmutableList;
032
033@JsonDeserialize(builder = Java.Builder.class)
034public final class Java {
035
036    private final File home;
037    private final File tmpDir;
038    private final String classVersion;
039    private final ImmutableList<File> classPaths;
040    private final ImmutableList<File> libraryPaths;
041    private final ImmutableList<File> extensionDirs;
042    private final RuntimeEnvironment runtime;
043    private final VirtualMachine vm;
044
045    private Java(Builder builder) {
046        this.home = builder.home;
047        this.tmpDir = builder.tmpDir;
048        this.classVersion = builder.classVersion;
049        this.classPaths = ImmutableList.copyOf(builder.classPaths);
050        this.libraryPaths = ImmutableList.copyOf(builder.libraryPaths);
051        this.extensionDirs = ImmutableList.copyOf(builder.extensionDirs);
052        this.runtime = builder.runtime;
053        this.vm = builder.vm;
054    }
055
056    public static Java build() {
057        Builder builder = builder();
058        builder.withHome(new File(JAVA_HOME.value()));
059        builder.withTmpDir(new File(JAVA_IO_TMPDIR.value()));
060        builder.withClassVersion(JAVA_CLASS_VERSION.value());
061        builder.withClassPaths(fromSystemProperty(JAVA_CLASS_PATH));
062        builder.withExtensionDirs(fromSystemProperty(JAVA_EXT_DIRS));
063        builder.withLibraryPaths(fromSystemProperty(JAVA_LIBRARY_PATH));
064        builder.withRuntime(RuntimeEnvironment.build());
065        builder.withVm(VirtualMachine.build());
066        return builder.build();
067    }
068
069    public static Builder builder() {
070        return new Builder();
071    }
072
073    public static class Builder implements org.apache.commons.lang3.builder.Builder<Java> {
074
075        private File home;
076        private File tmpDir;
077        private String classVersion;
078        private List<File> classPaths;
079        private List<File> libraryPaths;
080        private List<File> extensionDirs;
081        private RuntimeEnvironment runtime;
082        private VirtualMachine vm;
083
084        public Builder withHome(File home) {
085            this.home = home;
086            return this;
087        }
088
089        public Builder withTmpDir(File tmpDir) {
090            this.tmpDir = tmpDir;
091            return this;
092        }
093
094        public Builder withClassVersion(String classVersion) {
095            this.classVersion = classVersion;
096            return this;
097        }
098
099        public Builder withClassPaths(List<File> classPaths) {
100            this.classPaths = classPaths;
101            return this;
102        }
103
104        public Builder withLibraryPaths(List<File> libraryPaths) {
105            this.libraryPaths = libraryPaths;
106            return this;
107        }
108
109        public Builder withExtensionDirs(List<File> extensionDirs) {
110            this.extensionDirs = extensionDirs;
111            return this;
112        }
113
114        public Builder withRuntime(RuntimeEnvironment runtime) {
115            this.runtime = runtime;
116            return this;
117        }
118
119        public Builder withVm(VirtualMachine vm) {
120            this.vm = vm;
121            return this;
122        }
123
124        @Override
125        public Java build() {
126            return checkNoNulls(new Java(this));
127        }
128    }
129
130    public File getHome() {
131        return home;
132    }
133
134    public File getTmpDir() {
135        return tmpDir;
136    }
137
138    public String getClassVersion() {
139        return classVersion;
140    }
141
142    public List<File> getClassPaths() {
143        return classPaths;
144    }
145
146    public List<File> getLibraryPaths() {
147        return libraryPaths;
148    }
149
150    public List<File> getExtensionDirs() {
151        return extensionDirs;
152    }
153
154    public RuntimeEnvironment getRuntime() {
155        return runtime;
156    }
157
158    public VirtualMachine getVm() {
159        return vm;
160    }
161
162}