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