Example usage for java.util LinkedHashMap keySet

List of usage examples for java.util LinkedHashMap keySet

Introduction

In this page you can find the example usage for java.util LinkedHashMap keySet.

Prototype

public Set<K> keySet() 

Source Link

Document

Returns a Set view of the keys contained in this map.

Usage

From source file:com.logsniffer.event.es.EsEventPersistence.java

private void prepareMapping(final long snifferId) {
    logger.info("Rebuilding mapping for sniffer {}", snifferId);
    final Sniffer sniffer = snifferPersistence.getSniffer(snifferId);
    if (sniffer == null) {
        logger.info("Skip rebuilding mapping due to no more existing sniffer: {}", snifferId);
        return;// w w w. j a v a  2 s  . c  o m
    }
    final LinkedHashMap<String, FieldBaseTypes> snifferTypes = new LinkedHashMap<>();

    final LogSource<?> source = logSourceProvider.getSourceById(sniffer.getLogSourceId());
    final LinkedHashMap<String, FieldBaseTypes> entriesTypes = new LinkedHashMap<>();
    try {
        entriesTypes.putAll(source.getReader().getFieldTypes());
    } catch (final FormatException e) {
        logger.warn("Failed to access entries fields, these won't be considered", e);
    }

    try {
        clientTpl.executeWithClient(new ClientCallback<Object>() {
            @Override
            public Object execute(final Client client) {
                final StringWriter jsonMapping = new StringWriter();
                final JSONBuilder mappingBuilder = new JSONBuilder(jsonMapping).object();
                final JSONBuilder props = mappingBuilder.key(getType(snifferId)).object().key("properties")
                        .object();

                // TODO: Map sniffer fields dynamically
                props.key(Event.FIELD_TIMESTAMP).object().key("type").value("date").endObject();
                props.key(Event.FIELD_PUBLISHED).object().key("type").value("date").endObject();

                for (final String key : entriesTypes.keySet()) {
                    mapField(props, Event.FIELD_ENTRIES + "." + key, entriesTypes.get(key));
                }

                mappingBuilder.endObject().endObject().endObject();
                logger.info("Creating mapping for sniffer {}: {}", snifferId, jsonMapping);
                client.admin().indices().preparePutMapping(indexNamingStrategy.buildActiveName(snifferId))
                        .setType(getType(snifferId)).setSource(jsonMapping.toString()).get();
                return null;
            }
        });
    } catch (final Exception e) {
        logger.warn("Failed to update mapping for sniffer " + snifferId + ", try to delete all events", e);
    }
}

From source file:org.mycore.frontend.editor.MCREditorSubmission.java

MCREditorSubmission(Element saved, List submitted, String root, MCRRequestParameters parms) {
    Element input = saved.getChild("input");
    List children = input.getChildren();

    String varpath = parms.getParameter("subselect.varpath");
    boolean merge = "true".equals(parms.getParameter("subselect.merge"));

    LinkedHashMap<String, String> table = new LinkedHashMap<String, String>();

    for (Object aChildren : children) {
        Element var = (Element) aChildren;
        String path = var.getAttributeValue("name");
        String value = var.getAttributeValue("value");

        if (merge || !(path.equals(varpath) || path.startsWith(varpath + "/"))) {
            table.put(path, value);//  w w  w  .  j av a 2  s. com
        }
    }

    for (Object aSubmitted : submitted) {
        MCREditorVariable var = (MCREditorVariable) aSubmitted;
        String path = var.getPath();
        String value = var.getValue();
        path = varpath + path.substring(root.length());
        table.put(path, value);
    }

    for (String path : table.keySet()) {
        String value = table.get(path);
        addVariable(path, value);
    }

    Collections.sort(variables);
    setRepeatsFromVariables();
}

From source file:com.impetus.ankush2.cassandra.monitor.CassandraClusterMonitor.java

private String getNodeOwnership(JmxUtil jmxUtil, ObjectName mObjNameStorageService, String hostName)
        throws AnkushException {

    // Getting ownership
    Object attrOwnership = jmxUtil.getAttribute(mObjNameStorageService,
            CassandraConstants.Cassandra_JMX_Attributes.CASSANDRA_JMX_ATTRIBUTE_OWNERSHIP);
    System.out.println("attrOwnership: " + attrOwnership);
    if (attrOwnership == null) {
        throw new AnkushException("Could not get node ownership.");
    }/*  w  ww . j a  va2 s .c  om*/
    LinkedHashMap ownership = (LinkedHashMap) attrOwnership;
    for (Object key : ownership.keySet()) {
        if (key.toString().contains(hostName + "/")) {
            DecimalFormat df = new DecimalFormat("###.##");
            return String.valueOf(df.format(((Float) ownership.get(key)) * 100)) + " %";
        }
    }
    throw new AnkushException("Could not get node ownership.");
}

From source file:com.espertech.esper.rowregex.EventRowRegexNFAView.java

/**
 * Ctor.// w  ww .  ja va 2s.  c o  m
 * @param compositeEventType final event type
 * @param rowEventType event type for input rows
 * @param matchRecognizeSpec specification
 * @param variableStreams variables and their assigned stream number
 * @param streamsVariables stream number and the assigned variable
 * @param variablesSingle single variables
 * @param callbacksPerIndex  for handling the 'prev' function
 * @param aggregationService handles aggregations
 * @param isUnbound true if unbound stream
 * @param isIterateOnly true for iterate-only
 * @param isSelectAsksMultimatches if asking for multimatches
 */
public EventRowRegexNFAView(EventType compositeEventType, EventType rowEventType,
        MatchRecognizeSpec matchRecognizeSpec, LinkedHashMap<String, Pair<Integer, Boolean>> variableStreams,
        Map<Integer, String> streamsVariables, Set<String> variablesSingle,
        AgentInstanceContext agentInstanceContext,
        TreeMap<Integer, List<ExprPreviousMatchRecognizeNode>> callbacksPerIndex,
        AggregationServiceMatchRecognize aggregationService, boolean isUnbound, boolean isIterateOnly,
        boolean isSelectAsksMultimatches) {
    this.matchRecognizeSpec = matchRecognizeSpec;
    this.compositeEventType = compositeEventType;
    this.rowEventType = rowEventType;
    this.variableStreams = variableStreams;
    this.variablesArray = variableStreams.keySet().toArray(new String[variableStreams.keySet().size()]);
    this.streamsVariables = streamsVariables;
    this.variablesSingle = variablesSingle;
    this.aggregationService = aggregationService;
    this.isUnbound = isUnbound;
    this.isIterateOnly = isIterateOnly;
    this.agentInstanceContext = agentInstanceContext;
    this.isSelectAsksMultimatches = isSelectAsksMultimatches;

    if (matchRecognizeSpec.getInterval() != null) {
        scheduleSlot = agentInstanceContext.getStatementContext().getScheduleBucket().allocateSlot();
        ScheduleHandleCallback callback = new ScheduleHandleCallback() {
            public void scheduledTrigger(ExtensionServicesContext extensionServicesContext) {
                EventRowRegexNFAView.this.triggered();
            }
        };
        handle = new EPStatementHandleCallback(agentInstanceContext.getEpStatementAgentInstanceHandle(),
                callback);
        schedule = new TreeMap<Long, Object>();

        agentInstanceContext.addTerminationCallback(this);
    } else {
        scheduleSlot = null;
        handle = null;
        schedule = null;
    }

    this.windowMatchedEventset = new LinkedHashSet<EventBean>();

    // handle "previous" function nodes (performance-optimized for direct index access)
    RegexPartitionStateRandomAccessGetter randomAccessByIndexGetter;
    if (!callbacksPerIndex.isEmpty()) {
        // Build an array of indexes
        int[] randomAccessIndexesRequested = new int[callbacksPerIndex.size()];
        int count = 0;
        for (Map.Entry<Integer, List<ExprPreviousMatchRecognizeNode>> entry : callbacksPerIndex.entrySet()) {
            randomAccessIndexesRequested[count] = entry.getKey();
            count++;
        }
        randomAccessByIndexGetter = new RegexPartitionStateRandomAccessGetter(randomAccessIndexesRequested,
                isUnbound);

        // Since an expression such as "prior(2, price), prior(8, price)" translates into {2, 8} the relative index is {0, 1}.
        // Map the expression-supplied index to a relative index
        count = 0;
        for (Map.Entry<Integer, List<ExprPreviousMatchRecognizeNode>> entry : callbacksPerIndex.entrySet()) {
            for (ExprPreviousMatchRecognizeNode callback : entry.getValue()) {
                callback.setGetter(randomAccessByIndexGetter);
                callback.setAssignedIndex(count);
            }
            count++;
        }
    } else {
        randomAccessByIndexGetter = null;
    }

    Map<String, ExprNode> variableDefinitions = new LinkedHashMap<String, ExprNode>();
    for (MatchRecognizeDefineItem defineItem : matchRecognizeSpec.getDefines()) {
        variableDefinitions.put(defineItem.getIdentifier(), defineItem.getExpression());
    }

    // build states
    RegexNFAStrandResult strand = EventRowRegexHelper.recursiveBuildStartStates(matchRecognizeSpec.getPattern(),
            variableDefinitions, variableStreams);
    startStates = strand.getStartStates().toArray(new RegexNFAState[strand.getStartStates().size()]);
    allStates = strand.getAllStates().toArray(new RegexNFAState[strand.getAllStates().size()]);

    if (log.isDebugEnabled() || IS_DEBUG) {
        log.info("NFA tree:\n" + print(startStates));
    }

    // create evaluators
    columnNames = new String[matchRecognizeSpec.getMeasures().size()];
    columnEvaluators = new ExprEvaluator[matchRecognizeSpec.getMeasures().size()];
    int count = 0;
    for (MatchRecognizeMeasureItem measureItem : matchRecognizeSpec.getMeasures()) {
        columnNames[count] = measureItem.getName();
        columnEvaluators[count] = measureItem.getExpr().getExprEvaluator();
        count++;
    }

    // create state repository
    if (this.matchRecognizeSpec.getPartitionByExpressions().isEmpty()) {
        regexPartitionStateRepo = new RegexPartitionStateRepoNoGroup(randomAccessByIndexGetter,
                matchRecognizeSpec.getInterval() != null);
    } else {
        regexPartitionStateRepo = new RegexPartitionStateRepoGroup(randomAccessByIndexGetter,
                ExprNodeUtility.getEvaluators(matchRecognizeSpec.getPartitionByExpressions()),
                matchRecognizeSpec.getInterval() != null, agentInstanceContext);
    }
}

From source file:com.karus.danktitles.commands.HelpSubcommand.java

@Override
public void execute(CommandSender sender, String[] args) {

    // Methods inheritied from CommandChecker
    if (!checkLength(sender, args, 1, 3))
        return;//from ww w.j  av  a 2s . com
    if (!checkSender(sender, "danktitles.help"))
        return;

    LinkedHashMap<String, MutablePair<String, String>> parsedCommands;

    // Checks if the list needs to be filtered
    if (args.length == 1 || args[1].equals("all")) {
        parsedCommands = new LinkedHashMap<>(
                commands.entrySet().stream().filter(entry -> sender.hasPermission(entry.getValue().getLeft()))
                        .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    } else {
        parsedCommands = new LinkedHashMap<>(commands.entrySet().stream().filter(
                entry -> entry.getKey().contains(args[1]) && sender.hasPermission(entry.getValue().getLeft()))
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    }

    if (parsedCommands.isEmpty()) {
        sender.sendMessage(ChatColor.RED + "No matches found.");
        return;
    }

    if (args.length == 3) {
        try {
            page = Integer.parseInt(args[2]);
        } catch (NumberFormatException e) {
            sender.sendMessage(ChatColor.RED + "Invalid page number!");
            return;
        }
    } else {
        page = 1;
    }

    int totalPages = (int) Math.max(1, Math.floor(parsedCommands.size() / SIZE));

    if (page <= 0 || page > totalPages) {
        sender.sendMessage(ChatColor.RED + "Invalid page number!");
        return;
    }

    sender.sendMessage(ChatColor.GOLD + "[Commands - (" + ChatColor.RED + page + "/" + totalPages
            + ChatColor.GOLD + ") ]");

    ArrayList<String> keys = new ArrayList<>(parsedCommands.keySet());

    IntStream.range(page * SIZE - SIZE, parsedCommands.size()).limit(SIZE)
            .forEach(i -> sender.sendMessage(ChatColor.GOLD + commands.get(keys.get(i)).getRight()));

}

From source file:pt.lsts.neptus.util.logdownload.LogsDownloaderWorkerActions.java

/**
 * For the given server with serverKey ID, takes his {@link #getBaseLogListFrom(String)}
 * reply as toProcessLogList and fill the serversLogPresenceList for each base log
 * adding the serverKey to the list of presence for that base log.
 * //from   www.  j a v a  2s . c om
 * If finalLogList is not null, also adds the missing entries to it.
 * 
 * @param serverKey
 * @param toProcessLogList
 * @param finalLogList
 * @param serversLogPresenceList
 */
private void fillServerPresenceList(String serverKey, LinkedHashMap<FTPFile, String> toProcessLogList,
        LinkedHashMap<FTPFile, String> finalLogList, LinkedHashMap<String, String> serversLogPresenceList) {

    if (toProcessLogList != null && !toProcessLogList.isEmpty()) {
        if (finalLogList == null || finalLogList.isEmpty()) {
            for (String partialUri : toProcessLogList.values()) {
                serversLogPresenceList.put(partialUri, serverKey);
            }
            if (finalLogList != null)
                finalLogList.putAll(toProcessLogList);
        } else {
            for (FTPFile ftpFile : toProcessLogList.keySet()) {
                String val = toProcessLogList.get(ftpFile);
                if (finalLogList.containsValue(val)) {
                    serversLogPresenceList.put(val, serversLogPresenceList.get(val) + " " + serverKey);
                    continue;
                } else {
                    finalLogList.put(ftpFile, val);
                    serversLogPresenceList.put(val, serverKey);
                }
            }
        }
    }
}

From source file:org.kuali.rice.kew.docsearch.DocumentSearchCustomizationMediatorImpl.java

@Override
public DocumentSearchCriteriaConfiguration getDocumentSearchCriteriaConfiguration(DocumentType documentType) {

    List<DocumentTypeAttributeBo> searchableAttributes = documentType.getSearchableAttributes();

    // This first map is used to partition our attributes by application id.  It maps an application id to the
    // list of searchable attribute names that are associated with that application id.  Note that 'null' is a
    // valid key in this map for those attributes that have no application id.
    LinkedHashMap<String, List<String>> applicationIdToAttributeNameMap = new LinkedHashMap<String, List<String>>();

    // This second map is used to map the searchable attribute name to the List of RemotableAttributeFields
    // that are returned by invocations of it's getSearchFields method.  This is a LinkedHashMap because it
    // preserves the order of the keys as they are entered.  This allows us to return attribute fields in the
    // proper order as defined by the order of searchable attributes on the doc type, despite the partitioning
    // of our attributes by application id.
    LinkedHashMap<String, AttributeFields> orderedSearchFieldMap = new LinkedHashMap<String, AttributeFields>();
    LinkedHashMap<String, AttributeFields> orderedResultSetFieldMap = new LinkedHashMap<String, AttributeFields>();

    for (DocumentTypeAttributeBo searchableAttribute : searchableAttributes) {
        RuleAttribute ruleAttribute = searchableAttribute.getRuleAttribute();
        String attributeName = ruleAttribute.getName();
        String applicationId = ruleAttribute.getApplicationId();
        if (!applicationIdToAttributeNameMap.containsKey(applicationId)) {
            applicationIdToAttributeNameMap.put(applicationId, new ArrayList<String>());
        }//from w w w.ja v  a 2  s .co  m
        applicationIdToAttributeNameMap.get(applicationId).add(attributeName);
        // reserve a spot in the field map
        orderedSearchFieldMap.put(attributeName, null);
    }

    for (String applicationId : applicationIdToAttributeNameMap.keySet()) {
        DocumentSearchCustomizationHandlerService documentSearchCustomizationService = loadCustomizationService(
                applicationId);
        List<String> searchableAttributeNames = applicationIdToAttributeNameMap.get(applicationId);
        DocumentSearchCriteriaConfiguration documentSearchConfiguration = documentSearchCustomizationService
                .getDocumentSearchConfiguration(documentType.getName(), searchableAttributeNames);
        mergeAttributeFields(documentSearchConfiguration.getSearchAttributeFields(), orderedSearchFieldMap);
    }

    DocumentSearchCriteriaConfiguration.Builder configBuilder = DocumentSearchCriteriaConfiguration.Builder
            .create();
    configBuilder.setSearchAttributeFields(flattenOrderedFieldMap(orderedSearchFieldMap));
    return configBuilder.build();
}

From source file:com.fizzed.rocker.compiler.JavaGenerator.java

private void createSourceTemplate(TemplateModel model, Writer w) throws GeneratorException, IOException {
    if (model.getOptions().getPostProcessing() != null) {
        // allow post-processors to transform the model
        try {// w ww  . j  a va 2 s.c o  m
            model = postProcess(model);
        } catch (PostProcessorException ppe) {
            throw new GeneratorException("Error during post-processing of model.", ppe);
        }
    }

    // Used to register any withstatements we encounter, so we can generate all dynamic consumers at the end.
    final WithStatementConsumerGenerator withStatementConsumerGenerator = new WithStatementConsumerGenerator();

    // simple increment to help create unique var names
    int varCounter = -1;

    if (model.getPackageName() != null && !model.getPackageName().equals("")) {
        w.append("package ").append(model.getPackageName()).append(";").append(CRLF);
    }

    // imports regardless of template
    w.append(CRLF);
    w.append("import ").append(java.io.IOException.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.ForIterator.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RenderingException.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RockerContent.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RockerOutput.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.runtime.DefaultRockerTemplate.class.getName()).append(";")
            .append(CRLF);
    w.append("import ").append(com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader.class.getName())
            .append(";").append(CRLF);

    // template imports
    if (model.getImports().size() > 0) {
        for (JavaImport i : model.getImports()) {
            w.append("// import ").append(sourceRef(i)).append(CRLF);
            w.append("import ").append(i.getStatement()).append(";").append(CRLF);
        }
    }

    w.append(CRLF);
    w.append("/*").append(CRLF);
    w.append(" * Auto generated code to render template ").append(model.getPackageName().replace('.', '/'))
            .append("/").append(model.getTemplateName()).append(CRLF);
    w.append(" * Do not edit this file. Changes will eventually be overwritten by Rocker parser!").append(CRLF);
    w.append(" */").append(CRLF);

    int indent = 0;

    // MODEL CLASS

    // class definition
    tab(w, indent).append("public class ").append(model.getName()).append(" extends ")
            .append(model.getOptions().getExtendsModelClass()).append(" {").append(CRLF);

    indent++;

    w.append(CRLF);

    // static info about this template
    tab(w, indent).append("static public final ").append(ContentType.class.getCanonicalName())
            .append(" CONTENT_TYPE = ").append(ContentType.class.getCanonicalName()).append(".")
            .append(model.getContentType().toString()).append(";").append(CRLF);
    tab(w, indent).append("static public final String TEMPLATE_NAME = \"").append(model.getTemplateName())
            .append("\";").append(CRLF);
    tab(w, indent).append("static public final String TEMPLATE_PACKAGE_NAME = \"")
            .append(model.getPackageName()).append("\";").append(CRLF);
    tab(w, indent).append("static public final String HEADER_HASH = \"").append(model.createHeaderHash() + "")
            .append("\";").append(CRLF);

    // Don't include MODIFIED_AT header when optimized compiler is used since this implicitly disables hot reloading anyhow
    if (!model.getOptions().getOptimize()) {
        tab(w, indent).append("static public final long MODIFIED_AT = ").append(model.getModifiedAt() + "")
                .append("L;").append(CRLF);
    }

    tab(w, indent).append("static public final String[] ARGUMENT_NAMES = {");
    StringBuilder argNameList = new StringBuilder();
    for (Argument arg : model.getArgumentsWithoutRockerBody()) {
        if (argNameList.length() > 0) {
            argNameList.append(",");
        }
        argNameList.append(" \"").append(arg.getExternalName()).append("\"");
    }
    w.append(argNameList).append(" };").append(CRLF);

    // model arguments as members of model class
    appendArgumentMembers(model, w, "private", false, indent);

    // model setters & getters with builder-style pattern
    // special case for the RockerBody argument which sorta "hides" its getter/setter
    if (model.getArguments().size() > 0) {
        for (Argument arg : model.getArguments()) {
            // setter
            w.append(CRLF);
            tab(w, indent).append("public ").append(model.getName()).append(" ").append(arg.getExternalName())
                    .append("(" + arg.getExternalType()).append(" ").append(arg.getName()).append(") {")
                    .append(CRLF);
            tab(w, indent + 1).append("this.").append(arg.getName()).append(" = ").append(arg.getName())
                    .append(";").append(CRLF);
            tab(w, indent + 1).append("return this;").append(CRLF);
            tab(w, indent).append("}").append(CRLF);

            // getter
            w.append(CRLF);
            tab(w, indent).append("public ").append(arg.getExternalType()).append(" ")
                    .append(arg.getExternalName()).append("() {").append(CRLF);
            tab(w, indent + 1).append("return this.").append(arg.getName()).append(";").append(CRLF);
            tab(w, indent).append("}").append(CRLF);
        }
    }

    w.append(CRLF);

    //
    // model "template" static builder
    //
    tab(w, indent).append("static public ").append(model.getName()).append(" template(");

    if (model.getArguments().size() > 0) {
        int i = 0;
        // RockerBody is NOT included (it is passed via a closure block in other templates)
        // so we only care about the other arguments
        for (Argument arg : model.getArgumentsWithoutRockerBody()) {
            if (i != 0) {
                w.append(", ");
            }
            w.append(arg.getType()).append(" ").append(arg.getName());
            i++;
        }
    }
    w.append(") {").append(CRLF);

    tab(w, indent + 1).append("return new ").append(model.getName()).append("()");
    if (model.getArguments().size() > 0) {
        int i = 0;
        for (Argument arg : model.getArgumentsWithoutRockerBody()) {
            w.append(CRLF);
            tab(w, indent + 2).append(".").append(arg.getName()).append("(").append(arg.getName()).append(")");
            i++;
        }
    }
    w.append(";").append(CRLF);
    tab(w, indent).append("}").append(CRLF);

    //
    // render of model
    //
    w.append(CRLF);

    tab(w, indent).append("@Override").append(CRLF);
    tab(w, indent).append("protected DefaultRockerTemplate buildTemplate() throws RenderingException {")
            .append(CRLF);

    if (model.getOptions().getOptimize()) {
        // model "template" static builder (not reloading support, fastest performance)
        tab(w, indent + 1).append("// optimized for performance (via rocker.optimize flag; no auto reloading)")
                .append(CRLF);
        tab(w, indent + 1).append("return new Template(this);").append(CRLF);
        //tab(w, indent+1).append("return template.__render(context);").append(CRLF);
    } else {
        tab(w, indent + 1).append(
                "// optimized for convenience (runtime auto reloading enabled if rocker.reloading=true)")
                .append(CRLF);
        // use bootstrap to create underlying template
        tab(w, indent + 1).append("return ").append(RockerRuntime.class.getCanonicalName())
                .append(".getInstance().getBootstrap().template(this.getClass(), this);").append(CRLF);
        //tab(w, indent+1).append("return template.__render(context);").append(CRLF);
    }

    tab(w, indent).append("}").append(CRLF);

    //
    // TEMPLATE CLASS
    //

    w.append(CRLF);

    // class definition
    tab(w, indent).append("static public class Template extends ").append(model.getOptions().getExtendsClass());

    w.append(" {").append(CRLF);

    indent++;

    // plain text -> map of chunks of text (Java only supports 2-byte length of string constant)
    LinkedHashMap<String, LinkedHashMap<String, String>> plainTextMap = model
            .createPlainTextMap(PLAIN_TEXT_CHUNK_LENGTH);

    if (!plainTextMap.isEmpty()) {

        w.append(CRLF);

        for (String plainText : plainTextMap.keySet()) {

            // include static text as comments in source (limit to 500)
            tab(w, indent).append("// ")
                    .append(StringUtils.abbreviate(RockerUtil.ESCAPE_JAVA.translate(plainText), 500))
                    .append(CRLF);
            for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                if (this.plainTextStrategy == PlainTextStrategy.STATIC_STRINGS) {
                    tab(w, indent).append("static private final String ").append(chunk.getKey()).append(" = \"")
                            .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF);
                } else if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {
                    tab(w, indent).append("static private final byte[] ").append(chunk.getKey()).append(";")
                            .append(CRLF);
                }

            }
        }

        // generate the static initializer
        if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {

            w.append(CRLF);

            tab(w, indent).append("static {").append(CRLF);

            String loaderClassName = unqualifiedClassName(PlainTextUnloadedClassLoader.class);
            tab(w, indent + 1).append(loaderClassName).append(" loader = ").append(loaderClassName)
                    .append(".tryLoad(").append(model.getName()).append(".class.getClassLoader(), ")
                    .append(model.getName()).append(".class.getName()").append(" + \"$PlainText\", \"")
                    .append(model.getOptions().getTargetCharset()).append("\");").append(CRLF);

            for (String plainText : plainTextMap.keySet()) {

                for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                    if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {
                        tab(w, indent + 1).append(chunk.getKey()).append(" = loader.tryGet(\"")
                                .append(chunk.getKey()).append("\");").append(CRLF);
                    }

                }

            }

            tab(w, indent).append("}").append(CRLF);
        }

    }

    // arguments as members of template class
    appendArgumentMembers(model, w, "protected", true, indent);

    w.append(CRLF);

    // constructor
    tab(w, indent).append("public Template(").append(model.getName()).append(" model) {").append(CRLF);

    tab(w, indent + 1).append("super(model);").append(CRLF);
    tab(w, indent + 1).append("__internal.setCharset(\"").append(model.getOptions().getTargetCharset())
            .append("\");").append(CRLF);
    tab(w, indent + 1).append("__internal.setContentType(CONTENT_TYPE);").append(CRLF);
    tab(w, indent + 1).append("__internal.setTemplateName(TEMPLATE_NAME);").append(CRLF);
    tab(w, indent + 1).append("__internal.setTemplatePackageName(TEMPLATE_PACKAGE_NAME);").append(CRLF);

    // each model argument passed along as well
    for (Argument arg : model.getArguments()) {
        tab(w, indent + 1).append("this.").append(arg.getName()).append(" = model.")
                .append(arg.getExternalName()).append("();").append(CRLF);
    }

    tab(w, indent).append("}").append(CRLF);

    w.append(CRLF);

    tab(w, indent).append("@Override").append(CRLF);
    tab(w, indent).append("protected void __doRender() throws IOException, RenderingException {").append(CRLF);

    // build rendering code
    int depth = 1;
    Deque<String> blockEnd = new ArrayDeque<>();

    for (TemplateUnit unit : model.getUnits()) {
        if (unit instanceof Comment) {
            continue;
        }

        // something like
        // IfBeginBlock
        // __internal.aboutToExecutePosInSourceTemplate(5, 10);
        appendCommentAndSourcePositionUpdate(w, depth + indent, unit);

        if (unit instanceof PlainText) {
            PlainText plain = (PlainText) unit;

            LinkedHashMap<String, String> chunks = plainTextMap.get(plain.getText());

            for (String chunkName : chunks.keySet()) {

                tab(w, depth + indent).append("__internal.writeValue(").append(chunkName).append(");")
                        .append(CRLF);

            }

        } else if (unit instanceof ValueExpression) {
            ValueExpression value = (ValueExpression) unit;
            tab(w, depth + indent).append("__internal.renderValue(").append(value.getExpression()).append(", ")
                    .append("" + value.isNullSafe()).append(");").append(CRLF);
        } else if (unit instanceof NullTernaryExpression) {
            NullTernaryExpression nullTernary = (NullTernaryExpression) unit;
            tab(w, depth + indent).append("{").append(CRLF);
            tab(w, depth + indent + 1).append("final Object __v = ").append(nullTernary.getLeftExpression())
                    .append(";").append(CRLF);
            tab(w, depth + indent + 1).append("if (__v != null) { __internal.renderValue(__v, false); }")
                    .append(CRLF);
            if (nullTernary.getRightExpression() != null) {
                tab(w, depth + indent + 1).append("else {__internal.renderValue(")
                        .append(nullTernary.getRightExpression()).append(", true); }").append(CRLF);
            }
            tab(w, depth + indent).append("}").append(CRLF);
        } else if (unit instanceof ValueClosureBegin) {

            ValueClosureBegin closure = (ValueClosureBegin) unit;
            tab(w, depth + indent).append("__internal.renderValue(").append(closure.getExpression())
                    .append(".__body(");

            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                w.append("() -> {").append(CRLF);

                depth++;

                blockEnd.push("}), false);");
            }
            // Java 1.7- uses anonymous inner class
            else {
                w.append("new ").append(unqualifiedClassName(RockerContent.class)).append("() {").append(CRLF);

                depth++;

                blockEnd.push("}), false);");

                tab(w, depth + indent).append("@Override").append(CRLF);

                tab(w, depth + indent).append("public void render() throws IOException, RenderingException {")
                        .append(CRLF);

                depth++;

                blockEnd.push("}");
            }
        } else if (unit instanceof ValueClosureEnd) {
            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
            // Java 1.7- uses anonymous inner class
            else {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(CRLF);

                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
        } else if (unit instanceof ContentClosureBegin) {

            ContentClosureBegin closure = (ContentClosureBegin) unit;
            tab(w, depth + indent).append("RockerContent ").append(closure.getIdentifier()).append(" = ");

            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                w.append("() -> {").append(CRLF);

                depth++;

                blockEnd.push("};");
            }
            // Java 1.7- uses anonymous inner class
            else {
                w.append("new ").append(unqualifiedClassName(com.fizzed.rocker.RockerContent.class))
                        .append("() {").append(CRLF);

                depth++;

                blockEnd.push("};");

                tab(w, depth + indent).append("@Override").append(CRLF);

                tab(w, depth + indent).append("public void render() throws IOException, RenderingException {")
                        .append(CRLF);

                depth++;

                blockEnd.push("}");
            }
        } else if (unit instanceof ContentClosureEnd) {
            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
            // Java 1.7- uses anonymous inner class
            else {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(CRLF);

                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
        } else if (unit instanceof IfBlockBegin) {
            IfBlockBegin block = (IfBlockBegin) unit;

            tab(w, depth + indent).append("if ").append(block.getExpression()).append(" {").append(CRLF);

            blockEnd.push("}");
            depth++;
        } else if (unit instanceof IfBlockElseIf) {
            final IfBlockElseIf block = (IfBlockElseIf) unit;

            depth--;

            // This keeps else-if nicely formatted in generated code.
            tab(w, depth + indent).append("} else if ").append(block.getExpression()).append(" {").append(CRLF);

            depth++;
        } else if (unit instanceof IfBlockElse) {
            depth--;

            tab(w, depth + indent).append("} else {").append(" // else ").append(sourceRef(unit)).append(CRLF);

            depth++;
        } else if (unit instanceof IfBlockEnd) {
            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // if end ").append(sourceRef(unit))
                    .append(CRLF);

        } else if (unit instanceof WithBlockBegin) {
            WithBlockBegin block = (WithBlockBegin) unit;
            WithStatement stmt = block.getStatement();

            String statementConsumerName = withStatementConsumerGenerator.register(stmt);

            final List<WithStatement.VariableWithExpression> variables = stmt.getVariables();

            if (isJava8Plus(model)) {
                tab(w, depth + indent)
                        .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class)
                                : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME)
                        .append(".with(");
                // All expressions
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getValueExpression());
                }
                w.append(", ").append(stmt.isNullSafe() + "").append(", (");
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getVariable().getName());
                }
                w.append(") -> {").append(CRLF);

                depth++;

                blockEnd.push("});");

            } else {
                tab(w, depth + indent)
                        // Note for standard 1 variable with block we use the predefined consumers.
                        // Otherwise we fallback to the generated ones.
                        .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class)
                                : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME)
                        .append(".with(");

                // All expressions
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getValueExpression());
                }
                w.append(", ").append(stmt.isNullSafe() + "").append(", (new ").append(statementConsumerName)
                        .append('<');

                // Types for the .with(..)
                for (int i = 0; i < variables.size(); i++) {
                    final JavaVariable variable = variables.get(i).getVariable();
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(variable.getType());
                }
                w.append(">() {").append(CRLF);
                tab(w, depth + indent + 1).append("@Override public void accept(");
                for (int i = 0; i < variables.size(); i++) {
                    final JavaVariable variable = variables.get(i).getVariable();
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append("final ").append(variable.toString());
                }
                w.append(") throws IOException {").append(CRLF);

                depth++;

                blockEnd.push("}}));");
            }
        } else if (unit instanceof WithBlockElse) {
            depth--;

            if (isJava8Plus(model)) {
                tab(w, depth + indent).append("}, () -> {").append(CRLF);
            } else {
                tab(w, depth + indent).append("}}), (new ")
                        .append(qualifiedClassName(WithBlock.Consumer0.class)).append("() { ")
                        .append("@Override public void accept() throws IOException {").append(CRLF);
            }
            depth++;
        } else if (unit instanceof WithBlockEnd) {
            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // with end ").append(sourceRef(unit))
                    .append(CRLF);
        } else if (unit instanceof ForBlockBegin) {
            ForBlockBegin block = (ForBlockBegin) unit;
            ForStatement stmt = block.getStatement();

            // break support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("try {").append(CRLF);

            depth++;

            if (stmt.getForm() == ForStatement.Form.GENERAL) {
                // print out raw statement including parentheses
                tab(w, depth + indent).append("for ").append(block.getExpression()).append(" {").append(CRLF);

                blockEnd.push("}");
            } else if (stmt.getForm() == ForStatement.Form.ENHANCED) {
                // Java 1.8+ (use lambdas)
                if (stmt.hasAnyUntypedArguments() && isJava8Plus(model)) {

                    // build list of lambda vars
                    String localVars = "";
                    for (JavaVariable arg : stmt.getArguments()) {
                        if (localVars.length() != 0) {
                            localVars += ",";
                        }
                        localVars += arg.getName();
                    }

                    tab(w, depth + indent).append(Java8Iterator.class.getName()).append(".forEach(")
                            .append(stmt.getValueExpression()).append(", (").append(localVars).append(") -> {")
                            .append(CRLF);

                    blockEnd.push("});");
                } else {

                    // is the first argument a "ForIterator" ?
                    boolean forIterator = isForIteratorType(stmt.getArguments().get(0).getType());
                    int collectionCount = (forIterator ? 2 : 1);
                    int mapCount = (forIterator ? 3 : 2);

                    // type and value we are going to iterate thru
                    String iterateeType = null;
                    String valueExpression = null;
                    if (stmt.getArguments().size() == collectionCount) {
                        iterateeType = stmt.getArguments().get(collectionCount - 1).getTypeAsNonPrimitiveType();
                        valueExpression = stmt.getValueExpression();
                    } else if (stmt.getArguments().size() == mapCount) {
                        iterateeType = "java.util.Map.Entry<"
                                + stmt.getArguments().get(mapCount - 2).getTypeAsNonPrimitiveType() + ","
                                + stmt.getArguments().get(mapCount - 1).getTypeAsNonPrimitiveType() + ">";
                        valueExpression = stmt.getValueExpression() + ".entrySet()";
                    }

                    // create unique variable name for iterator
                    String forIteratorVarName = "__forIterator" + (++varCounter);

                    // ForIterator for collection and make it final to assure nested anonymous
                    // blocks can access it as well.
                    tab(w, depth + indent).append("final ")
                            .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<")
                            .append(iterateeType).append(">").append(" ").append(forIteratorVarName)
                            .append(" = new ")
                            .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<")
                            .append(iterateeType).append(">").append("(").append(valueExpression).append(");")
                            .append(CRLF);

                    // for loop same regardless of map vs. collection
                    tab(w, depth + indent).append("while (").append(forIteratorVarName).append(".hasNext()) {")
                            .append(CRLF);

                    // if forIterator request assign to local var and make it final to assure nested anonymous
                    // blocks can access it as well.
                    if (forIterator) {
                        tab(w, depth + indent + 1).append("final ")
                                .append(com.fizzed.rocker.ForIterator.class.getName()).append(" ")
                                .append(stmt.getArguments().get(0).getName()).append(" = ")
                                .append(forIteratorVarName).append(";").append(CRLF);
                    }

                    if (stmt.getArguments().size() == collectionCount) {
                        // assign item to local var and make it final to assure nested anonymous
                        // blocks can access it as well.
                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(collectionCount - 1).toString()).append(" = ")
                                .append(forIteratorVarName).append(".next();").append(CRLF);
                    } else if (stmt.getArguments().size() == mapCount) {
                        // create unique variable name for iterator
                        String entryVarName = "__entry" + (++varCounter);

                        // assign map entry to local var
                        tab(w, depth + indent + 1).append("final ").append(iterateeType).append(" ")
                                .append(entryVarName).append(" = ").append(forIteratorVarName)
                                .append(".next();").append(CRLF);

                        // assign entry to local values  make it final to assure nested anonymous
                        // blocks can access it as well.
                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(mapCount - 2).toString()).append(" = ")
                                .append(entryVarName).append(".getKey();").append(CRLF);

                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(mapCount - 1).toString()).append(" = ")
                                .append(entryVarName).append(".getValue();").append(CRLF);
                    } else {
                        throw new GeneratorException("Unsupported number of arguments for for loop");
                    }

                    blockEnd.push("}");
                }
            }

            depth++;

            // continue support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("try {").append(CRLF);

            depth++;

        } else if (unit instanceof ForBlockEnd) {
            depth--;

            // continue support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("} catch (").append(ContinueException.class.getCanonicalName())
                    .append(" e) {").append(CRLF);

            tab(w, depth + indent + 1).append("// support for continuing for loops").append(CRLF);

            tab(w, depth + indent).append("}").append(CRLF);

            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // for end ").append(sourceRef(unit))
                    .append(CRLF);

            depth--;

            // break support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("} catch (").append(BreakException.class.getCanonicalName())
                    .append(" e) {").append(CRLF);

            tab(w, depth + indent + 1).append("// support for breaking for loops").append(CRLF);

            tab(w, depth + indent).append("}").append(CRLF);
        } else if (unit instanceof BreakStatement) {
            tab(w, depth + indent).append("__internal.throwBreakException();").append(CRLF);
        } else if (unit instanceof ContinueStatement) {
            tab(w, depth + indent).append("__internal.throwContinueException();").append(CRLF);
        }
        //log.info(" src (@ {}): [{}]", unit.getSourceRef(), unit.getSourceRef().getConsoleFriendlyText());
    }

    // end of render()
    tab(w, indent).append("}").append(CRLF);

    indent--;

    // end of template class
    tab(w, indent).append("}").append(CRLF);

    // Generate class with all gathered consumer interfaces for all withblocks
    withStatementConsumerGenerator.generate(this, w);

    if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS
            && !plainTextMap.isEmpty()) {

        w.append(CRLF);

        tab(w, indent).append("private static class PlainText {").append(CRLF);
        w.append(CRLF);

        for (String plainText : plainTextMap.keySet()) {

            for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                tab(w, indent + 1).append("static private final String ").append(chunk.getKey()).append(" = \"")
                        .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF);

            }

        }

        w.append(CRLF);
        tab(w, indent).append("}").append(CRLF);
    }

    w.append(CRLF);
    w.append("}").append(CRLF);
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.TestElasticsearchIndexUtils.java

@Test
public void test_parseDefaultMapping() throws JsonProcessingException, IOException {

    // Check the different components

    // Build/"unbuild" match pair

    assertEquals(Tuples._2T("*", "*"), ElasticsearchIndexUtils.buildMatchPair(_mapper.readTree("{}")));

    assertEquals(Tuples._2T("field*", "*"),
            ElasticsearchIndexUtils.buildMatchPair(_mapper.readTree("{\"match\":\"field*\"}")));

    assertEquals(Tuples._2T("field*field", "type*"), ElasticsearchIndexUtils.buildMatchPair(
            _mapper.readTree("{\"match\":\"field*field\", \"match_mapping_type\": \"type*\"}")));

    assertEquals("testBARSTAR_string",
            ElasticsearchIndexUtils.getFieldNameFromMatchPair(Tuples._2T("test_*", "string")));

    // More complex objects

    final String properties = Resources.toString(
            Resources.getResource("com/ikanow/aleph2/search_service/elasticsearch/utils/properties_test.json"),
            Charsets.UTF_8);/*from   w w  w  .  ja  v a  2 s.c  o m*/
    final String templates = Resources.toString(
            Resources.getResource("com/ikanow/aleph2/search_service/elasticsearch/utils/templates_test.json"),
            Charsets.UTF_8);
    final String both = Resources.toString(
            Resources
                    .getResource("com/ikanow/aleph2/search_service/elasticsearch/utils/full_mapping_test.json"),
            Charsets.UTF_8);

    final JsonNode properties_json = _mapper.readTree(properties);
    final JsonNode templates_json = _mapper.readTree(templates);
    final JsonNode both_json = _mapper.readTree(both);

    // Properties, empty + non-empty

    final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> props_test1 = ElasticsearchIndexUtils
            .getProperties(templates_json);
    assertTrue("Empty map if not present", props_test1.isEmpty());

    final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> props_test2 = ElasticsearchIndexUtils
            .getProperties(properties_json);
    assertEquals(4, props_test2.size());
    assertEquals(Arrays.asList("@version", "@timestamp", "sourceKey", "geoip"),
            props_test2.keySet().stream().map(e -> e.left().value()).collect(Collectors.toList()));

    assertEquals("{\"type\":\"string\",\"index\":\"not_analyzed\"}",
            props_test2.get(Either.left("sourceKey")).toString());

    // Templates, empty + non-empty

    final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> templates_test1 = ElasticsearchIndexUtils
            .getTemplates(properties_json, _mapper.readTree("{}"), Collections.emptySet());
    assertTrue("Empty map if not present", templates_test1.isEmpty());

    final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> templates_test2 = ElasticsearchIndexUtils
            .getTemplates(templates_json, _mapper.readTree("{}"), Collections.emptySet());
    assertEquals("getTemplates: " + templates_test2, 2, templates_test2.size());
    assertEquals(Arrays.asList(Tuples._2T("*", "string"), Tuples._2T("*", "number")),
            templates_test2.keySet().stream().map(e -> e.right().value()).collect(Collectors.toList()));

    // Some more properties test

    final List<String> nested_properties = ElasticsearchIndexUtils.getAllFixedFields_internal(properties_json)
            .collect(Collectors.toList());
    assertEquals(Arrays.asList("@version", "@timestamp", "sourceKey", "geoip", "geoip.location"),
            nested_properties);

    final Set<String> nested_properties_2 = ElasticsearchIndexUtils.getAllFixedFields(both_json);
    assertEquals(Arrays.asList("sourceKey", "@timestamp", "geoip", "geoip.location", "@version"),
            new ArrayList<String>(nested_properties_2));

    // Putting it all together...

    final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> total_result1 = ElasticsearchIndexUtils
            .parseDefaultMapping(both_json, Optional.of("type_test"), Optional.empty(), Optional.empty(),
                    _config.search_technology_override(), _mapper);

    assertEquals(4, total_result1.size());
    assertEquals(
            "{\"mapping\":{\"type\":\"number\",\"index\":\"analyzed\"},\"path_match\":\"test*\",\"match_mapping_type\":\"number\"}",
            total_result1.get(Either.right(Tuples._2T("test*", "number"))).toString());
    assertEquals("{\"type\":\"date\"}", total_result1.get(Either.left("@timestamp1")).toString());

    final LinkedHashMap<Either<String, Tuple2<String, String>>, JsonNode> total_result2 = ElasticsearchIndexUtils
            .parseDefaultMapping(both_json, Optional.empty(), Optional.empty(), Optional.empty(),
                    _config.search_technology_override(), _mapper);

    assertEquals(7, total_result2.size());
    assertEquals(true, total_result2.get(Either.right(Tuples._2T("*", "string"))).get("mapping")
            .get("omit_norms").asBoolean());
    assertEquals("{\"type\":\"date\",\"fielddata\":{}}",
            total_result2.get(Either.left("@timestamp")).toString());

    // A couple of error checks:
    // - Missing mapping
    // - Mapping not an object
}

From source file:OSFFM_ORC.FederationActionManager.java

/**
 * This function verify if the site is present and if it isn't it adds it.
 * @param mapContainer/* w  w w.  j a va2  s.  c o m*/
 * @param sClient
 * @param fedsdnURL
 * @return
 * @throws WSException
 * @throws JSONException
 * @author gtricomi
 */
private boolean checkSiteandInsertFEDSDN(FednetsLink mapContainer, Site sClient, String fedsdnURL, DBMongo m)
        throws WSException, JSONException {
    Response r = sClient.getAllSite(fedsdnURL);
    JSONArray ja = new JSONArray(r.readEntity(String.class));
    LinkedHashMap<String, OpenstackInfoContainer> CloudId_To_OIC = mapContainer.getCloudId_To_OIC();
    Set siteSet = new HashSet();
    for (int i = 0; i < ja.length(); i++) {
        JSONObject jo = (JSONObject) ja.get(i);
        //
        siteSet.add((String) jo.get("name"));
    }
    Iterator it = CloudId_To_OIC.keySet().iterator();
    while (it.hasNext()) {
        String siteNameToCheck = (String) it.next();
        if (!(siteSet.contains(siteNameToCheck))) {
            boolean ok = false;
            for (int k = 0; k < 3; k++) {
                if (CloudId_To_OIC.get(siteNameToCheck) != null)
                    ok = this.addSiteOnFedSDN(siteNameToCheck, sClient,
                            CloudId_To_OIC.get(siteNameToCheck).getEndpoint(), m,
                            CloudId_To_OIC.get(siteNameToCheck).getTenant());
                else
                    ok = true;//ONE site is already present on BNM
                if (ok) {
                    break;
                } else if (k == 3) {
                    LOGGER.error("Something going wrong! It's Impossible add site on FEDSDN");
                    return false; //03/07/2017: inserito per bloccare il flusso nel caso in cui qualche sito non venga inserito !!!
                }
            }
        }
    }
    return true;
}