Exemplo n.º 1
0
 public static void main(String[] args) {
   WebResource wr =
       new WebResource(
           "https://path.com/p/MmptI",
           new Place(42, "Place", 0, 0),
           new Message("42", "horst", Calendar.getInstance().getTime(), "this sucks"));
   wr.resolve();
   log.info(wr);
 }
Exemplo n.º 2
0
  public static Collection<WebResource> resolveLinks(List<Message> messages, final Place p) {

    List<Future<WebResource>> futures = new ArrayList<Future<WebResource>>();

    for (final Message m : messages) {
      for (final String url : m.findLinks()) {
        futures.add(
            resolverThreadPool.submit(
                new Callable<WebResource>() {
                  @Override
                  public WebResource call() throws Exception {
                    return new WebResource(url, p, m).resolve();
                  }
                }));
      }
    }
    Map<String, WebResource> resources = new HashMap<String, WebResource>();
    do {
      Iterator<Future<WebResource>> futureIterator = futures.iterator();
      while (futureIterator.hasNext()) {
        Future<WebResource> wrf = futureIterator.next();
        if (wrf.isDone()) {
          futureIterator.remove();
          try {
            WebResource wr = wrf.get();
            if (wr == null || wr.hadErrors()) {
              continue;
            }
            URL u = wr.getUrlObj();
            if (u == null) {
              continue;
            }
            if (!resources.containsKey(wr.getUrl())) {
              resources.put(wr.getUrl(), wr);
            }

          } catch (Exception e) {
            // should not happen, we have already checked if
            // the
            // result is ready.
            e.printStackTrace();
          }
        }
      }
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        log.warn("Unable to sleep");
      }

    } while (futures.size() > 0);
    return resources.values();
  }