Example usage for java.util TreeSet pollFirst

List of usage examples for java.util TreeSet pollFirst

Introduction

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

Prototype

public E pollFirst() 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

    TreeSet<Integer> treeadd = new TreeSet<Integer>();

    treeadd.add(1);//ww  w  . java 2s.  c  o m
    treeadd.add(3);
    treeadd.add(17);
    treeadd.add(2);

    System.out.println("First element is: " + treeadd.pollFirst());

    Iterator<Integer> iterator = treeadd.iterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

From source file:it.ozimov.springboot.templating.mail.service.PriorityQueueSchedulerService.java

private synchronized Optional<EmailSchedulingWrapper> dequeue() throws InterruptedException {
    EmailSchedulingWrapper emailSchedulingWrapper = null;
    timeOfNextScheduledMessage = null;/*w  w w.  j a  v  a  2  s.c o m*/
    while (consumer.enabled() && isNull(emailSchedulingWrapper)) {
        //try to find a message in queue
        final long now = TimeUtils.now();
        for (final TreeSet<EmailSchedulingWrapper> queue : queues) {
            if (!queue.isEmpty()) {
                final long time = queue.first().getScheduledDateTime().toInstant().toEpochMilli();
                if (time - now <= DELTA) {
                    //message found!
                    emailSchedulingWrapper = queue.pollFirst();
                    break;
                } else if (isNull(timeOfNextScheduledMessage) || time < timeOfNextScheduledMessage) {
                    timeOfNextScheduledMessage = time;
                }

            }
        }
        if (isNull(emailSchedulingWrapper)) {
            //no message was found, let's sleep, some message may arrive in the meanwhile
            if (isNull(timeOfNextScheduledMessage)) { //all the queues are empty
                wait(); //the consumer starts waiting for a new email to be scheduled
            } else {
                final long waitTime = timeOfNextScheduledMessage - TimeUtils.now() - DELTA;
                if (waitTime > 0) {
                    wait(waitTime); //wait before sending the most imminent scheduled email
                }
            }
        }
    }
    //here emailSchedulingWrapper is the message to send
    return Optional.ofNullable(emailSchedulingWrapper);
}

From source file:bes.injector.InjectorBurnTest.java

private void testPromptnessOfExecution(long intervalNanos, float loadIncrement)
        throws InterruptedException, ExecutionException, TimeoutException {
    final int executorCount = 4;
    int threadCount = 8;
    int maxQueued = 1024;
    final WeibullDistribution workTime = new WeibullDistribution(3, 200000);
    final long minWorkTime = TimeUnit.MICROSECONDS.toNanos(1);
    final long maxWorkTime = TimeUnit.MILLISECONDS.toNanos(1);

    final int[] threadCounts = new int[executorCount];
    final WeibullDistribution[] workCount = new WeibullDistribution[executorCount];
    final ExecutorService[] executors = new ExecutorService[executorCount];
    final Injector injector = new Injector("");
    for (int i = 0; i < executors.length; i++) {
        executors[i] = injector.newExecutor(threadCount, maxQueued);
        threadCounts[i] = threadCount;//www . ja va 2  s.  c o  m
        workCount[i] = new WeibullDistribution(2, maxQueued);
        threadCount *= 2;
        maxQueued *= 2;
    }

    long runs = 0;
    long events = 0;
    final TreeSet<Batch> pending = new TreeSet<Batch>();
    final BitSet executorsWithWork = new BitSet(executorCount);
    long until = 0;
    // basic idea is to go through different levels of load on the executor service; initially is all small batches
    // (mostly within max queue size) of very short operations, moving to progressively larger batches
    // (beyond max queued size), and longer operations
    for (float multiplier = 0f; multiplier < 2.01f;) {
        if (System.nanoTime() > until) {
            System.out.println(String.format("Completed %.0fK batches with %.1fM events", runs * 0.001f,
                    events * 0.000001f));
            events = 0;
            until = System.nanoTime() + intervalNanos;
            multiplier += loadIncrement;
            System.out.println(String.format("Running for %ds with load multiplier %.1f",
                    TimeUnit.NANOSECONDS.toSeconds(intervalNanos), multiplier));
        }

        // wait a random amount of time so we submit new tasks in various stages of
        long timeout;
        if (pending.isEmpty())
            timeout = 0;
        else if (Math.random() > 0.98)
            timeout = Long.MAX_VALUE;
        else if (pending.size() == executorCount)
            timeout = pending.first().timeout;
        else
            timeout = (long) (Math.random() * pending.last().timeout);

        while (!pending.isEmpty() && timeout > System.nanoTime()) {
            Batch first = pending.first();
            boolean complete = false;
            try {
                for (Result result : first.results.descendingSet())
                    result.future.get(timeout - System.nanoTime(), TimeUnit.NANOSECONDS);
                complete = true;
            } catch (TimeoutException e) {
            }
            if (!complete && System.nanoTime() > first.timeout) {
                for (Result result : first.results)
                    if (!result.future.isDone())
                        throw new AssertionError();
                complete = true;
            }
            if (complete) {
                pending.pollFirst();
                executorsWithWork.clear(first.executorIndex);
            }
        }

        // if we've emptied the executors, give all our threads an opportunity to spin down
        if (timeout == Long.MAX_VALUE) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        }

        // submit a random batch to the first free executor service
        int executorIndex = executorsWithWork.nextClearBit(0);
        if (executorIndex >= executorCount)
            continue;
        executorsWithWork.set(executorIndex);
        ExecutorService executor = executors[executorIndex];
        TreeSet<Result> results = new TreeSet<Result>();
        int count = (int) (workCount[executorIndex].sample() * multiplier);
        long targetTotalElapsed = 0;
        long start = System.nanoTime();
        long baseTime;
        if (Math.random() > 0.5)
            baseTime = 2 * (long) (workTime.sample() * multiplier);
        else
            baseTime = 0;
        for (int j = 0; j < count; j++) {
            long time;
            if (baseTime == 0)
                time = (long) (workTime.sample() * multiplier);
            else
                time = (long) (baseTime * Math.random());
            if (time < minWorkTime)
                time = minWorkTime;
            if (time > maxWorkTime)
                time = maxWorkTime;
            targetTotalElapsed += time;
            Future<?> future = executor.submit(new WaitTask(time));
            results.add(new Result(future, System.nanoTime() + time));
        }
        long end = start + (long) Math.ceil(targetTotalElapsed / (double) threadCounts[executorIndex])
                + TimeUnit.MILLISECONDS.toNanos(100L);
        long now = System.nanoTime();
        if (runs++ > executorCount && now > end)
            throw new AssertionError();
        events += results.size();
        pending.add(new Batch(results, end, executorIndex));
        //            System.out.println(String.format("Submitted batch to executor %d with %d items and %d permitted millis", executorIndex, count, TimeUnit.NANOSECONDS.toMillis(end - start)));
    }
}

From source file:com.joliciel.talismane.parser.TransitionBasedGlobalLearningParser.java

public List<ParseConfiguration> parseSentence(List<PosTagSequence> posTagSequences,
        FeatureWeightVector weightVector, RankingSolution correctSolution) {
    MONITOR.startTask("parseSentence");
    try {//from w  w  w .  j  a  va2s .c om
        long startTime = (new Date()).getTime();
        int maxAnalysisTimeMilliseconds = maxAnalysisTimePerSentence * 1000;
        int minFreeMemoryBytes = minFreeMemory * KILOBYTE;

        TokenSequence tokenSequence = posTagSequences.get(0).getTokenSequence();

        TreeMap<Integer, TreeSet<ParseConfiguration>> heaps = new TreeMap<Integer, TreeSet<ParseConfiguration>>();

        TreeSet<ParseConfiguration> heap0 = new TreeSet<ParseConfiguration>();
        for (PosTagSequence posTagSequence : posTagSequences) {
            // add an initial ParseConfiguration for each postag sequence
            ParseConfiguration initialConfiguration = this.getParserServiceInternal()
                    .getInitialConfiguration(posTagSequence);
            initialConfiguration.setScoringStrategy(new SimpleRankingScoringStrategy());
            initialConfiguration.setRankingScore(0.0);
            heap0.add(initialConfiguration);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding initial posTagSequence: " + posTagSequence);
            }
        }
        heaps.put(0, heap0);
        TreeSet<ParseConfiguration> backupHeap = null;

        TreeSet<ParseConfiguration> finalHeap = null;
        while (heaps.size() > 0) {
            Entry<Integer, TreeSet<ParseConfiguration>> heapEntry = heaps.firstEntry();
            TreeSet<ParseConfiguration> currentHeap = heapEntry.getValue();
            int currentHeapIndex = heapEntry.getKey();
            if (LOG.isTraceEnabled()) {
                LOG.trace("##### Polling next heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
            }

            boolean finished = false;
            // systematically set the final heap here, just in case we exit "naturally" with no more heaps
            finalHeap = heapEntry.getValue();
            backupHeap = new TreeSet<ParseConfiguration>();

            // we jump out when either (a) all tokens have been attached or (b) we go over the max alloted time
            ParseConfiguration topConf = currentHeap.first();
            if (topConf.isTerminal()) {
                LOG.trace("Exiting with terminal heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
                finished = true;
            }

            // check if we've gone over alloted time for this sentence
            long analysisTime = (new Date()).getTime() - startTime;
            if (maxAnalysisTimePerSentence > 0 && analysisTime > maxAnalysisTimeMilliseconds) {
                LOG.info("Parse tree analysis took too long for sentence: " + tokenSequence.getText());
                LOG.info("Breaking out after " + maxAnalysisTimePerSentence + " seconds.");
                finished = true;
            }

            // check if we've enough memory to process this sentence
            if (minFreeMemory > 0) {
                long freeMemory = Runtime.getRuntime().freeMemory();
                if (freeMemory < minFreeMemoryBytes) {
                    LOG.info("Not enough memory left to parse sentence: " + tokenSequence.getText());
                    LOG.info("Min free memory (bytes):" + minFreeMemoryBytes);
                    LOG.info("Current free memory (bytes): " + freeMemory);
                    finished = true;
                }
            }

            // check if any of the remaining top-N solutions on any heap can lead to the correct solution
            if (correctSolution != null) {
                boolean canReachCorrectSolution = false;
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    int j = 1;
                    for (ParseConfiguration solution : heap) {
                        if (j > beamWidth)
                            break;
                        if (solution.canReach(correctSolution)) {
                            canReachCorrectSolution = true;
                            break;
                        }
                        j++;
                    }
                    if (canReachCorrectSolution)
                        break;
                }
                if (!canReachCorrectSolution) {
                    LOG.debug("None of the solutions on the heap can reach the gold solution. Exiting.");
                    finished = true;
                }
            }

            if (finished) {
                // combine any remaining heaps
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    if (finalHeap != heap) {
                        finalHeap.addAll(heap);
                    }
                }
                break;
            }

            // remove heap from set of heaps
            heapEntry = heaps.pollFirstEntry();

            // limit the breadth to K
            int maxSolutions = currentHeap.size() > this.beamWidth ? this.beamWidth : currentHeap.size();

            int j = 0;
            while (currentHeap.size() > 0) {
                ParseConfiguration history = currentHeap.pollFirst();
                backupHeap.add(history);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("### Next configuration on heap " + heapEntry.getKey() + ":");
                    LOG.trace(history.toString());
                    LOG.trace("Score: " + df.format(history.getScore()));
                    LOG.trace(history.getPosTagSequence());
                }

                Set<Transition> transitions = new HashSet<Transition>();

                // test the positive rules on the current configuration
                boolean ruleApplied = false;
                if (parserPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (ParserRule rule : parserPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.getCondition().getName());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                transitions.add(rule.getTransition());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting transition to: "
                                            + rule.getTransition().getCode());
                                }

                                if (!rule.getTransition().checkPreconditions(history)) {
                                    LOG.error("Cannot apply rule, preconditions not met.");
                                    ruleApplied = false;
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    transitions = parsingConstrainer.getPossibleTransitions(history);

                    Set<Transition> eliminatedTransitions = new HashSet<Transition>();
                    for (Transition transition : transitions) {
                        if (!transition.checkPreconditions(history)) {
                            eliminatedTransitions.add(transition);
                        }
                    }
                    transitions.removeAll(eliminatedTransitions);

                    // apply the negative rules
                    eliminatedTransitions = new HashSet<Transition>();
                    if (parserNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (ParserRule rule : parserNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.getCondition().getName());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedTransitions.add(rule.getTransition());
                                    if (LOG.isTraceEnabled()) {
                                        LOG.debug("Rule applies. Eliminating transition: "
                                                + rule.getTransition().getCode());
                                    }
                                }
                            }

                            if (eliminatedTransitions.size() == transitions.size()) {
                                LOG.debug("All transitions eliminated! Restoring original transitions.");
                            } else {
                                transitions.removeAll(eliminatedTransitions);
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }
                } // has a positive rule been applied?

                if (transitions.size() == 0) {
                    // just in case the we run out of both heaps and analyses, we build this backup heap
                    backupHeap.add(history);
                    if (LOG.isTraceEnabled())
                        LOG.trace(
                                "No transitions could be applied: not counting this solution as part of the beam");
                } else {
                    // up the counter, since we will count this solution towards the heap
                    j++;
                    // add solutions to the heap, one per valid transition
                    MONITOR.startTask("heap sort");
                    try {
                        Map<Transition, Double> deltaScorePerTransition = new HashMap<Transition, Double>();
                        double absoluteMax = 1;

                        for (Transition transition : transitions) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Applying transition: " + transition.getCode());
                            }
                            ParseConfiguration configuration = this.parserServiceInternal
                                    .getConfiguration(history);
                            transition.apply(configuration);
                            configuration.setRankingScore(history.getRankingScore());
                            configuration.getIncrementalFeatureResults()
                                    .addAll(history.getIncrementalFeatureResults());

                            // test the features on the new configuration
                            double scoreDelta = 0.0;
                            MONITOR.startTask("feature analyse");
                            List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
                            try {
                                for (ParseConfigurationFeature<?> feature : this.parseFeatures) {
                                    MONITOR.startTask(feature.getName());
                                    try {
                                        RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                        FeatureResult<?> featureResult = feature.check(configuration, env);
                                        if (featureResult != null) {
                                            featureResults.add(featureResult);
                                            double weight = weightVector.getWeight(featureResult);
                                            scoreDelta += weight;
                                            if (LOG.isTraceEnabled()) {
                                                LOG.trace(featureResult.toString() + " = " + weight);
                                            }
                                        }
                                    } finally {
                                        MONITOR.endTask(feature.getName());
                                    }
                                }
                                configuration.getIncrementalFeatureResults().add(featureResults);
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Score = " + configuration.getRankingScore() + " + " + scoreDelta
                                            + " = " + (configuration.getRankingScore() + scoreDelta));
                                }
                                configuration.setRankingScore(configuration.getRankingScore() + scoreDelta);
                                deltaScorePerTransition.put(transition, scoreDelta);
                                if (Math.abs(scoreDelta) > absoluteMax)
                                    absoluteMax = Math.abs(scoreDelta);

                            } finally {
                                MONITOR.endTask("feature analyse");
                            }

                            int nextHeapIndex = parseComparisonStrategy.getComparisonIndex(configuration)
                                    * 1000;
                            while (nextHeapIndex <= currentHeapIndex)
                                nextHeapIndex++;

                            TreeSet<ParseConfiguration> nextHeap = heaps.get(nextHeapIndex);
                            if (nextHeap == null) {
                                nextHeap = new TreeSet<ParseConfiguration>();
                                heaps.put(nextHeapIndex, nextHeap);
                                if (LOG.isTraceEnabled())
                                    LOG.trace("Created heap with index: " + nextHeapIndex);
                            }
                            nextHeap.add(configuration);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Added configuration with score " + configuration.getScore()
                                        + " to heap: " + nextHeapIndex + ", total size: " + nextHeap.size());
                            }

                            configuration.clearMemory();
                        } // next transition

                        // Create a probability distribution of transitions
                        // normalise probabilities for each transition via normalised exponential
                        // e^(x/absmax)/sum(e^(x/absmax))
                        // where x/absmax is in [-1,1]
                        // e^(x/absmax) is in [1/e,e]

                        double total = 0.0;
                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double deltaScore = deltaScorePerTransition.get(transition);
                            deltaScore = Math.exp(deltaScore / absoluteMax);
                            deltaScorePerTransition.put(transition, deltaScore);
                            total += deltaScore;
                        }

                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double probability = deltaScorePerTransition.get(transition);
                            probability /= total;
                            Decision<Transition> decision = machineLearningService.createDecision(transition,
                                    probability);
                            transition.setDecision(decision);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Transition: " + transition.getCode() + ", Prob: " + probability);
                            }
                        }

                    } finally {
                        MONITOR.endTask("heap sort");
                    }
                } // have we any transitions?

                // beam width test
                if (j == maxSolutions)
                    break;
            } // next history   
        } // next atomic index

        // return the best sequences on the heap
        List<ParseConfiguration> bestConfigurations = new ArrayList<ParseConfiguration>();
        int i = 0;

        if (finalHeap.isEmpty())
            finalHeap = backupHeap;

        while (!finalHeap.isEmpty()) {
            bestConfigurations.add(finalHeap.pollFirst());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }
        if (LOG.isDebugEnabled()) {
            if (correctSolution != null) {
                LOG.debug("Gold transitions: " + correctSolution.getIncrementalOutcomes());
            }
            for (ParseConfiguration finalConfiguration : bestConfigurations) {
                LOG.debug(df.format(finalConfiguration.getScore()) + ": " + finalConfiguration.toString());
                LOG.debug("Pos tag sequence: " + finalConfiguration.getPosTagSequence());
                LOG.debug("Transitions: " + finalConfiguration.getTransitions());
                if (LOG.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(" * PosTag sequence score ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getScore()));
                    sb.append(" = ");
                    for (PosTaggedToken posTaggedToken : finalConfiguration.getPosTagSequence()) {
                        sb.append(" * ");
                        sb.append(df.format(posTaggedToken.getDecision().getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getPosTagSequence().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * Token sequence score = ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getTokenSequence().getScore()));
                    LOG.trace(sb.toString());

                }
            }
        }
        return bestConfigurations;
    } finally {
        MONITOR.endTask("parseSentence");
    }
}

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

private static TreeSet<String> getPostmaterSet(String query) {
    TreeSet<String> emailSet = new TreeSet<String>();
    if (Subnet.isValidIP(query)) {
        String ip = Subnet.normalizeIP(query);
        Reverse reverse = Reverse.get(ip);
        TreeSet<String> reverseSet = reverse.getAddressSet();
        if (!reverseSet.isEmpty()) {
            String hostname = reverseSet.pollFirst();
            do {//from   w  ww . ja v a2 s  .c o  m
                hostname = Domain.normalizeHostname(hostname, true);
                if (!hostname.endsWith(".arpa")) {
                    String domain;
                    try {
                        domain = Domain.extractDomain(hostname, true);
                    } catch (ProcessException ex) {
                        domain = null;
                    }
                    if (domain != null) {
                        String email = "postmaster@" + domain.substring(1);
                        if (!NoReply.contains(email, true)) {
                            emailSet.add(email);
                        }
                        if (!Generic.containsGeneric(hostname) && SPF.matchHELO(ip, hostname)) {
                            String subdominio = hostname;
                            while (!subdominio.equals(domain)) {
                                email = "postmaster@" + subdominio.substring(1);
                                if (!NoReply.contains(email, true)) {
                                    emailSet.add(email);
                                }
                                int index = subdominio.indexOf('.', 1);
                                subdominio = subdominio.substring(index);
                            }
                        }
                    }
                }
            } while ((hostname = reverseSet.pollFirst()) != null);
        }
    } else if (Domain.isHostname(query)) {
        String hostname = Domain.normalizeHostname(query, false);
        String domain;
        try {
            domain = Domain.extractDomain(hostname, false);
        } catch (ProcessException ex) {
            domain = null;
        }
        if (domain != null) {
            String subdominio = hostname;
            while (subdominio.endsWith(domain)) {
                String email = "postmaster@" + subdominio;
                if (!NoReply.contains(email, true)) {
                    emailSet.add(email);
                }
                int index = subdominio.indexOf('.', 1) + 1;
                subdominio = subdominio.substring(index);
            }
        }
    }
    return emailSet;
}

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

private static String getDNSBLHTML(Locale locale, Client client, final String query, String message) {
    StringBuilder builder = new StringBuilder();
    boolean isSLAAC = SubnetIPv6.isSLAAC(query) && !Subnet.isReservedIP(query);
    Boolean isOpenSMTP = openSMTP.get(query);
    builder.append("<!DOCTYPE html>\n");
    builder.append("<html lang=\"");
    builder.append(locale.getLanguage());
    builder.append("\">\n");
    String title;//from  w w  w  .  ja  v a  2s. c om
    if (locale.getLanguage().toLowerCase().equals("pt")) {
        title = "Pgina de checagem DNSBL";
    } else {
        title = "DNSBL check page";
    }
    if (isSLAAC && isOpenSMTP == null) {
        if (openSMTP.containsKey(query)) {
            buildHead(builder, title, Core.getURL(locale, query), 5);
        } else {
            buildHead(builder, title, Core.getURL(locale, query), 10);
            openSMTP.put(query, null);
            new Thread() {
                @Override
                public void run() {
                    Thread.currentThread().setName("BCKGROUND");
                    openSMTP.put(query, Analise.isOpenSMTP(query, 30000));
                }
            }.start();
        }
    } else {
        buildHead(builder, title);
    }
    builder.append("  <body>\n");
    builder.append("    <div id=\"container\">\n");
    builder.append(
            "      <iframe data-aa='455818' src='//ad.a-ads.com/455818?size=468x60' scrolling='no' style='width:468px; height:60px; border:0px; padding:0;overflow:hidden' allowtransparency='true'></iframe>");
    buildMessage(builder, message);
    TreeSet<String> emailSet = new TreeSet<String>();
    if (Subnet.isValidIP(query)) {
        String ip = Subnet.normalizeIP(query);
        if (Subnet.isReservedIP(ip)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder, "Este  um IP reservado e por este motivo no  abordado nesta lista.");
            } else {
                buildText(builder, "This is a reserved IP and for this reason is not addressed in this list.");
            }
        } else if (isSLAAC && isOpenSMTP == null) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este IP contm sinalizao de autoconfigurao de endereo de rede (SLAAC).");
                buildText(builder, "Estamos verificando se existe servio SMTP neste IP. Aguarde...");
            } else {
                buildText(builder, "This IP contains network address autoconfiguration flag (SLAAC).");
                buildText(builder, "We are checking if there is SMTP service on this IP. Wait...");
            }
        } else if (isSLAAC && !isOpenSMTP) {
            openSMTP.remove(query);
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este IP contm sinalizao de autoconfigurao de endereo de rede (SLAAC) mas no foi possvel verificar se existe um servio de e-mail vlido nele.");
                buildText(builder,
                        "Se este IP est sendo usado por um servidor de e-mail legtimo, abra a porta 25 para que possamos verificar que existe um servio SMTP nele.");
            } else {
                buildText(builder,
                        "This IP contains network address autoconfiguration flag (SLAAC) but it was not possible to verify that there is a valid email service on it.");
                buildText(builder,
                        "If this IP is being used by genuine email server, open port 25 so we can check that there is a SMTP service on it.");
            }
        } else {
            openSMTP.remove(query);
            boolean generic = false;
            Reverse reverse = Reverse.get(ip, true);
            TreeSet<String> reverseSet = reverse.getAddressSet();
            if (reverseSet.isEmpty()) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Nenhum <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> foi encontrado.");
                } else {
                    buildText(builder,
                            "No <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> was found.");
                }
            } else {
                if (reverseSet.size() == 1) {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Este  o <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> encontrado:");
                    } else {
                        buildText(builder,
                                "This is the <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> found:");
                    }
                } else {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Estes so os <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> encontrados:");
                    } else {
                        buildText(builder,
                                "These are the <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> found:");
                    }
                }
                Client abuseClient = Client.getByIP(ip);
                String abuseEmail = abuseClient == null || abuseClient.hasPermission(NONE) ? null
                        : abuseClient.getEmail();
                String clientEmail = client == null || client.hasPermission(NONE) ? null : client.getEmail();
                builder.append("        <ul>\n");
                String hostname = reverseSet.pollFirst();
                do {
                    hostname = Domain.normalizeHostname(hostname, false);
                    builder.append("          <li>&lt;");
                    builder.append(hostname);
                    builder.append("&gt; ");
                    if (Generic.containsDynamic(hostname)) {
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("<a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> ");
                            builder.append(
                                    "de <a target=\"_blank\" href=\"http://spfbl.net/dynamic/\">IP dinmico</a>.</li>\n");
                        } else {
                            builder.append(
                                    "<a target=\"_blank\" href=\"http://spfbl.net/en/dynamic/\">dynamic IP</a> ");
                            builder.append(
                                    "<a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a>.</li>\n");
                        }
                    } else if (SPF.matchHELO(ip, hostname, true)) {
                        String domain;
                        try {
                            domain = Domain.extractDomain(hostname, false);
                        } catch (ProcessException ex) {
                            domain = null;
                        }
                        if (domain == null) {
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append("domnio reservado.</li>\n");
                            } else {
                                builder.append("reserved domain.</li>\n");
                            }
                        } else if (hostname.endsWith(".arpa")) {
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append("domnio reservado.</li>\n");
                            } else {
                                builder.append("reserved domain.</li>\n");
                            }
                        } else if (Generic.containsGeneric(domain)) {
                            if (abuseEmail != null) {
                                emailSet.add(abuseEmail);
                            }
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append("domnio genrico.</li>\n");
                            } else {
                                builder.append("generic domain.</li>\n");
                            }
                        } else if (Generic.containsGeneric(hostname)) {
                            emailSet.add("postmaster@" + domain);
                            if (abuseEmail != null) {
                                emailSet.add(abuseEmail);
                            }
                            if (clientEmail != null) {
                                emailSet.add(clientEmail);
                            }
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append(
                                        "<a target=\"_blank\" href=\"http://spfbl.net/generic/\">rDNS genrico</a>.</li>\n");
                            } else {
                                builder.append(
                                        "<a target=\"_blank\" href=\"http://spfbl.net/en/generic/\">generic rDNS</a>.</li>\n");
                            }
                        } else {
                            int loop = 0;
                            String subdominio = hostname;
                            while (loop++ < 32 && subdominio.endsWith(domain)) {
                                emailSet.add("postmaster@" + subdominio);
                                int index = subdominio.indexOf('.', 1) + 1;
                                subdominio = subdominio.substring(index);
                            }
                            if (abuseEmail != null) {
                                emailSet.add(abuseEmail);
                            }
                            if (clientEmail != null) {
                                emailSet.add(clientEmail);
                            }
                            if (locale.getLanguage().toLowerCase().equals("pt")) {
                                builder.append(
                                        "<a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.</li>\n");
                            } else {
                                builder.append(
                                        "valid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.</li>\n");
                            }
                        }
                    } else {
                        if (abuseEmail != null) {
                            emailSet.add(abuseEmail);
                        }
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append(
                                    "<a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> invlido.</li>\n");
                        } else {
                            builder.append(
                                    "invalid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.</li>\n");
                        }
                    }
                } while ((hostname = reverseSet.pollFirst()) != null);
                builder.append("        </ul>\n");
            }
            Distribution distribution;
            if ((distribution = SPF.getDistribution(ip, true)).isNotGreen(ip)) {
                float probability = distribution.getSpamProbability(ip);
                boolean blocked = Block.containsCIDR(ip);
                if (blocked || distribution.isRed(ip)) {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Este IP " + (blocked ? "foi bloqueado" : "est listado")
                                        + " por m reputao com " + Core.PERCENT_FORMAT.format(probability)
                                        + " de pontos negativos.");
                        buildText(builder,
                                "Para que este IP possa ser removido desta lista,  necessrio que o MTA de origem reduza o volume de envios para os destinatrios com <a target=\"_blank\" href=\"http://spfbl.net/feedback/\">prefixo de rejeio SPFBL</a> na camada SMTP.");
                    } else {
                        buildText(builder,
                                "This IP " + (blocked ? "was blocked" : "is listed") + " by poor reputation in "
                                        + Core.PERCENT_FORMAT.format(probability) + " of negative points.");
                        buildText(builder,
                                "In order for this IP to be removed from this list, it is necessary that the source MTA reduce the sending volume for the recipients with <a target=\"_blank\" href=\"http://spfbl.net/en/feedback/\">SPFBL rejection prefix</a> at SMTP layer.");
                    }
                } else {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "Este IP no est listado neste sistema porm sua reputao est com "
                                        + Core.PERCENT_FORMAT.format(probability) + "de pontos negativos.");
                        buildText(builder,
                                "Se esta reputao tiver aumento significativo na quantidade de pontos negativos, este IP ser automaticamente listado neste sistema.");
                        buildText(builder,
                                "Para evitar que isto ocorra, reduza os envios com <a target=\"_blank\" href=\"http://spfbl.net/feedback/\">prefixo de rejeio SPFBL</a>.");
                    } else {
                        buildText(builder, "This IP is not listed in this system but its reputation is with "
                                + Core.PERCENT_FORMAT.format(probability) + " of negative points.");
                        buildText(builder,
                                "If this reputation have significant increase in the number of negative points, this IP will automatically be listed in the system.");
                        buildText(builder,
                                "To prevent this from occurring, reduce sending with <a target=\"_blank\" href=\"http://spfbl.net/en/feedback/\">SPFBL rejection prefix</a>.");
                    }
                }
            } else if (emailSet.isEmpty()) {
                boolean good = SPF.isGood(ip);
                boolean blocked = Block.containsCIDR(ip);
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    if (blocked) {
                        buildText(builder,
                                "Este IP foi bloqueado por no ter <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    } else if (good) {
                        buildText(builder,
                                "Este IP est com reputao muito boa, porm no tem um <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    } else {
                        buildText(builder,
                                "Este IP no est bloqueado porm no tem um <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    }
                    if (generic) {
                        buildText(builder,
                                "No sero aceitos <a target=\"_blank\" href=\"http://spfbl.net/generic/\">rDNS genricos</a>.");
                    }
                    buildText(builder,
                            "Cadastre um <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> vlido para este IP, que aponte para o mesmo IP.");
                    if (blocked) {
                        buildText(builder,
                                "O <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> deve estar sob seu prprio domnio para que a liberao seja efetivada.");
                    } else {
                        buildText(builder,
                                "Qualquer IP com <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> invlido pode ser bloqueado a qualquer momento.");
                    }
                } else {
                    if (blocked) {
                        buildText(builder,
                                "This IP has been blocked because have none valid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.");
                    } else if (good) {
                        buildText(builder,
                                "This IP has a very good reputation, but does not have a <a target=\"_blank\" href=\"http://spfbl.net/fcrdns/\">FCrDNS</a> vlido.");
                    } else {
                        buildText(builder,
                                "This IP isn't blocked but have none valid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a>.");
                    }
                    if (generic) {
                        buildText(builder,
                                "<a target=\"_blank\" href=\"http://spfbl.net/en/generic/\"Generic rDNS</a> will not be accepted.");
                    }
                    buildText(builder,
                            "Register a valid <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> for this IP, which points to the same IP.");
                    if (blocked) {
                        buildText(builder,
                                "The <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> must be registered under your own domain for us to be able to delist your system.");
                    } else {
                        buildText(builder,
                                "Any IP with invalid <a target=\"_blank\" href=\"http://spfbl.net/en/fcrdns/\">FCrDNS</a> can be blocked at any time.");
                    }
                }
            } else if (Block.containsCIDR(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP foi bloqueado, porm a reputao dele no est mais ruim.");
                } else {
                    buildText(builder, "This IP has been blocked, but it's reputation is not bad anymore.");
                }
                builder.append("      <hr>\n");
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Para que a chave de desbloqueio possa ser enviada, selecione o endereo de e-mail do responsvel pelo IP:");
                } else {
                    buildText(builder,
                            "For the delist key can be sent, select the e-mail address responsible for this IP:");
                }
                builder.append("      <form method=\"POST\">\n");
                builder.append("        <ul>\n");
                int permittedCount = 0;
                for (String email : emailSet) {
                    if (Domain.isValidEmail(email)) {
                        if (!Trap.contaisAnything(email)) {
                            if (!NoReply.contains(email, false)) {
                                permittedCount++;
                            }
                        }
                    }
                }
                boolean permittedChecked = false;
                String email = emailSet.pollFirst();
                do {
                    if (!Domain.isValidEmail(email)) {
                        builder.append("          <input type=\"radio\" name=\"identifier\" value=\"");
                        builder.append(email);
                        builder.append("\" disabled>");
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("invlido.<br>\n");
                        } else {
                            builder.append("invalid.<br>\n");
                        }
                    } else if (Trap.contaisAnything(email)) {
                        builder.append("          <input type=\"radio\" name=\"identifier\" value=\"");
                        builder.append(email);
                        builder.append("\" disabled>");
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("inexistente.</li><br>\n");
                        } else {
                            builder.append("non-existent.</li><br>\n");
                        }
                    } else if (NoReply.contains(email, false)) {
                        builder.append("          <input type=\"radio\" name=\"identifier\" value=\"");
                        builder.append(email);
                        builder.append("\" disabled>");
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("no permitido.<br>\n");
                        } else {
                            builder.append("not permitted.<br>\n");
                        }
                    } else {
                        builder.append("          <input type=\"radio\" name=\"identifier\" ");
                        builder.append(
                                "onclick=\"document.getElementById('btngo').disabled = false;\" value=\"");
                        builder.append(email);
                        if (permittedChecked) {
                            builder.append("\">");
                        } else if (permittedCount == 1) {
                            builder.append("\" checked>");
                            permittedChecked = true;
                        } else {
                            builder.append("\">");
                        }
                        builder.append("&lt;");
                        builder.append(email);
                        builder.append("&gt; ");
                        if (locale.getLanguage().toLowerCase().equals("pt")) {
                            builder.append("permitido.<br>\n");
                        } else {
                            builder.append("permitted.<br>\n");
                        }
                    }
                } while ((email = emailSet.pollFirst()) != null);
                builder.append("        </ul>\n");
                if (permittedCount == 0) {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder, "Nenhum e-mail do responsvel pelo IP  permitido neste sistema.");
                    } else {
                        buildText(builder,
                                "None of the responsible for IP has e-mail permitted under this system.");
                    }
                } else {
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        buildText(builder,
                                "O <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> do IP deve estar sob seu prprio domnio. No aceitamos <a target=\"_blank\" href=\"http://spfbl.net/rdns/\">rDNS</a> com domnios de terceiros.");
                    } else {
                        buildText(builder,
                                "The <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> must be registered under your own domain. We do not accept <a target=\"_blank\" href=\"http://spfbl.net/en/rdns/\">rDNS</a> with third-party domains.");
                    }
                    builder.append("        <div id=\"divcaptcha\">\n");
                    if (Core.hasRecaptchaKeys()) {
                        String recaptchaKeySite = Core.getRecaptchaKeySite();
                        String recaptchaKeySecret = Core.getRecaptchaKeySecret();
                        ReCaptcha captcha = ReCaptchaFactory.newReCaptcha(recaptchaKeySite, recaptchaKeySecret,
                                false);
                        builder.append("        ");
                        builder.append(captcha.createRecaptchaHtml(null, null).replace("\r", ""));
                        // novo reCAPCHA
                        //            builder.append("      <div class=\"g-recaptcha\" data-sitekey=\"");
                        //            builder.append(recaptchaKeySite);
                        //            builder.append("\"></div>\n");
                    }
                    if (locale.getLanguage().toLowerCase().equals("pt")) {
                        builder.append(
                                "          <input id=\"btngo\" type=\"submit\" value=\"Solicitar chave de delist\"");
                    } else {
                        builder.append(
                                "          <input id=\"btngo\" type=\"submit\" value=\"Request delist key\"");
                    }
                    if (permittedCount == 1) {
                        builder.append(">\n");
                    } else {
                        builder.append(" disabled>\n");
                    }
                    builder.append("        </div>\n");
                    builder.append("      </form>\n");
                }
            } else if (SPF.isGood(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP est com reputao extremamente boa e por isso foi colocado em lista branca.");
                } else {
                    buildText(builder,
                            "This IP is extremely good reputation and therefore has been whitelisted.");
                }
            } else if (Ignore.containsCIDR(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP est marcado como servio essencial e por isso foi colocado em lista branca.");
                } else {
                    buildText(builder,
                            "This IP is marked as an essential service and therefore has been whitelisted.");
                }
            } else if (White.containsIP(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder,
                            "Este IP est marcado como servio de mensagem estritamente corporativo e por isso foi colocado em lista branca.");
                    if (Core.hasAbuseEmail()) {
                        buildText(builder,
                                "Se voc tiver recebido alguma mensagem promocional deste IP, sem prvia autorizao, faa uma denuncia para "
                                        + Core.getAbuseEmail() + ".");
                    }
                } else {
                    buildText(builder,
                            "This IP is marked as strictly corporate message service and therefore has been whitelisted.");
                    if (Core.hasAbuseEmail()) {
                        buildText(builder,
                                "If you received any promotional message from this IP, without permission, make a complaint to "
                                        + Core.getAbuseEmail() + ".");
                    }
                }
            } else if (Block.containsHREF(ip)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Este IP est bloqueado para uso de URL.");
                    buildText(builder,
                            "Para que este IP seja removido desta lista,  necessrio enviar uma solicitao para "
                                    + Core.getAdminEmail() + ".");
                } else {
                    buildText(builder, "This IP is blocked for URL usage.");
                    buildText(builder, "In order to remove this IP from this list, you must send a request to "
                            + Core.getAdminEmail() + ".");
                }
            } else {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Nenhum bloqueio foi encontrado para este IP.");
                    buildText(builder,
                            "Se este IP estiver sendo rejeitado por algum MTA, aguarde a propagao de DNS deste servio.");
                    buildText(builder, "O tempo de propagao pode levar alguns dias.");
                } else {
                    buildText(builder, "No block was found for this IP.");
                    buildText(builder,
                            "If this IP is being rejected by some MTA, wait for the DNS propagation of this service.");
                    buildText(builder, "The propagation time can take a few days.");
                }
            }
        }
    } else if (Domain.isHostname(query)) {
        Distribution distribution;
        String domain = Domain.normalizeHostname(query, true);
        if (domain.equals(".test") || domain.equals(".invalid")) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este  um domnio reservado e por este motivo no  abordado nesta lista.");
            } else {
                buildText(builder,
                        "This is a reserved domain and for this reason is not addressed in this list.");
            }
        } else if (Generic.containsDynamic(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este hostname est listado por referncia a um padro de <a target=\"_blank\" href=\"http://spfbl.net/dynamic/\">IP dinmico</a>.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "Se voc discorda que se trata de hostname com padro de <a target=\"_blank\" href=\"http://spfbl.net/dynamic/\">IP dinmico</a>, entre em contato conosco atravs do e-mail "
                                    + Core.getAdminEmail() + ".");
                }
            } else {
                buildText(builder,
                        "This hostname is listed by reference to a <a target=\"_blank\" href=\"http://spfbl.net/en/dynamic/\">dynamic IP</a> pattern.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "If you disagree that this is hostname with <a target=\"_blank\" href=\"http://spfbl.net/en/dynamic/\">dynamic IP</a> pattern, contact us by email "
                                    + Core.getAdminEmail() + ".");
                }
            }
        } else if ((distribution = SPF.getDistribution(domain, true)).isNotGreen(domain)) {
            float probability = distribution.getSpamProbability(domain);
            boolean blocked = Block.containsDomain(domain, false);
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio " + (blocked ? "foi bloqueado" : "est listado")
                                + " por m reputao com " + Core.PERCENT_FORMAT.format(probability)
                                + " de pontos negativos do volume total de envio.");
                buildText(builder,
                        "Para que este domnio possa ser removido desta lista,  necessrio que todos os MTAs de origem reduzam o volume de envios para os destinatrios com <a target=\"_blank\" href=\"http://spfbl.net/feedback/\">prefixo de rejeio SPFBL</a> na camada SMTP.");
            } else {
                buildText(builder, "This domain " + (blocked ? "was blocked" : "is listed")
                        + " by poor reputation in " + Core.PERCENT_FORMAT.format(probability)
                        + " of negative points of total sending the recipients with <a target=\"_blank\" href=\"http://spfbl.net/en/feedback/\">SPFBL rejection prefix</a> at SMTP layer.");
            }
        } else if (Block.containsDomain(domain, true)) {
            if (Reverse.isInexistentDomain(domain)) {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Este domnio est listado por no existir oficialmente.");
                } else {
                    buildText(builder, "This domain is listed because it does not exist officially.");
                }
            } else {
                if (locale.getLanguage().toLowerCase().equals("pt")) {
                    buildText(builder, "Este domnio est listado por bloqueio manual.");
                    if (Core.hasAdminEmail()) {
                        buildText(builder,
                                "Para que este domnio seja removido desta lista,  necessrio enviar uma solicitao para "
                                        + Core.getAdminEmail() + ".");
                    }
                } else {
                    buildText(builder, "This domain is listed by manual block.");
                    if (Core.hasAdminEmail()) {
                        buildText(builder,
                                "In order to remove this domain from this list, you must send a request to "
                                        + Core.getAdminEmail() + ".");
                    }
                }
            }
        } else if (SPF.isGood(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio est com reputao extremamente boa e por isso foi colocado em lista branca.");
            } else {
                buildText(builder,
                        "This domain is extremely good reputation and therefore has been whitelisted.");
            }
        } else if (Ignore.containsHost(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio est marcado como servio essencial e por isso foi colocado em lista branca.");
            } else {
                buildText(builder,
                        "This domain is marked as an essential service and therefore has been whitelisted.");
            }
        } else if (White.containsDomain(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder,
                        "Este domnio est marcado como servio de mensagem estritamente corporativo e por isso foi colocado em lista branca.");
                if (Core.hasAbuseEmail()) {
                    buildText(builder,
                            "Se voc tiver recebido alguma mensagem promocional deste domnio, sem prvia autorizao, faa uma denuncia para "
                                    + Core.getAbuseEmail() + ".");
                }
            } else {
                buildText(builder,
                        "This domain is marked as strictly corporate message service and therefore has been whitelisted.");
                if (Core.hasAbuseEmail()) {
                    buildText(builder,
                            "If you received any promotional message from this domain, without permission, make a complaint to "
                                    + Core.getAbuseEmail() + ".");
                }
            }
        } else if (Block.containsHREF(domain)) {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder, "Este domnio est bloqueado para uso em URL.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "Para que este domnio seja removido desta lista,  necessrio enviar uma solicitao para "
                                    + Core.getAdminEmail() + ".");
                }
            } else {
                buildText(builder, "This domain is blocked for URL usage.");
                if (Core.hasAdminEmail()) {
                    buildText(builder,
                            "In order to remove this domain from this list, you must send a request to "
                                    + Core.getAdminEmail() + ".");
                }
            }
        } else {
            if (locale.getLanguage().toLowerCase().equals("pt")) {
                buildText(builder, "Nenhum bloqueio foi encontrado para este domnio.");
            } else {
                buildText(builder, "No block was found for this domain.");
            }
        }
    }
    buildFooter(builder, locale);
    builder.append("    </div>\n");
    builder.append("  </body>\n");
    builder.append("</html>\n");
    return builder.toString();
}

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   www.ja va  2  s. c  om
        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.apache.bookkeeper.stream.storage.impl.sc.DefaultStorageContainerController.java

@Override
public ClusterAssignmentData computeIdealState(ClusterMetadata clusterMetadata,
        ClusterAssignmentData currentState, Set<BookieSocketAddress> currentCluster) {

    if (currentCluster.isEmpty()) {
        log.info("Current cluster is empty. No alive server is found.");
        return currentState;
    }/*from   w w w .jav  a 2 s  .  c o  m*/

    // 1. get current server assignments
    Map<BookieSocketAddress, Set<Long>> currentServerAssignments;
    try {
        currentServerAssignments = currentState.getServersMap().entrySet().stream()
                .collect(Collectors.toMap(e1 -> {
                    try {
                        return new BookieSocketAddress(e1.getKey());
                    } catch (UnknownHostException uhe) {
                        log.error("Invalid cluster ");
                        throw new UncheckedExecutionException(
                                "Invalid server found in current assignment map" + e1.getKey(), uhe);
                    }
                }, e2 -> e2.getValue().getContainersList().stream().collect(Collectors.toSet())));
    } catch (UncheckedExecutionException uee) {
        log.warn("Invalid cluster assignment data is found : {} - {}. Recompute assignment from empty state",
                currentState, uee.getCause().getMessage());
        currentServerAssignments = Maps.newHashMap();
    }
    Set<BookieSocketAddress> currentServersAssigned = currentServerAssignments.keySet();

    // 2. if no servers is assigned, initialize the ideal state
    if (currentServersAssigned.isEmpty()) {
        return initializeIdealState(clusterMetadata, currentCluster);
    }

    // 3. get the cluster diffs
    Set<BookieSocketAddress> serversAdded = Sets.difference(currentCluster, currentServersAssigned)
            .immutableCopy();
    Set<BookieSocketAddress> serversRemoved = Sets.difference(currentServersAssigned, currentCluster)
            .immutableCopy();

    if (serversAdded.isEmpty() && serversRemoved.isEmpty()) {
        // cluster is unchanged, assuming the current state is ideal, no re-assignment is required.
        return currentState;
    }

    log.info(
            "Storage container controller detects cluster changed:\n"
                    + "\t {} servers added: {}\n\t {} servers removed: {}",
            serversAdded.size(), serversAdded, serversRemoved.size(), serversRemoved);

    // 4. compute the containers that owned by servers removed. these containers are needed to be reassigned.
    Set<Long> containersToReassign = currentServerAssignments.entrySet().stream()
            .filter(serverEntry -> !currentCluster.contains(serverEntry.getKey()))
            .flatMap(serverEntry -> serverEntry.getValue().stream()).collect(Collectors.toSet());

    // 5. use an ordered set as priority deque to sort the servers by the number of assigned containers
    TreeSet<Pair<BookieSocketAddress, LinkedList<Long>>> assignmentQueue = new TreeSet<>(
            new ServerAssignmentDataComparator());
    for (Map.Entry<BookieSocketAddress, Set<Long>> entry : currentServerAssignments.entrySet()) {
        BookieSocketAddress host = entry.getKey();

        if (!currentCluster.contains(host)) {
            if (log.isTraceEnabled()) {
                log.trace("Host {} is not in current cluster anymore", host);
            }
            continue;
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Adding host {} to assignment queue", host);
            }
            assignmentQueue.add(Pair.of(host, Lists.newLinkedList(entry.getValue())));
        }
    }

    // 6. add new servers
    for (BookieSocketAddress server : serversAdded) {
        assignmentQueue.add(Pair.of(server, Lists.newLinkedList()));
    }

    // 7. assign the containers that are needed to be reassigned.
    for (Long containerId : containersToReassign) {
        Pair<BookieSocketAddress, LinkedList<Long>> leastLoadedServer = assignmentQueue.pollFirst();
        leastLoadedServer.getValue().add(containerId);
        assignmentQueue.add(leastLoadedServer);
    }

    // 8. rebalance the containers if needed
    int diffAllowed;
    if (assignmentQueue.size() > clusterMetadata.getNumStorageContainers()) {
        diffAllowed = 1;
    } else {
        diffAllowed = clusterMetadata.getNumStorageContainers() % assignmentQueue.size() == 0 ? 0 : 1;
    }

    Pair<BookieSocketAddress, LinkedList<Long>> leastLoaded = assignmentQueue.first();
    Pair<BookieSocketAddress, LinkedList<Long>> mostLoaded = assignmentQueue.last();
    while (mostLoaded.getValue().size() - leastLoaded.getValue().size() > diffAllowed) {
        leastLoaded = assignmentQueue.pollFirst();
        mostLoaded = assignmentQueue.pollLast();

        // move container from mostLoaded to leastLoaded
        Long containerId = mostLoaded.getValue().removeFirst();
        // add the container to the end to avoid balancing this container again.
        leastLoaded.getValue().addLast(containerId);

        assignmentQueue.add(leastLoaded);
        assignmentQueue.add(mostLoaded);

        leastLoaded = assignmentQueue.first();
        mostLoaded = assignmentQueue.last();
    }

    // 9. the new ideal state is computed, finalize it
    Map<String, ServerAssignmentData> newAssignmentMap = Maps.newHashMap();
    assignmentQueue.forEach(assignment -> newAssignmentMap.put(assignment.getKey().toString(),
            ServerAssignmentData.newBuilder().addAllContainers(assignment.getValue()).build()));
    return ClusterAssignmentData.newBuilder().putAllServers(newAssignmentMap).build();
}

From source file:org.apache.cassandra.concurrent.LongSharedExecutorPoolTest.java

private void testPromptnessOfExecution(long intervalNanos, float loadIncrement)
        throws InterruptedException, ExecutionException {
    final int executorCount = 4;
    int threadCount = 8;
    int maxQueued = 1024;
    final WeibullDistribution workTime = new WeibullDistribution(3, 200000);
    final long minWorkTime = TimeUnit.MICROSECONDS.toNanos(1);
    final long maxWorkTime = TimeUnit.MILLISECONDS.toNanos(1);

    final int[] threadCounts = new int[executorCount];
    final WeibullDistribution[] workCount = new WeibullDistribution[executorCount];
    final ExecutorService[] executors = new ExecutorService[executorCount];
    for (int i = 0; i < executors.length; i++) {
        executors[i] = SharedExecutorPool.SHARED.newExecutor(threadCount, maxQueued, "test" + i, "test" + i);
        threadCounts[i] = threadCount;// w ww .  ja  va 2s .c om
        workCount[i] = new WeibullDistribution(2, maxQueued);
        threadCount *= 2;
        maxQueued *= 2;
    }

    long runs = 0;
    long events = 0;
    final TreeSet<Batch> pending = new TreeSet<>();
    final BitSet executorsWithWork = new BitSet(executorCount);
    long until = 0;
    // basic idea is to go through different levels of load on the executor service; initially is all small batches
    // (mostly within max queue size) of very short operations, moving to progressively larger batches
    // (beyond max queued size), and longer operations
    for (float multiplier = 0f; multiplier < 2.01f;) {
        if (System.nanoTime() > until) {
            System.out.println(String.format("Completed %.0fK batches with %.1fM events", runs * 0.001f,
                    events * 0.000001f));
            events = 0;
            until = System.nanoTime() + intervalNanos;
            multiplier += loadIncrement;
            System.out.println(String.format("Running for %ds with load multiplier %.1f",
                    TimeUnit.NANOSECONDS.toSeconds(intervalNanos), multiplier));
        }

        // wait a random amount of time so we submit new tasks in various stages of
        long timeout;
        if (pending.isEmpty())
            timeout = 0;
        else if (Math.random() > 0.98)
            timeout = Long.MAX_VALUE;
        else if (pending.size() == executorCount)
            timeout = pending.first().timeout;
        else
            timeout = (long) (Math.random() * pending.last().timeout);

        while (!pending.isEmpty() && timeout > System.nanoTime()) {
            Batch first = pending.first();
            boolean complete = false;
            try {
                for (Result result : first.results.descendingSet())
                    result.future.get(timeout - System.nanoTime(), TimeUnit.NANOSECONDS);
                complete = true;
            } catch (TimeoutException e) {
            }
            if (!complete && System.nanoTime() > first.timeout) {
                for (Result result : first.results)
                    if (!result.future.isDone())
                        throw new AssertionError();
                complete = true;
            }
            if (complete) {
                pending.pollFirst();
                executorsWithWork.clear(first.executorIndex);
            }
        }

        // if we've emptied the executors, give all our threads an opportunity to spin down
        if (timeout == Long.MAX_VALUE)
            Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);

        // submit a random batch to the first free executor service
        int executorIndex = executorsWithWork.nextClearBit(0);
        if (executorIndex >= executorCount)
            continue;
        executorsWithWork.set(executorIndex);
        ExecutorService executor = executors[executorIndex];
        TreeSet<Result> results = new TreeSet<>();
        int count = (int) (workCount[executorIndex].sample() * multiplier);
        long targetTotalElapsed = 0;
        long start = System.nanoTime();
        long baseTime;
        if (Math.random() > 0.5)
            baseTime = 2 * (long) (workTime.sample() * multiplier);
        else
            baseTime = 0;
        for (int j = 0; j < count; j++) {
            long time;
            if (baseTime == 0)
                time = (long) (workTime.sample() * multiplier);
            else
                time = (long) (baseTime * Math.random());
            if (time < minWorkTime)
                time = minWorkTime;
            if (time > maxWorkTime)
                time = maxWorkTime;
            targetTotalElapsed += time;
            Future<?> future = executor.submit(new WaitTask(time));
            results.add(new Result(future, System.nanoTime() + time));
        }
        long end = start + (long) Math.ceil(targetTotalElapsed / (double) threadCounts[executorIndex])
                + TimeUnit.MILLISECONDS.toNanos(100L);
        long now = System.nanoTime();
        if (runs++ > executorCount && now > end)
            throw new AssertionError();
        events += results.size();
        pending.add(new Batch(results, end, executorIndex));
        //            System.out.println(String.format("Submitted batch to executor %d with %d items and %d permitted millis", executorIndex, count, TimeUnit.NANOSECONDS.toMillis(end - start)));
    }
}

From source file:org.apache.qpid.disttest.controller.ParticipatingClients.java

private BidiMap mapConfiguredToRegisteredClientNames(List<String> configuredClientNamesForTest,
        ClientRegistry clientRegistry) {
    BidiMap configuredToRegisteredNameMap = new DualHashBidiMap();

    TreeSet<String> registeredClients = new TreeSet<String>(clientRegistry.getClients());
    for (String configuredClientName : configuredClientNamesForTest) {
        String allocatedClientName = registeredClients.pollFirst();
        if (allocatedClientName == null) {
            throw new IllegalArgumentException("Too few clients in registry " + clientRegistry
                    + " configured clients " + configuredClientNamesForTest);
        }//from  w w w  .j a va  2s .c o  m
        configuredToRegisteredNameMap.put(configuredClientName, allocatedClientName);
    }

    return configuredToRegisteredNameMap;
}