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

io.github.theprez.dotenv_ibmi.IBMiDotEnv Maven / Gradle / Ivy

The newest version!
package io.github.theprez.dotenv_ibmi;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;

import com.github.theprez.jcmdutils.StringUtils;
import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400SecurityException;

import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

public class IBMiDotEnv {
    private static Dotenv s_dotEnv = null;
    private static boolean s_isIBMi = checkIsIBMi();
    private volatile static AS400 s_conn = null;

    private static boolean checkIsIBMi() {
        final String osName = System.getProperty("os.name", "Unknown");
        return "os400".equalsIgnoreCase(osName) || "os/400".equalsIgnoreCase(osName);
    }

    public static synchronized Dotenv getDotEnv() {
        if (null != s_dotEnv) {
            return s_dotEnv;
        }
        final String cwd = System.getProperty("user.dir", ".");
        final Dotenv dotenv = Dotenv.configure().directory(cwd).ignoreIfMalformed().ignoreIfMissing().load();
        return s_dotEnv = dotenv;
    }

    /**
     * Load environment variables from the given configuration file
     */
    public static synchronized Dotenv loadDotEnv(File configFile) throws DotenvException {
        final String parent = configFile.getParent();
        final String baseName = configFile.getName();
        if (StringUtils.isNonEmpty(parent) && StringUtils.isNonEmpty(baseName)) {
            final Dotenv dotenv = Dotenv.configure().directory(parent).filename(baseName).ignoreIfMalformed().ignoreIfMissing().load();
            return s_dotEnv = dotenv;
        }
        else
            return s_dotEnv = null;
    }

    public static synchronized AS400 getCachedSystemConnection(final boolean _starCurrentIfPossible) throws IOException, AS400SecurityException {
        if (null != s_conn) {
            return s_conn;
        }
        return s_conn = getNewSystemConnection(_starCurrentIfPossible);
    }

    public static AS400 getNewSystemConnection(final boolean _starCurrentIfPossible)
            throws IOException, AS400SecurityException {
        final Dotenv dotenv = getDotEnv();
        final boolean isStarCurrentDefault = isIBMi() && _starCurrentIfPossible;
        final String hostname = dotenv.get("IBMI_HOSTNAME", isIBMi() ? "localhost" : null);
        final String username = dotenv.get("IBMI_USERNAME", isStarCurrentDefault ? "*CURRENT" : null);
        final String pw = dotenv.get("IBMI_PASSWORD", isStarCurrentDefault ? "*CURRENT" : null);

        final AS400 ret = new AS400(hostname);
        try {
            ret.setUserId(username);
            if (StringUtils.isNonEmpty(pw)) {
                ret.setPassword(pw.toCharArray());
            }

            if (isIBMi()) {
                ret.setGuiAvailable(false);
            }
        } catch (final PropertyVetoException e) {
            throw new IOException(e);
        }
        return ret;

    }

    public static boolean isIBMi() {
        return s_isIBMi;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy