1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package org.apache.commons.lang3;
|
---|
18 |
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * <p>
|
---|
22 | * Helpers for {@code java.lang.System}.
|
---|
23 | * </p>
|
---|
24 | * <p>
|
---|
25 | * If a system property cannot be read due to security restrictions, the corresponding field in this class will be set
|
---|
26 | * to {@code null} and a message will be written to {@code System.err}.
|
---|
27 | * </p>
|
---|
28 | * <p>
|
---|
29 | * #ThreadSafe#
|
---|
30 | * </p>
|
---|
31 | *
|
---|
32 | * @since 1.0
|
---|
33 | * @version $Id: SystemUtils.java 1199816 2011-11-09 16:11:34Z bayard $
|
---|
34 | */
|
---|
35 | public class SystemUtils {
|
---|
36 |
|
---|
37 | // System property constants
|
---|
38 | // -----------------------------------------------------------------------
|
---|
39 | // These MUST be declared first. Other constants depend on this.
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * <p>
|
---|
43 | * The {@code line.separator} System Property. Line separator (<code>"\n"</code> on UNIX).
|
---|
44 | * </p>
|
---|
45 | * <p>
|
---|
46 | * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
|
---|
47 | * not exist.
|
---|
48 | * </p>
|
---|
49 | * <p>
|
---|
50 | * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
|
---|
51 | * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
|
---|
52 | * sync with that System property.
|
---|
53 | * </p>
|
---|
54 | *
|
---|
55 | * @since Java 1.1
|
---|
56 | */
|
---|
57 | public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
|
---|
58 |
|
---|
59 |
|
---|
60 | // -----------------------------------------------------------------------
|
---|
61 | /**
|
---|
62 | * <p>
|
---|
63 | * Gets a System property, defaulting to {@code null} if the property cannot be read.
|
---|
64 | * </p>
|
---|
65 | * <p>
|
---|
66 | * If a {@code SecurityException} is caught, the return value is {@code null} and a message is written to
|
---|
67 | * {@code System.err}.
|
---|
68 | * </p>
|
---|
69 | *
|
---|
70 | * @param property the system property name
|
---|
71 | * @return the system property value or {@code null} if a security problem occurs
|
---|
72 | */
|
---|
73 | private static String getSystemProperty(String property) {
|
---|
74 | try {
|
---|
75 | return System.getProperty(property);
|
---|
76 | } catch (SecurityException ex) {
|
---|
77 | // we are not allowed to look at this property
|
---|
78 | System.err.println("Caught a SecurityException reading the system property '" + property
|
---|
79 | + "'; the SystemUtils property value will default to null.");
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | // -----------------------------------------------------------------------
|
---|
86 | /**
|
---|
87 | * <p>
|
---|
88 | * SystemUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
|
---|
89 | * {@code SystemUtils.FILE_SEPARATOR}.
|
---|
90 | * </p>
|
---|
91 | * <p>
|
---|
92 | * This constructor is public to permit tools that require a JavaBean instance to operate.
|
---|
93 | * </p>
|
---|
94 | */
|
---|
95 | public SystemUtils() {
|
---|
96 | super();
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|