View Javadoc
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      // Empty
16    }
17  
18    /**
19     * System.exit(value)
20     *
21     * @param value
22     */
23    public static void systemExit(final int value) {
24      if (isJunit()) {
25        return;
26      }
27      stopLogger(false);
28      System.exit(value);//NOSONAR
29    }
30  
31    /**
32     * Stop logger
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     * @param classz
53     *
54     * @return a new Instance for this Class
55     *
56     * @throws InvocationTargetException
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     * @param className
69     *
70     * @return a new Instance for this Class
71     *
72     * @throws InvocationTargetException
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     * Replacement for Runtime.getRuntime().halt(value)
88     *
89     * @param value
90     */
91    public static void runtimeGetRuntimeHalt(final int value) {
92      if (!isJunit()) {
93        Runtime.getRuntime().halt(value);//NOSONAR
94      }
95    }
96  
97    /**
98     * JUnit usage only
99     *
100    * @return True if in JUnit role
101    */
102   public static boolean isJunit() {
103     return isJunit;
104   }
105 
106   /**
107    * JUnit usage only
108    *
109    * @param isJunit
110    */
111   public static void setJunit(final boolean isJunit) {
112     WaarpSystemUtil.isJunit = isJunit;
113   }
114 }