/** Times out old responders. */
  protected void timeout(long now) {
    List<Responder> responders = null;
    synchronized (RESPONDERS) {
      Responder next = null;
      for (int i = RESPONDERS.size() - 1; i >= 0; i--) {
        next = RESPONDERS.get(i);
        if (next.dead <= now) {
          REQUESTED.remove(next.urn);
          TIMEOUTS.add(next.urn);
          if (responders == null) responders = new ArrayList<Responder>(2);
          responders.add(next);
          RESPONDERS.remove(i);
          next = null;
        } else {
          break;
        }
      }
    }

    // Now call outside of lock.
    if (responders != null) {
      for (int i = 0; i < responders.size(); i++) {
        Responder next = responders.get(i);
        if (LOG.isDebugEnabled())
          LOG.debug("Timing out responder: " + next + " for URN: " + next.urn);
        try {
          next.observer.handleResponse(next.urn, null);
        } catch (Throwable t) {
          ErrorService.error(t, "Content ContentResponseData Error");
        }
      }
    }
  }
  /** Adds XML to the responses in a QueryReply. */
  public static boolean addXMLToResponses(
      QueryReply qr, LimeXMLDocumentHelper limeXMLDocumentHelper) {
    // get xml collection string, then get dis-aggregated docs, then
    // in loop
    // you can match up metadata to responses
    String xmlCollectionString = "";
    try {
      LOG.trace("Trying to do uncompress XML.....");
      byte[] xmlCompressed = qr.getXMLBytes();
      if (xmlCompressed.length > 1) {
        byte[] xmlUncompressed = LimeXMLUtils.uncompress(xmlCompressed);
        xmlCollectionString = new String(xmlUncompressed, "UTF-8");
      }
    } catch (UnsupportedEncodingException use) {
      // b/c this should never happen, we will show and error
      // if it ever does for some reason.
      // we won't throw a BadPacketException here but we will show it.
      // the uee will effect the xml part of the reply but we could
      // still show the reply so there shouldn't be any ill effect if
      // xmlCollectionString is ""
      ErrorService.error(use);
    } catch (IOException ignored) {
    }

    // valid response, no XML in EQHD.
    if (xmlCollectionString.equals("")) return true;

    Response[] responses;
    int responsesLength;
    try {
      responses = qr.getResultsArray();
      responsesLength = responses.length;
    } catch (BadPacketException bpe) {
      LOG.trace("Unable to get responses", bpe);
      return false;
    }

    if (LOG.isDebugEnabled()) LOG.debug("xmlCollectionString = " + xmlCollectionString);

    List<LimeXMLDocument[]> allDocsArray =
        limeXMLDocumentHelper.getDocuments(xmlCollectionString, responsesLength);

    for (int i = 0; i < responsesLength; i++) {
      Response response = responses[i];
      LimeXMLDocument[] metaDocs;
      for (int schema = 0; schema < allDocsArray.size(); schema++) {
        metaDocs = allDocsArray.get(schema);
        // If there are no documents in this schema, try another.
        if (metaDocs == null) continue;
        // If this schema had a document for this response, use it.
        if (metaDocs[i] != null) {
          response.setDocument(metaDocs[i]);
          break; // we only need one, so break out.
        }
      }
    }
    return true;
  }
Exemple #3
0
 /** Deflates (compresses) the data. */
 public static byte[] deflate(byte[] data) {
   OutputStream dos = null;
   Deflater def = null;
   try {
     def = new Deflater();
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     dos = new DeflaterOutputStream(baos, def);
     dos.write(data, 0, data.length);
     dos.close(); // flushes bytes
     return baos.toByteArray();
   } catch (IOException impossible) {
     ErrorService.error(impossible);
     return null;
   } finally {
     close(dos);
     close(def);
   }
 }