Search in sources :

Example 1 with SystemProperty

use of org.apache.ignite.SystemProperty in project ignite by apache.

the class CommandLineStartup method printSystemPropertiesInfo.

/**
 * Prints properties info to console.
 */
private static void printSystemPropertiesInfo() {
    Map<String, Field> props = new TreeMap<>();
    int maxLength = 0;
    for (Class<?> cls : PROPS_CLS) {
        for (Field field : cls.getFields()) {
            SystemProperty ann = field.getAnnotation(SystemProperty.class);
            if (ann != null) {
                try {
                    String name = U.staticField(cls, field.getName());
                    maxLength = Math.max(maxLength, name.length());
                    props.put(name, field);
                } catch (IgniteCheckedException ignored) {
                // No-op.
                }
            }
        }
    }
    String fmt = "%-" + maxLength + "s - %s[%s] %s.%s";
    props.forEach((name, field) -> {
        String deprecated = field.isAnnotationPresent(Deprecated.class) ? "[Deprecated] " : "";
        SystemProperty prop = field.getAnnotation(SystemProperty.class);
        String defaults = prop.defaults();
        if (prop.type() == Boolean.class && defaults.isEmpty())
            defaults = " Default is false.";
        else if (!defaults.isEmpty())
            defaults = " Default is " + defaults + '.';
        X.println(format(fmt, name, deprecated, prop.type().getSimpleName(), prop.value(), defaults));
    });
}
Also used : Field(java.lang.reflect.Field) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TreeMap(java.util.TreeMap) SystemProperty(org.apache.ignite.SystemProperty)

Example 2 with SystemProperty

use of org.apache.ignite.SystemProperty in project ignite by apache.

the class CommandLinePrintPropertiesTest method testPrintProperties.

/**
 * @throws Exception If failed.
 */
@Test
public void testPrintProperties() throws Exception {
    Map<String, Field> expProps = new HashMap<>();
    for (Field field : IgniteSystemProperties.class.getFields()) {
        int mod = field.getModifiers();
        if (isPublic(mod) && isStatic(mod) && isFinal(mod) && String.class.equals(field.getType())) {
            expProps.put(U.staticField(IgniteSystemProperties.class, field.getName()), field);
            SystemProperty ann = field.getAnnotation(SystemProperty.class);
            assertNotNull("Field " + field.getName() + " has no SystemProperty annotation.", ann);
            assertFalse(field.getName() + " value shouldn't ends with dot.", ann.value().endsWith("."));
            assertFalse(field.getName() + " defaults shouldn't ends with dot.", ann.defaults().endsWith("."));
            assertTrue("Ignite system property must be annotated by @" + SystemProperty.class.getSimpleName() + " [field=" + field + ']', field.isAnnotationPresent(SystemProperty.class));
        }
    }
    for (Class<?> cls : PROPS_CLS) {
        if (cls.equals(IgniteSystemProperties.class))
            continue;
        for (Field field : cls.getFields()) {
            SystemProperty ann = field.getAnnotation(SystemProperty.class);
            if (ann == null)
                continue;
            assertFalse(field.getName() + " value shouldn't ends with dot.", ann.value().endsWith("."));
            assertFalse(field.getName() + " defaults shouldn't ends with dot.", ann.defaults().endsWith("."));
            expProps.put(U.staticField(cls, field.getName()), field);
        }
    }
    assertFalse(expProps.isEmpty());
    GridJavaProcess proc = GridJavaProcess.exec(CommandLineStartup.class, PRINT_PROPS_COMMAND, log, s -> {
        Matcher matcher = propPtrn.matcher(s);
        if (matcher.matches()) {
            String name = matcher.group(1);
            boolean deprecated = !matcher.group(2).isEmpty();
            String desc = matcher.group(3);
            Field field = expProps.remove(name);
            assertNotNull("Unexpected or duplicated property found [name=" + name + ']', field);
            assertEquals(field.isAnnotationPresent(Deprecated.class), deprecated);
            assertFalse("Description is empty.", desc.isEmpty());
        }
    }, null);
    proc.getProcess().waitFor();
    assertEquals(0, proc.getProcess().exitValue());
    assertTrue("Not all properties printed: " + expProps, expProps.isEmpty());
}
Also used : Field(java.lang.reflect.Field) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) HashMap(java.util.HashMap) GridJavaProcess(org.apache.ignite.internal.util.GridJavaProcess) Matcher(java.util.regex.Matcher) SystemProperty(org.apache.ignite.SystemProperty) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)2 SystemProperty (org.apache.ignite.SystemProperty)2 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 Matcher (java.util.regex.Matcher)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteSystemProperties (org.apache.ignite.IgniteSystemProperties)1 GridJavaProcess (org.apache.ignite.internal.util.GridJavaProcess)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 Test (org.junit.Test)1