Example usage for org.apache.lucene.util Constants OS_ARCH

List of usage examples for org.apache.lucene.util Constants OS_ARCH

Introduction

In this page you can find the example usage for org.apache.lucene.util Constants OS_ARCH.

Prototype

String OS_ARCH

To view the source code for org.apache.lucene.util Constants OS_ARCH.

Click Source Link

Usage

From source file:com.clearclouds.driver.monitor.os.OsProbe.java

License:Apache License

public OsInfo osInfo() {
    OsInfo info = new OsInfo();
    info.availableProcessors = Runtime.getRuntime().availableProcessors();
    info.name = Constants.OS_NAME;//from w  w w  .ja va 2  s .c om
    info.arch = Constants.OS_ARCH;
    info.version = Constants.OS_VERSION;
    return info;
}

From source file:io.crate.expression.reference.sys.node.NodeStatsContext.java

License:Apache License

public NodeStatsContext(boolean complete) {
    this.complete = complete;
    if (complete) {
        osName = BytesRefs.toBytesRef(Constants.OS_NAME);
        osArch = BytesRefs.toBytesRef(Constants.OS_ARCH);
        osVersion = BytesRefs.toBytesRef(Constants.OS_VERSION);
        javaVersion = BytesRefs.toBytesRef(Constants.JAVA_VERSION);
        jvmName = BytesRefs.toBytesRef(Constants.JVM_NAME);
        jvmVendor = BytesRefs.toBytesRef(Constants.JVM_VENDOR);
        jvmVersion = BytesRefs.toBytesRef(Constants.JVM_VERSION);
    }/*ww w  .ja  v a2s .  co m*/
}

From source file:io.crate.integrationtests.NodeStatsTest.java

License:Apache License

@Test
public void testSysNodsOsInfo() throws Exception {
    SQLResponse response = execute("select os_info from sys.nodes limit 1");
    Map results = (Map) response.rows()[0][0];
    assertThat(response.rowCount(), is(1L));

    assertThat((Integer) results.get("available_processors"), greaterThan(0));
    assertEquals(Constants.OS_NAME, results.get("name"));
    assertEquals(Constants.OS_ARCH, results.get("arch"));
    assertEquals(Constants.OS_VERSION, results.get("version"));

    Map<String, Object> jvmObj = new HashMap<>(4);
    jvmObj.put("version", Constants.JAVA_VERSION);
    jvmObj.put("vm_name", Constants.JVM_NAME);
    jvmObj.put("vm_vendor", Constants.JVM_VENDOR);
    jvmObj.put("vm_version", Constants.JVM_VERSION);
    assertEquals(jvmObj, results.get("jvm"));
}

From source file:io.crate.node.CrateNode.java

License:Apache License

@Override
protected void startUpLogging(Logger logger, Settings tmpSettings, boolean hadPredefinedNodeName) {
    final JvmInfo jvmInfo = JvmInfo.jvmInfo();
    logger.info("CrateDB version[{}], pid[{}], build[{}/{}], OS[{}/{}/{}], JVM[{}/{}/{}/{}]", Version.CURRENT,
            jvmInfo.pid(), Build.CURRENT.hashShort(), Build.CURRENT.timestamp(), Constants.OS_NAME,
            Constants.OS_VERSION, Constants.OS_ARCH, Constants.JVM_VENDOR, Constants.JVM_NAME,
            Constants.JAVA_VERSION, Constants.JVM_VERSION);

    if (logger.isDebugEnabled()) {
        Environment environment = getEnvironment();
        logger.debug("using config [{}], data [{}], logs [{}], plugins [{}]", environment.configFile(),
                Arrays.toString(environment.dataFiles()), environment.logsFile(), environment.pluginsFile());
    }/* w w w.  j  ava  2 s. com*/
}

From source file:org.elasticsearch.bootstrap.Seccomp.java

License:Apache License

/** try to install our BPF filters via seccomp() or prctl() to block execution */
private static int linuxImpl() {
    // first be defensive: we can give nice errors this way, at the very least.
    // also, some of these security features get backported to old versions, checking kernel version here is a big no-no! 
    final Arch arch = ARCHITECTURES.get(Constants.OS_ARCH);
    boolean supported = Constants.LINUX && arch != null;
    if (supported == false) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: '" + Constants.OS_ARCH + "' architecture unsupported");
    }/*from   w  ww  .  java 2 s.c  o  m*/

    // we couldn't link methods, could be some really ancient kernel (e.g. < 2.1.57) or some bug
    if (linux_libc == null) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: could not link methods. requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in");
    }

    // pure paranoia:

    // check that unimplemented syscalls actually return ENOSYS
    // you never know (e.g. https://code.google.com/p/chromium/issues/detail?id=439795)
    if (linux_syscall(999) >= 0 || Native.getLastError() != ENOSYS) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: your kernel is buggy and you should upgrade");
    }

    // try to check system calls really are who they claim
    // you never know (e.g. https://chromium.googlesource.com/chromium/src.git/+/master/sandbox/linux/seccomp-bpf/sandbox_bpf.cc#57)
    final int bogusArg = 0xf7a46a5c;

    // test seccomp(BOGUS)
    long ret = linux_syscall(arch.seccomp, bogusArg);
    if (ret != -1) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: seccomp(BOGUS_OPERATION) returned " + ret);
    } else {
        int errno = Native.getLastError();
        switch (errno) {
        case ENOSYS:
            break; // ok
        case EINVAL:
            break; // ok
        default:
            throw new UnsupportedOperationException("seccomp(BOGUS_OPERATION): " + JNACLibrary.strerror(errno));
        }
    }

    // test seccomp(VALID, BOGUS)
    ret = linux_syscall(arch.seccomp, SECCOMP_SET_MODE_FILTER, bogusArg);
    if (ret != -1) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: seccomp(SECCOMP_SET_MODE_FILTER, BOGUS_FLAG) returned " + ret);
    } else {
        int errno = Native.getLastError();
        switch (errno) {
        case ENOSYS:
            break; // ok
        case EINVAL:
            break; // ok
        default:
            throw new UnsupportedOperationException(
                    "seccomp(SECCOMP_SET_MODE_FILTER, BOGUS_FLAG): " + JNACLibrary.strerror(errno));
        }
    }

    // test prctl(BOGUS)
    ret = linux_prctl(bogusArg, 0, 0, 0, 0);
    if (ret != -1) {
        throw new UnsupportedOperationException("seccomp unavailable: prctl(BOGUS_OPTION) returned " + ret);
    } else {
        int errno = Native.getLastError();
        switch (errno) {
        case ENOSYS:
            break; // ok
        case EINVAL:
            break; // ok
        default:
            throw new UnsupportedOperationException("prctl(BOGUS_OPTION): " + JNACLibrary.strerror(errno));
        }
    }

    // now just normal defensive checks

    // check for GET_NO_NEW_PRIVS
    switch (linux_prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0)) {
    case 0:
        break; // not yet set
    case 1:
        break; // already set by caller
    default:
        int errno = Native.getLastError();
        if (errno == EINVAL) {
            throw new UnsupportedOperationException(
                    "seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in");
        } else {
            throw new UnsupportedOperationException(
                    "prctl(PR_GET_NO_NEW_PRIVS): " + JNACLibrary.strerror(errno));
        }
    }
    // check for SECCOMP
    switch (linux_prctl(PR_GET_SECCOMP, 0, 0, 0, 0)) {
    case 0:
        break; // not yet set
    case 2:
        break; // already in filter mode by caller
    default:
        int errno = Native.getLastError();
        if (errno == EINVAL) {
            throw new UnsupportedOperationException(
                    "seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed");
        } else {
            throw new UnsupportedOperationException("prctl(PR_GET_SECCOMP): " + JNACLibrary.strerror(errno));
        }
    }
    // check for SECCOMP_MODE_FILTER
    if (linux_prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0, 0, 0) != 0) {
        int errno = Native.getLastError();
        switch (errno) {
        case EFAULT:
            break; // available
        case EINVAL:
            throw new UnsupportedOperationException(
                    "seccomp unavailable: CONFIG_SECCOMP_FILTER not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed");
        default:
            throw new UnsupportedOperationException("prctl(PR_SET_SECCOMP): " + JNACLibrary.strerror(errno));
        }
    }

    // ok, now set PR_SET_NO_NEW_PRIVS, needed to be able to set a seccomp filter as ordinary user
    if (linux_prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
        throw new UnsupportedOperationException(
                "prctl(PR_SET_NO_NEW_PRIVS): " + JNACLibrary.strerror(Native.getLastError()));
    }

    // check it worked
    if (linux_prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) != 1) {
        throw new UnsupportedOperationException(
                "seccomp filter did not really succeed: prctl(PR_GET_NO_NEW_PRIVS): "
                        + JNACLibrary.strerror(Native.getLastError()));
    }

    // BPF installed to check arch, limit, then syscall. See https://www.kernel.org/doc/Documentation/prctl/seccomp_filter.txt for details.
    SockFilter insns[] = { /* 1  */ BPF_STMT(BPF_LD + BPF_W + BPF_ABS, SECCOMP_DATA_ARCH_OFFSET), //
            /* 2  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.audit, 0, 7), // if (arch != audit) goto fail;
            /* 3  */ BPF_STMT(BPF_LD + BPF_W + BPF_ABS, SECCOMP_DATA_NR_OFFSET), //
            /* 4  */ BPF_JUMP(BPF_JMP + BPF_JGT + BPF_K, arch.limit, 5, 0), // if (syscall > LIMIT) goto fail;
            /* 5  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.fork, 4, 0), // if (syscall == FORK) goto fail;
            /* 6  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.vfork, 3, 0), // if (syscall == VFORK) goto fail;
            /* 7  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.execve, 2, 0), // if (syscall == EXECVE) goto fail;
            /* 8  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.execveat, 1, 0), // if (syscall == EXECVEAT) goto fail;
            /* 9  */ BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), // pass: return OK;
            /* 10 */ BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (EACCES & SECCOMP_RET_DATA)), // fail: return EACCES;
    };
    // seccomp takes a long, so we pass it one explicitly to keep the JNA simple
    SockFProg prog = new SockFProg(insns);
    prog.write();
    long pointer = Pointer.nativeValue(prog.getPointer());

    int method = 1;
    // install filter, if this works, after this there is no going back!
    // first try it with seccomp(SECCOMP_SET_MODE_FILTER), falling back to prctl()
    if (linux_syscall(arch.seccomp, SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
            new NativeLong(pointer)) != 0) {
        method = 0;
        int errno1 = Native.getLastError();
        if (logger.isDebugEnabled()) {
            logger.debug("seccomp(SECCOMP_SET_MODE_FILTER): " + JNACLibrary.strerror(errno1)
                    + ", falling back to prctl(PR_SET_SECCOMP)...");
        }
        if (linux_prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, pointer, 0, 0) != 0) {
            int errno2 = Native.getLastError();
            throw new UnsupportedOperationException(
                    "seccomp(SECCOMP_SET_MODE_FILTER): " + JNACLibrary.strerror(errno1)
                            + ", prctl(PR_SET_SECCOMP): " + JNACLibrary.strerror(errno2));
        }
    }

    // now check that the filter was really installed, we should be in filter mode.
    if (linux_prctl(PR_GET_SECCOMP, 0, 0, 0, 0) != 2) {
        throw new UnsupportedOperationException(
                "seccomp filter installation did not really succeed. seccomp(PR_GET_SECCOMP): "
                        + JNACLibrary.strerror(Native.getLastError()));
    }

    logger.debug("Linux seccomp filter installation successful, threads: [{}]", method == 1 ? "all" : "app");
    return method;
}

From source file:org.elasticsearch.bootstrap.SpawnerTests.java

License:Apache License

public void testMakePlatformName() {
    String platformName = Spawner.makePlatformName(Constants.OS_NAME, Constants.OS_ARCH);

    assertFalse(platformName, platformName.isEmpty());
    assertTrue(platformName, platformName.equals(platformName.toLowerCase(Locale.ROOT)));
    assertTrue(platformName, platformName.indexOf("-") > 0);
    assertTrue(platformName, platformName.indexOf("-") < platformName.length() - 1);
    assertFalse(platformName, platformName.contains(" "));
}

From source file:org.elasticsearch.bootstrap.SystemCallFilter.java

License:Apache License

/** try to install our BPF filters via seccomp() or prctl() to block execution */
private static int linuxImpl() {
    // first be defensive: we can give nice errors this way, at the very least.
    // also, some of these security features get backported to old versions, checking kernel version here is a big no-no!
    final Arch arch = ARCHITECTURES.get(Constants.OS_ARCH);
    boolean supported = Constants.LINUX && arch != null;
    if (supported == false) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: '" + Constants.OS_ARCH + "' architecture unsupported");
    }//from   ww  w  . ja v  a  2  s . c  o m

    // we couldn't link methods, could be some really ancient kernel (e.g. < 2.1.57) or some bug
    if (linux_libc == null) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: could not link methods. requires kernel 3.5+ "
                        + "with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in");
    }

    // pure paranoia:

    // check that unimplemented syscalls actually return ENOSYS
    // you never know (e.g. https://code.google.com/p/chromium/issues/detail?id=439795)
    if (linux_syscall(999) >= 0) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: your kernel is buggy and you should upgrade");
    }

    switch (Native.getLastError()) {
    case ENOSYS:
        break; // ok
    case EPERM:
        // NOT ok, but likely a docker container
        if (logger.isDebugEnabled()) {
            logger.debug("syscall(BOGUS) bogusly gets EPERM instead of ENOSYS");
        }
        break;
    default:
        throw new UnsupportedOperationException(
                "seccomp unavailable: your kernel is buggy and you should upgrade");
    }

    // try to check system calls really are who they claim
    // you never know (e.g. https://chromium.googlesource.com/chromium/src.git/+/master/sandbox/linux/seccomp-bpf/sandbox_bpf.cc#57)
    final int bogusArg = 0xf7a46a5c;

    // test seccomp(BOGUS)
    long ret = linux_syscall(arch.seccomp, bogusArg);
    if (ret != -1) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: seccomp(BOGUS_OPERATION) returned " + ret);
    } else {
        int errno = Native.getLastError();
        switch (errno) {
        case ENOSYS:
            break; // ok
        case EINVAL:
            break; // ok
        default:
            throw new UnsupportedOperationException("seccomp(BOGUS_OPERATION): " + JNACLibrary.strerror(errno));
        }
    }

    // test seccomp(VALID, BOGUS)
    ret = linux_syscall(arch.seccomp, SECCOMP_SET_MODE_FILTER, bogusArg);
    if (ret != -1) {
        throw new UnsupportedOperationException(
                "seccomp unavailable: seccomp(SECCOMP_SET_MODE_FILTER, BOGUS_FLAG) returned " + ret);
    } else {
        int errno = Native.getLastError();
        switch (errno) {
        case ENOSYS:
            break; // ok
        case EINVAL:
            break; // ok
        default:
            throw new UnsupportedOperationException(
                    "seccomp(SECCOMP_SET_MODE_FILTER, BOGUS_FLAG): " + JNACLibrary.strerror(errno));
        }
    }

    // test prctl(BOGUS)
    ret = linux_prctl(bogusArg, 0, 0, 0, 0);
    if (ret != -1) {
        throw new UnsupportedOperationException("seccomp unavailable: prctl(BOGUS_OPTION) returned " + ret);
    } else {
        int errno = Native.getLastError();
        switch (errno) {
        case ENOSYS:
            break; // ok
        case EINVAL:
            break; // ok
        default:
            throw new UnsupportedOperationException("prctl(BOGUS_OPTION): " + JNACLibrary.strerror(errno));
        }
    }

    // now just normal defensive checks

    // check for GET_NO_NEW_PRIVS
    switch (linux_prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0)) {
    case 0:
        break; // not yet set
    case 1:
        break; // already set by caller
    default:
        int errno = Native.getLastError();
        if (errno == EINVAL) {
            // friendly error, this will be the typical case for an old kernel
            throw new UnsupportedOperationException("seccomp unavailable: requires kernel 3.5+ with"
                    + " CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in");
        } else {
            throw new UnsupportedOperationException(
                    "prctl(PR_GET_NO_NEW_PRIVS): " + JNACLibrary.strerror(errno));
        }
    }
    // check for SECCOMP
    switch (linux_prctl(PR_GET_SECCOMP, 0, 0, 0, 0)) {
    case 0:
        break; // not yet set
    case 2:
        break; // already in filter mode by caller
    default:
        int errno = Native.getLastError();
        if (errno == EINVAL) {
            throw new UnsupportedOperationException(
                    "seccomp unavailable: CONFIG_SECCOMP not compiled into kernel,"
                            + " CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed");
        } else {
            throw new UnsupportedOperationException("prctl(PR_GET_SECCOMP): " + JNACLibrary.strerror(errno));
        }
    }
    // check for SECCOMP_MODE_FILTER
    if (linux_prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, 0, 0, 0) != 0) {
        int errno = Native.getLastError();
        switch (errno) {
        case EFAULT:
            break; // available
        case EINVAL:
            throw new UnsupportedOperationException("seccomp unavailable: CONFIG_SECCOMP_FILTER not"
                    + " compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed");
        default:
            throw new UnsupportedOperationException("prctl(PR_SET_SECCOMP): " + JNACLibrary.strerror(errno));
        }
    }

    // ok, now set PR_SET_NO_NEW_PRIVS, needed to be able to set a seccomp filter as ordinary user
    if (linux_prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
        throw new UnsupportedOperationException(
                "prctl(PR_SET_NO_NEW_PRIVS): " + JNACLibrary.strerror(Native.getLastError()));
    }

    // check it worked
    if (linux_prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) != 1) {
        throw new UnsupportedOperationException(
                "seccomp filter did not really succeed: prctl(PR_GET_NO_NEW_PRIVS): "
                        + JNACLibrary.strerror(Native.getLastError()));
    }

    // BPF installed to check arch, limit, then syscall.
    // See https://www.kernel.org/doc/Documentation/prctl/seccomp_filter.txt for details.
    SockFilter insns[] = { /* 1  */ BPF_STMT(BPF_LD + BPF_W + BPF_ABS, SECCOMP_DATA_ARCH_OFFSET), //
            /* 2  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.audit, 0, 7), // if (arch != audit) goto fail;
            /* 3  */ BPF_STMT(BPF_LD + BPF_W + BPF_ABS, SECCOMP_DATA_NR_OFFSET), //
            /* 4  */ BPF_JUMP(BPF_JMP + BPF_JGT + BPF_K, arch.limit, 5, 0), // if (syscall > LIMIT) goto fail;
            /* 5  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.fork, 4, 0), // if (syscall == FORK) goto fail;
            /* 6  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.vfork, 3, 0), // if (syscall == VFORK) goto fail;
            /* 7  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.execve, 2, 0), // if (syscall == EXECVE) goto fail;
            /* 8  */ BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch.execveat, 1, 0), // if (syscall == EXECVEAT) goto fail;
            /* 9  */ BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), // pass: return OK;
            /* 10 */ BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (EACCES & SECCOMP_RET_DATA)), // fail: return EACCES;
    };
    // seccomp takes a long, so we pass it one explicitly to keep the JNA simple
    SockFProg prog = new SockFProg(insns);
    prog.write();
    long pointer = Pointer.nativeValue(prog.getPointer());

    int method = 1;
    // install filter, if this works, after this there is no going back!
    // first try it with seccomp(SECCOMP_SET_MODE_FILTER), falling back to prctl()
    if (linux_syscall(arch.seccomp, SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
            new NativeLong(pointer)) != 0) {
        method = 0;
        int errno1 = Native.getLastError();
        if (logger.isDebugEnabled()) {
            logger.debug("seccomp(SECCOMP_SET_MODE_FILTER): {}, falling back to prctl(PR_SET_SECCOMP)...",
                    JNACLibrary.strerror(errno1));
        }
        if (linux_prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, pointer, 0, 0) != 0) {
            int errno2 = Native.getLastError();
            throw new UnsupportedOperationException(
                    "seccomp(SECCOMP_SET_MODE_FILTER): " + JNACLibrary.strerror(errno1)
                            + ", prctl(PR_SET_SECCOMP): " + JNACLibrary.strerror(errno2));
        }
    }

    // now check that the filter was really installed, we should be in filter mode.
    if (linux_prctl(PR_GET_SECCOMP, 0, 0, 0, 0) != 2) {
        throw new UnsupportedOperationException(
                "seccomp filter installation did not really succeed. seccomp(PR_GET_SECCOMP): "
                        + JNACLibrary.strerror(Native.getLastError()));
    }

    logger.debug("Linux seccomp filter installation successful, threads: [{}]", method == 1 ? "all" : "app");
    return method;
}

From source file:org.elasticsearch.monitor.os.OsProbeTests.java

License:Apache License

@Test
public void testOsInfo() {
    OsInfo info = probe.osInfo();/*from  w w  w. j  a v a 2  s .  co  m*/
    assertNotNull(info);
    assertThat(info.getRefreshInterval(), anyOf(equalTo(-1L), greaterThanOrEqualTo(0L)));
    assertThat(info.getName(), equalTo(Constants.OS_NAME));
    assertThat(info.getArch(), equalTo(Constants.OS_ARCH));
    assertThat(info.getVersion(), equalTo(Constants.OS_VERSION));
    assertThat(info.getAvailableProcessors(), equalTo(Runtime.getRuntime().availableProcessors()));
}

From source file:org.elasticsearch.node.Node.java

License:Apache License

protected Node(final Environment environment, Collection<Class<? extends Plugin>> classpathPlugins) {
    final List<Closeable> resourcesToClose = new ArrayList<>(); // register everything we need to release in the case of an error
    boolean success = false;
    {//  w  w  w .j  a  v a  2s. c o  m
        // use temp logger just to say we are starting. we can't use it later on because the node name might not be set
        Logger logger = Loggers.getLogger(Node.class, NODE_NAME_SETTING.get(environment.settings()));
        logger.info("initializing ...");

    }
    try {
        Settings tmpSettings = Settings.builder().put(environment.settings())
                .put(Client.CLIENT_TYPE_SETTING_S.getKey(), CLIENT_TYPE).build();

        tmpSettings = TribeService.processSettings(tmpSettings);

        // create the node environment as soon as possible, to recover the node id and enable logging
        try {
            nodeEnvironment = new NodeEnvironment(tmpSettings, environment);
            resourcesToClose.add(nodeEnvironment);
        } catch (IOException ex) {
            throw new IllegalStateException("Failed to created node environment", ex);
        }

        final boolean hadPredefinedNodeName = NODE_NAME_SETTING.exists(tmpSettings);
        tmpSettings = addNodeNameIfNeeded(tmpSettings, nodeEnvironment.nodeId());
        Logger logger = Loggers.getLogger(Node.class, tmpSettings);
        if (hadPredefinedNodeName == false) {
            logger.info("node name [{}] derived from node ID; set [{}] to override",
                    NODE_NAME_SETTING.get(tmpSettings), NODE_NAME_SETTING.getKey());
        }

        final String displayVersion = Version.CURRENT + (Build.CURRENT.isSnapshot() ? "-SNAPSHOT" : "");
        final JvmInfo jvmInfo = JvmInfo.jvmInfo();
        logger.info("version[{}], pid[{}], build[{}/{}], OS[{}/{}/{}], JVM[{}/{}/{}/{}]", displayVersion,
                jvmInfo.pid(), Build.CURRENT.shortHash(), Build.CURRENT.date(), Constants.OS_NAME,
                Constants.OS_VERSION, Constants.OS_ARCH, Constants.JVM_VENDOR, Constants.JVM_NAME,
                Constants.JAVA_VERSION, Constants.JVM_VERSION);

        if (logger.isDebugEnabled()) {
            logger.debug("using config [{}], data [{}], logs [{}], plugins [{}]", environment.configFile(),
                    Arrays.toString(environment.dataFiles()), environment.logsFile(),
                    environment.pluginsFile());
        }
        // TODO: Remove this in Elasticsearch 6.0.0
        if (JsonXContent.unquotedFieldNamesSet) {
            DeprecationLogger dLogger = new DeprecationLogger(logger);
            dLogger.deprecated("[{}] has been set, but will be removed in Elasticsearch 6.0.0",
                    JsonXContent.JSON_ALLOW_UNQUOTED_FIELD_NAMES);
        }

        this.pluginsService = new PluginsService(tmpSettings, environment.modulesFile(),
                environment.pluginsFile(), classpathPlugins);
        this.settings = pluginsService.updatedSettings();
        // create the environment based on the finalized (processed) view of the settings
        // this is just to makes sure that people get the same settings, no matter where they ask them from
        this.environment = new Environment(this.settings);
        Environment.assertEquivalent(environment, this.environment);

        final List<ExecutorBuilder<?>> executorBuilders = pluginsService.getExecutorBuilders(settings);

        final ThreadPool threadPool = new ThreadPool(settings,
                executorBuilders.toArray(new ExecutorBuilder[0]));
        resourcesToClose.add(() -> ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS));
        // adds the context to the DeprecationLogger so that it does not need to be injected everywhere
        DeprecationLogger.setThreadContext(threadPool.getThreadContext());
        resourcesToClose.add(() -> DeprecationLogger.removeThreadContext(threadPool.getThreadContext()));

        final List<Setting<?>> additionalSettings = new ArrayList<>();
        final List<String> additionalSettingsFilter = new ArrayList<>();
        additionalSettings.addAll(pluginsService.getPluginSettings());
        additionalSettingsFilter.addAll(pluginsService.getPluginSettingsFilter());
        for (final ExecutorBuilder<?> builder : threadPool.builders()) {
            additionalSettings.addAll(builder.getRegisteredSettings());
        }
        final ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, threadPool);
        final ScriptModule scriptModule = ScriptModule.create(settings, this.environment,
                resourceWatcherService, pluginsService.filterPlugins(ScriptPlugin.class));
        AnalysisModule analysisModule = new AnalysisModule(this.environment,
                pluginsService.filterPlugins(AnalysisPlugin.class));
        additionalSettings.addAll(scriptModule.getSettings());
        // this is as early as we can validate settings at this point. we already pass them to ScriptModule as well as ThreadPool
        // so we might be late here already
        final SettingsModule settingsModule = new SettingsModule(this.settings, additionalSettings,
                additionalSettingsFilter);
        scriptModule.registerClusterSettingsListeners(settingsModule.getClusterSettings());
        resourcesToClose.add(resourceWatcherService);
        final NetworkService networkService = new NetworkService(settings,
                getCustomNameResolvers(pluginsService.filterPlugins(DiscoveryPlugin.class)));
        final ClusterService clusterService = new ClusterService(settings, settingsModule.getClusterSettings(),
                threadPool);
        clusterService.add(scriptModule.getScriptService());
        resourcesToClose.add(clusterService);
        final TribeService tribeService = new TribeService(settings, clusterService, nodeEnvironment.nodeId());
        resourcesToClose.add(tribeService);
        final IngestService ingestService = new IngestService(settings, threadPool, this.environment,
                scriptModule.getScriptService(), analysisModule.getAnalysisRegistry(),
                pluginsService.filterPlugins(IngestPlugin.class));
        final UsageService usageService = new UsageService(clusterService::localNode, settings);

        ModulesBuilder modules = new ModulesBuilder();
        // plugin modules must be added here, before others or we can get crazy injection errors...
        for (Module pluginModule : pluginsService.createGuiceModules()) {
            modules.add(pluginModule);
        }
        final MonitorService monitorService = new MonitorService(settings, nodeEnvironment, threadPool);
        modules.add(new NodeModule(this, monitorService));
        NetworkModule networkModule = new NetworkModule(networkService, settings, false);
        modules.add(networkModule);
        modules.add(new DiscoveryModule(this.settings));
        ClusterModule clusterModule = new ClusterModule(settings, clusterService,
                pluginsService.filterPlugins(ClusterPlugin.class));
        modules.add(clusterModule);
        IndicesModule indicesModule = new IndicesModule(pluginsService.filterPlugins(MapperPlugin.class));
        modules.add(indicesModule);
        SearchModule searchModule = new SearchModule(settings, false,
                pluginsService.filterPlugins(SearchPlugin.class));
        modules.add(searchModule);
        if (config.getisUsageStatisticsActivated()) {
            modules.add(new ActionModule(DiscoveryNode.isIngestNode(settings), false, settings,
                    clusterModule.getIndexNameExpressionResolver(), settingsModule.getClusterSettings(),
                    pluginsService.filterPlugins(ActionPlugin.class), usageService));
        } else {
            modules.add(new ActionModule(DiscoveryNode.isIngestNode(settings), false, settings,
                    clusterModule.getIndexNameExpressionResolver(), settingsModule.getClusterSettings(),
                    pluginsService.filterPlugins(ActionPlugin.class), null));
        }
        modules.add(new GatewayModule());
        modules.add(
                new RepositoriesModule(this.environment, pluginsService.filterPlugins(RepositoryPlugin.class)));
        pluginsService.processModules(modules);
        CircuitBreakerService circuitBreakerService = createCircuitBreakerService(settingsModule.getSettings(),
                settingsModule.getClusterSettings());
        resourcesToClose.add(circuitBreakerService);
        BigArrays bigArrays = createBigArrays(settings, circuitBreakerService);
        resourcesToClose.add(bigArrays);
        modules.add(settingsModule);
        List<NamedWriteableRegistry.Entry> namedWriteables = Stream
                .of(networkModule.getNamedWriteables().stream(), indicesModule.getNamedWriteables().stream(),
                        searchModule.getNamedWriteables().stream(),
                        pluginsService.filterPlugins(Plugin.class).stream()
                                .flatMap(p -> p.getNamedWriteables().stream()))
                .flatMap(Function.identity()).collect(Collectors.toList());
        final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(namedWriteables);
        final MetaStateService metaStateService = new MetaStateService(settings, nodeEnvironment);
        final IndicesService indicesService = new IndicesService(settings, pluginsService, nodeEnvironment,
                settingsModule.getClusterSettings(), analysisModule.getAnalysisRegistry(),
                searchModule.getQueryParserRegistry(), clusterModule.getIndexNameExpressionResolver(),
                indicesModule.getMapperRegistry(), namedWriteableRegistry, threadPool,
                settingsModule.getIndexScopedSettings(), circuitBreakerService, metaStateService);
        client = new NodeClient(settings, threadPool);
        Collection<Object> pluginComponents = pluginsService.filterPlugins(Plugin.class).stream()
                .flatMap(p -> p
                        .createComponents(client, clusterService, threadPool, resourceWatcherService,
                                scriptModule.getScriptService(), searchModule.getSearchRequestParsers())
                        .stream())
                .collect(Collectors.toList());
        Collection<UnaryOperator<Map<String, MetaData.Custom>>> customMetaDataUpgraders = pluginsService
                .filterPlugins(Plugin.class).stream().map(Plugin::getCustomMetaDataUpgrader)
                .collect(Collectors.toList());
        final MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(customMetaDataUpgraders);
        modules.add(b -> {
            b.bind(PluginsService.class).toInstance(pluginsService);
            b.bind(Client.class).toInstance(client);
            b.bind(NodeClient.class).toInstance(client);
            b.bind(Environment.class).toInstance(this.environment);
            b.bind(ThreadPool.class).toInstance(threadPool);
            b.bind(NodeEnvironment.class).toInstance(nodeEnvironment);
            b.bind(TribeService.class).toInstance(tribeService);
            b.bind(ResourceWatcherService.class).toInstance(resourceWatcherService);
            b.bind(CircuitBreakerService.class).toInstance(circuitBreakerService);
            b.bind(BigArrays.class).toInstance(bigArrays);
            b.bind(ScriptService.class).toInstance(scriptModule.getScriptService());
            b.bind(AnalysisRegistry.class).toInstance(analysisModule.getAnalysisRegistry());
            b.bind(IngestService.class).toInstance(ingestService);
            b.bind(NamedWriteableRegistry.class).toInstance(namedWriteableRegistry);
            b.bind(UsageService.class).toInstance(usageService);
            b.bind(MetaDataUpgrader.class).toInstance(metaDataUpgrader);
            b.bind(MetaStateService.class).toInstance(metaStateService);
            b.bind(IndicesService.class).toInstance(indicesService);
            Class<? extends SearchService> searchServiceImpl = pickSearchServiceImplementation();
            if (searchServiceImpl == SearchService.class) {
                b.bind(SearchService.class).asEagerSingleton();
            } else {
                b.bind(SearchService.class).to(searchServiceImpl).asEagerSingleton();
            }
            pluginComponents.stream().forEach(p -> b.bind((Class) p.getClass()).toInstance(p));

        });
        injector = modules.createInjector();

        List<LifecycleComponent> pluginLifecycleComponents = pluginComponents.stream()
                .filter(p -> p instanceof LifecycleComponent).map(p -> (LifecycleComponent) p)
                .collect(Collectors.toList());
        pluginLifecycleComponents.addAll(pluginsService.getGuiceServiceClasses().stream()
                .map(injector::getInstance).collect(Collectors.toList()));
        resourcesToClose.addAll(pluginLifecycleComponents);
        this.pluginLifecycleComponents = Collections.unmodifiableList(pluginLifecycleComponents);

        client.intialize(injector.getInstance(new Key<Map<GenericAction, TransportAction>>() {
        }));

        logger.info("initialized");

        success = true;
    } catch (IOException ex) {
        throw new ElasticsearchException("failed to bind service", ex);
    } finally {
        if (!success) {
            IOUtils.closeWhileHandlingException(resourcesToClose);
        }
    }
}

From source file:org.elasticsearch.plugins.PluginsTests.java

License:Apache License

public void testMakePlatformName() {
    final String platformName = Platforms.platformName(Constants.OS_NAME, Constants.OS_ARCH);

    assertFalse(platformName, platformName.isEmpty());
    assertTrue(platformName, platformName.equals(platformName.toLowerCase(Locale.ROOT)));
    assertTrue(platformName, platformName.indexOf("-") > 0);
    assertTrue(platformName, platformName.indexOf("-") < platformName.length() - 1);
    assertFalse(platformName, platformName.contains(" "));
}