Example usage for java.util TreeSet contains

List of usage examples for java.util TreeSet contains

Introduction

In this page you can find the example usage for java.util TreeSet contains.

Prototype

public boolean contains(Object o) 

Source Link

Document

Returns true if this set contains the specified element.

Usage

From source file:com.datatorrent.stram.StreamingContainerManager.java

/**
 * Compute checkpoints required for a given operator instance to be recovered.
 * This is done by looking at checkpoints available for downstream dependencies first,
 * and then selecting the most recent available checkpoint that is smaller than downstream.
 *
 * @param operator Operator instance for which to find recovery checkpoint
 * @param ctx      Context into which to collect traversal info
 *//*from w ww  .  java  2  s. c  om*/
public void updateRecoveryCheckpoints(PTOperator operator, UpdateCheckpointsContext ctx) {
    if (operator.getRecoveryCheckpoint().windowId < ctx.committedWindowId.longValue()) {
        ctx.committedWindowId.setValue(operator.getRecoveryCheckpoint().windowId);
    }

    if (operator.getState() == PTOperator.State.ACTIVE && (ctx.currentTms
            - operator.stats.lastWindowIdChangeTms) > operator.stats.windowProcessingTimeoutMillis) {
        // if the checkpoint is ahead, then it is not blocked but waiting for activation (state-less recovery, at-most-once)
        if (ctx.committedWindowId.longValue() >= operator.getRecoveryCheckpoint().windowId) {
            LOG.debug("Marking operator {} blocked committed window {}, recovery window {}", operator,
                    Codec.getStringWindowId(ctx.committedWindowId.longValue()),
                    Codec.getStringWindowId(operator.getRecoveryCheckpoint().windowId));
            ctx.blocked.add(operator);
        }
    }

    // the most recent checkpoint eligible for recovery based on downstream state
    Checkpoint maxCheckpoint = Checkpoint.INITIAL_CHECKPOINT;

    Set<OperatorMeta> checkpointGroup = ctx.checkpointGroups.get(operator.getOperatorMeta());
    if (checkpointGroup == null) {
        checkpointGroup = Collections.singleton(operator.getOperatorMeta());
    }
    // find intersection of checkpoints that group can collectively move to
    TreeSet<Checkpoint> commonCheckpoints = new TreeSet<>(new Checkpoint.CheckpointComparator());
    synchronized (operator.checkpoints) {
        commonCheckpoints.addAll(operator.checkpoints);
    }
    Set<PTOperator> groupOpers = new HashSet<>(checkpointGroup.size());
    boolean pendingDeploy = operator.getState() == PTOperator.State.PENDING_DEPLOY;
    if (checkpointGroup.size() > 1) {
        for (OperatorMeta om : checkpointGroup) {
            Collection<PTOperator> operators = plan.getAllOperators(om);
            for (PTOperator groupOper : operators) {
                synchronized (groupOper.checkpoints) {
                    commonCheckpoints.retainAll(groupOper.checkpoints);
                }
                // visit all downstream operators of the group
                ctx.visited.add(groupOper);
                groupOpers.add(groupOper);
                pendingDeploy |= operator.getState() == PTOperator.State.PENDING_DEPLOY;
            }
        }
        // highest common checkpoint
        if (!commonCheckpoints.isEmpty()) {
            maxCheckpoint = commonCheckpoints.last();
        }
    } else {
        // without logical grouping, treat partitions as independent
        // this is especially important for parallel partitioning
        ctx.visited.add(operator);
        groupOpers.add(operator);
        maxCheckpoint = operator.getRecentCheckpoint();
        if (ctx.recovery && maxCheckpoint.windowId == Stateless.WINDOW_ID && operator.isOperatorStateLess()) {
            long currentWindowId = WindowGenerator.getWindowId(ctx.currentTms, this.vars.windowStartMillis,
                    this.getLogicalPlan().getValue(LogicalPlan.STREAMING_WINDOW_SIZE_MILLIS));
            maxCheckpoint = new Checkpoint(currentWindowId, 0, 0);
        }
    }

    // DFS downstream operators
    for (PTOperator groupOper : groupOpers) {
        for (PTOperator.PTOutput out : groupOper.getOutputs()) {
            for (PTOperator.PTInput sink : out.sinks) {
                PTOperator sinkOperator = sink.target;
                if (groupOpers.contains(sinkOperator)) {
                    continue; // downstream operator within group
                }
                if (!ctx.visited.contains(sinkOperator)) {
                    // downstream traversal
                    updateRecoveryCheckpoints(sinkOperator, ctx);
                }
                // recovery window id cannot move backwards
                // when dynamically adding new operators
                if (sinkOperator.getRecoveryCheckpoint().windowId >= operator
                        .getRecoveryCheckpoint().windowId) {
                    maxCheckpoint = Checkpoint.min(maxCheckpoint, sinkOperator.getRecoveryCheckpoint());
                }

                if (ctx.blocked.contains(sinkOperator)) {
                    if (sinkOperator.stats.getCurrentWindowId() == operator.stats.getCurrentWindowId()) {
                        // downstream operator is blocked by this operator
                        ctx.blocked.remove(sinkOperator);
                    }
                }
            }
        }
    }

    // find the common checkpoint that is <= downstream recovery checkpoint
    if (!commonCheckpoints.contains(maxCheckpoint)) {
        if (!commonCheckpoints.isEmpty()) {
            maxCheckpoint = Objects.firstNonNull(commonCheckpoints.floor(maxCheckpoint), maxCheckpoint);
        }
    }

    for (PTOperator groupOper : groupOpers) {
        // checkpoint frozen during deployment
        if (!pendingDeploy || ctx.recovery) {
            // remove previous checkpoints
            Checkpoint c1 = Checkpoint.INITIAL_CHECKPOINT;
            LinkedList<Checkpoint> checkpoints = groupOper.checkpoints;
            synchronized (checkpoints) {
                if (!checkpoints.isEmpty() && (checkpoints.getFirst()).windowId <= maxCheckpoint.windowId) {
                    c1 = checkpoints.getFirst();
                    Checkpoint c2;
                    while (checkpoints.size() > 1
                            && ((c2 = checkpoints.get(1)).windowId) <= maxCheckpoint.windowId) {
                        checkpoints.removeFirst();
                        //LOG.debug("Checkpoint to delete: operator={} windowId={}", operator.getName(), c1);
                        this.purgeCheckpoints.add(new Pair<PTOperator, Long>(groupOper, c1.windowId));
                        c1 = c2;
                    }
                } else {
                    if (ctx.recovery && checkpoints.isEmpty() && groupOper.isOperatorStateLess()) {
                        LOG.debug("Adding checkpoint for stateless operator {} {}", groupOper,
                                Codec.getStringWindowId(maxCheckpoint.windowId));
                        c1 = groupOper.addCheckpoint(maxCheckpoint.windowId, this.vars.windowStartMillis);
                    }
                }
            }
            //LOG.debug("Operator {} checkpoints: commit {} recent {}", new Object[] {operator.getName(), c1, operator.checkpoints});
            groupOper.setRecoveryCheckpoint(c1);
        } else {
            LOG.debug("Skipping checkpoint update {} during {}", groupOper, groupOper.getState());
        }
    }

}

From source file:net.spfbl.spf.SPF.java

/**
 * Atualiza o registro SPF de um hostname.
 *
 * @throws ProcessException se houver falha no processamento.
 *///from   w w w.ja va 2  s  .co m
private synchronized void refresh(boolean load, boolean bgWhenUnavailable) throws ProcessException {
    long time = System.currentTimeMillis();
    LinkedList<String> registryList = getRegistrySPF(hostname, bgWhenUnavailable);
    if (registryList == null) {
        // Domnimo no encontrado.
        this.mechanismList = null;
        this.all = null;
        this.redirect = null;
        this.explanation = null;
        this.error = false;
        CacheSPF.CHANGED = true;
        this.addInexistent();
        updateLastRefresh();
        Server.logLookupSPF(time, hostname, "NXDOMAIN");
    } else if (registryList.isEmpty()) {
        // Sem registro SPF.
        this.mechanismList = new ArrayList<Mechanism>();
        this.all = null;
        this.redirect = null;
        this.explanation = null;
        this.error = false;
        CacheSPF.CHANGED = true;
        this.nxdomain = 0;
        updateLastRefresh();
        Server.logLookupSPF(time, hostname, "NO REGISTRY");
    } else {
        ArrayList<Mechanism> mechanismListIP = new ArrayList<Mechanism>();
        ArrayList<Mechanism> mechanismListDNS = new ArrayList<Mechanism>();
        ArrayList<Mechanism> mechanismListInclude = new ArrayList<Mechanism>();
        ArrayList<Mechanism> mechanismListPTR = new ArrayList<Mechanism>();
        TreeSet<String> visitedTokens = new TreeSet<String>();
        Qualifier allLocal = null;
        String redirectLocal = null;
        String explanationLocal = null;
        boolean errorQuery = false;
        String fixed;
        String result = null;
        for (String registry : registryList) {
            boolean errorRegistry = false;
            StringTokenizer tokenizer = new StringTokenizer(registry, " ");
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                if (visitedTokens.contains(token)) {
                    // Token j visitado.
                } else if (token.equals("spf1")) {
                    // Nada deve ser feito.
                } else if (token.equals("v=spf1")) {
                    // Nada deve ser feito.
                } else if (token.equals("v=msv1")) {
                    // Nada deve ser feito.
                } else if (token.equals("+")) {
                    // Ignorar qualificadores isolados.
                } else if (token.startsWith("t=") && token.length() == 32) {
                    // Nada deve ser feito.
                } else if (isMechanismAll(token)) {
                    // No permitir qualificadores permissivos para all.
                    switch (token.charAt(0)) {
                    case '-':
                        allLocal = Qualifier.FAIL;
                        break;
                    case '~':
                        allLocal = Qualifier.SOFTFAIL;
                        break;
                    default:
                        allLocal = Qualifier.NEUTRAL; // Default qualifier or all.
                    }
                } else if (isMechanismIPv4(token)) {
                    mechanismListIP.add(new MechanismIPv4(token));
                } else if (isMechanismIPv6(token)) {
                    mechanismListIP.add(new MechanismIPv6(token));
                } else if (isMechanismA(token)) {
                    mechanismListDNS.add(new MechanismA(token, load));
                } else if (isMechanismMX(token)) {
                    mechanismListDNS.add(new MechanismMX(token, load));
                } else if (isMechanismPTR(token)) {
                    mechanismListPTR.add(new MechanismPTR(token));
                } else if (isMechanismExistis(token)) {
                    mechanismListDNS.add(new MechanismExists(token));
                } else if (isMechanismInclude(token)) {
                    mechanismListInclude.add(new MechanismInclude(token));
                } else if (isModifierRedirect(token)) {
                    int index = token.indexOf("=") + 1;
                    redirectLocal = token.substring(index);
                } else if (isModifierExplanation(token)) {
                    int index = token.indexOf("=") + 1;
                    explanationLocal = token.substring(index);
                } else if ((fixed = extractIPv4CIDR(token)) != null) {
                    // Tenta recuperar um erro de sintaxe.
                    if (!visitedTokens.contains(token = "ip4:" + fixed)) {
                        mechanismListIP.add(new MechanismIPv4(token));
                    }
                    errorRegistry = true;
                } else if ((fixed = extractIPv6CIDR(token)) != null) {
                    // Tenta recuperar um erro de sintaxe.
                    if (!visitedTokens.contains(token = "ip4:" + fixed)) {
                        mechanismListIP.add(new MechanismIPv6(token));
                    }
                    errorRegistry = true;
                } else {
                    // Um erro durante o processamento foi encontrado.
                    Server.logDebug("SPF token not defined: " + token);
                    errorRegistry = true;
                    errorQuery = true;
                }
                visitedTokens.add(token);
            }
            if (result == null) {
                result = (errorRegistry ? "ERR" : "OK") + " \"" + registry + "\"";
            } else {
                result += (errorRegistry ? "\\nERR" : "\\nOK") + " \"" + registry + "\"";
            }
        }
        // Considerar os mecanismos na ordem crescente
        // de complexidade de processamento.
        ArrayList<Mechanism> mechanismListLocal = new ArrayList<Mechanism>();
        mechanismListLocal.addAll(mechanismListIP);
        mechanismListLocal.addAll(mechanismListDNS);
        mechanismListLocal.addAll(mechanismListInclude);
        mechanismListLocal.addAll(mechanismListPTR);
        // Atribuio dos novos valores.
        this.mechanismList = mechanismListLocal;
        this.all = allLocal;
        this.redirect = redirectLocal;
        this.explanation = explanationLocal;
        this.error = errorQuery;
        CacheSPF.CHANGED = true;
        this.nxdomain = 0;
        updateLastRefresh();
        Server.logLookupSPF(time, hostname, result);
    }
}

From source file:com.knowgate.dfs.FileSystem.java

/**
 * Download an HTML page and all its referenced files into a ZIP
 * @param sBasePath String Base path for page and its referenced files
 * @param sFilePath String File path from sBasePath
 * @param oOutStrm OutputStream where ZIP is written
 * @param sDefaultEncoding Character encoding of file to be downloaded
 * @throws IOException//from  w  ww.  j av a  2 s. c om
 * @since 7.0
 */
public void downloadhtmlpage(String sBasePath, String sFilePath, OutputStream oOutStrm, String sDefaultEncoding)
        throws IOException {

    if (DebugFile.trace) {
        DebugFile.writeln("Begin FileSystem.downloadhtmlpage(" + sBasePath + "," + sFilePath
                + ",[OutputStream]," + sDefaultEncoding + ")");
        DebugFile.incIdent();
    }

    String sEncoding = sDefaultEncoding;
    String sBaseHref = "";
    boolean bAutoDetectEncoding = (sDefaultEncoding == null);
    TreeSet<String> oFiles = new TreeSet<String>();
    TreeSet<String> oEntries = new TreeSet<String>();
    Perl5Matcher oMatcher = new Perl5Matcher();
    Perl5Matcher oReplacer = new Perl5Matcher();
    Perl5Compiler oCompiler = new Perl5Compiler();

    if (sDefaultEncoding == null)
        sDefaultEncoding = "ASCII";

    try {
        String sHtml = readfilestr(sBasePath + sFilePath, sDefaultEncoding);

        if (null == sHtml) {
            if (DebugFile.trace) {
                DebugFile.writeln("Could not read file " + sBasePath + sFilePath);
                DebugFile.decIdent();
                throw new IOException("Could not read file " + sBasePath + sFilePath);
            }
        }

        if (DebugFile.trace) {
            DebugFile.writeln(
                    String.valueOf(sHtml.length()) + " characters readed from file " + sBasePath + sFilePath);
        }

        if (bAutoDetectEncoding) {
            if (oMatcher.contains(sHtml, oCompiler.compile(
                    "<meta\\x20+http-equiv=(\"|')?Content-Type(\"|')?\\x20+content=(\"|')?text/html;\\x20+charset=(\\w|-){3,32}(\"|')?>",
                    Perl5Compiler.CASE_INSENSITIVE_MASK))) {
                if (DebugFile.trace)
                    DebugFile.writeln("<meta http-equiv> tag found");
                String sHttpEquiv = oMatcher.getMatch().toString();
                int iCharset = Gadgets.indexOfIgnoreCase(sHttpEquiv, "charset=");
                if (iCharset > 0) {
                    int iQuoute = sHttpEquiv.indexOf('"', iCharset);
                    if (iQuoute < 0)
                        iQuoute = sHttpEquiv.indexOf((char) 39, iCharset);
                    if (iQuoute < 0) {
                        bAutoDetectEncoding = true;
                    } else {
                        sEncoding = sHttpEquiv.substring(iCharset + 8, iQuoute);
                        if (DebugFile.trace)
                            DebugFile.writeln("setting charset encoding to " + sEncoding);
                        bAutoDetectEncoding = false;
                        try {
                            byte[] aTest = new String("Test").getBytes(sEncoding);
                        } catch (UnsupportedEncodingException uex) {
                            bAutoDetectEncoding = true;
                        }
                    }
                } else {
                    bAutoDetectEncoding = true;
                }
            } else {
                bAutoDetectEncoding = true;
            }
        }

        if (bAutoDetectEncoding) {
            if (DebugFile.trace)
                DebugFile.writeln("Autodetecting encoding");
            ByteArrayInputStream oHtmlStrm = new ByteArrayInputStream(sHtml.getBytes(sDefaultEncoding));
            sEncoding = new CharacterSetDetector().detect(oHtmlStrm, sDefaultEncoding);
            oHtmlStrm.close();
            if (DebugFile.trace)
                DebugFile.writeln("Encoding set to " + sEncoding);
        }

        Pattern oPattern = oCompiler.compile("<base(\\x20)+href=(\"|')?([^'\"\\r\\n]+)(\"|')?(\\x20)*/?>",
                Perl5Compiler.CASE_INSENSITIVE_MASK);
        if (oMatcher.contains(sHtml, oPattern)) {
            sBaseHref = Gadgets.chomp(oMatcher.getMatch().group(3), "/");
            if (DebugFile.trace)
                DebugFile.writeln("<base href=" + sBaseHref + ">");
        }

        PatternMatcherInput oMatchInput = new PatternMatcherInput(sHtml);
        oPattern = oCompiler.compile(
                "\\x20(src=|background=|background-image:url\\x28)(\"|')?([^'\"\\r\\n]+)(\"|')?(\\x20|\\x29|/|>)",
                Perl5Compiler.CASE_INSENSITIVE_MASK);
        StringSubstitution oSrcSubs = new StringSubstitution();
        int nMatches = 0;
        while (oMatcher.contains(oMatchInput, oPattern)) {
            nMatches++;
            String sMatch = oMatcher.getMatch().toString();
            String sAttr = oMatcher.getMatch().group(1);
            String sQuo = oMatcher.getMatch().group(2);
            if (sQuo == null)
                sQuo = "";
            String sSrc = oMatcher.getMatch().group(3);
            if (DebugFile.trace)
                DebugFile.writeln("Source file found at " + sSrc);
            String sEnd = oMatcher.getMatch().group(5);
            if (!oFiles.contains(sSrc))
                oFiles.add(sSrc);
            String sFilename = sSrc.substring(sSrc.replace('\\', '/').lastIndexOf('/') + 1);
            if (DebugFile.trace)
                DebugFile.writeln("StringSubstitution.setSubstitution(" + sMatch + " replace with "
                        + sMatch.substring(0, sAttr.length() + 1) + sQuo + sFilename + sQuo + sEnd + ")");
            oSrcSubs.setSubstitution(sMatch.substring(0, sAttr.length() + 1) + sQuo + sFilename + sQuo + sEnd);
            sHtml = Util.substitute(oReplacer, oCompiler.compile(sMatch), oSrcSubs, sHtml, Util.SUBSTITUTE_ALL);
        } //wend

        oMatchInput = new PatternMatcherInput(sHtml);
        oPattern = oCompiler.compile(
                "<link\\x20+(rel=(\"|')?stylesheet(\"|')?\\x20+)?(type=(\"|')?text/css(\"|')?\\x20+)?href=(\"|')?([^'\"\\r\\n]+)(\"|')?");
        while (oMatcher.contains(oMatchInput, oPattern)) {
            nMatches++;
            String sMatch = oMatcher.getMatch().toString();
            String sSrc = oMatcher.getMatch().group(8);
            String sFilename = sSrc.substring(sSrc.replace('\\', '/').lastIndexOf('/') + 1);
            if (!oFiles.contains(sSrc))
                oFiles.add(sSrc);
            if (DebugFile.trace)
                DebugFile.writeln("StringSubstitution.setSubstitution(" + sMatch + " replace with "
                        + Gadgets.replace(sMatch, sSrc, sFilename) + ")");
            oSrcSubs.setSubstitution(Gadgets.replace(sMatch, sSrc, sFilename));
            sHtml = Util.substitute(oReplacer, oCompiler.compile(sMatch), oSrcSubs, sHtml);
        } // wend     

        if (DebugFile.trace) {
            DebugFile.writeln(String.valueOf(nMatches) + " matches found");
            DebugFile.write("\n" + sHtml + "\n");
        }

        ZipOutputStream oZOut = new ZipOutputStream(oOutStrm);
        String sLocalName = sFilePath.substring(sFilePath.replace('\\', '/').lastIndexOf('/') + 1);
        int iDot = sLocalName.lastIndexOf('.');
        if (iDot > 0)
            sLocalName = Gadgets.ASCIIEncode(sLocalName.substring(0, iDot)).toLowerCase() + ".html";
        else
            sLocalName = Gadgets.ASCIIEncode(sLocalName).toLowerCase();
        oEntries.add(sLocalName);
        if (DebugFile.trace)
            DebugFile.writeln("Putting entry " + sLocalName + " into ZIP");
        oZOut.putNextEntry(new ZipEntry(sLocalName));
        StringBufferInputStream oHtml = new StringBufferInputStream(sHtml);
        new StreamPipe().between(oHtml, oZOut);
        oHtml.close();
        oZOut.closeEntry();

        for (String sName : oFiles) {
            String sZipEntryName = sName.substring(sName.replace('\\', '/').lastIndexOf('/') + 1);
            if (!oEntries.contains(sZipEntryName)) {
                oEntries.add(sZipEntryName);
                if (DebugFile.trace)
                    DebugFile.writeln("Putting entry " + sZipEntryName + " into ZIP");
                oZOut.putNextEntry(new ZipEntry(sZipEntryName));
                if (sName.startsWith("http://") || sName.startsWith("https://") || sName.startsWith("file://")
                        || sBaseHref.length() > 0) {
                    try {
                        new StreamPipe().between(new ByteArrayInputStream(readfilebin(sBaseHref + sName)),
                                oZOut);
                    } catch (IOException ioe) {
                        if (DebugFile.trace) {
                            DebugFile.decIdent();
                            DebugFile.writeln("Could not download file " + sName);
                        }
                    }
                } else {
                    try {
                        byte[] aFile = readfilebin(
                                sBasePath + (sName.startsWith("/") ? sName.substring(1) : sName));
                        if (null != aFile) {
                            if (aFile.length > 0)
                                new StreamPipe().between(new ByteArrayInputStream(aFile), oZOut);
                        } else {
                            DebugFile.writeln("Could not find file " + sBasePath
                                    + (sName.startsWith("/") ? sName.substring(1) : sName));
                        }
                    } catch (IOException ioe) {
                        if (DebugFile.trace) {
                            DebugFile.decIdent();
                            DebugFile.writeln("Could not download file " + sBasePath
                                    + (sName.startsWith("/") ? sName.substring(1) : sName));
                        }
                    }
                }
                oZOut.closeEntry();
            } // fi (sName!=sLocalName)
        } // next
        oZOut.close();

    } catch (MalformedPatternException mpe) {

    } catch (FTPException ftpe) {

    }

    if (DebugFile.trace) {
        DebugFile.decIdent();
        DebugFile.writeln("End FileSystem.downloadhtmlpage()");
    }
}

From source file:net.spfbl.http.ServerHTTP.java

private static void buildQueryRow(Locale locale, StringBuilder builder, DateFormat dateFormat,
        GregorianCalendar calendar, long time, User.Query query, boolean highlight) {
    if (query != null) {
        calendar.setTimeInMillis(time);/*from   w  w  w.  j  a v a  2  s .co  m*/
        String ip = query.getIP();
        String hostname = query.getValidHostname();
        String sender = query.getSender();
        String from = query.getFrom();
        String replyto = query.getReplyTo();
        String subject = query.getSubject();
        String malware = query.getMalware();
        String recipient = query.getRecipient();
        String result = query.getResult();
        builder.append("        <tr id=\"");
        builder.append(time);
        builder.append("\"");
        if (highlight) {
            builder.append(" class=\"highlight\"");
        } else {
            builder.append(" class=\"click\"");
        }
        builder.append(" onclick=\"view('");
        builder.append(time);
        builder.append("')\">\n");
        if (locale.getLanguage().toLowerCase().equals("pt")) {
            builder.append("          <td style=\"width:120px;\">");
        } else {
            builder.append("          <td style=\"width:160px;\">");
        }
        builder.append(dateFormat.format(calendar.getTime()));
        builder.append("<br>");
        builder.append(query.getClient());
        builder.append("</td>\n");
        builder.append("          <td>");
        if (hostname == null) {
            String helo = query.getHELO();
            if (helo == null) {
                builder.append(ip);
            } else if (Subnet.isValidIP(helo)) {
                builder.append(ip);
            } else {
                builder.append(ip);
                builder.append("<br>");
                builder.append("<strike>");
                builder.append(helo);
                builder.append("</strike>");
            }
        } else if (Generic.containsDynamicDomain(hostname)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("<small><i>Dinmico</i></small>");
            } else {
                builder.append("<small><i>Dynamic</i></small>");
            }
            builder.append("<br>");
            builder.append(hostname);
        } else if (Generic.containsGenericDomain(hostname)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("<small><i>Genrico</i></small>");
            } else {
                builder.append("<small><i>Generic</i></small>");
            }
            builder.append("<br>");
            builder.append(hostname);
        } else if (Provider.containsDomain(hostname)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("<small><i>Provedor</i></small>");
            } else {
                builder.append("<small><i>Provider</i></small>");
            }
            builder.append("<br>");
            builder.append(hostname);
        } else {
            builder.append(hostname);
        }
        builder.append("</td>\n");
        TreeSet<String> senderSet = new TreeSet<String>();
        builder.append("          <td>");
        if (sender == null) {
            builder.append("MAILER-DAEMON");
        } else {
            senderSet.add(sender);
            String qualifier = query.getQualifierName();
            if (qualifier.equals("PASS")) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append("<small><i>Comprovadamente autntico</i></small>");
                } else {
                    builder.append("<small><i>Proved genuine</i></small>");
                }
            } else if (qualifier.equals("FAIL")) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append("<small><i>Comprovadamente falso</i></small>");
                } else {
                    builder.append("<small><i>Proved false</i></small>");
                }
            } else if (qualifier.equals("SOFTFAIL")) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append("<small><i>Pode ser falso</i></small>");
                } else {
                    builder.append("<small><i>May be false</i></small>");
                }
            } else {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append("<small><i>Pode ser autntico</i></small>");
                } else {
                    builder.append("<small><i>May be genuine</i></small>");
                }
            }
            builder.append("<br>");
            builder.append(sender);
        }
        boolean lineSeparator = false;
        if (from != null && !senderSet.contains(from)) {
            senderSet.add(from);
            builder.append("<hr style=\"height:0px;visibility:hidden;margin-bottom:0px;\">");
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("<small><b>De:</b> ");
            } else {
                builder.append("<small><b>From:</b> ");
            }
            builder.append(from);
            builder.append("</small>");
            lineSeparator = true;
        }
        if (replyto != null && !senderSet.contains(replyto)) {
            senderSet.add(replyto);
            if (lineSeparator) {
                builder.append("<br>");
            } else {
                builder.append("<hr style=\"height:0px;visibility:hidden;margin-bottom:0px;\">");
            }
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("<small><b>Responder para:</b> ");
            } else {
                builder.append("<small><b>Reply to:</b> ");
            }
            builder.append(replyto);
            builder.append("</small>");
        }
        builder.append("</td>\n");
        builder.append("          <td>");
        if (subject != null) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("<small><b>Assunto:</b> ");
            } else {
                builder.append("<small><b>Subject:</b> ");
            }
            builder.append(subject);
            builder.append("</small>");
            builder.append("<hr style=\"height:0px;visibility:hidden;margin-bottom:0px;\">");
        }
        if (malware == null) {
            TreeSet<String> linkSet = query.getLinkSet();
            if (linkSet == null) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append("<small><i>Corpo no verificado</i></small>");
                } else {
                    builder.append("<small><i>Body not verified</i></small>");
                }
            } else if (linkSet.isEmpty()) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    builder.append("<small><i>Sem links</i></small>");
                } else {
                    builder.append("<small><i>No links</i></small>");
                }
            } else {
                String link = linkSet.pollFirst();
                if (query.isLinkBlocked(link)) {
                    builder.append("<font color=\"DarkRed\"><b>");
                    builder.append(link);
                    builder.append("</b></font>");
                } else {
                    builder.append(link);
                }
                while (!linkSet.isEmpty()) {
                    builder.append("<br>");
                    link = linkSet.pollFirst();
                    if (query.isLinkBlocked(link)) {
                        builder.append("<font color=\"DarkRed\"><b>");
                        builder.append(link);
                        builder.append("</b></font>");
                    } else {
                        builder.append(link);
                    }
                }
            }
        } else {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("<small><i>Malware encontrado</i></small>");
            } else {
                builder.append("<small><i>Malware found</i></small>");
            }
            if (!malware.equals("FOUND")) {
                builder.append("<br>");
                builder.append("<font color=\"DarkRed\"><b>");
                builder.append(malware);
                builder.append("</b></font>");
            }
        }
        builder.append("</td>\n");
        builder.append("          <td>");
        if (result.equals("REJECT")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Rejeitada pelo contedo");
            } else {
                builder.append("Rejected by content");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("BLOCK") || result.equals("BLOCKED")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Rejeitada por bloqueio");
            } else {
                builder.append("Rejected by blocking");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("FAIL") || result.equals("FAILED")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Rejeitada por falsidade");
            } else {
                builder.append("Rejected by falseness");
            }
        } else if (result.equals("INVALID")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Rejeitada por origem invlida");
            } else {
                builder.append("Rejected by invalid source");
            }
        } else if (result.equals("GREYLIST")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Atrasada por greylisting");
            } else {
                builder.append("Delayed by greylisting");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("SPAMTRAP") || result.equals("TRAP")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Descartado pela armadilha");
            } else {
                builder.append("Discarded by spamtrap");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("INEXISTENT")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Rejeitada por inexistncia");
            } else {
                builder.append("Rejected by non-existence");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("WHITE")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Entrega prioritria");
            } else {
                builder.append("Priority delivery");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("ACCEPT")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Entrega aceita");
            } else {
                builder.append("Accepted for delivery");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("FLAG")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Marcada como suspeita");
            } else {
                builder.append("Marked as suspect");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("HOLD")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Entrega retida");
            } else {
                builder.append("Delivery retained");
            }
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        } else if (result.equals("NXDOMAIN")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Rejeitada por domnio inexistente");
            } else {
                builder.append("Rejected by non-existent domain");
            }
        } else if (result.equals("NXSENDER")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                builder.append("Rejeitada por remetente inexistente");
            } else {
                builder.append("Rejected by non-existent sender");
            }
        } else {
            builder.append(result);
            if (recipient != null) {
                builder.append("<br>");
                builder.append(recipient);
            }
        }
        builder.append("</td>\n");
        builder.append("        </tr>\n");
    }
}

From source file:org.dasein.cloud.aws.compute.EC2Instance.java

@Override
public Iterable<VirtualMachineProduct> listProducts(VirtualMachineProductFilterOptions options,
        Architecture architecture) throws InternalException, CloudException {
    ProviderContext ctx = getProvider().getContext();
    if (ctx == null) {
        throw new CloudException("No context was set for this request");
    }/*  w  w  w . j a v  a2 s  .  com*/
    // FIXME: until core fixes the annotation for architecture let's assume it's nullable
    String cacheName = "productsALL";
    if (architecture != null) {
        cacheName = "products" + architecture.name();
    }
    Cache<VirtualMachineProduct> cache = Cache.getInstance(getProvider(), cacheName,
            VirtualMachineProduct.class, CacheLevel.REGION, new TimePeriod<Day>(1, TimePeriod.DAY));
    Iterable<VirtualMachineProduct> products = cache.get(ctx);

    if (products == null) {
        List<VirtualMachineProduct> list = new ArrayList<VirtualMachineProduct>();

        try {
            InputStream input = EC2Instance.class.getResourceAsStream("/org/dasein/cloud/aws/vmproducts.json");

            if (input != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder json = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    json.append(line);
                    json.append("\n");
                }
                JSONArray arr = new JSONArray(json.toString());
                JSONObject toCache = null;

                for (int i = 0; i < arr.length(); i++) {
                    JSONObject productSet = arr.getJSONObject(i);
                    String cloud, providerName;

                    if (productSet.has("cloud")) {
                        cloud = productSet.getString("cloud");
                    } else {
                        continue;
                    }
                    if (productSet.has("provider")) {
                        providerName = productSet.getString("provider");
                    } else {
                        continue;
                    }
                    if (!productSet.has("products")) {
                        continue;
                    }
                    if (toCache == null || (providerName.equals("AWS") && cloud.equals("AWS"))) {
                        toCache = productSet;
                    }
                    if (providerName.equalsIgnoreCase(getProvider().getProviderName())
                            && cloud.equalsIgnoreCase(getProvider().getCloudName())) {
                        toCache = productSet;
                        break;
                    }
                }
                if (toCache == null) {
                    logger.warn("No products were defined");
                    return Collections.emptyList();
                }
                JSONArray plist = toCache.getJSONArray("products");

                for (int i = 0; i < plist.length(); i++) {
                    JSONObject product = plist.getJSONObject(i);
                    boolean supported = false;

                    if (architecture != null) {
                        if (product.has("architectures")) {
                            JSONArray architectures = product.getJSONArray("architectures");

                            for (int j = 0; j < architectures.length(); j++) {
                                String a = architectures.getString(j);

                                if (architecture.name().equals(a)) {
                                    supported = true;
                                    break;
                                }
                            }
                        }
                        if (!supported) {
                            continue;
                        }
                    }
                    if (product.has("excludesRegions")) {
                        JSONArray regions = product.getJSONArray("excludesRegions");

                        for (int j = 0; j < regions.length(); j++) {
                            String r = regions.getString(j);

                            if (r.equals(ctx.getRegionId())) {
                                supported = false;
                                break;
                            }
                        }
                    }
                    if (!supported) {
                        continue;
                    }
                    VirtualMachineProduct prd = toProduct(product);

                    if (prd != null) {
                        if (options != null) {
                            if (options.matches(prd)) {
                                list.add(prd);
                            }
                        } else {
                            list.add(prd);
                        }
                    }

                }

            } else {
                logger.warn("No standard products resource exists for /org/dasein/cloud/aws/vmproducts.json");
            }
            input = EC2Instance.class.getResourceAsStream("/org/dasein/cloud/aws/vmproducts-custom.json");
            if (input != null) {
                ArrayList<VirtualMachineProduct> customList = new ArrayList<VirtualMachineProduct>();
                TreeSet<String> discard = new TreeSet<String>();
                boolean discardAll = false;

                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder json = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    json.append(line);
                    json.append("\n");
                }
                JSONArray arr = new JSONArray(json.toString());
                JSONObject toCache = null;

                for (int i = 0; i < arr.length(); i++) {
                    JSONObject listing = arr.getJSONObject(i);
                    String cloud, providerName, endpoint = null;

                    if (listing.has("cloud")) {
                        cloud = listing.getString("cloud");
                    } else {
                        continue;
                    }
                    if (listing.has("provider")) {
                        providerName = listing.getString("provider");
                    } else {
                        continue;
                    }
                    if (listing.has("endpoint")) {
                        endpoint = listing.getString("endpoint");
                    }
                    if (!cloud.equals(getProvider().getCloudName())
                            || !providerName.equals(getProvider().getProviderName())) {
                        continue;
                    }
                    if (endpoint != null && endpoint.equals(ctx.getCloud().getEndpoint())) {
                        toCache = listing;
                        break;
                    }
                    if (endpoint == null && toCache == null) {
                        toCache = listing;
                    }
                }
                if (toCache != null) {
                    if (toCache.has("discardDefaults")) {
                        discardAll = toCache.getBoolean("discardDefaults");
                    }
                    if (toCache.has("discard")) {
                        JSONArray dlist = toCache.getJSONArray("discard");

                        for (int i = 0; i < dlist.length(); i++) {
                            discard.add(dlist.getString(i));
                        }
                    }
                    if (toCache.has("products")) {
                        JSONArray plist = toCache.getJSONArray("products");

                        for (int i = 0; i < plist.length(); i++) {
                            JSONObject product = plist.getJSONObject(i);
                            boolean supported = false;
                            if (architecture != null) {

                                if (product.has("architectures")) {
                                    JSONArray architectures = product.getJSONArray("architectures");

                                    for (int j = 0; j < architectures.length(); j++) {
                                        String a = architectures.getString(j);

                                        if (architecture.name().equals(a)) {
                                            supported = true;
                                            break;
                                        }
                                    }
                                }
                                if (!supported) {
                                    continue;
                                }
                            }
                            if (product.has("excludesRegions")) {
                                JSONArray regions = product.getJSONArray("excludesRegions");

                                for (int j = 0; j < regions.length(); j++) {
                                    String r = regions.getString(j);

                                    if (r.equals(ctx.getRegionId())) {
                                        supported = false;
                                        break;
                                    }
                                }
                            }
                            if (!supported) {
                                continue;
                            }
                            VirtualMachineProduct prd = toProduct(product);

                            if (prd != null) {
                                customList.add(prd);
                            }
                        }
                    }
                    if (!discardAll) {
                        for (VirtualMachineProduct product : list) {
                            if (!discard.contains(product.getProviderProductId())) {
                                customList.add(product);
                            }
                        }
                    }
                    list = customList;
                }
            }
            products = list;
            cache.put(ctx, products);

        } catch (IOException e) {
            throw new InternalException(e);
        } catch (JSONException e) {
            throw new InternalException(e);
        }
    }
    return products;
}