查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.util.internal;
17  
18  import java.security.AccessController;
19  import java.security.PrivilegedActionException;
20  import java.security.PrivilegedExceptionAction;
21  import java.util.Queue;
22  import java.util.concurrent.BlockingQueue;
23  import java.util.concurrent.atomic.AtomicInteger;
24  
25  /**
26   * Utility that detects various properties specific to the current runtime
27   * environment, such as Java version and the availability of the
28   * {@code sun.misc.Unsafe} object.
29   *
30   * <br>
31   * You can disable the use of {@code sun.misc.Unsafe} if you specify
32   * the System property <strong>org.jboss.netty.tryUnsafe</strong> with
33   * value of {@code false}. Default is {@code true}.
34   */
35  public final class DetectionUtil {
36  
37      private static final int JAVA_VERSION = javaVersion0();
38      private static final boolean HAS_UNSAFE = hasUnsafe(AtomicInteger.class.getClassLoader());
39      private static final boolean IS_WINDOWS;
40      static {
41          String os = SystemPropertyUtil.get("os.name", "").toLowerCase();
42          // windows
43          IS_WINDOWS =  os.contains("win");
44      }
45  
46      /**
47       * Return {@code true} if the JVM is running on Windows
48       *
49       */
50      public static boolean isWindows() {
51          return IS_WINDOWS;
52      }
53  
54      public static boolean hasUnsafe() {
55          return HAS_UNSAFE;
56      }
57  
58      public static int javaVersion() {
59          return JAVA_VERSION;
60      }
61  
62      private static boolean hasUnsafe(ClassLoader loader) {
63          boolean noUnsafe = SystemPropertyUtil.getBoolean("io.netty.noUnsafe", false);
64          if (noUnsafe) {
65              return false;
66          }
67  
68          // Legacy properties
69          boolean tryUnsafe;
70          if (SystemPropertyUtil.contains("io.netty.tryUnsafe")) {
71              tryUnsafe = SystemPropertyUtil.getBoolean("io.netty.tryUnsafe", true);
72          } else {
73              tryUnsafe = SystemPropertyUtil.getBoolean("org.jboss.netty.tryUnsafe", true);
74          }
75  
76          if (!tryUnsafe) {
77              return false;
78          }
79  
80          try {
81              Class<?> unsafeClazz = Class.forName("sun.misc.Unsafe", true, loader);
82              return hasUnsafeField(unsafeClazz);
83          } catch (Exception e) {
84              // Ignore
85          }
86  
87          return false;
88      }
89  
90      private static boolean hasUnsafeField(final Class<?> unsafeClass) throws PrivilegedActionException {
91          return AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
92              public Boolean run() throws Exception {
93                  unsafeClass.getDeclaredField("theUnsafe");
94                  return true;
95              }
96          });
97      }
98  
99      private static int javaVersion0() {
100         try {
101             // Check if its android, if so handle it the same way as java6.
102             //
103             // See https://github.com/netty/netty/issues/282
104             Class.forName("android.app.Application");
105             return 6;
106         } catch (ClassNotFoundException e) {
107             //Ignore
108         }
109 
110         try {
111             Class.forName(
112                     "java.util.concurrent.LinkedTransferQueue", false,
113                     BlockingQueue.class.getClassLoader());
114             return 7;
115         } catch (Exception e) {
116             // Ignore
117         }
118 
119         try {
120             Class.forName(
121                     "java.util.ArrayDeque", false, Queue.class.getClassLoader());
122             return 6;
123         } catch (Exception e) {
124             // Ignore
125         }
126 
127         return 5;
128     }
129 
130     private DetectionUtil() {
131         // only static method supported
132     }
133 }