View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.javadoc;
20  
21  import java.io.File;
22  
23  /**
24   * Contains several OS-specific methods from Commons-Lang3's SystemUtils. We don't want to use that class because it
25   * uses enums for Java versions, which implies that with every new Java version a new commons-lang3 is required.
26   *
27   * @author Robert Scholte
28   * @since 3.0.1
29   */
30  class SystemUtils {
31      /**
32       * <p>
33       * The {@code os.name} System Property. Operating system name.
34       * </p>
35       * <p>
36       * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
37       * not exist.
38       * </p>
39       * <p>
40       * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
41       * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
42       * sync with that System property.
43       * </p>
44       *
45       * @since Java 1.1
46       */
47      public static final String OS_NAME = getSystemProperty("os.name");
48  
49      /**
50       * The prefix String for all Windows OS.
51       */
52      private static final String OS_NAME_WINDOWS_PREFIX = "Windows";
53  
54      /**
55       * <p>
56       * Is {@code true} if this is AIX.
57       * </p>
58       * <p>
59       * The field will return {@code false} if {@code OS_NAME} is {@code null}.
60       * </p>
61       */
62      public static final boolean IS_OS_AIX = getOSMatchesName("AIX");
63  
64      /**
65       * <p>
66       * Is {@code true} if this is Mac.
67       * </p>
68       * <p>
69       * The field will return {@code false} if {@code OS_NAME} is {@code null}.
70       * </p>
71       */
72      public static final boolean IS_OS_MAC_OSX = getOSMatchesName("Mac OS X");
73  
74      /**
75       * <p>
76       * Is {@code true} if this is Windows.
77       * </p>
78       * <p>
79       * The field will return {@code false} if {@code OS_NAME} is {@code null}.
80       * </p>
81       */
82      public static final boolean IS_OS_WINDOWS = getOSMatchesName(OS_NAME_WINDOWS_PREFIX);
83  
84      /**
85       * The System property key for the Java home directory.
86       */
87      private static final String JAVA_HOME_KEY = "java.home";
88  
89      /**
90       * <p>
91       * The {@code line.separator} System Property. Line separator (<code>&quot;\n&quot;</code> on UNIX).
92       * </p>
93       * <p>
94       * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
95       * not exist.
96       * </p>
97       * <p>
98       * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
99       * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
100      * sync with that System property.
101      * </p>
102      *
103      * @since Java 1.1
104      */
105     public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
106 
107     /**
108      * Decides if the operating system matches.
109      *
110      * @param osNamePrefix the prefix for the os name
111      * @return true if matches, or false if not or can't determine
112      */
113     private static boolean getOSMatchesName(final String osNamePrefix) {
114         return isOSNameMatch(OS_NAME, osNamePrefix);
115     }
116 
117     /**
118      * Decides if the operating system matches.
119      * <p>
120      * This method is package private instead of private to support unit test invocation.
121      * </p>
122      *
123      * @param osName the actual OS name
124      * @param osNamePrefix the prefix for the expected OS name
125      * @return true if matches, or false if not or can't determine
126      */
127     static boolean isOSNameMatch(final String osName, final String osNamePrefix) {
128         if (osName == null) {
129             return false;
130         }
131         return osName.startsWith(osNamePrefix);
132     }
133 
134     /**
135      * <p>
136      * Gets the Java home directory as a {@code File}.
137      * </p>
138      *
139      * @return a directory
140      * @throws SecurityException if a security manager exists and its {@code checkPropertyAccess} method doesn't allow
141      *             access to the specified system property.
142      * @see System#getProperty(String)
143      * @since 2.1
144      */
145     public static File getJavaHome() {
146         return new File(System.getProperty(JAVA_HOME_KEY));
147     }
148 
149     /**
150      * <p>
151      * Gets a System property, defaulting to {@code null} if the property cannot be read.
152      * </p>
153      * <p>
154      * If a {@code SecurityException} is caught, the return value is {@code null} and a message is written to
155      * {@code System.err}.
156      * </p>
157      *
158      * @param property the system property name
159      * @return the system property value or {@code null} if a security problem occurs
160      */
161     private static String getSystemProperty(final String property) {
162         try {
163             return System.getProperty(property);
164         } catch (final SecurityException ex) {
165             // we are not allowed to look at this property
166             System.err.println("Caught a SecurityException reading the system property '" + property
167                     + "'; the SystemUtils property value will default to null.");
168             return null;
169         }
170     }
171 }