All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.CommonUtils Maven / Gradle / Ivy

package com.aliyun;

import com.aliyun.bean.DefaultConfigBean;
import com.aliyun.enums.Constants;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.maven.plugin.logging.Log;

import java.io.Closeable;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

public class CommonUtils {

    public static Log logger;

    static {
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Edas", "edas.cn-hangzhou.aliyuncs.com");
            DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", "Edas", "edas.cn-beijing.aliyuncs.com");
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Edas", "edas.cn-shanghai.aliyuncs.com");
            DefaultProfile.addEndpoint("cn-shenzhen", "cn-shenzhen", "Edas", "edas.cn-shenzhen.aliyuncs.com");
            DefaultProfile.addEndpoint("cn-qingdao", "cn-qingdao", "Edas", "edas.cn-qingdao.aliyuncs.com");
            DefaultProfile.addEndpoint("ap-southeast-1", "ap-southeast-1", "Edas", "edas.ap-southeast-1.aliyuncs.com");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static DefaultAcsClient getACSClient(DefaultConfigBean config) throws ClientException {
        DefaultConfigBean.Env env = config.getEnv();
        DefaultProfile defaultProfile = DefaultProfile.getProfile(env.getRegionId(),
            env.getAccessKeyId(), env.getAccessKeySecret());
        DefaultAcsClient client = new DefaultAcsClient(defaultProfile);
        client.setAutoRetry(false);
        return client;
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj instanceof String) {
            return "".equals(String.valueOf(obj).trim());
        } else if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        } else if (obj instanceof Collection) {
            return ((Collection)obj).isEmpty();
        } else {
            return obj instanceof Map ? ((Map)obj).isEmpty() : false;
        }
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    public static void initLogger(Log logger) {
        CommonUtils.logger = logger;
    }

    public static String getCurrentTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    public static void close(Closeable c) {
        try {
            if (c != null) {
                c.close();
            }
        } catch (Exception ex) {
            //ignore
        }
    }

    public static String getPopApiRouteKey() {
        if (System.getProperty(Constants.POP_API_ROUTE_KEY) != null) {
            return System.getProperty(Constants.POP_API_ROUTE_KEY);
        }

        if (System.getenv(Constants.POP_API_ROUTE_KEY) != null) {
            return System.getenv(Constants.POP_API_ROUTE_KEY);
        }

        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy