1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.jute.system;
17
18 import static com.google.common.base.StandardSystemProperty.FILE_SEPARATOR;
19 import static com.google.common.base.StandardSystemProperty.LINE_SEPARATOR;
20 import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
21 import static org.kuali.common.jute.reflect.Reflection.checkNoNulls;
22
23 import java.nio.charset.Charset;
24 import java.util.Locale;
25 import java.util.Properties;
26 import java.util.TimeZone;
27
28 import javax.inject.Inject;
29 import javax.inject.Provider;
30
31 import org.kuali.common.jute.collect.ImmutableProperties;
32 import org.kuali.common.jute.system.annotation.EnvironmentVariables;
33 import org.kuali.common.jute.system.annotation.FileSeparator;
34 import org.kuali.common.jute.system.annotation.LineSeparator;
35 import org.kuali.common.jute.system.annotation.PathSeparator;
36 import org.kuali.common.jute.system.annotation.SystemCharset;
37 import org.kuali.common.jute.system.annotation.SystemLocale;
38 import org.kuali.common.jute.system.annotation.SystemProperties;
39 import org.kuali.common.jute.system.annotation.SystemTimezone;
40
41 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
42
43
44
45
46
47
48 @JsonDeserialize(builder = VirtualSystem.Builder.class)
49 public final class VirtualSystem {
50
51 private final User user;
52 private final OperatingSystem os;
53 private final Java java;
54 private final String lineSeparator;
55 private final String pathSeparator;
56 private final String fileSeparator;
57 private final ImmutableProperties properties;
58 private final ImmutableProperties environment;
59 private final Locale locale;
60 private final Charset charset;
61
62 private final String timezone;
63
64
65
66
67 public static VirtualSystem build() {
68 Builder builder = builder();
69 builder.withUser(User.build());
70 builder.withOs(OperatingSystem.build());
71 builder.withJava(Java.build());
72 builder.withFileSeparator(FILE_SEPARATOR.value());
73 builder.withLineSeparator(LINE_SEPARATOR.value());
74 builder.withPathSeparator(PATH_SEPARATOR.value());
75 builder.withProperties(ImmutableProperties.copyOf(System.getProperties()));
76 builder.withEnvironment(ImmutableProperties.copyOf(System.getenv()));
77 builder.withLocale(Locale.getDefault());
78 builder.withCharset(Charset.defaultCharset());
79 builder.withTimezone(TimeZone.getDefault().getID());
80 return builder.build();
81 }
82
83 public static Builder builder() {
84 return new Builder();
85 }
86
87 private VirtualSystem(Builder builder) {
88 this.user = builder.user;
89 this.os = builder.os;
90 this.java = builder.java;
91 this.lineSeparator = builder.lineSeparator;
92 this.pathSeparator = builder.pathSeparator;
93 this.fileSeparator = builder.fileSeparator;
94 this.properties = ImmutableProperties.copyOf(builder.properties);
95 this.environment = ImmutableProperties.copyOf(builder.environment);
96 this.locale = builder.locale;
97 this.charset = builder.charset;
98 this.timezone = builder.timezone;
99 }
100
101 public static class Builder implements org.apache.commons.lang3.builder.Builder<VirtualSystem>, Provider<VirtualSystem> {
102
103 private User user;
104 private OperatingSystem os;
105 private Java java;
106 private String lineSeparator;
107 private String pathSeparator;
108 private String fileSeparator;
109 private Properties properties;
110 private Properties environment;
111 private Locale locale;
112 private Charset charset;
113 private String timezone;
114
115 @Inject
116 public Builder withCharset(@SystemCharset Charset charset) {
117 this.charset = charset;
118 return this;
119 }
120
121 @Inject
122 public Builder withTimezone(@SystemTimezone String timezone) {
123 this.timezone = timezone;
124 return this;
125 }
126
127 @Inject
128 public Builder withLocale(@SystemLocale Locale locale) {
129 this.locale = locale;
130 return this;
131 }
132
133 @Inject
134 public Builder withUser(User user) {
135 this.user = user;
136 return this;
137 }
138
139 @Inject
140 public Builder withOs(OperatingSystem os) {
141 this.os = os;
142 return this;
143 }
144
145 @Inject
146 public Builder withJava(Java java) {
147 this.java = java;
148 return this;
149 }
150
151 @Inject
152 public Builder withLineSeparator(@LineSeparator String lineSeparator) {
153 this.lineSeparator = lineSeparator;
154 return this;
155 }
156
157 @Inject
158 public Builder withPathSeparator(@PathSeparator String pathSeparator) {
159 this.pathSeparator = pathSeparator;
160 return this;
161 }
162
163 @Inject
164 public Builder withFileSeparator(@FileSeparator String fileSeparator) {
165 this.fileSeparator = fileSeparator;
166 return this;
167 }
168
169 @Inject
170 public Builder withProperties(@SystemProperties Properties properties) {
171 this.properties = properties;
172 return this;
173 }
174
175 @Inject
176 public Builder withEnvironment(@EnvironmentVariables Properties environment) {
177 this.environment = environment;
178 return this;
179 }
180
181 @Override
182 public VirtualSystem get() {
183 return build();
184 }
185
186 @Override
187 public VirtualSystem build() {
188 return checkNoNulls(new VirtualSystem(this));
189 }
190
191 }
192
193 public User getUser() {
194 return user;
195 }
196
197 public OperatingSystem getOs() {
198 return os;
199 }
200
201 public Java getJava() {
202 return java;
203 }
204
205 public String getLineSeparator() {
206 return lineSeparator;
207 }
208
209 public String getPathSeparator() {
210 return pathSeparator;
211 }
212
213 public String getFileSeparator() {
214 return fileSeparator;
215 }
216
217 public ImmutableProperties getProperties() {
218 return properties;
219 }
220
221 public ImmutableProperties getEnvironment() {
222 return environment;
223 }
224
225 public Locale getLocale() {
226 return locale;
227 }
228
229 public Charset getCharset() {
230 return charset;
231 }
232
233 public String getTimezone() {
234 return timezone;
235 }
236
237 }