net.ymate.platform.commons.util.RuntimeUtils.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.commons.util.RuntimeUtils.java

Source

/*
 * Copyright 2007-2107 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.commons.util;

import java.lang.reflect.InvocationTargetException;
import java.net.URL;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;

/**
 * <p>
 * RuntimeUtils
 * </p>
 * <p>
 * ????
 * </p>
 * 
 * @author (suninformation@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">?</th><th width="100px"></th><th
 *          width="100px"></th><th width="100px"></th>
 *          </tr>
 *          <!--  Table ?? -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td></td>
 *          <td></td>
 *          <td>2010-8-2?10:10:16</td>
 *          </tr>
 *          </table>
 */
public class RuntimeUtils {

    /**
     * ???Unix
     * 
     * @return
     */
    public static boolean isUnixOrLinux() {
        return SystemUtils.IS_OS_UNIX;
    }

    /**
     * ???Windows
     * 
     * @return
     */
    public static boolean isWindows() {
        return SystemUtils.IS_OS_WINDOWS;
    }

    /**
     * @return ?WEB.../WEB-INF/
     */
    public static String getRootPath() {
        return getRootPath(true);
    }

    /**
     * @param safe WEB??WEB-INF
     * @return 
     */
    public static String getRootPath(boolean safe) {
        URL _rootURL = RuntimeUtils.class.getClassLoader().getResource("/");
        boolean _isWeb = false;
        if (_rootURL == null) {
            _rootURL = RuntimeUtils.class.getClassLoader().getResource("");
        } else {
            _isWeb = true;
        }
        String _rootPath = _isWeb ? StringUtils.substringBefore(_rootURL.getPath(), safe ? "classes/" : "WEB-INF/")
                : _rootURL.getPath();
        if (isWindows()) {
            if (_rootPath.startsWith("/")) {
                _rootPath = _rootPath.substring(1);
            }
        }
        return _rootPath;
    }

    /**
     * ????
     * 
     * @param format ?
     * @param args ?
     * @return ?
     */
    public static RuntimeException makeRuntimeThrow(String format, Object... args) {
        return new RuntimeException(String.format(format, args));
    }

    /**
     * ????
     * 
     * @param e 
     * @param fmt ?
     * @param args ?
     * @return ?
     */
    public static RuntimeException wrapRuntimeThrow(Throwable e, String fmt, Object... args) {
        return new RuntimeException(String.format(fmt, args), e);
    }

    /**
     * ??
     * <p>
     *  e  InvocationTargetException TargetException 
     * </p>
     * 
     * @param e 
     * @return ?
     */
    public static RuntimeException wrapRuntimeThrow(Throwable e) {
        if (e instanceof RuntimeException) {
            return (RuntimeException) e;
        }
        if (e instanceof InvocationTargetException) {
            return wrapRuntimeThrow(((InvocationTargetException) e).getTargetException());
        }
        return new RuntimeException(e);
    }

    public static Throwable unwrapThrow(Throwable e) {
        if (e == null) {
            return null;
        }
        if (e instanceof InvocationTargetException) {
            InvocationTargetException itE = (InvocationTargetException) e;
            if (itE.getTargetException() != null) {
                return unwrapThrow(itE.getTargetException());
            }
        }
        if (e.getCause() != null) {
            return unwrapThrow(e.getCause());
        }
        return e;
    }

    /**
     * 
     *
     * @return ??
     */
    public static final long gc() {
        Runtime rt = Runtime.getRuntime();
        long lastUsed = rt.totalMemory() - rt.freeMemory();
        rt.gc();
        return lastUsed - rt.totalMemory() + rt.freeMemory();
    }

}