1   package org.waarp.common.utility;
2   
3   import org.waarp.common.logging.WaarpLogLevel;
4   import org.waarp.common.logging.WaarpLoggerFactory;
5   import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
6   
7   import java.lang.reflect.InvocationTargetException;
8   
9   public class WaarpSystemUtil {
10  
11    private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
12    static boolean isJunit;
13  
14    private WaarpSystemUtil() {
15      
16    }
17  
18    
19  
20  
21  
22  
23    public static void systemExit(final int value) {
24      if (isJunit()) {
25        return;
26      }
27      stopLogger(false);
28      System.exit(value);
29    }
30  
31    
32  
33  
34    public static void stopLogger(final boolean force) {
35      if (!force && isJunit()) {
36        return;
37      }
38      if (WaarpLoggerFactory.getLogLevel() == WaarpLogLevel.NONE) {
39        return;
40      }
41      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
42          new WaarpSlf4JLoggerFactory(WaarpLogLevel.NONE));
43      WaarpLoggerFactory.getLogger("io.netty").setLevel(WaarpLogLevel.NONE);
44      WaarpLoggerFactory.getLogger("io.netty.channel.AbstractChannel")
45                        .setLevel(WaarpLogLevel.NONE);
46      WaarpLoggerFactory.getLogger(
47                            "io.netty.util.concurrent.AbstractEventExecutor")
48                        .setLevel(WaarpLogLevel.NONE);
49    }
50  
51    
52  
53  
54  
55  
56  
57  
58    public static Object newInstance(final Class classz)
59        throws InvocationTargetException {
60      try {
61        return classz.getDeclaredConstructor(EMPTY_CLASS_ARRAY).newInstance();
62      } catch (final Exception e) {
63        throw new InvocationTargetException(e);
64      }
65    }
66  
67    
68  
69  
70  
71  
72  
73  
74    public static Object newInstance(final String className)
75        throws InvocationTargetException {
76      try {
77        return newInstance(Class.forName(className));
78      } catch (final Exception e) {
79        if (e instanceof InvocationTargetException) {
80          throw (InvocationTargetException) e;
81        }
82        throw new InvocationTargetException(e);
83      }
84    }
85  
86    
87  
88  
89  
90  
91    public static void runtimeGetRuntimeHalt(final int value) {
92      if (!isJunit()) {
93        Runtime.getRuntime().halt(value);
94      }
95    }
96  
97    
98  
99  
100 
101 
102   public static boolean isJunit() {
103     return isJunit;
104   }
105 
106   
107 
108 
109 
110 
111   public static void setJunit(final boolean isJunit) {
112     WaarpSystemUtil.isJunit = isJunit;
113   }
114 }