Search in sources :

Example 71 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class NavajoContextInstanceFactory method readResources.

private Map<String, Message> readResources(File resource, Map<String, Set<String>> aliases) {
    Map<String, Message> result = new HashMap<String, Message>();
    FileReader fr = null;
    String deployment = repositoryInstance.getDeployment();
    try {
        fr = new FileReader(resource);
        Navajo n = NavajoFactory.getInstance().createNavajo(fr);
        Message resources = n.getMessage("datasources");
        Message deployments = n.getMessage("deployments");
        Message aliasMessage = n.getMessage("alias");
        if (resources != null) {
            logger.warn("In datasource definitions, please use 'resources' instead of 'datasources' as top level message name");
        } else {
            resources = n.getMessage("resources");
        }
        appendResources(aliases, result, resources, aliasMessage, null);
        logger.info("# of (deployment independent): resources {}", result.size());
        if (deployment != null && deployments != null) {
            for (Message deploymentMessage : deployments.getAllMessages()) {
                String name = deploymentMessage.getName();
                if (name.equals(deployment)) {
                    Message deploymentResources = deploymentMessage.getMessage("resources");
                    Message deploymentAliasMessage = deploymentMessage.getMessage("alias");
                    Message aliasConditionalMessage = deploymentMessage.getMessage("alias_conditional");
                    appendResources(aliases, result, deploymentResources, deploymentAliasMessage, aliasConditionalMessage);
                } else {
                    logger.debug("Ignoring not-matching datasource ({} vs {})", name, deployment);
                }
            }
        } else {
            logger.warn("No deployment whatsoever, ignoring all deployment specific sources. My deployment: {}", deployment);
            if (deployments != null) {
                deployments.write(System.err);
            } else {
                logger.warn("No deployments message either!");
            }
        }
    } catch (FileNotFoundException e) {
        logger.debug("Problem reading file: {}", resource, e);
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                logger.debug("Problem closing file: {}", resource, e);
            }
        }
    }
    return result;
}
Also used : Message(com.dexels.navajo.document.Message) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) Navajo(com.dexels.navajo.document.Navajo) IOException(java.io.IOException)

Example 72 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class NavajoContextInstanceFactory method appendInstance.

private void appendInstance(String name, File instanceFolder, Map<String, Object> globalProperties, Map<String, Message> globalResources) throws IOException {
    logger.info("Instance name: " + name);
    if (skipDeployment(instanceFolder)) {
        return;
    }
    Map<String, Object> copyOfProperties = new HashMap<String, Object>(globalProperties);
    File config = new File(instanceFolder, "config");
    File instanceProperties = new File(config, "application.properties");
    if (instanceProperties.exists()) {
        final Map<String, Object> instanceSpecific = readProperties(instanceProperties);
        logger.info("spec: {}, {}", instanceSpecific.size(), instanceProperties.getAbsolutePath());
        copyOfProperties.putAll(instanceSpecific);
    }
    File instanceResource = new File(config, "resources.xml");
    Map<String, Set<String>> aliases = new HashMap<String, Set<String>>();
    Map<String, Message> resources = readResources(instanceResource, aliases);
    registerInstanceProperties(name, copyOfProperties);
    registerInstanceResources(name, resources, aliases);
    registerLocalClients(name, instanceFolder);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Message(com.dexels.navajo.document.Message) HashMap(java.util.HashMap) File(java.io.File)

Example 73 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class NavajoContextInstanceFactory method appendResources.

private void appendResources(Map<String, Set<String>> aliases, Map<String, Message> result, Message resources, Message aliasMessage, Message aliasConditionalMessage) {
    if (aliasMessage != null) {
        List<Property> aliasProps = aliasMessage.getAllProperties();
        for (Property property : aliasProps) {
            String aliasValue = (String) property.getTypedValue();
            String name = property.getName();
            Set<String> found = aliases.get(aliasValue);
            if (found == null) {
                found = new HashSet<String>();
                aliases.put(aliasValue, found);
            }
            found.add(name);
        }
    }
    if (aliasConditionalMessage != null) {
        appendConditionalAlias(aliasConditionalMessage, aliases);
    }
    if (resources != null) {
        List<Message> sources = resources.getAllMessages();
        for (Message rsrc : sources) {
            result.put(rsrc.getName(), rsrc);
        }
    }
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Example 74 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class NavajoContextInstanceFactory method appendConditionalAlias.

private void appendConditionalAlias(Message aliasConditionalMessage, Map<String, Set<String>> aliases) {
    for (Message m : aliasConditionalMessage.getAllMessages()) {
        String name = m.getName();
        Property conditionProp = m.getProperty("condition");
        Property trueValueProp = m.getProperty("true_value");
        Property falseValueProp = m.getProperty("false_value");
        if (conditionProp == null || conditionProp.getTypedValue() == null) {
            logger.warn("Invalid conditional alias message! {}", m);
            continue;
        }
        boolean evaluated = checkEliasCondition(conditionProp.getValue());
        String aliasValue = null;
        if (evaluated && trueValueProp != null) {
            aliasValue = trueValueProp.getValue();
        } else if (!evaluated && falseValueProp != null) {
            aliasValue = falseValueProp.getValue();
        }
        if (aliasValue != null) {
            Set<String> found = aliases.get(aliasValue);
            if (found == null) {
                found = new HashSet<String>();
                aliases.put(aliasValue, found);
            }
            found.add(name);
        }
    }
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Example 75 with Message

use of com.dexels.navajo.document.Message in project navajo by Dexels.

the class DomainObjectMapper method mapObjectToProperties.

/**
 * Automatically maps all 'getters' to a property.
 *
 * Only add property if:
 *  1. attribute 'getter' does not expect any arguments.
 *  2. attribute has a 'getter'.
 *  3. corresponding property does not already exist in message.
 *  4. corresponding property is not in exclusion list.
 * @throws Exception
 */
private void mapObjectToProperties(Class myClass) throws Exception {
    java.lang.reflect.Method[] all = myClass.getMethods();
    if (myAccess == null) {
        return;
    }
    Navajo out = myAccess.getOutputDoc();
    Message currentOutMsg = myAccess.getCurrentOutMessage();
    if (currentOutMsg == null) {
        throw new Exception("No current message for mapping object attributes to properties.");
    }
    for (int i = 0; i < all.length; i++) {
        String attributeName = all[i].getName().substring(3);
        if (all[i].getParameterTypes().length == 0 && all[i].getName().startsWith("get") && !all[i].getName().startsWith("getClass") && currentOutMsg.getProperty(attributeName) == null && !isAnExcludedProperty(attributeName)) {
            Object result = all[i].invoke(myObject, (Object[]) null);
            if (result == null || !(result.toString().startsWith("[L") || List.class.isAssignableFrom(result.getClass()))) {
                Property p = NavajoFactory.getInstance().createProperty(out, attributeName, "string", "", 0, "", "", "");
                p.setAnyValue(result);
                p.setDirection(isAnInputProperty(attributeName) ? Property.DIR_IN : Property.DIR_OUT);
                currentOutMsg.addProperty(p);
            }
        }
    }
}
Also used : Message(com.dexels.navajo.document.Message) Method(java.lang.reflect.Method) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) MappingException(com.dexels.navajo.script.api.MappingException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Aggregations

Message (com.dexels.navajo.document.Message)312 Property (com.dexels.navajo.document.Property)149 Navajo (com.dexels.navajo.document.Navajo)127 Test (org.junit.Test)95 NavajoException (com.dexels.navajo.document.NavajoException)39 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)35 Access (com.dexels.navajo.script.api.Access)27 UserException (com.dexels.navajo.script.api.UserException)27 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 Selection (com.dexels.navajo.document.Selection)25 Operand (com.dexels.navajo.document.Operand)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)19 MappableException (com.dexels.navajo.script.api.MappableException)18 Ignore (org.junit.Ignore)17 SystemException (com.dexels.navajo.script.api.SystemException)16 Optional (java.util.Optional)15 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)13 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)13 HashMap (java.util.HashMap)13