Example usage for org.jdom2 Element getTextTrim

List of usage examples for org.jdom2 Element getTextTrim

Introduction

In this page you can find the example usage for org.jdom2 Element getTextTrim.

Prototype

public String getTextTrim() 

Source Link

Document

Returns the textual content of this element with all surrounding whitespace removed.

Usage

From source file:com.kixeye.scout.eureka.EurekaServiceInstanceDescriptor.java

License:Apache License

/**
 * Creates a descriptor from a parent and a raw element.
 * /*from ww  w  .j av a  2 s  . c o m*/
 * @param parent
 * @param instanceElement
 */
protected EurekaServiceInstanceDescriptor(EurekaApplication parent, Element instanceElement) {
    this.parent = parent;

    this.app = instanceElement.getChildText("app");
    this.ipAddress = instanceElement.getChildText("ipAddr");
    this.hostname = instanceElement.getChildText("hostName");
    this.vipAddress = instanceElement.getChildText("vipAddress");

    int lastUpdatedTimestampRaw;
    try {
        lastUpdatedTimestampRaw = Integer.parseInt(instanceElement.getChildText("lastUpdatedTimestamp"));
    } catch (Exception e) {
        lastUpdatedTimestampRaw = -11;
    }

    this.lastUpdatedTimestamp = lastUpdatedTimestampRaw;

    int lastDirtyTimestampRaw;
    try {
        lastDirtyTimestampRaw = Integer.parseInt(instanceElement.getChildText("lastDirtyTimestamp"));
    } catch (Exception e) {
        lastDirtyTimestampRaw = -11;
    }

    this.lastDirtyTimestamp = lastDirtyTimestampRaw;

    Element port = instanceElement.getChild("port");

    if (port != null) {
        this.isPortEnabled = Boolean.valueOf(port.getAttributeValue("enabled", "true"));
        this.port = Integer.valueOf(port.getTextTrim());
    } else {
        this.isPortEnabled = false;
        this.port = -1;
    }

    Element securePort = instanceElement.getChild("securePort");

    if (securePort != null) {
        this.isSecurePortEnabled = Boolean.valueOf(securePort.getAttributeValue("enabled", "true"));
        this.securePort = Integer.valueOf(securePort.getTextTrim());
    } else {
        this.isSecurePortEnabled = false;
        this.securePort = -1;
    }

    Element statusElement = instanceElement.getChild("status");
    ServiceStatus status = null;

    if (statusElement != null) {
        switch (statusElement.getTextTrim()) {
        case "UP":
            status = ServiceStatus.UP;
            break;
        case "DOWN":
            status = ServiceStatus.DOWN;
            break;
        default:
            status = ServiceStatus.UNKNOWN;
        }
    }

    this.status = status;

    Element overridenStatusElement = instanceElement.getChild("overriddenstatus");
    ServiceStatus overridenStatus = null;

    if (overridenStatusElement != null) {
        switch (overridenStatusElement.getTextTrim()) {
        case "UP":
            overridenStatus = ServiceStatus.UP;
            break;
        case "DOWN":
            overridenStatus = ServiceStatus.DOWN;
            break;
        default:
            overridenStatus = ServiceStatus.UNKNOWN;
        }
    }

    this.overridenStatus = overridenStatus;

    Element metadata = instanceElement.getChild("metadata");

    if (metadata != null) {
        for (Element element : metadata.getChildren()) {
            this.metadata.put(element.getName(), element.getText());
        }
    }

    Element dataCenterInfo = instanceElement.getChild("dataCenterInfo");

    if (dataCenterInfo != null) {
        Attribute dataCenterInfoClass = instanceElement.getAttribute("class");

        if (dataCenterInfoClass != null && dataCenterInfoClass.getValue() != null) {
            switch (dataCenterInfoClass.getValue()) {
            case EurekaServiceAmazonDataCenterInfo.DATA_CENTER_INFO_CLASS:
                this.dataCenterInfo = new EurekaServiceAmazonDataCenterInfo(this, dataCenterInfo);
                break;
            default:
                this.dataCenterInfo = null;
            }
        } else {
            this.dataCenterInfo = null;
        }
    } else {
        this.dataCenterInfo = null;
    }
}

From source file:com.rhythm.louie.email.MailProperties.java

License:Apache License

public static void processProperties(Element email) {
    for (Element prop : email.getChildren()) {
        String propName = prop.getName().toLowerCase();
        if (null != propName) {
            switch (propName) {
            case JNDI:
                jndi = prop.getText().trim();
                break;
            case CUSTOM:
                for (Element customProp : prop.getChildren()) {
                    props.put(customProp.getName(), customProp.getTextTrim());
                }/*w w  w.j  ava  2  s . c  o  m*/
                break;
            default:
                LoggerFactory.getLogger(MailProperties.class).warn("Unknown Mail Element:{}", propName);
                break;

            }
        }
    }
}

From source file:com.rhythm.louie.server.AccessManager.java

License:Apache License

protected static void loadGroups(Element groups) {
    for (Element group : groups.getChildren()) {
        String name = group.getAttributeValue(GROUP_NAME).toLowerCase();
        if (ADMIN.equals(name)) {
            adminUsers.clear(); //to allow re-loading from a default set by a specific louie.xml impl
            wild = false;//from  w  w  w  .  j a va  2  s.  co  m
            for (Element user : group.getChildren()) {
                String u = user.getTextTrim();
                if (WILDCARD.equals(u)) {
                    wild = true;
                    break;
                }
                adminUsers.add(user.getTextTrim());
            }
        } else {
            Set<String> users = new HashSet<>();
            for (Element user : group.getChildren()) {
                users.add(user.getTextTrim());
            }
            groupUsers.put(name, users);
        }
    }
}

From source file:com.rhythm.louie.server.AccessManager.java

License:Apache License

/**
 * Requires loadGroups has already been executed!
 * @param service/*from  w  w w .java 2s  .  co m*/
 * @param serviceGroup 
 */
protected static void loadServiceAccess(String service, Element serviceGroup) {
    Set<String> superset = new HashSet<>();
    for (Element child : serviceGroup.getChildren()) {
        String group = child.getTextTrim().toLowerCase();
        Set<String> users = groupUsers.get(group);
        if (users != null) {
            superset.addAll(users);
        }
    }
    serviceUsers.put(service, superset);
}

From source file:com.rhythm.louie.server.AlertProperties.java

License:Apache License

public static void processProperties(Element alerts) {
    for (Element child : alerts.getChildren()) {
        String alert = child.getName();
        AlertProperties prop = new AlertProperties();
        properties.put(alert, prop);/*from   w w w .  j  a v a  2 s  .c  o m*/
        for (Element grandchild : child.getChildren()) {
            String propName = grandchild.getName();
            String value = grandchild.getTextTrim();
            switch (propName) {
            case EMAIL:
                prop.setEmail(value);
                break;
            case REQUEST_DURATION:
                prop.setDuration(Long.parseLong(value));
                break;
            case MONITOR_CYCLE:
                prop.setMonitorPollCycle(Integer.parseInt(value));
                break;
            case SUMMARY_HOUR:
                prop.setSummaryHour(Integer.parseInt(value));
                break;
            case PERCENTAGE:
                int capture = Integer.parseInt(value);
                double adjusted = (double) capture / 100;
                prop.setPercentThreshold(adjusted);
                break;
            default:
                LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected alert property  {}", propName);
                break;
            }
        }
    }
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

public static void processProperties(URL configs, String contextGateway) throws Exception {
    loadInternals(); //this code organization is weird but it's from iterations of design

    if (contextGateway != null) {
        //Overrides a default set by internal properties
        Server.setDefaultGateway(contextGateway);
    }/*from  w ww  . j  a  v a  2s  .co  m*/

    Document properties = loadDocument(configs);
    if (properties == null) {
        return;
    }

    Element louie = properties.getRootElement();

    //Check for alternate loading point 
    boolean resetRoot = false;
    for (Element elem : louie.getChildren()) {
        if (ALT_PATH.equalsIgnoreCase(elem.getName())) {
            String altPath = elem.getTextTrim();
            LoggerFactory.getLogger(LouieProperties.class).info("Loading Louie configs from alternate file: {}",
                    altPath);
            //overwrite document with values from alternate config 
            properties = loadDocument(new File(altPath).toURI().toURL());
            if (properties == null)
                return;
            resetRoot = true;
        }
    }
    if (resetRoot) {
        //reset root to new properties obj root
        louie = properties.getRootElement();
    }

    Element groups = louie.getChild(GROUPS);
    if (groups != null) {
        AccessManager.loadGroups(groups);
    }

    boolean serversConfigured = false;
    for (Element elem : louie.getChildren()) {
        String elemName = elem.getName().toLowerCase();

        if (null != elemName)
            switch (elemName) {
            case ALT_PATH:
                LoggerFactory.getLogger(LouieProperties.class)
                        .warn("Extra config_path alternate loading point specified. "
                                + "Only one file-switch can be performed.\n"
                                + "  Please verify what is specified in the embedded xml file.\n"
                                + "  Found: {}", elem.getText());
                break;
            case SERVER_PARENT:
                processServers(elem);
                serversConfigured = true;
                break;
            case SERVICE_PARENT:
                processServices(elem, false);
                break;
            case MESSAGING:
                MessagingProperties.processMessaging(elem);
                break;
            case MAIL:
                MailProperties.processProperties(elem);
                break;
            case SCHEDULER:
                TaskSchedulerProperties.processProperties(elem);
                break;
            case ALERTS:
                AlertProperties.processProperties(elem);
                break;
            case CUSTOM:
                processCustomProperties(elem);
                break;
            case GROUPS:
                break;
            default:
                LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected top level property  {}",
                        elemName);
                break;
            }
    }
    if (!serversConfigured)
        processServers(null); //ugly bootstrapping workflow
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void processServers(Element servers) {
    List<Server> serverList = new ArrayList<>();

    if (servers == null) {
        List<Server> empty = Collections.emptyList();
        Server.processServers(empty);/*from  www  .  j a  va  2s  .co  m*/
        return;
    }

    for (Element server : servers.getChildren()) {
        if (!SERVER.equals(server.getName().toLowerCase()))
            continue;

        String name = null;
        for (Attribute attr : server.getAttributes()) {
            if (NAME.equals(attr.getName().toLowerCase()))
                name = attr.getValue();
        }
        if (name == null) {
            LoggerFactory.getLogger(LouieProperties.class)
                    .error("A server was missing it's 'name' attribute and will be skipped!");
            continue;
        }
        Server prop = new Server(name);

        for (Element serverProp : server.getChildren()) {
            String propName = serverProp.getName().toLowerCase();
            String propValue = serverProp.getTextTrim();
            if (null != propName)
                switch (propName) {
                case HOST:
                    prop.setHost(propValue);
                    break;
                case DISPLAY:
                    prop.setDisplay(propValue);
                    break;
                case LOCATION:
                    prop.setLocation(propValue);
                    break;
                case GATEWAY:
                    prop.setGateway(propValue);
                    break;
                case IP:
                    prop.setIp(propValue);
                    break;
                case EXTERNAL_IP:
                    prop.setExternalIp(propValue);
                    break;
                case CENTRAL_AUTH:
                    prop.setCentralAuth(Boolean.parseBoolean(propValue));
                    break;
                case PORT:
                    prop.setPort(Integer.parseInt(propValue));
                    break;
                case SECURE:
                    prop.setSecure(Boolean.parseBoolean(propValue));
                    break;
                case TIMEZONE:
                    prop.setTimezone(propValue);
                    break;
                case CUSTOM:
                    for (Element child : serverProp.getChildren()) {
                        prop.addCustomProperty(child.getName(), child.getTextTrim());
                    }
                    break;
                default:
                    LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected server property  {}:{}",
                            propName, propValue);
                    break;
                }
        }
        serverList.add(prop);
    }
    Server.processServers(serverList);
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void processServiceDefaults(Element defaults) {
    for (Element defaultProp : defaults.getChildren()) {
        String propName = defaultProp.getName().toLowerCase();
        String propValue = defaultProp.getTextTrim();
        if (null != propName)
            switch (propName) {
            case CACHING:
                ServiceProperties.setDefaultCaching(Boolean.parseBoolean(propValue));
                break;
            case ENABLE:
                ServiceProperties.setDefaultEnable(Boolean.parseBoolean(propValue));
                break;
            case REMOTE_SERVER:
                ServiceProperties.setDefaultRemoteServer(propValue);
                break;
            case READ_ONLY:
                ServiceProperties.setDefaultReadOnly(Boolean.parseBoolean(propValue));
                break;
            default:
                LoggerFactory.getLogger(LouieProperties.class)
                        .warn("Unexpected default service config key {}:{}", propName, propValue);
                break;
            }/*from  w  ww  .  ja v a 2s . c  o  m*/
    }
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void processServices(Element services, boolean internal) {
    if (services == null)
        return;//from w  ww  . j  a v  a  2  s. c o  m

    for (Element elem : services.getChildren()) {
        if (DEFAULT.equalsIgnoreCase(elem.getName())) {
            processServiceDefaults(elem);
            break;
        }
    }

    List<ServiceProperties> servicesList = new ArrayList<>();
    for (Element service : services.getChildren()) {
        String elementName = service.getName();
        if (!SERVICE.equalsIgnoreCase(elementName)) {
            if (!DEFAULT.equalsIgnoreCase(elementName)) {
                LoggerFactory.getLogger(LouieProperties.class).warn("Unknown {} element: {}", SERVICE_PARENT,
                        elementName);
            }
            continue;
        }

        String serviceName = null;
        Boolean enable = null;
        for (Attribute attr : service.getAttributes()) {
            String propName = attr.getName().toLowerCase();
            String propValue = attr.getValue();
            if (null != propName)
                switch (propName) {
                case NAME:
                    serviceName = propValue;
                    break;
                case ENABLE:
                    enable = Boolean.valueOf(propValue);
                    break;
                default:
                    LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected service attribute {}:{}",
                            propName, propValue);
                    break;
                }
        }

        if (serviceName == null) {
            LoggerFactory.getLogger(LouieProperties.class)
                    .error("A service was missing it's 'name' attribute and will be skipped");
            continue;
        }

        ServiceProperties prop = new ServiceProperties(serviceName);
        if (enable != null) {
            prop.setEnable(enable);
        }

        for (Element serviceProp : service.getChildren()) {
            String propName = serviceProp.getName().toLowerCase();
            String propValue = serviceProp.getTextTrim();
            if (null != propName)
                switch (propName) {
                case CACHING:
                    prop.setCaching(Boolean.valueOf(propValue));
                    break;
                case READ_ONLY:
                    prop.setReadOnly(Boolean.valueOf(propValue));
                    break;
                case PROVIDER_CL:
                    prop.setProviderClass(propValue);
                    break;
                case RESPECTED_GROUPS:
                    AccessManager.loadServiceAccess(serviceName, serviceProp);
                    break;
                case RESERVED:
                    if (internal)
                        prop.setReserved(Boolean.valueOf(propValue));
                    break;
                case LAYERS:
                    processServiceLayers(serviceProp, prop);
                    break;
                case CUSTOM:
                    for (Element child : serviceProp.getChildren()) {
                        prop.addCustomProp(child.getName(), child.getTextTrim());
                    }
                    break;
                default:
                    LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected server property  {}:{}",
                            propName, propValue);
                }
        }
        servicesList.add(prop);
    }
    ServiceProperties.processServices(servicesList);
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void processCustomProperties(Element customElem) {
    for (Element customProp : customElem.getChildren()) {
        String propName = customProp.getName();
        CustomProperty custom = new CustomProperty(propName);
        for (Element child : customProp.getChildren()) {
            custom.setProperty(child.getName(), child.getTextTrim());
        }//from ww  w .ja  v a2s  . c  o  m
        customProperties.put(propName, custom);
    }
}