/**
   * method to make adjustments for known counter problems. This method depends on the availability
   * of certain counters, which is generally guaranteed by the synchWithTarget() method.
   */
  protected void kludge(Map<String, Monitor> map) {
    if (Boolean.getBoolean("sun.jvmstat.perfdata.disableKludge")) {
      // bypass all kludges
      return;
    }

    String name = "java.vm.version";
    StringMonitor jvm_version = (StringMonitor) map.get(name);
    if (jvm_version == null) {
      jvm_version = (StringMonitor) findByAlias(name);
    }

    name = "java.vm.name";
    StringMonitor jvm_name = (StringMonitor) map.get(name);
    if (jvm_name == null) {
      jvm_name = (StringMonitor) findByAlias(name);
    }

    name = "hotspot.vm.args";
    StringMonitor args = (StringMonitor) map.get(name);
    if (args == null) {
      args = (StringMonitor) findByAlias(name);
    }

    assert ((jvm_name != null) && (jvm_version != null) && (args != null));

    if (jvm_name.stringValue().indexOf("HotSpot") >= 0) {
      if (jvm_version.stringValue().startsWith("1.4.2")) {
        kludgeMantis(map, args);
      }
    }
  }
Beispiel #2
0
  public String _toclasspath(String args[]) {
    verifyCommand(args, _toclasspathHelp, null, 2, 3);
    boolean cl = true;
    if (args.length > 2) cl = Boolean.valueOf(args[2]);

    Collection<String> names = Processor.split(args[1]);
    Collection<String> paths = new ArrayList<String>(names.size());
    for (String name : names) {
      String path = name.replace('.', '/') + (cl ? ".class" : "");
      paths.add(path);
    }
    return Processor.join(paths, ",");
  }
Beispiel #3
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType(XML_RESPONSE_HEADER); // Talkback happens in XML form.
    response.setCharacterEncoding("UTF-8"); // Unicode++
    request.setCharacterEncoding("UTF-8");

    PrintWriter out = null; // The talkback buffer.

    // handle startrecord
    Integer startRecord = 0;

    if (!(request.getParameter("startRecord") == null)) {
      try {
        startRecord = Integer.parseInt(request.getParameter("startRecord")) - 1;
      } catch (NumberFormatException e) {
        startRecord = 0;
      }
    }

    // maximumrecords
    Integer maximumRecords = Integer.parseInt(this.config.getProperty("default_maximumRecords"));
    if (!(request.getParameter("maximumRecords") == null)) {
      maximumRecords = Integer.parseInt(request.getParameter("maximumRecords"));
    }

    // operation
    String operation = request.getParameter("operation");

    // x_collection
    String x_collection = request.getParameter("x-collection");
    if (x_collection == null) x_collection = this.config.getProperty("default_x_collection");
    if (x_collection == null) operation = null;

    // sortkeys
    String sortKeys = request.getParameter("sortKeys");

    // sortorder
    String sortOrder = request.getParameter("sortOrder");

    // recordschema
    String recordSchema = request.getParameter("recordSchema");
    if (recordSchema == null) recordSchema = "dc";

    if (recordSchema.equalsIgnoreCase("dcx")) {
      recordSchema = "dcx";
    }

    if (recordSchema.equalsIgnoreCase("solr")) {
      recordSchema = "solr";
    }

    // query request
    String query = request.getParameter("query");
    String q = request.getParameter("q");

    // who is requestor ?
    String remote_ip = request.getHeader("X-FORWARDED-FOR");

    if (remote_ip == null) {
      remote_ip = request.getRemoteAddr().trim();
    } else {
      remote_ip = request.getHeader("X-FORWARDED-FOR");
    }

    // handle debug
    Boolean debug = Boolean.parseBoolean(request.getParameter("debug"));
    if (!debug) {
      out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
    }

    // handle query
    if ((query == null) && (q != null)) {
      query = q;
    } else {
      if ((query != null) && (q == null)) {
        q = query;
      } else {
        operation = null;
      }
    }

    // handle operation
    if (operation == null) {
      if (query != null) {
        operation = "searchRetrieve";
      } else {
        operation = "explain";
      }
    }

    //  searchRetrieve
    if (operation.equalsIgnoreCase("searchRetrieve")) {
      if (query == null) {
        operation = "explain";
        log.debug(operation + ":" + query);
      }
    }

    // start talking back.
    String[] sq = {""};
    String solrquery = "";

    // facet

    String facet = null;
    List<FacetField> fct = null;

    if (request.getParameter("facet") != null) {
      facet = request.getParameter("facet");
      log.debug("facet : " + facet);
    }

    if (operation == null) {
      operation = "searchretrieve";
    } else { // explain response
      if (operation.equalsIgnoreCase("explain")) {
        log.debug("operation = explain");
        out.write("<srw:explainResponse xmlns:srw=\"http://www.loc.gov/zing/srw/\">");
        out.write("</srw:explainResponse>");
      } else { // DEBUG routine
        operation = "searchretrieve";

        String triplequery = null;

        if (query.matches(".*?\\[.+?\\].*?")) { // New symantic syntax
          triplequery = symantic_query(query);
          query = query.split("\\[")[0] + " " + triplequery;
          log.fatal(triplequery);

          solrquery = CQLtoLucene.translate(query, log, config);
        } else {
          solrquery = CQLtoLucene.translate(query, log, config);
        }
        log.debug(solrquery);

        if (debug == true) {
          response.setContentType(HTML_RESPONSE_HEADER);
          out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
          out.write("<html><body>\n\n");
          out.write("'" + remote_ip + "'<br>\n");
          out.write("<form action='http://www.kbresearch.nl/kbSRU'>");
          out.write("<input type=text name=q value='" + query + "' size=120>");
          out.write("<input type=hidden name=debug value=True>");
          out.write("<input type=submit>");
          out.write("<table border=1><tr><td>");
          out.write("q</td><td>" + query + "</td></tr><tr>");
          out.write("<td>query out</td><td>" + URLDecoder.decode(solrquery) + "</td></tr>");
          out.write(
              "<tr><td>SOLR_URL</td><td> <a href='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/?q="
                  + solrquery
                  + "'>"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/select/?q="
                  + solrquery
                  + "</a><br>"
                  + this.config.getProperty("solr_url")
                  + solrquery
                  + "</td></tr>");
          out.write(
              "<b>SOLR_QUERY</b> : <BR> <iframe width=900 height=400 src='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/../?q="
                  + solrquery
                  + "'></iframe><BR>");
          out.write(
              "<b>SRU_QUERY</b> : <BR> <a href="
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "'>"
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "</a><br><iframe width=901 height=400 src='http://www.kbresearch.nl/kbSRU/?q="
                  + query
                  + "'></iframe><BR>");
          out.write(
              "<br><b>JSRU_QUERY</b> : <BR><a href='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection="
                  + x_collection
                  + "'>http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC</a><br><iframe width=900 height=400 src='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC'></iframe>");

        } else { // XML SearchRetrieve response
          String url =
              this.config.getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl");
          String buffer = "";
          CommonsHttpSolrServer server = null;
          server = new CommonsHttpSolrServer(url);
          log.fatal("URSING " + url);
          server.setParser(new XMLResponseParser());
          int numfound = 0;
          try {
            SolrQuery do_query = new SolrQuery();
            do_query.setQuery(solrquery);
            do_query.setRows(maximumRecords);
            do_query.setStart(startRecord);

            if ((sortKeys != null) && (sortKeys.length() > 1)) {
              if (sortOrder != null) {
                if (sortOrder.equals("asc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.asc);
                }
                if (sortOrder.equals("desc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.desc);
                }
              } else {
                for (String str : sortKeys.trim().split(",")) {
                  str = str.trim();
                  if (str.length() > 1) {
                    if (str.equals("date")) {
                      do_query.setSortField("date_date", SolrQuery.ORDER.desc);
                      log.debug("SORTORDERDEBUG | DATE! " + str + " | ");
                      break;
                    } else {
                      do_query.setSortField(str + "_str", SolrQuery.ORDER.asc);
                      log.debug("SORTORDERDEBUG | " + str + " | ");
                      break;
                    }
                  }
                }
              }
            }

            if (facet != null) {
              if (facet.indexOf(",") > 1) {
                for (String str : facet.split(",")) {
                  if (str.indexOf("date") > 1) {
                    do_query.addFacetField(str);
                  } else {
                    do_query.addFacetField(str);
                  }
                  // do_query.setParam("facet.method", "enum");
                }
                // q.setFacetSort(false);
              } else {
                do_query.addFacetField(facet);
              }
              do_query.setFacet(true);
              do_query.setFacetMinCount(1);
              do_query.setFacetLimit(-1);
            }

            log.fatal(solrquery);

            QueryResponse rsp = null;
            boolean do_err = false;
            boolean do_sugg = false;
            SolrDocumentList sdl = null;
            String diag = "";
            StringBuffer suggest = new StringBuffer("");

            String content = "1";

            SolrQuery spellq = do_query;
            try {
              rsp = server.query(do_query);
            } catch (SolrServerException e) {
              String header = this.SRW_HEADER.replaceAll("\\$numberOfRecords", "0");
              out.write(header);
              diag = this.SRW_DIAG.replaceAll("\\$error", e.getMessage());
              do_err = true;
              rsp = null;
            }

            log.fatal("query done..");
            if (!(do_err)) { // XML dc response

              SolrDocumentList docs = rsp.getResults();
              numfound = (int) docs.getNumFound();
              int count = startRecord;
              String header =
                  this.SRW_HEADER.replaceAll("\\$numberOfRecords", Integer.toString(numfound));
              out.write(header);
              out.write("<srw:records>");

              Iterator<SolrDocument> iter = rsp.getResults().iterator();

              while (iter.hasNext()) {
                count += 1;
                if (recordSchema.equalsIgnoreCase("dc")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" xmlns:facets=\"info:srw/extension/4/facets\" >");
                  StringBuffer result = new StringBuffer("");

                  construct_lucene_dc(result, resultDoc);

                  out.write(result.toString());
                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("solr")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/solr</srw:recordSchema>");
                  out.write("<srw:recordData xmlns:expand=\"http://www.kbresearch.nl/expand\">");
                  StringBuffer result = new StringBuffer("");
                  construct_lucene_solr(result, resultDoc);
                  out.write(result.toString());

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("dcx")) { // XML dcx response
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData><srw_dc:dc xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/marc.relators/\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");

                  String dcx_data =
                      helpers.getOAIdcx(
                          "http://services.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                          log);
                  if (x_collection.equalsIgnoreCase("ggc-thes")) {
                    dcx_data =
                        helpers.getOAIdcx(
                            "http://serviceso.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                            log);
                  }

                  if (!(dcx_data.length() == 0)) {
                    out.write(dcx_data);
                  } else {
                    // Should not do this!!

                    out.write("<srw:record>");
                    out.write("<srw:recordPacking>xml</srw:recordPacking>");
                    out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                    out.write(
                        "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" >");
                    StringBuffer result = new StringBuffer("");

                    construct_lucene_dc(result, resultDoc);

                    out.write(result.toString());
                    out.write("</srw:recordData>");
                    out.write(
                        "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                    out.write("</srw:record>");
                  }

                  out.write("</srw_dc:dc>");

                  StringBuffer expand_data;
                  boolean expand = false;

                  if (content.startsWith("GGC-THES:AC:")) {
                    String tmp_content = "";
                    tmp_content = content.replaceFirst("GGC-THES:AC:", "");
                    log.fatal("calling get");
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/general/lod_new/get/"
                                    + tmp_content
                                    + "?format=rdf",
                                log));
                    log.fatal("get finini");

                    if (expand_data.toString().length() > 4) {

                      out.write(
                          "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                      out.write(expand_data.toString());
                      expand = true;
                    }
                  } else {
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/ANP.cgi?q=" + content, log));
                    if (expand_data.toString().length() > 0) {
                      if (!expand) {
                        out.write(
                            "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                        expand = true;
                      }
                      out.write(expand_data.toString());
                    }
                  }
                  if (expand) {
                    out.write("</srw_dc:expand>");
                  }

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }
              }
            }

            if ((do_err) || (numfound == 0)) {
              log.fatal("I haz suggestions");

              try {
                spellq.setParam("spellcheck", true);
                spellq.setQueryType("/spell");
                server = new CommonsHttpSolrServer(url);
                rsp = server.query(spellq);
                sdl = rsp.getResults();
                SpellCheckResponse spell;
                spell = rsp.getSpellCheckResponse();
                List<SpellCheckResponse.Suggestion> suggestions = spell.getSuggestions();
                if (suggestions.isEmpty() == false) {
                  suggest.append("<srw:extraResponseData>");
                  suggest.append("<suggestions>");

                  for (SpellCheckResponse.Suggestion sugg : suggestions) {
                    suggest.append("<suggestionfor>" + sugg.getToken() + "</suggestionfor>");
                    for (String item : sugg.getSuggestions()) {
                      suggest.append("<suggestion>" + item + "</suggestion>");
                    }
                    suggest.append("</suggestions>");
                    suggest.append("</srw:extraResponseData>");
                  }
                  do_sugg = true;
                }
              } catch (Exception e) {
                rsp = null;
                // log.fatal(e.toString());
              }
              ;
            }
            ;

            if (!do_err) {
              if (facet != null) {

                try {
                  fct = rsp.getFacetFields();
                  out.write("<srw:facets>");

                  for (String str : facet.split(",")) {
                    out.write("<srw:facet>");
                    out.write("<srw:facetType>");
                    out.write(str);
                    out.write("</srw:facetType>");

                    for (FacetField f : fct) {
                      log.debug(f.getName());
                      // if (f.getName().equals(str+"_str") || (f.getName().equals(str+"_date")) ) {
                      List<FacetField.Count> facetEnties = f.getValues();
                      for (FacetField.Count fcount : facetEnties) {
                        out.write("<srw:facetValue>");
                        out.write("<srw:valueString>");
                        out.write(helpers.xmlEncode(fcount.getName()));
                        out.write("</srw:valueString>");
                        out.write("<srw:count>");
                        out.write(Double.toString(fcount.getCount()));
                        out.write("</srw:count>");
                        out.write("</srw:facetValue>");
                        //   }
                      }
                    }
                    out.write("</srw:facet>");
                  }
                  out.write("</srw:facets>");
                  startRecord += 1;
                } catch (Exception e) {
                }

                // log.fatal(e.toString()); }
              }
            } else {
              out.write(diag);
            }
            out.write("</srw:records>"); // SearchRetrieve response footer
            String footer = this.SRW_FOOTER.replaceAll("\\$query", helpers.xmlEncode(query));
            footer = footer.replaceAll("\\$startRecord", (startRecord).toString());
            footer = footer.replaceAll("\\$maximumRecords", maximumRecords.toString());
            footer = footer.replaceAll("\\$recordSchema", recordSchema);
            if (do_sugg) {
              out.write(suggest.toString());
            }
            out.write(footer);
          } catch (MalformedURLException e) {
            out.write(e.getMessage());
          } catch (IOException e) {
            out.write("TO ERR is Human");
          }
        }
      }
    }
    out.close();
  }
/**
 * Serializes a <code>DBObject</code> into a string that can be sent to the database.
 *
 * <p>There is a pool of available encoders. Create a new one as follows:
 *
 * <blockquote>
 *
 * </pre>
 *
 * ByteEncoder encoder = ByteEncoder.get(); // try forever until an encoder is available </pre>
 *
 * </blockquote>
 *
 * <p>Each key/value pair in the <code>DBObject</code> is encoded in the following format:
 *
 * <blockquote>
 *
 * <i>&lt;type (</i><code>byte</code><i>)&gt;&lt;name (</i><code>String</code></i>)&gt;&lt;0 (</i>
 * <code>byte</code><i>)&gt;&lt;data (serialized data)&gt;</i>
 *
 * </blockquote>
 *
 * For example:
 *
 * <blockquote>
 *
 * &lt;<code>NUMBER</code>&gt;&lt;name&gt;0&lt;double&gt; <code>// NUMBER = 1</code><br>
 * &lt;<code>STRING</code>&gt;&lt;name&gt;0&lt;len&gt;&lt;string&gt;0 <code>// STRING = 2</code>
 *
 * </blockquote>
 */
public class ByteEncoder extends Bytes {

  static final boolean DEBUG = Boolean.getBoolean("DEBUG.BE");

  // things that won't get sent in the scope
  static final Set<String> BAD_GLOBALS = new HashSet<String>();

  static {
    BAD_GLOBALS.add("db");
    BAD_GLOBALS.add("local");
    BAD_GLOBALS.add("core");
    BAD_GLOBALS.add("args"); // TODO: should we get rid of this
    BAD_GLOBALS.add("obj"); // TODO: get rid of this
  }

  /**
   * Whether a given field is generated by the db for use by the db
   *
   * @param o an object to check
   * @return whether the object is a db field
   */
  public static boolean dbOnlyField(Object o) {
    if (o == null) return false;

    if (o instanceof String) return dbOnlyField(o.toString());

    return false;
  }

  /**
   * Whether a given field is generated by the db for use by the db
   *
   * @param s a string to check
   * @return whether the string is a db field
   */
  public static boolean dbOnlyField(String s) {
    return s.equals("_ns") || s.equals("_save") || s.equals("_update");
  }

  /**
   * Fetches a new <code>ByteEncoder</code> from the pool of available <code>ByteEncoder</code>s.
   *
   * @return a new <code>ByteEncoder</code>
   */
  public static ByteEncoder get() {
    return _pool.get();
  }

  /**
   * Get the current position of this ByteEncoder
   *
   * @return
   */
  public long getPosition() {
    return _buf.position();
  }

  /**
   * Get the remaining capacity of this ByteEncoder
   *
   * @return
   */
  public long getRemaining() {
    return _buf.remaining();
  }

  /** Resets and returns this encoder to the pool. */
  protected void done() {
    reset();
    _pool.done(this);
  }

  static final SimplePool<ByteEncoder> _pool =
      new SimplePool<ByteEncoder>("ByteEncoders", NUM_ENCODERS, NUM_ENCODERS * 2) {
        protected ByteEncoder createNew() {
          if (D) System.out.println("creating new ByteEncoder");
          return new ByteEncoder();
        }

        protected long memSize(ByteEncoder d) {
          return d._buf.capacity() + (2 * MAX_STRING) + 1024;
        }
      };

  // ----

  private ByteEncoder() {
    _buf = ByteBuffer.allocateDirect(MAX_OBJECT_SIZE + 2048);
    _buf.order(Bytes.ORDER);
  }

  /**
   * Returns the bytes in the bytebuffer. Attempts to leave the bytebuffer in the same state. Note
   * that mark, if set, is lost.
   *
   * @return array of bytes
   */
  public byte[] getBytes() {

    int pos = _buf.position();
    int limit = _buf.limit();

    flip();

    byte[] arr = new byte[_buf.limit()];

    _buf.get(arr);

    flip();

    _buf.position(pos);
    _buf.limit(limit);

    return arr;
  }

  /** Returns encoder to its starting state, ready to encode an object. */
  protected void reset() {
    _buf.position(0);
    _buf.limit(_buf.capacity());
    _flipped = false;
    _dontRef.clear();
  }

  /** Switches the encoder from being write-only to being read-only. */
  protected void flip() {
    _buf.flip();
    _flipped = true;
  }

  /**
   * Encodes a <code>DBObject</code>. This is for the higher level api calls
   *
   * <p>If encoding an object fails, the buffer will be reset to the position prior to this put call
   * and a BufferOverflowException will be thrown
   *
   * @param o the object to encode
   * @return the number of characters in the encoding
   */
  public int putObject(DBObject o) {

    _buf.mark();

    try {
      return putObject(null, o);
    } catch (BufferOverflowException bof) {
      // reset to marked offset and wipe any written data
      _buf.reset();
      _buf.put(new byte[_buf.remaining()]);
      _buf.reset();

      throw new BufferOverflowException();
    }
  }

  /** this is really for embedded objects */
  private int putObject(String name, DBObject o) {
    if (o == null) throw new NullPointerException("can't save a null object");

    if (DEBUG)
      System.out.println(
          "putObject : " + name + " [" + o.getClass() + "]" + " # keys " + o.keySet().size());

    if (_flipped) throw new IllegalStateException("already flipped");
    final int start = _buf.position();

    byte myType = OBJECT;
    if (o instanceof List) myType = ARRAY;

    if (_handleSpecialObjects(name, o)) return _buf.position() - start;

    if (name != null) {
      _put(myType, name);
    }

    final int sizePos = _buf.position();
    _buf.putInt(0); // leaving space for this.  set it at the end

    List transientFields = null;

    if (myType == OBJECT) {
      if (o.containsField("_id")) _putObjectField("_id", o.get("_id"));

      {
        Object temp = o.get("_transientFields");
        if (temp instanceof List) transientFields = (List) temp;
      }
    }

    for (String s : o.keySet()) {

      if (s.equals("_id")) continue;

      if (transientFields != null && transientFields.contains(s)) continue;

      Object val = o.get(s);

      _putObjectField(s, val);
    }
    _buf.put(EOO);

    _buf.putInt(sizePos, _buf.position() - sizePos);
    return _buf.position() - start;
  }

  private void _putObjectField(String name, Object val) {

    if (dbOnlyField(name) || name.equals("_transientFields")) return;

    if (DEBUG) System.out.println("\t put thing : " + name);

    if (name.equals("$where") && val instanceof String) {
      _put(CODE, name);
      _putValueString(val.toString());
      return;
    }

    val = Bytes.applyEncodingHooks(val);

    if (val == null) putNull(name);
    else if (val instanceof Date) putDate(name, (Date) val);
    else if (val instanceof Number) putNumber(name, (Number) val);
    else if (val instanceof String) putString(name, val.toString());
    else if (val instanceof ObjectId) putObjectId(name, (ObjectId) val);
    else if (val instanceof DBObject) putObject(name, (DBObject) val);
    else if (val instanceof Boolean) putBoolean(name, (Boolean) val);
    else if (val instanceof Pattern) putPattern(name, (Pattern) val);
    else if (val instanceof DBRegex) {
      putDBRegex(name, (DBRegex) val);
    } else if (val instanceof Map) putMap(name, (Map) val);
    else if (val instanceof List) putList(name, (List) val);
    else if (val instanceof byte[]) putBinary(name, (byte[]) val);
    else if (val instanceof DBBinary) putBinary(name, (DBBinary) val);
    else if (val.getClass().isArray()) putList(name, Arrays.asList((Object[]) val));
    else if (val instanceof DBPointer) {

      // temporary - there's the notion of "special object" , but for simple level 0...
      DBPointer r = (DBPointer) val;
      putDBPointer(name, r._ns, (ObjectId) r._id);
    } else if (val instanceof DBRefBase) {
      putDBRef(name, (DBRefBase) val);
    } else if (val instanceof DBSymbol) {
      putSymbol(name, (DBSymbol) val);
    } else if (val instanceof DBUndefined) {
      putUndefined(name);
    } else if (val instanceof DBTimestamp) {
      putTimestamp(name, (DBTimestamp) val);
    } else throw new IllegalArgumentException("can't serialize " + val.getClass());
  }

  private void putList(String name, List l) {
    _put(ARRAY, name);
    final int sizePos = _buf.position();
    _buf.putInt(0);

    for (int i = 0; i < l.size(); i++) _putObjectField(String.valueOf(i), l.get(i));

    _buf.put(EOO);
    _buf.putInt(sizePos, _buf.position() - sizePos);
  }

  private void putMap(String name, Map m) {
    _put(OBJECT, name);
    final int sizePos = _buf.position();
    _buf.putInt(0);

    for (Object key : m.keySet()) _putObjectField(key.toString(), m.get(key));

    _buf.put(EOO);
    _buf.putInt(sizePos, _buf.position() - sizePos);
  }

  private boolean _handleSpecialObjects(String name, DBObject o) {

    if (o == null) return false;

    if (o instanceof DBCollection) {
      DBCollection c = (DBCollection) o;
      putDBPointer(name, c.getName(), Bytes.COLLECTION_REF_ID);
      return true;
    }

    if (!_dontRefContains(o) && name != null && o instanceof DBPointer) {
      DBPointer r = (DBPointer) o;
      putDBPointer(name, r._ns, (ObjectId) r._id);
      return true;
    }

    if (!(o instanceof List) && o.get(Bytes.NO_REF_HACK) != null) {
      o.removeField(Bytes.NO_REF_HACK);
      return false;
    }

    if (!_dontRefContains(o) && name != null && !(o instanceof List) && cameFromDB(o)) {
      putDBPointer(name, o.get("_ns").toString(), (ObjectId) (o.get("_id")));
      return true;
    }

    return false;
  }

  protected int putNull(String name) {
    int start = _buf.position();
    _put(NULL, name);
    return _buf.position() - start;
  }

  protected int putUndefined(String name) {
    int start = _buf.position();
    _put(UNDEFINED, name);
    return _buf.position() - start;
  }

  protected int putTimestamp(String name, DBTimestamp ts) {
    int start = _buf.position();
    _put(TIMESTAMP, name);
    _buf.putInt(ts.getTime());
    _buf.putInt(ts.getInc());
    return _buf.position() - start;
  }

  protected int putBoolean(String name, Boolean b) {
    int start = _buf.position();
    _put(BOOLEAN, name);
    _buf.put(b ? (byte) 0x1 : (byte) 0x0);
    return _buf.position() - start;
  }

  protected int putDate(String name, Date d) {
    int start = _buf.position();
    _put(DATE, name);
    _buf.putLong(d.getTime());
    return _buf.position() - start;
  }

  protected int putNumber(String name, Number n) {
    int start = _buf.position();
    if (n instanceof Integer) {
      _put(NUMBER_INT, name);
      _buf.putInt(n.intValue());
    } else if (n instanceof Long) {
      _put(NUMBER_LONG, name);
      _buf.putLong(n.longValue());
    } else {
      _put(NUMBER, name);
      _buf.putDouble(n.doubleValue());
    }
    return _buf.position() - start;
  }

  protected void putBinary(String name, byte[] data) {

    _put(BINARY, name);
    _buf.putInt(4 + data.length);

    _buf.put(B_BINARY);
    _buf.putInt(data.length);
    int before = _buf.position();
    _buf.put(data);
    int after = _buf.position();

    com.mongodb.util.MyAsserts.assertEquals(after - before, data.length);
  }

  protected void putBinary(String name, DBBinary val) {
    _put(BINARY, name);
    _buf.putInt(val._data.length);
    _buf.put(val._type);
    _buf.put(val._data);
  }

  protected int putSymbol(String name, DBSymbol s) {
    return _putString(name, s.getSymbol(), SYMBOL);
  }

  protected int putString(String name, String s) {
    return _putString(name, s, STRING);
  }

  private int _putString(String name, String s, byte type) {
    int start = _buf.position();
    _put(type, name);
    _putValueString(s);
    return _buf.position() - start;
  }

  protected int putObjectId(String name, ObjectId oid) {
    int start = _buf.position();
    _put(OID, name);
    _buf.putInt(oid._time);
    _buf.putInt(oid._machine);
    _buf.putInt(oid._inc);
    return _buf.position() - start;
  }

  protected int putDBPointer(String name, String ns, ObjectId oid) {
    int start = _buf.position();
    _put(REF, name);

    _putValueString(ns);
    _buf.putInt(oid._time);
    _buf.putInt(oid._machine);
    _buf.putInt(oid._inc);

    return _buf.position() - start;
  }

  protected void putDBRef(String name, DBRefBase ref) {
    _put(OBJECT, name);
    final int sizePos = _buf.position();
    _buf.putInt(0);

    _putObjectField("$ref", ref.getRef());
    _putObjectField("$id", ref.getId());

    _buf.put(EOO);
    _buf.putInt(sizePos, _buf.position() - sizePos);
  }

  private int putDBRegex(String name, DBRegex regex) {

    int start = _buf.position();
    _put(REGEX, name);
    _put(regex.getPattern());

    String options = regex.getOptions();

    TreeMap<Character, Character> sm = new TreeMap<Character, Character>();

    for (int i = 0; i < options.length(); i++) {
      sm.put(options.charAt(i), options.charAt(i));
    }

    StringBuffer sb = new StringBuffer();

    for (char c : sm.keySet()) {
      sb.append(c);
    }

    _put(sb.toString());
    return _buf.position() - start;
  }

  private int putPattern(String name, Pattern p) {
    int start = _buf.position();
    _put(REGEX, name);
    _put(p.pattern());
    _put(patternFlags(p.flags()));
    return _buf.position() - start;
  }

  // ----------------------------------------------

  /** Encodes the type and key. */
  private void _put(byte type, String name) {
    _buf.put(type);
    _put(name);
  }

  void _putValueString(String s) {
    int lenPos = _buf.position();
    _buf.putInt(0); // making space for size
    int strLen = _put(s);
    _buf.putInt(lenPos, strLen);
  }

  int _put(String name) {

    _cbuf.position(0);
    _cbuf.limit(_cbuf.capacity());
    _cbuf.append(name);

    _cbuf.flip();
    final int start = _buf.position();
    _encoder.encode(_cbuf, _buf, false);

    _buf.put((byte) 0);

    return _buf.position() - start;
  }

  boolean _dontRefContains(Object o) {
    if (_dontRef.size() == 0) return false;
    return _dontRef.peek().contains(o);
  }

  private final CharBuffer _cbuf = CharBuffer.allocate(MAX_STRING);
  private final CharsetEncoder _encoder = _utf8.newEncoder();
  private Stack<IdentitySet> _dontRef = new Stack<IdentitySet>();

  private boolean _flipped = false;
  final ByteBuffer _buf;
}
Beispiel #5
0
  /**
   * Reads the data from a given reader.
   *
   * @param aProject the project to read the settings to;
   * @param aReader the reader to read the data from, cannot be <code>null</code>.
   * @throws IOException in case of I/O problems.
   */
  @SuppressWarnings("boxing")
  public static void read(final StubDataSet aDataSet, final Reader aReader) throws IOException {
    int size = -1;
    Integer rate = null, channels = null, enabledChannels = null;
    long triggerPos = -1L;
    long absLen = -1L;

    // assume 'new' file format is in use, don't support uncompressed ones...
    boolean compressed = true;

    final BufferedReader br = new BufferedReader(aReader);

    final List<String[]> dataValues = new ArrayList<String[]>();

    String line;
    while ((line = br.readLine()) != null) {
      // Determine whether the line is an instruction, or data...
      final Matcher instructionMatcher = OLS_INSTRUCTION_PATTERN.matcher(line);
      final Matcher dataMatcher = OLS_DATA_PATTERN.matcher(line);

      if (dataMatcher.matches()) {
        final String[] dataPair = new String[] {dataMatcher.group(1), dataMatcher.group(2)};
        dataValues.add(dataPair);
      } else if (instructionMatcher.matches()) {
        // Ok; found an instruction...
        final String instrKey = instructionMatcher.group(1);
        final String instrValue = instructionMatcher.group(2);

        if ("Size".equals(instrKey)) {
          size = safeParseInt(instrValue);
        } else if ("Rate".equals(instrKey)) {
          rate = safeParseInt(instrValue);
        } else if ("Channels".equals(instrKey)) {
          channels = safeParseInt(instrValue);
        } else if ("TriggerPosition".equals(instrKey)) {
          triggerPos = Long.parseLong(instrValue);
        } else if ("EnabledChannels".equals(instrKey)) {
          enabledChannels = safeParseInt(instrValue);
        } else if ("CursorEnabled".equals(instrKey)) {
          aDataSet.setCursorsEnabled(Boolean.parseBoolean(instrValue));
        } else if ("Compressed".equals(instrKey)) {
          compressed = Boolean.parseBoolean(instrValue);
        } else if ("AbsoluteLength".equals(instrKey)) {
          absLen = Long.parseLong(instrValue);
        } else if ("CursorA".equals(instrKey)) {
          final long value = safeParseLong(instrValue);
          if (value > Long.MIN_VALUE) {
            aDataSet.getCursor(0).setTimestamp(value);
          }
        } else if ("CursorB".equals(instrKey)) {
          final long value = safeParseLong(instrValue);
          if (value > Long.MIN_VALUE) {
            aDataSet.getCursor(1).setTimestamp(value);
          }
        } else if (instrKey.startsWith("Cursor")) {
          final int idx = safeParseInt(instrKey.substring(6));
          final long pos = Long.parseLong(instrValue);
          if (pos > Long.MIN_VALUE) {
            aDataSet.getCursor(idx).setTimestamp(pos);
          }
        }
      }
    }

    // Perform some sanity checks, make it not possible to import invalid
    // data...
    if (dataValues.isEmpty()) {
      throw new IOException("Data file does not contain any sample data!");
    }
    if (!compressed) {
      throw new IOException(
          "Uncompressed data file found! Please send this file to the OLS developers!");
    }
    // In case the size is not provided (as of 0.9.4 no longer mandatory),
    // take the length of the data values as size indicator...
    if (size < 0) {
      size = dataValues.size();
    }
    if (size != dataValues.size()) {
      throw new IOException("Data file is corrupt?! Data size does not match sample count!");
    }
    if (rate == null) {
      throw new IOException("Data file is corrupt?! Sample rate is not provided!");
    }
    if ((channels == null) || (channels <= 0) || (channels > 32)) {
      throw new IOException("Data file is corrupt?! Channel count is not provided!");
    }
    // Make sure the enabled channels are defined...
    if (enabledChannels == null) {
      enabledChannels = NumberUtils.getBitMask(channels);
    }

    int[] values = new int[size];
    long[] timestamps = new long[size];

    try {
      for (int i = 0; i < size; i++) {
        final String[] dataPair = dataValues.get(i);

        values[i] = (int) Long.parseLong(dataPair[0], 16);
        timestamps[i] = Long.parseLong(dataPair[1], 10) & Long.MAX_VALUE;
      }
    } catch (final NumberFormatException exception) {
      throw new IOException("Invalid data encountered.", exception);
    }

    // Allow the absolute length to be undefined, in which case the last
    // time stamp is used (+ some margin to be able to see the last
    // sample)...
    long absoluteLength = Math.max(absLen, timestamps[size - 1]);

    // Finally set the captured data, and notify all event listeners...
    aDataSet.setCapturedData(
        new CapturedData(
            values, timestamps, triggerPos, rate, channels, enabledChannels, absoluteLength));
  }
Beispiel #6
0
  /**
   * Writes the data to the given writer.
   *
   * @param aDataSet the project to write the settings for, cannot be <code>null</code> ;
   * @param aWriter the writer to write the data to, cannot be <code>null</code>.
   * @throws IOException in case of I/O problems.
   */
  public static void write(final StubDataSet aDataSet, final Writer aWriter) throws IOException {
    final BufferedWriter bw = new BufferedWriter(aWriter);

    final AcquisitionResult capturedData = aDataSet.getCapturedData();

    final Cursor[] cursors = aDataSet.getCursors();
    final boolean cursorsEnabled = aDataSet.isCursorsEnabled();

    try {
      final int[] values = capturedData.getValues();
      final long[] timestamps = capturedData.getTimestamps();

      bw.write(";Size: ");
      bw.write(Integer.toString(values.length));
      bw.newLine();

      bw.write(";Rate: ");
      bw.write(Integer.toString(capturedData.getSampleRate()));
      bw.newLine();

      bw.write(";Channels: ");
      bw.write(Integer.toString(capturedData.getChannels()));
      bw.newLine();

      bw.write(";EnabledChannels: ");
      bw.write(Integer.toString(capturedData.getEnabledChannels()));
      bw.newLine();

      if (capturedData.hasTriggerData()) {
        bw.write(";TriggerPosition: ");
        bw.write(Long.toString(capturedData.getTriggerPosition()));
        bw.newLine();
      }

      bw.write(";Compressed: ");
      bw.write(Boolean.toString(true));
      bw.newLine();

      bw.write(";AbsoluteLength: ");
      bw.write(Long.toString(capturedData.getAbsoluteLength()));
      bw.newLine();

      bw.write(";CursorEnabled: ");
      bw.write(Boolean.toString(cursorsEnabled));
      bw.newLine();

      for (int i = 0; cursorsEnabled && (i < cursors.length); i++) {
        if (cursors[i].isDefined()) {
          bw.write(String.format(";Cursor%d: ", Integer.valueOf(i)));
          bw.write(Long.toString(cursors[i].getTimestamp()));
          bw.newLine();
        }
      }
      for (int i = 0; i < values.length; i++) {
        bw.write(formatSample(values[i], timestamps[i]));
        bw.newLine();
      }
    } finally {
      bw.flush();
    }
  }
Beispiel #7
0
  @SuppressWarnings("unchecked")
  public static Object directBind(
      String name, Annotation[] annotations, String value, Class<?> clazz, Type type)
      throws Exception {
    Logger.trace(
        "directBind: value ["
            + value
            + "] annotation ["
            + Utils.join(annotations, " ")
            + "] Class ["
            + clazz
            + "]");

    boolean nullOrEmpty = value == null || value.trim().length() == 0;

    if (annotations != null) {
      for (Annotation annotation : annotations) {
        if (annotation.annotationType().equals(As.class)) {
          Class<? extends TypeBinder<?>> toInstanciate = ((As) annotation).binder();
          if (!(toInstanciate.equals(As.DEFAULT.class))) {
            // Instantiate the binder
            TypeBinder<?> myInstance = toInstanciate.newInstance();
            return myInstance.bind(name, annotations, value, clazz, type);
          }
        }
      }
    }

    // custom types
    for (Class<?> c : supportedTypes.keySet()) {
      Logger.trace("directBind: value [" + value + "] c [" + c + "] Class [" + clazz + "]");
      if (c.isAssignableFrom(clazz)) {
        Logger.trace("directBind: isAssignableFrom is true");
        return supportedTypes.get(c).bind(name, annotations, value, clazz, type);
      }
    }

    // application custom types
    for (Class<TypeBinder<?>> c : Play.classloader.getAssignableClasses(TypeBinder.class)) {
      if (c.isAnnotationPresent(Global.class)) {
        Class<?> forType =
            (Class) ((ParameterizedType) c.getGenericInterfaces()[0]).getActualTypeArguments()[0];
        if (forType.isAssignableFrom(clazz)) {
          return c.newInstance().bind(name, annotations, value, clazz, type);
        }
      }
    }

    // raw String
    if (clazz.equals(String.class)) {
      return value;
    }

    // Enums
    if (Enum.class.isAssignableFrom(clazz)) {
      if (nullOrEmpty) {
        return null;
      }
      return Enum.valueOf((Class<Enum>) clazz, value);
    }

    // int or Integer binding
    if (clazz.getName().equals("int") || clazz.equals(Integer.class)) {
      if (nullOrEmpty) {
        return clazz.isPrimitive() ? 0 : null;
      }

      return Integer.parseInt(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
    }

    // long or Long binding
    if (clazz.getName().equals("long") || clazz.equals(Long.class)) {
      if (nullOrEmpty) {
        return clazz.isPrimitive() ? 0l : null;
      }

      return Long.parseLong(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
    }

    // byte or Byte binding
    if (clazz.getName().equals("byte") || clazz.equals(Byte.class)) {
      if (nullOrEmpty) {
        return clazz.isPrimitive() ? (byte) 0 : null;
      }

      return Byte.parseByte(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
    }

    // short or Short binding
    if (clazz.getName().equals("short") || clazz.equals(Short.class)) {
      if (nullOrEmpty) {
        return clazz.isPrimitive() ? (short) 0 : null;
      }

      return Short.parseShort(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
    }

    // float or Float binding
    if (clazz.getName().equals("float") || clazz.equals(Float.class)) {
      if (nullOrEmpty) {
        return clazz.isPrimitive() ? 0f : null;
      }

      return Float.parseFloat(value);
    }

    // double or Double binding
    if (clazz.getName().equals("double") || clazz.equals(Double.class)) {
      if (nullOrEmpty) {
        return clazz.isPrimitive() ? 0d : null;
      }

      return Double.parseDouble(value);
    }

    // BigDecimal binding
    if (clazz.equals(BigDecimal.class)) {
      if (nullOrEmpty) {
        return null;
      }

      return new BigDecimal(value);
    }

    // boolean or Boolean binding
    if (clazz.getName().equals("boolean") || clazz.equals(Boolean.class)) {
      if (nullOrEmpty) {
        return clazz.isPrimitive() ? false : null;
      }

      if (value.equals("1")
          || value.toLowerCase().equals("on")
          || value.toLowerCase().equals("yes")) {
        return true;
      }

      return Boolean.parseBoolean(value);
    }

    return null;
  }
public class UnitTestObserverImpl implements UnitTestObserver {

  private static DUnitRun dunitRun = null;

  private static boolean dunitRecordResults = Boolean.getBoolean("dunitRecordResults");

  public UnitTestObserverImpl() {}

  public void incCurrentTestCount() {
    // TODO Auto-generated method stub
    SwarmBB.incCurrentTestCount();
  }

  public void testTypeDetected(TestType t) {
    // TODO Auto-generated method stub
    SwarmBB.setTestType(t);
  }

  public void totalTestCountDetected(int count) {
    // TODO Auto-generated method stub
    SwarmBB.setTotalTestCount(count);
  }

  public long getCurrentTestCount() {
    return SwarmBB.getCurrentTestCount();
  }

  public long getTotalTestCount() {
    return SwarmBB.getTotalTestCount();
  }

  private long startTime;
  private String testInProgress = null;
  String lastPassClass = null;
  String lastFailClass = null;
  String lastFailMethod = null;

  Throwable lastThrowable = null;

  public void startTest(Test test) {
    this.startTime = System.currentTimeMillis();
    String s = "zzzzzSTART " + test;
    Log.getLogWriter().info(s);
    testInProgress = test.toString();
  }

  public void endTest(Test test) {
    if (!dunitRecordResults) {
      Log.getLogWriter().info("Not recording results");
      return;
    }
    long delta = System.currentTimeMillis() - this.startTime;
    String s = "zzzzzEND " + test + " (took " + delta + "ms)";
    Log.getLogWriter().info(s);
    testInProgress = null;
    String className = getClassName(test);
    String methodName = getMethodName(test);

    if (className.equals(lastFailClass)) {
      if (methodName.equals(lastFailMethod)) {
        // Ok the test that just failed is ending
        recordFail(className, methodName, delta);
      } else {
        lastFailMethod = null;
        lastThrowable = null;
        // I think this means another method passed in a failing test
        recordPass(className, methodName, delta);
      }
      // Test failed, do nothing
      // recordFail = true;
      return;
    } else if (lastPassClass == null) {
      // The first test to pass
      lastPassClass = className;
      lastFailMethod = null;
      lastFailClass = null;
      recordPass(className, methodName, delta);
    } else if (!lastPassClass.equals(className)) {
      // Note that the previous test passed
      lastPassClass = className;
      lastFailMethod = null;
      lastFailClass = null;
      recordPass(className, methodName, delta);
    } else {
      recordPass(className, methodName, delta);
    }
  }

  public void recordPass(String className, String methodName, long tookMs) {
    try {
      DUnitRun du = getDUnitRun();
      DUnitClassInfo duci = Swarm.getOrCreateDUnitClassInfo(className);
      DUnitMethodInfo dumi = Swarm.getOrCreateDUnitMethodInfo(methodName, duci.getId());
      Swarm.recordPass(du, dumi, tookMs);
    } catch (SQLException se) {
      se.printStackTrace();
    }
  }

  public void recordFail(String className, String methodName, long tookMs) {
    try {
      DUnitRun du = getDUnitRun();
      DUnitClassInfo duci = Swarm.getOrCreateDUnitClassInfo(className);
      DUnitMethodInfo dumi = Swarm.getOrCreateDUnitMethodInfo(methodName, duci.getId());
      Swarm.recordFailure(du, dumi, lastThrowable, tookMs);
    } catch (SQLException se) {
      se.printStackTrace();
    }
  }

  public void addError(Test test, Throwable t) {
    StringBuffer sb = new StringBuffer();
    sb.append(test.toString());
    sb.append("\n");
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw, true));
    sb.append(sw.toString());
    Log.getLogWriter().severe("zzzzzERROR IN " + test, t);
    // reportFailure(test, sb.toString());
    lastFailClass = getClassName(test);
    lastFailMethod = getMethodName(test);
    lastThrowable = t;
  }

  public void addFailure(Test test, AssertionFailedError t) {
    StringBuffer sb = new StringBuffer();
    sb.append(test.toString());
    sb.append("\n");
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw, true));
    sb.append(sw.toString());
    Log.getLogWriter().severe("zzzzzFAILURE IN " + test, t);
    // reportFailure(test, sb.toString());
    lastFailClass = getClassName(test);
    lastFailMethod = getMethodName(test);
    lastThrowable = t;
  }

  private static Pattern methodpattern = Pattern.compile("(.*\\w+)\\(.*\\)");

  private static Pattern classpattern = Pattern.compile(".*\\w+\\((.*)\\)");
  //                   \\w+\\((.*)\\)

  /** Returns the name of the given test's class */
  protected static String getClassName(Test test) {
    String className;
    String desc = test.toString();
    Matcher matcher = classpattern.matcher(desc);
    if (matcher.matches()) {
      className = matcher.group(1);

    } else {
      className = desc;
    }

    return className;
  }

  protected static String getMethodName(Test test) {
    String className;
    String desc = test.toString();
    Matcher matcher = methodpattern.matcher(desc);
    if (matcher.matches()) {
      className = matcher.group(1);

    } else {
      className = desc;
    }

    return className;
  }

  /** Returns true if this VM has acquired the right to run this TestTask */
  private static DUnitRun getDUnitRun() {
    if (dunitRun != null) {
      System.out.println("BBB dunitRun not null returning");
      return dunitRun;
    }

    try {
      SwarmBB.getBB().getSharedLock().lock();
      Integer runId = (Integer) SwarmBB.getBB().getSharedMap().get(SwarmBB.RUN_ID);
      System.out.println("BBB runID=" + runId);
      if (runId != null) {
        dunitRun = Swarm.getDUnitRun(runId);
        System.out.println("BBB lookedUp RUN:" + dunitRun);
      } else {
        dunitRun = Swarm.generateNewDUnitRun();
        System.out.println(
            "BBB GENNED UP A RUN:" + dunitRun + " mapping to id:" + dunitRun.getId());
        SwarmBB.getBB().getSharedMap().put(SwarmBB.RUN_ID, dunitRun.getId());
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      SwarmBB.getBB().getSharedLock().unlock();
    }
    return dunitRun;
  }
}