Example usage for java.util SortedMap isEmpty

List of usage examples for java.util SortedMap isEmpty

Introduction

In this page you can find the example usage for java.util SortedMap isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:org.orekit.time.UTCTAIBulletinAFilesLoader.java

/** {@inheritDoc} */
@Override// ww w . j a  v a  2  s  . c  o m
public List<OffsetModel> loadOffsets() throws OrekitException {

    final Parser parser = new Parser();
    DataProvidersManager.getInstance().feed(supportedNames, parser);
    final SortedMap<Integer, Integer> taiUtc = parser.getTaiUtc();
    final SortedMap<Integer, Double> ut1Utc = parser.getUt1Utc();

    // identify UT1-UTC discontinuities
    final List<Integer> leapDays = new ArrayList<Integer>();
    Map.Entry<Integer, Double> previous = null;
    for (final Map.Entry<Integer, Double> entry : ut1Utc.entrySet()) {
        if (previous != null) {
            final double delta = entry.getValue() - previous.getValue();
            if (FastMath.abs(delta) > 0.5) {
                // discontinuity found between previous and current entry, a leap second has occurred
                leapDays.add(entry.getKey());
            }
        }
        previous = entry;
    }

    final List<OffsetModel> offsets = new ArrayList<OffsetModel>();

    if (!taiUtc.isEmpty()) {

        // find the start offset, before the first UT1-UTC entry
        final Map.Entry<Integer, Integer> firstTaiMUtc = taiUtc.entrySet().iterator().next();
        int offset = firstTaiMUtc.getValue();
        final int refMJD = firstTaiMUtc.getKey();
        for (final int leapMJD : leapDays) {
            if (leapMJD > refMJD) {
                break;
            }
            --offset;
        }

        // set all known time steps
        for (final int leapMJD : leapDays) {
            offsets.add(new OffsetModel(new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, leapMJD),
                    ++offset));
        }

        // check for missing time steps
        for (final Map.Entry<Integer, Integer> refTaiMUtc : taiUtc.entrySet()) {
            final DateComponents refDC = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH,
                    refTaiMUtc.getKey() + 1);
            OffsetModel before = null;
            for (final OffsetModel o : offsets) {
                if (o.getStart().compareTo(refDC) < 0) {
                    before = o;
                }
            }
            if (before != null) {
                if (refTaiMUtc.getValue() != (int) FastMath.rint(before.getOffset())) {
                    throw new OrekitException(OrekitMessages.MISSING_EARTH_ORIENTATION_PARAMETERS_BETWEEN_DATES,
                            before.getStart(), refDC);
                }
            }
        }

        // make sure we stop the linear drift that was used before 1972
        if (offsets.isEmpty()) {
            offsets.add(0, new OffsetModel(new DateComponents(1972, 1, 1), taiUtc.get(taiUtc.firstKey())));
        } else {
            if (offsets.get(0).getStart().getYear() > 1972) {
                offsets.add(0, new OffsetModel(new DateComponents(1972, 1, 1),
                        ((int) FastMath.rint(offsets.get(0).getOffset())) - 1));
            }
        }

    }

    return offsets;

}

From source file:cerrla.LocalCrossEntropyDistribution.java

/**
 * Generates a policy from the current distribution.
 * //from  www. j a  v a2  s  . c o m
 * @param existingSubGoals
 *            A collection of all existing sub-goals in the parent policy
 *            this policy is to be put into.
 * @return A newly generated policy from the current distribution.
 */
public ModularPolicy generatePolicy(Collection<ModularPolicy> existingSubGoals) {
    // If testing greedy policies
    if (Config.getInstance().getGeneratorFile() != null) {
        if (bestPolicy_ == null || testEpisode_ >= ProgramArgument.TEST_ITERATIONS.intValue()) {
            SortedMap<Integer, RelationalPolicy> greedyPolicies = policyGenerator_.getGreedyPolicyMap();
            SortedMap<Integer, RelationalPolicy> nextKey = greedyPolicies.tailMap(currentEpisode_ + 1);

            if (ProgramArgument.TESTING.booleanValue()) {
                currentEpisode_ = greedyPolicies.lastKey();
                bestPolicy_ = new ModularPolicy(greedyPolicies.get(currentEpisode_), this);
                testEpisode_ = 0;
            } else if (nextKey == null || nextKey.isEmpty()) {
                // End of testing. Exit.
                bestPolicyEpisode_ = ProgramArgument.TEST_ITERATIONS.intValue();
            } else {
                // Next policy and next episode.
                currentEpisode_ = nextKey.firstKey();
                bestPolicy_ = new ModularPolicy(greedyPolicies.get(currentEpisode_), this);
                testEpisode_ = 0;
            }
        }

        bestPolicy_.clearPolicyRewards();
        return bestPolicy_;
    }

    if (frozen_ && state_ == AlgorithmState.BEST_POLICY) {
        bestPolicy_.clearPolicyRewards();
        return bestPolicy_;
    }

    // Initialise undertested
    if (undertestedPolicies_ == null)
        undertestedPolicies_ = new LinkedList<ModularPolicy>();

    // If there remains an undertested policy not already in the parent
    // policy, use that
    for (Iterator<ModularPolicy> iter = undertestedPolicies_.iterator(); iter.hasNext();) {
        ModularPolicy undertested = iter.next();
        if (undertested.shouldRegenerate() || !isValidSample(undertested, false))
            // If the element is fully tested, remove it.
            iter.remove();
        else if (!existingSubGoals.contains(undertested)) {
            // If the parent policy doesn't already contain the undertested
            // policy, return it.
            undertested.clearChildren();
            return undertested;
        }
    }

    // Otherwise generate a new policy
    RelationalPolicy newPol = policyGenerator_.generatePolicy(true, false);
    ModularPolicy newModPol = null;
    if (newPol instanceof ModularPolicy)
        newModPol = new ModularPolicy((ModularPolicy) newPol);
    else
        newModPol = new ModularPolicy(newPol, this);
    undertestedPolicies_.add(newModPol);
    return newModPol;
}

From source file:org.jahia.services.templates.TemplatePackageRegistry.java

public boolean areVersionsForModuleAvailable(String moduleNameOrId) {
    SortedMap<ModuleVersion, JahiaTemplatesPackage> m = packagesWithVersionById.get(moduleNameOrId);
    if (m == null) {
        m = packagesWithVersionByName.get(moduleNameOrId);
    }/*w ww . jav a  2 s  .c  om*/
    return m != null && !m.isEmpty();
}

From source file:org.torproject.ernie.web.ExoneraTorServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    /* Start writing response. */
    PrintWriter out = response.getWriter();
    writeHeader(out);//from   w w  w  .ja  v  a2s.c o  m

    /* Look up first and last consensus in the database. */
    long firstValidAfter = -1L, lastValidAfter = -1L;
    try {
        Connection conn = this.ds.getConnection();
        Statement statement = conn.createStatement();
        String query = "SELECT MIN(validafter) AS first, " + "MAX(validafter) AS last FROM consensus";
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            firstValidAfter = rs.getTimestamp(1).getTime();
            lastValidAfter = rs.getTimestamp(2).getTime();
        }
        rs.close();
        statement.close();
        conn.close();
    } catch (SQLException e) {
        /* Looks like we don't have any consensuses. */
    }
    if (firstValidAfter < 0L || lastValidAfter < 0L) {
        out.println("<p><font color=\"red\"><b>Warning: </b></font>This "
                + "server doesn't have any relay lists available. If this " + "problem persists, please "
                + "<a href=\"mailto:tor-assistants@freehaven.net\">let us " + "know</a>!</p>\n");
        writeFooter(out);
        return;
    }

    out.println("<a name=\"relay\"></a><h3>Was there a Tor relay running " + "on this IP address?</h3>");

    /* Parse IP parameter. */
    Pattern ipAddressPattern = Pattern
            .compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                    + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
    String ipParameter = request.getParameter("ip");
    String relayIP = "", ipWarning = "";
    if (ipParameter != null && ipParameter.length() > 0) {
        Matcher ipParameterMatcher = ipAddressPattern.matcher(ipParameter);
        if (ipParameterMatcher.matches()) {
            String[] ipParts = ipParameter.split("\\.");
            relayIP = Integer.parseInt(ipParts[0]) + "." + Integer.parseInt(ipParts[1]) + "."
                    + Integer.parseInt(ipParts[2]) + "." + Integer.parseInt(ipParts[3]);
        } else {
            ipWarning = "\""
                    + (ipParameter.length() > 20 ? ipParameter.substring(0, 20) + "[...]" : ipParameter)
                    + "\" is not a valid IP address.";
        }
    }

    /* Parse timestamp parameter. */
    String timestampParameter = request.getParameter("timestamp");
    long timestamp = 0L;
    String timestampStr = "", timestampWarning = "";
    SimpleDateFormat shortDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    shortDateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    if (timestampParameter != null && timestampParameter.length() > 0) {
        try {
            timestamp = shortDateTimeFormat.parse(timestampParameter).getTime();
            timestampStr = shortDateTimeFormat.format(timestamp);
            if (timestamp < firstValidAfter || timestamp > lastValidAfter) {
                timestampWarning = "Please pick a value between \""
                        + shortDateTimeFormat.format(firstValidAfter) + "\" and \""
                        + shortDateTimeFormat.format(lastValidAfter) + "\".";
            }
        } catch (ParseException e) {
            /* We have no way to handle this exception, other than leaving
               timestampStr at "". */
            timestampWarning = "\""
                    + (timestampParameter.length() > 20 ? timestampParameter.substring(0, 20) + "[...]"
                            : timestampParameter)
                    + "\" is not a valid timestamp.";
        }
    }

    /* If either IP address or timestamp is provided, the other one must
     * be provided, too. */
    if (relayIP.length() < 1 && timestampStr.length() > 0 && ipWarning.length() < 1) {
        ipWarning = "Please provide an IP address.";
    }
    if (relayIP.length() > 0 && timestampStr.length() < 1 && timestampWarning.length() < 1) {
        timestampWarning = "Please provide a timestamp.";
    }

    /* Parse target IP parameter. */
    String targetIP = "", targetPort = "", target = "";
    String[] targetIPParts = null;
    String targetAddrParameter = request.getParameter("targetaddr");
    String targetAddrWarning = "";
    if (targetAddrParameter != null && targetAddrParameter.length() > 0) {
        Matcher targetAddrParameterMatcher = ipAddressPattern.matcher(targetAddrParameter);
        if (targetAddrParameterMatcher.matches()) {
            String[] targetAddrParts = targetAddrParameter.split("\\.");
            targetIP = Integer.parseInt(targetAddrParts[0]) + "." + Integer.parseInt(targetAddrParts[1]) + "."
                    + Integer.parseInt(targetAddrParts[2]) + "." + Integer.parseInt(targetAddrParts[3]);
            target = targetIP;
            targetIPParts = targetIP.split("\\.");
        } else {
            targetAddrWarning = "\""
                    + (targetAddrParameter.length() > 20 ? timestampParameter.substring(0, 20) + "[...]"
                            : timestampParameter)
                    + "\" is not a valid IP address.";
        }
    }

    /* Parse target port parameter. */
    String targetPortParameter = request.getParameter("targetport");
    String targetPortWarning = "";
    if (targetPortParameter != null && targetPortParameter.length() > 0) {
        Pattern targetPortPattern = Pattern.compile("\\d+");
        if (targetPortParameter.length() < 5 && targetPortPattern.matcher(targetPortParameter).matches()
                && !targetPortParameter.equals("0") && Integer.parseInt(targetPortParameter) < 65536) {
            targetPort = targetPortParameter;
            if (target != null) {
                target += ":" + targetPort;
            } else {
                target = targetPort;
            }
        } else {
            targetPortWarning = "\""
                    + (targetPortParameter.length() > 8 ? targetPortParameter.substring(0, 8) + "[...]"
                            : targetPortParameter)
                    + "\" is not a valid TCP port.";
        }
    }

    /* If target port is provided, a target address must be provided,
     * too. */
    if (targetPort.length() > 0 && targetIP.length() < 1 && targetAddrWarning.length() < 1) {
        targetAddrWarning = "Please provide an IP address.";
    }

    /* Write form with IP address and timestamp. */
    out.println("        <form action=\"exonerator.html#relay\">\n"
            + "          <input type=\"hidden\" name=\"targetaddr\" "
            + (targetIP.length() > 0 ? " value=\"" + targetIP + "\"" : "") + ">\n"
            + "          <input type=\"hidden\" name=\"targetPort\""
            + (targetPort.length() > 0 ? " value=\"" + targetPort + "\"" : "") + ">\n" + "          <table>\n"
            + "            <tr>\n" + "              <td align=\"right\">IP address in question:" + "</td>\n"
            + "              <td><input type=\"text\" name=\"ip\""
            + (relayIP.length() > 0 ? " value=\"" + relayIP + "\"" : "") + ">"
            + (ipWarning.length() > 0 ? "<br><font color=\"red\">" + ipWarning + "</font>" : "") + "</td>\n"
            + "              <td><i>(Ex.: 1.2.3.4)</i></td>\n" + "            </tr>\n" + "            <tr>\n"
            + "              <td align=\"right\">Timestamp, in UTC:</td>\n"
            + "              <td><input type=\"text\" name=\"timestamp\""
            + (timestampStr.length() > 0 ? " value=\"" + timestampStr + "\"" : "") + ">"
            + (timestampWarning.length() > 0 ? "<br><font color=\"red\">" + timestampWarning + "</font>" : "")
            + "</td>\n" + "              <td><i>(Ex.: 2010-01-01 12:00)</i></td>\n" + "            </tr>\n"
            + "            <tr>\n" + "              <td></td>\n" + "              <td>\n"
            + "                <input type=\"submit\">\n" + "                <input type=\"reset\">\n"
            + "              </td>\n" + "              <td></td>\n" + "            </tr>\n"
            + "          </table>\n" + "        </form>\n");

    if (relayIP.length() < 1 || timestampStr.length() < 1) {
        writeFooter(out);
        return;
    }

    /* Look up relevant consensuses. */
    long timestampTooOld = timestamp - 15L * 60L * 60L * 1000L;
    long timestampFrom = timestamp - 3L * 60L * 60L * 1000L;
    long timestampTooNew = timestamp + 12L * 60L * 60L * 1000L;
    out.printf(
            "<p>Looking up IP address %s in the relay lists published " + "between %s and %s. "
                    + "Clients could have used any of these relay lists to "
                    + "select relays for their paths and build circuits using them. "
                    + "You may follow the links to relay lists and relay descriptors "
                    + "to grep for the lines printed below and confirm that results " + "are correct.<br>",
            relayIP, shortDateTimeFormat.format(timestampFrom), timestampStr);
    SimpleDateFormat validAfterTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    validAfterTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    String fromValidAfter = validAfterTimeFormat.format(timestampTooOld);
    String toValidAfter = validAfterTimeFormat.format(timestampTooNew);
    SortedMap<Long, String> tooOldConsensuses = new TreeMap<Long, String>();
    SortedMap<Long, String> relevantConsensuses = new TreeMap<Long, String>();
    SortedMap<Long, String> tooNewConsensuses = new TreeMap<Long, String>();
    try {
        Connection conn = this.ds.getConnection();
        Statement statement = conn.createStatement();
        String query = "SELECT validafter, rawdesc FROM consensus " + "WHERE validafter >= '" + fromValidAfter
                + "' AND validafter <= '" + toValidAfter + "'";
        ResultSet rs = statement.executeQuery(query);
        while (rs.next()) {
            long consensusTime = rs.getTimestamp(1).getTime();
            String rawConsensusString = new String(rs.getBytes(2), "US-ASCII");
            if (consensusTime < timestampFrom) {
                tooOldConsensuses.put(consensusTime, rawConsensusString);
            } else if (consensusTime > timestamp) {
                tooNewConsensuses.put(consensusTime, rawConsensusString);
            } else {
                relevantConsensuses.put(consensusTime, rawConsensusString);
            }
        }
        rs.close();
        statement.close();
        conn.close();
    } catch (SQLException e) {
        /* Looks like we don't have any consensuses in the requested
           interval. */
    }
    SortedMap<Long, String> allConsensuses = new TreeMap<Long, String>();
    allConsensuses.putAll(tooOldConsensuses);
    allConsensuses.putAll(relevantConsensuses);
    allConsensuses.putAll(tooNewConsensuses);
    if (allConsensuses.isEmpty()) {
        out.println("        <p>No relay lists found!</p>\n" + "        <p>Result is INDECISIVE!</p>\n"
                + "        <p>We cannot make any statement whether there was "
                + "a Tor relay running on IP address " + relayIP + " at " + timestampStr + "! We "
                + "did not find any relevant relay lists preceding the given "
                + "time. If you think this is an error on our side, please "
                + "<a href=\"mailto:tor-assistants@freehaven.net\">contact " + "us</a>!</p>\n");
        writeFooter(out);
        return;
    }

    /* Parse consensuses to find descriptors belonging to the IP
       address. */
    SortedSet<Long> positiveConsensusesNoTarget = new TreeSet<Long>();
    Set<String> addressesInSameNetwork = new HashSet<String>();
    SortedMap<String, Set<Long>> relevantDescriptors = new TreeMap<String, Set<Long>>();
    SimpleDateFormat validAfterUrlFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    validAfterUrlFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    for (Map.Entry<Long, String> e : allConsensuses.entrySet()) {
        long consensus = e.getKey();
        if (relevantConsensuses.containsKey(consensus)) {
            long validAfterTime = -1L;
            String validAfterDatetime = validAfterTimeFormat.format(consensus);
            String validAfterString = validAfterUrlFormat.format(consensus);
            out.println("        <br><tt>valid-after <b>" + "<a href=\"consensus?valid-after="
                    + validAfterString + "\" target=\"_blank\">" + validAfterDatetime + "</b></a></tt><br>");
        }
        String rawConsensusString = e.getValue();
        BufferedReader br = new BufferedReader(new StringReader(rawConsensusString));
        String line = null;
        while ((line = br.readLine()) != null) {
            if (!line.startsWith("r ")) {
                continue;
            }
            String[] parts = line.split(" ");
            String address = parts[6];
            if (address.equals(relayIP)) {
                String hex = String.format("%040x", new BigInteger(1, Base64.decodeBase64(parts[3] + "==")));
                if (!relevantDescriptors.containsKey(hex)) {
                    relevantDescriptors.put(hex, new HashSet<Long>());
                }
                relevantDescriptors.get(hex).add(consensus);
                positiveConsensusesNoTarget.add(consensus);
                if (relevantConsensuses.containsKey(consensus)) {
                    out.println("    <tt>r " + parts[1] + " " + parts[2] + " " + "<a href=\"serverdesc?desc-id="
                            + hex + "\" " + "target=\"_blank\">" + parts[3] + "</a> " + parts[4] + " "
                            + parts[5] + " <b>" + parts[6] + "</b> " + parts[7] + " " + parts[8] + "</tt><br>");
                }
            } else {
                if (relayIP.startsWith(address.substring(0, address.lastIndexOf(".")))) {
                    addressesInSameNetwork.add(address);
                }
            }
        }
        br.close();
    }
    if (relevantDescriptors.isEmpty()) {
        out.printf(
                "        <p>None found!</p>\n" + "        <p>Result is NEGATIVE with moderate certainty!</p>\n"
                        + "        <p>We did not find IP " + "address " + relayIP
                        + " in any of the relay lists that were "
                        + "published between %s and %s.\n\nA possible "
                        + "reason for false negatives is that the relay is using a "
                        + "different IP address when generating a descriptor than for "
                        + "exiting to the Internet. We hope to provide better checks "
                        + "for this case in the future.</p>\n",
                shortDateTimeFormat.format(timestampTooOld), shortDateTimeFormat.format(timestampTooNew));
        if (!addressesInSameNetwork.isEmpty()) {
            out.println("        <p>The following other IP addresses of Tor "
                    + "relays were found in the mentioned relay lists that "
                    + "are in the same /24 network and that could be related to " + "IP address " + relayIP
                    + ":</p>\n");
            for (String s : addressesInSameNetwork) {
                out.println("        <p>" + s + "</p>\n");
            }
        }
        writeFooter(out);
        return;
    }

    /* Print out result. */
    Set<Long> matches = positiveConsensusesNoTarget;
    if (matches.contains(relevantConsensuses.lastKey())) {
        out.println("        <p>Result is POSITIVE with high certainty!" + "</p>\n"
                + "        <p>We found one or more relays on IP address " + relayIP
                + " in the most recent relay list preceding " + timestampStr
                + " that clients were likely to know.</p>\n");
    } else {
        boolean inOtherRelevantConsensus = false, inTooOldConsensuses = false, inTooNewConsensuses = false;
        for (long match : matches) {
            if (relevantConsensuses.containsKey(match)) {
                inOtherRelevantConsensus = true;
            } else if (tooOldConsensuses.containsKey(match)) {
                inTooOldConsensuses = true;
            } else if (tooNewConsensuses.containsKey(match)) {
                inTooNewConsensuses = true;
            }
        }
        if (inOtherRelevantConsensus) {
            out.println("        <p>Result is POSITIVE " + "with moderate certainty!</p>\n");
            out.println("<p>We found one or more relays on IP address " + relayIP
                    + ", but not in the relay list immediately " + "preceding " + timestampStr
                    + ". A possible reason for the "
                    + "relay being missing in the last relay list preceding the "
                    + "given time might be that some of the directory "
                    + "authorities had difficulties connecting to the relay. "
                    + "However, clients might still have used the relay.</p>\n");
        } else {
            out.println("        <p>Result is NEGATIVE " + "with high certainty!</p>\n");
            out.println("        <p>We did not find any relay on IP address " + relayIP
                    + " in the relay lists 3 hours preceding " + timestampStr + ".</p>\n");
            if (inTooOldConsensuses || inTooNewConsensuses) {
                if (inTooOldConsensuses && !inTooNewConsensuses) {
                    out.println("        <p>Note that we found a matching relay "
                            + "in relay lists that were published between 5 and 3 " + "hours before "
                            + timestampStr + ".</p>\n");
                } else if (!inTooOldConsensuses && inTooNewConsensuses) {
                    out.println("        <p>Note that we found a matching relay "
                            + "in relay lists that were published up to 2 hours " + "after " + timestampStr
                            + ".</p>\n");
                } else {
                    out.println("        <p>Note that we found a matching relay "
                            + "in relay lists that were published between 5 and 3 "
                            + "hours before and in relay lists that were published " + "up to 2 hours after "
                            + timestampStr + ".</p>\n");
                }
                out.println("<p>Make sure that the timestamp you provided is "
                        + "in the correct timezone: UTC (or GMT).</p>");
            }
            writeFooter(out);
            return;
        }
    }

    /* Second part: target */
    out.println("<br><a name=\"exit\"></a><h3>Was this relay configured "
            + "to permit exiting to a given target?</h3>");

    out.println("        <form action=\"exonerator.html#exit\">\n"
            + "              <input type=\"hidden\" name=\"timestamp\"\n" + "                         value=\""
            + timestampStr + "\">\n" + "              <input type=\"hidden\" name=\"ip\" " + "value=\""
            + relayIP + "\">\n" + "          <table>\n" + "            <tr>\n"
            + "              <td align=\"right\">Target address:</td>\n"
            + "              <td><input type=\"text\" name=\"targetaddr\""
            + (targetIP.length() > 0 ? " value=\"" + targetIP + "\"" : "") + "\">"
            + (targetAddrWarning.length() > 0 ? "<br><font color=\"red\">" + targetAddrWarning + "</font>" : "")
            + "</td>\n" + "              <td><i>(Ex.: 4.3.2.1)</i></td>\n" + "            </tr>\n"
            + "            <tr>\n" + "              <td align=\"right\">Target port:</td>\n"
            + "              <td><input type=\"text\" name=\"targetport\""
            + (targetPort.length() > 0 ? " value=\"" + targetPort + "\"" : "") + ">"
            + (targetPortWarning.length() > 0 ? "<br><font color=\"red\">" + targetPortWarning + "</font>" : "")
            + "</td>\n" + "              <td><i>(Ex.: 80)</i></td>\n" + "            </tr>\n"
            + "            <tr>\n" + "              <td></td>\n" + "              <td>\n"
            + "                <input type=\"submit\">\n" + "                <input type=\"reset\">\n"
            + "              </td>\n" + "              <td></td>\n" + "            </tr>\n"
            + "          </table>\n" + "        </form>\n");

    if (targetIP.length() < 1) {
        writeFooter(out);
        return;
    }

    /* Parse router descriptors to check exit policies. */
    out.println("<p>Searching the relay descriptors published by the " + "relay on IP address " + relayIP
            + " to find out whether this " + "relay permitted exiting to " + target + ". You may follow the "
            + "links above to the relay descriptors and grep them for the "
            + "lines printed below to confirm that results are correct.</p>");
    SortedSet<Long> positiveConsensuses = new TreeSet<Long>();
    Set<String> missingDescriptors = new HashSet<String>();
    Set<String> descriptors = relevantDescriptors.keySet();
    for (String descriptor : descriptors) {
        byte[] rawDescriptor = null;
        try {
            Connection conn = this.ds.getConnection();
            Statement statement = conn.createStatement();
            String query = "SELECT rawdesc FROM descriptor " + "WHERE descriptor = '" + descriptor + "'";
            ResultSet rs = statement.executeQuery(query);
            if (rs.next()) {
                rawDescriptor = rs.getBytes(1);
            }
            rs.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            /* Consider this descriptors as 'missing'. */
            continue;
        }
        if (rawDescriptor != null && rawDescriptor.length > 0) {
            missingDescriptors.remove(descriptor);
            String rawDescriptorString = new String(rawDescriptor, "US-ASCII");
            try {
                BufferedReader br = new BufferedReader(new StringReader(rawDescriptorString));
                String line = null, routerLine = null, publishedLine = null;
                StringBuilder acceptRejectLines = new StringBuilder();
                boolean foundMatch = false;
                while ((line = br.readLine()) != null) {
                    if (line.startsWith("router ")) {
                        routerLine = line;
                    } else if (line.startsWith("published ")) {
                        publishedLine = line;
                    } else if (line.startsWith("reject ") || line.startsWith("accept ")) {
                        if (foundMatch) {
                            out.println("<tt> " + line + "</tt><br>");
                            continue;
                        }
                        boolean ruleAccept = line.split(" ")[0].equals("accept");
                        String ruleAddress = line.split(" ")[1].split(":")[0];
                        if (!ruleAddress.equals("*")) {
                            if (!ruleAddress.contains("/") && !ruleAddress.equals(targetIP)) {
                                /* IP address does not match. */
                                acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                                continue;
                            }
                            String[] ruleIPParts = ruleAddress.split("/")[0].split("\\.");
                            int ruleNetwork = ruleAddress.contains("/")
                                    ? Integer.parseInt(ruleAddress.split("/")[1])
                                    : 32;
                            for (int i = 0; i < 4; i++) {
                                if (ruleNetwork == 0) {
                                    break;
                                } else if (ruleNetwork >= 8) {
                                    if (ruleIPParts[i].equals(targetIPParts[i])) {
                                        ruleNetwork -= 8;
                                    } else {
                                        break;
                                    }
                                } else {
                                    int mask = 255 ^ 255 >>> ruleNetwork;
                                    if ((Integer.parseInt(ruleIPParts[i])
                                            & mask) == (Integer.parseInt(targetIPParts[i]) & mask)) {
                                        ruleNetwork = 0;
                                    }
                                    break;
                                }
                            }
                            if (ruleNetwork > 0) {
                                /* IP address does not match. */
                                acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                                continue;
                            }
                        }
                        String rulePort = line.split(" ")[1].split(":")[1];
                        if (targetPort.length() < 1 && !ruleAccept && !rulePort.equals("*")) {
                            /* With no port given, we only consider reject :* rules as
                               matching. */
                            acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                            continue;
                        }
                        if (targetPort.length() > 0 && !rulePort.equals("*") && rulePort.contains("-")) {
                            int fromPort = Integer.parseInt(rulePort.split("-")[0]);
                            int toPort = Integer.parseInt(rulePort.split("-")[1]);
                            int targetPortInt = Integer.parseInt(targetPort);
                            if (targetPortInt < fromPort || targetPortInt > toPort) {
                                /* Port not contained in interval. */
                                continue;
                            }
                        }
                        if (targetPort.length() > 0) {
                            if (!rulePort.equals("*") && !rulePort.contains("-")
                                    && !targetPort.equals(rulePort)) {
                                /* Ports do not match. */
                                acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                                continue;
                            }
                        }
                        boolean relevantMatch = false;
                        for (long match : relevantDescriptors.get(descriptor)) {
                            if (relevantConsensuses.containsKey(match)) {
                                relevantMatch = true;
                            }
                        }
                        if (relevantMatch) {
                            String[] routerParts = routerLine.split(" ");
                            out.println("<br><tt>" + routerParts[0] + " " + routerParts[1] + " <b>"
                                    + routerParts[2] + "</b> " + routerParts[3] + " " + routerParts[4] + " "
                                    + routerParts[5] + "</tt><br>");
                            String[] publishedParts = publishedLine.split(" ");
                            out.println("<tt>" + publishedParts[0] + " <b>" + publishedParts[1] + " "
                                    + publishedParts[2] + "</b></tt><br>");
                            out.println(acceptRejectLines.toString());
                            out.println("<tt><b>" + line + "</b></tt><br>");
                            foundMatch = true;
                        }
                        if (ruleAccept) {
                            positiveConsensuses.addAll(relevantDescriptors.get(descriptor));
                        }
                    }
                }
                br.close();
            } catch (IOException e) {
                /* Could not read descriptor string. */
                continue;
            }
        }
    }

    /* Print out result. */
    matches = positiveConsensuses;
    if (matches.contains(relevantConsensuses.lastKey())) {
        out.println("        <p>Result is POSITIVE with high certainty!</p>" + "\n"
                + "        <p>We found one or more relays on IP address " + relayIP + " permitting exit to "
                + target + " in the most recent relay list preceding " + timestampStr
                + " that clients were likely to know.</p>\n");
        writeFooter(out);
        return;
    }
    boolean resultIndecisive = target.length() > 0 && !missingDescriptors.isEmpty();
    if (resultIndecisive) {
        out.println("        <p>Result is INDECISIVE!</p>\n"
                + "        <p>At least one referenced descriptor could not be "
                + "found. This is a rare case, but one that (apparently) "
                + "happens. We cannot make any good statement about exit "
                + "relays without these descriptors. The following descriptors " + "are missing:</p>");
        for (String desc : missingDescriptors)
            out.println("        <p>" + desc + "</p>\n");
    }
    boolean inOtherRelevantConsensus = false, inTooOldConsensuses = false, inTooNewConsensuses = false;
    for (long match : matches) {
        if (relevantConsensuses.containsKey(match)) {
            inOtherRelevantConsensus = true;
        } else if (tooOldConsensuses.containsKey(match)) {
            inTooOldConsensuses = true;
        } else if (tooNewConsensuses.containsKey(match)) {
            inTooNewConsensuses = true;
        }
    }
    if (inOtherRelevantConsensus) {
        if (!resultIndecisive) {
            out.println("        <p>Result is POSITIVE " + "with moderate certainty!</p>\n");
        }
        out.println("<p>We found one or more relays on IP address " + relayIP + " permitting exit to " + target
                + ", but not in " + "the relay list immediately preceding " + timestampStr
                + ". A possible reason for the relay being missing in the last "
                + "relay list preceding the given time might be that some of "
                + "the directory authorities had difficulties connecting to "
                + "the relay. However, clients might still have used the " + "relay.</p>\n");
    } else {
        if (!resultIndecisive) {
            out.println("        <p>Result is NEGATIVE " + "with high certainty!</p>\n");
        }
        out.println("        <p>We did not find any relay on IP address " + relayIP + " permitting exit to "
                + target + " in the relay list 3 hours preceding " + timestampStr + ".</p>\n");
        if (inTooOldConsensuses || inTooNewConsensuses) {
            if (inTooOldConsensuses && !inTooNewConsensuses) {
                out.println("        <p>Note that we found a matching relay in "
                        + "relay lists that were published between 5 and 3 " + "hours before " + timestampStr
                        + ".</p>\n");
            } else if (!inTooOldConsensuses && inTooNewConsensuses) {
                out.println("        <p>Note that we found a matching relay in "
                        + "relay lists that were published up to 2 hours after " + timestampStr + ".</p>\n");
            } else {
                out.println("        <p>Note that we found a matching relay in "
                        + "relay lists that were published between 5 and 3 "
                        + "hours before and in relay lists that were published up " + "to 2 hours after "
                        + timestampStr + ".</p>\n");
            }
            out.println("<p>Make sure that the timestamp you provided is "
                    + "in the correct timezone: UTC (or GMT).</p>");
        }
    }
    if (target != null) {
        if (positiveConsensuses.isEmpty() && !positiveConsensusesNoTarget.isEmpty()) {
            out.println("        <p>Note that although the found relay(s) did " + "not permit exiting to "
                    + target + ", there have been one " + "or more relays running at the given time.</p>");
        }
    }

    /* Finish writing response. */
    writeFooter(out);
}

From source file:freemarker.ext.dump.DumpDirectiveTest.java

private Map<String, Object> getJaneSmithExpectedDump(int exposureLevel) {

    Map<String, Object> expectedDump = new HashMap<String, Object>();

    Map<String, Object> supervisorExpectedDump = new HashMap<String, Object>();
    supervisorExpectedDump.put(Key.VALUE.toString(), Value.NULL.toString());

    SortedMap<String, Object> propertiesExpectedDump = new TreeMap<String, Object>();

    // Properties 
    if (exposureLevel < BeansWrapper.EXPOSE_NOTHING) {

        Map<String, Object> birthdateExpectedDump = new HashMap<String, Object>();
        birthdateExpectedDump.put(Key.TYPE.toString(), Type.DATE);
        birthdateExpectedDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN);
        Calendar c = Calendar.getInstance();
        c.set(1975, Calendar.OCTOBER, 25);
        c = DateUtils.truncate(c, Calendar.DATE);
        birthdateExpectedDump.put(Key.VALUE.toString(), c.getTime());
        propertiesExpectedDump.put("birthdate", birthdateExpectedDump);

        Map<String, Object> fullNameExpectedDump = new HashMap<String, Object>();
        fullNameExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        fullNameExpectedDump.put(Key.VALUE.toString(), "Jane Smith");
        propertiesExpectedDump.put("fullName", fullNameExpectedDump);

        Map<String, Object> idExpectedDump = new HashMap<String, Object>();
        idExpectedDump.put(Key.TYPE.toString(), Type.NUMBER);
        idExpectedDump.put(Key.VALUE.toString(), 78234);
        propertiesExpectedDump.put("id", idExpectedDump);

        Map<String, Object> nicknameExpectedDump = new HashMap<String, Object>();
        nicknameExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        nicknameExpectedDump.put(Key.VALUE.toString(), "");
        propertiesExpectedDump.put("nickname", nicknameExpectedDump);

        Map<String, Object> middleNameExpectedDump = new HashMap<String, Object>();
        middleNameExpectedDump.put(Key.VALUE.toString(), Value.NULL.toString());
        propertiesExpectedDump.put("middleName", middleNameExpectedDump);

        Map<String, Object> marriedExpectedDump = new HashMap<String, Object>();
        marriedExpectedDump.put(Key.TYPE.toString(), Type.BOOLEAN);
        marriedExpectedDump.put(Key.VALUE.toString(), true);
        propertiesExpectedDump.put("married", marriedExpectedDump);

        propertiesExpectedDump.put("supervisor", supervisorExpectedDump);

        Map<String, Object> favoriteColorsExpectedDump = new HashMap<String, Object>();
        favoriteColorsExpectedDump.put(Key.TYPE.toString(), Type.SEQUENCE);
        List<Map<String, Object>> favoriteColorListExpectedDump = new ArrayList<Map<String, Object>>();
        Map<String, Object> color1ExpectedDump = new HashMap<String, Object>();
        color1ExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        color1ExpectedDump.put(Key.VALUE.toString(), "red");
        favoriteColorListExpectedDump.add(color1ExpectedDump);
        Map<String, Object> color2ExpectedDump = new HashMap<String, Object>();
        color2ExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        color2ExpectedDump.put(Key.VALUE.toString(), "orange");
        favoriteColorListExpectedDump.add(color2ExpectedDump);
        favoriteColorsExpectedDump.put(Key.VALUE.toString(), favoriteColorListExpectedDump);
        propertiesExpectedDump.put("favoriteColors", favoriteColorsExpectedDump);

        //            Map<String, Object> degreesExpectedDump = new HashMap<String, Object>();
        //            degreesExpectedDump.put(Key.VALUE.toString(), Value.NULL.toString());
        //            propertiesExpectedDump.put("degrees", degreesExpectedDump);            
    }//from w  w  w.  j a v  a2s. co  m
    expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);

    // Methods
    SortedMap<String, Object> methodDump = getEmployeeMethodsExpectedDump(exposureLevel, "Smith");
    if (!methodDump.isEmpty()) {
        methodDump.put("boss()", supervisorExpectedDump);
    }
    expectedDump.put(Key.METHODS.toString(), methodDump);

    return expectedDump;
}

From source file:freemarker.ext.dump.DumpDirectiveTest.java

private Map<String, Object> getJohnDoeExpectedDump(int exposureLevel) {

    Map<String, Object> expectedDump = new HashMap<String, Object>();

    Map<String, Object> supervisorExpectedDump = new HashMap<String, Object>();
    supervisorExpectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee");
    supervisorExpectedDump.put(Key.VALUE.toString(), getJaneSmithExpectedDump(exposureLevel));

    // Properties
    SortedMap<String, Object> propertiesExpectedDump = new TreeMap<String, Object>();

    if (exposureLevel < BeansWrapper.EXPOSE_NOTHING) {

        Map<String, Object> birthdateExpectedDump = new HashMap<String, Object>();
        birthdateExpectedDump.put(Key.TYPE.toString(), Type.DATE);
        birthdateExpectedDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN);
        Calendar c = Calendar.getInstance();
        c.set(1982, Calendar.MAY, 5);
        c = DateUtils.truncate(c, Calendar.DATE);
        birthdateExpectedDump.put(Key.VALUE.toString(), c.getTime());
        propertiesExpectedDump.put("birthdate", birthdateExpectedDump);

        Map<String, Object> fullNameExpectedDump = new HashMap<String, Object>();
        fullNameExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        fullNameExpectedDump.put(Key.VALUE.toString(), "John Doe");
        propertiesExpectedDump.put("fullName", fullNameExpectedDump);

        Map<String, Object> idExpectedDump = new HashMap<String, Object>();
        idExpectedDump.put(Key.TYPE.toString(), Type.NUMBER);
        idExpectedDump.put(Key.VALUE.toString(), 34523);
        propertiesExpectedDump.put("id", idExpectedDump);

        Map<String, Object> nicknameExpectedDump = new HashMap<String, Object>();
        nicknameExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        nicknameExpectedDump.put(Key.VALUE.toString(), "");
        propertiesExpectedDump.put("nickname", nicknameExpectedDump);

        Map<String, Object> middleNameExpectedDump = new HashMap<String, Object>();
        middleNameExpectedDump.put(Key.VALUE.toString(), Value.NULL.toString());
        propertiesExpectedDump.put("middleName", middleNameExpectedDump);

        Map<String, Object> marriedExpectedDump = new HashMap<String, Object>();
        marriedExpectedDump.put(Key.TYPE.toString(), Type.BOOLEAN);
        marriedExpectedDump.put(Key.VALUE.toString(), true);
        propertiesExpectedDump.put("married", marriedExpectedDump);

        propertiesExpectedDump.put("supervisor", supervisorExpectedDump);

        Map<String, Object> favoriteColorsExpectedDump = new HashMap<String, Object>();
        favoriteColorsExpectedDump.put(Key.TYPE.toString(), Type.SEQUENCE);
        List<Map<String, Object>> favoriteColorListExpectedDump = new ArrayList<Map<String, Object>>();
        Map<String, Object> color1ExpectedDump = new HashMap<String, Object>();
        color1ExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        color1ExpectedDump.put(Key.VALUE.toString(), "blue");
        favoriteColorListExpectedDump.add(color1ExpectedDump);
        Map<String, Object> color2ExpectedDump = new HashMap<String, Object>();
        color2ExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        color2ExpectedDump.put(Key.VALUE.toString(), "green");
        favoriteColorListExpectedDump.add(color2ExpectedDump);
        favoriteColorsExpectedDump.put(Key.VALUE.toString(), favoriteColorListExpectedDump);
        propertiesExpectedDump.put("favoriteColors", favoriteColorsExpectedDump);

        // This test fails, don't know why
        //            Map<String, Object> degreesExpectedDump = new HashMap<String, Object>();
        //            degreesExpectedDump.put(Key.TYPE.toString(), Type.HASH);
        //            Map<String, Map<String, Object>> degreeMapExpectedDump = new HashMap<String, Map<String, Object>>();
        //            Map<String, Object> degree1ExpectedDump = new HashMap<String, Object>();
        //            degree1ExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        //            degree1ExpectedDump.put(Key.VALUE.toString(), "Mathematics");
        //            degreeMapExpectedDump.put("BA", degree1ExpectedDump);
        //            Map<String, Object> degree2ExpectedDump = new HashMap<String, Object>();
        //            degree2ExpectedDump.put(Key.TYPE.toString(), Type.STRING);
        //            degree2ExpectedDump.put(Key.VALUE.toString(), "Computer Science");
        //            degreeMapExpectedDump.put("MS", degree2ExpectedDump);
        //            degreesExpectedDump.put(Key.VALUE.toString(), degreeMapExpectedDump);
        //            propertiesExpectedDump.put("degrees", degreesExpectedDump);

    }/*from  w  w w  .  j a  va2 s . c o  m*/

    expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);

    // Methods       
    SortedMap<String, Object> methodDump = getEmployeeMethodsExpectedDump(exposureLevel, "Doe");
    if (!methodDump.isEmpty()) {
        methodDump.put("boss()", supervisorExpectedDump);
    }
    expectedDump.put(Key.METHODS.toString(), methodDump);

    return expectedDump;
}

From source file:com.xpn.xwiki.store.migration.AbstractXWikiMigrationManager.java

/**
 * @return collection of {@link XWikiMigratorInterface} in ascending order, which need be executed.
 * @param context used everywhere//from www.j a  v  a  2  s.  c o  m
 * @throws Exception if any error
 */
protected Collection getNeededMigrations(XWikiContext context) throws Exception {
    XWikiDBVersion curversion = getDBVersion(context);
    SortedMap neededMigrations = new TreeMap();

    Map forcedMigrations = getForcedMigrations(context);
    if (!forcedMigrations.isEmpty()) {
        neededMigrations.putAll(forcedMigrations);
    } else {
        Set ignoredMigrations = new HashSet(Arrays
                .asList(context.getWiki().getConfig().getPropertyAsList("xwiki.store.migration.ignored")));
        List allMigrations = getAllMigrations(context);
        for (Iterator it = allMigrations.iterator(); it.hasNext();) {
            XWikiMigratorInterface migrator = (XWikiMigratorInterface) it.next();
            if (ignoredMigrations.contains(migrator.getClass().getName())
                    || ignoredMigrations.contains(migrator.getVersion().toString())) {
                continue;
            }
            if (migrator.getVersion().compareTo(curversion) >= 0) {
                XWikiMigration migration = new XWikiMigration(migrator, false);
                neededMigrations.put(migrator.getVersion(), migration);
            }
        }
    }

    Collection neededMigrationsAsCollection = neededMigrations.values();
    if (LOGGER.isInfoEnabled()) {
        if (!neededMigrations.isEmpty()) {
            LOGGER.info("Current storage version = [" + curversion.toString() + "]");
            LOGGER.info("List of migrations that will be executed:");
            for (Iterator it = neededMigrationsAsCollection.iterator(); it.hasNext();) {
                XWikiMigration migration = (XWikiMigration) it.next();
                if (migration.isForced || migration.migrator.shouldExecute(this.startupVersion)) {
                    LOGGER.info("  " + migration.migrator.getName() + " - "
                            + migration.migrator.getDescription() + (migration.isForced ? " (forced)" : ""));
                }
            }
        } else {
            LOGGER.info(
                    "No storage migration required since current version is [" + curversion.toString() + "]");
        }
    }

    return neededMigrationsAsCollection;
}

From source file:hudson.model.Job.java

/**
 * Returns the last build./*from w w  w.  ja v  a  2s. co  m*/
 * @see LazyBuildMixIn#getLastBuild
 */
@Exported
@QuickSilver
public RunT getLastBuild() {
    SortedMap<Integer, ? extends RunT> runs = _getRuns();

    if (runs.isEmpty())
        return null;
    return runs.get(runs.firstKey());
}

From source file:hudson.model.Job.java

/**
 * Returns the oldest build in the record.
 * @see LazyBuildMixIn#getFirstBuild//from  www . ja v  a  2  s  .c o  m
 */
@Exported
@QuickSilver
public RunT getFirstBuild() {
    SortedMap<Integer, ? extends RunT> runs = _getRuns();

    if (runs.isEmpty())
        return null;
    return runs.get(runs.lastKey());
}

From source file:hudson.model.Job.java

/**
 * Gets the latest build #m that satisfies <tt>m&lt;=n</tt>.
 * /*from  w  ww  .  j av  a2s .co m*/
 * This is useful when you'd like to fetch a build but the exact build might
 * be already gone (deleted, rotated, etc.)
 * @see LazyBuildMixIn#getNearestOldBuild
 */
public RunT getNearestOldBuild(int n) {
    SortedMap<Integer, ? extends RunT> m = _getRuns().tailMap(n);
    if (m.isEmpty())
        return null;
    return m.get(m.firstKey());
}