View Javadoc
1   /*
2    * @(#)JVM.java
3    *
4    * $Date: 2014-12-13 23:26:13 -0600 (Sat, 13 Dec 2014) $
5    *
6    * Copyright (c) 2011 by Jeremy Wood.
7    * All rights reserved.
8    *
9    * The copyright of this software is owned by Jeremy Wood. 
10   * You may not use, copy or modify this software, except in  
11   * accordance with the license agreement you entered into with  
12   * Jeremy Wood. For details see accompanying license terms.
13   * 
14   * This software is probably, but not necessarily, discussed here:
15   * https://javagraphics.java.net/
16   * 
17   * That site should also contain the most recent official version
18   * of this software.  (See the SVN repository for more details.)
19   */
20  package com.bric.multislider;
21  
22  import java.security.AccessControlException;
23  
24  /**
25   * Static methods relating to the JVM environment.
26   * <P>
27   * Instead of burying a constant like "isQuartz" in its most relevant class (such as OptimizedGraphics2D), it should be stored
28   * here so if other classes need to access it they don't necessary have to
29   */
30  public class Jvm
31  {
32  
33      /**
34       * Prints basic information about this session's JVM: the OS name &amp; version, the Java version, and (on Mac) whether
35       * Quartz is being used.
36       */
37      public static void printProfile()
38      {
39          System.out.println(getProfile());
40      }
41  
42      /**
43       * Gets basic information about this session's JVM: the OS name &amp; version, the Java version, and (on Mac) whether Quartz
44       * is being used.
45       * @return profile
46       */
47      public static String getProfile()
48      {
49          StringBuffer sb = new StringBuffer();
50          sb.append("OS = " + System.getProperty("os.name") + " (" + System.getProperty("os.version") + "), "
51                  + System.getProperty("os.arch") + "\n");
52          sb.append("Java Version = " + System.getProperty("java.version") + "\n");
53          return sb.toString();
54      }
55  
56      /**
57       * The major Java version being used (1.4, 1.5, 1.6, etc.), or -1 if this value couldn't be correctly determined.
58       */
59      public static final float javaVersion = Jvm.getMajorJavaVersion(true);
60  
61      private static final String osName = getOSName();
62  
63      private static String getOSName()
64      {
65          try
66          {
67              String s = (System.getProperty("os.name").toLowerCase());
68              return s;
69          }
70          catch (AccessControlException e)
71          {
72              e.printStackTrace();
73              return "unknown";
74          }
75      }
76  
77      /** Whether this session is on a Mac. */
78      public static final boolean isMac = (osName.indexOf("mac") != -1);
79  
80      /** Whether this session is on a Linux machine. */
81      public static final boolean isLinux = (osName.indexOf("linux") != -1);
82  
83      /** Whether this session is on Windows. */
84      public static final boolean isWindows = (osName.indexOf("windows") != -1);
85  
86      /** Whether this session is on Vista. */
87      public static final boolean isVista = (osName.indexOf("vista") != -1);
88  
89      /** Whether this session is on Windows 7. */
90      public static final boolean isWindows7 = isWindows && (osName.indexOf("7") != -1);
91  
92      /** Whether this session is on Windows XP. */
93      public static final boolean isWindowsXP = isWindows && (osName.indexOf("xp") != -1);
94  
95      /** Whether this session is on Windows Vista or Windows 7. */
96      public static final boolean isVistaOrWindows7 = isVista || isWindows7;
97  
98      /**
99       * If on a Mac: whether Quartz is the rendering pipeline. In applets this may throw a security exception; if this cannot be
100      * ascertained we assume it is false.
101      */
102     public static final boolean usingQuartz = isUsingQuartz();
103 
104     private static boolean isUsingQuartz()
105     {
106         try
107         {
108             return isMac
109                     && ((javaVersion > 0 && javaVersion < 1.4f) || (System.getProperty("apple.awt.graphics.UseQuartz") != null
110                             && System.getProperty("apple.awt.graphics.UseQuartz").toString().equals("true")));
111         }
112         catch (AccessControlException e)
113         {
114             e.printStackTrace();
115             return false;
116         }
117     }
118 
119     /**
120      * This converts the system property "java.version" to a float value. This drops rightmost digits until a legitimate float
121      * can be parsed. <BR>
122      * For example, this converts "1.6.0_05" to "1.6". <BR>
123      * This value is cached as the system property "java.major.version". Although technically this value is a String, it will
124      * always be parseable as a float.
125      * @return major java version such as 1.6
126      * @throws AccessControlException this may be thrown in unsigned applets! Beware!
127      */
128     public static float getMajorJavaVersion() throws AccessControlException
129     {
130         String majorVersion = null;
131         try
132         {
133             System.getProperty("java.major.version");
134         }
135         catch (java.security.AccessControlException e)
136         {
137             return -1;
138         }
139         if (majorVersion == null)
140         {
141             String s = System.getProperty("java.version");
142             float f = -1;
143             int i = s.length();
144             while (f < 0 && i > 0)
145             {
146                 try
147                 {
148                     f = Float.parseFloat(s.substring(0, i));
149                 }
150                 catch (Exception e)
151                 {
152                 }
153                 i--;
154             }
155             majorVersion = Float.toString(f);
156             System.setProperty("java.major.version", majorVersion);
157         }
158         return Float.parseFloat(majorVersion);
159     }
160 
161     /**
162      * @param catchSecurityException if true and an exception occurs, then -1 is returned.
163      * @return the major java version, or -1 if this can't be determined/
164      */
165     public static float getMajorJavaVersion(boolean catchSecurityException)
166     {
167         try
168         {
169             return getMajorJavaVersion();
170         }
171         catch (RuntimeException t)
172         {
173             if (catchSecurityException)
174             {
175                 System.err.println(
176                         "this exception was ignored without incident, but it means we can't determine the major java version:");
177                 t.printStackTrace();
178                 return -1;
179             }
180             throw t;
181         }
182     }
183 }