Example #1
0
  /**
   * Pings a connector by name.
   *
   * @param connectorName
   * @return RepositorySource - may be <code>null</code>)
   */
  @ManagementOperation(description = "Pings a connector by name", impact = Impact.ReadOnly)
  public boolean pingConnector(String connectorName) {
    if (!isRunning()) return false;

    // Get engine to use for the rest of the method (this is synchronized)
    // ...
    final JcrEngine engine = getEngine();
    assert engine != null;

    boolean success = false;
    String pingDuration = null;
    try {
      RepositoryConnectionPool pool =
          engine.getRepositoryService().getRepositoryLibrary().getConnectionPool(connectorName);
      if (pool != null) {
        Stopwatch sw = new Stopwatch();
        sw.start();
        success = pool.ping();
        sw.stop();
        pingDuration = sw.getTotalDuration().toString();
      }
    } catch (Exception e) {
      Logger.getLogger(getClass())
          .error(e, JBossManagedI18n.errorDeterminingIfConnectionIsAlive, connectorName);
    }
    if (pingDuration == null) pingDuration = new Duration(0L).toString();
    return success;
  }
  protected Binary storeAndCheck(int contentIndex, Class<? extends Binary> valueClass)
      throws Exception {
    String content = CONTENT[contentIndex];
    String sha1 = CONTENT_HASHES[contentIndex];
    InputStream stream = new ByteArrayInputStream(content.getBytes());

    Stopwatch sw = new Stopwatch();
    sw.start();
    Binary binary = store.storeValue(stream, false);
    sw.stop();
    if (print) System.out.println("Time to store 18MB file: " + sw.getTotalDuration());

    if (valueClass != null) {
      assertThat(binary, is(instanceOf(valueClass)));
    }
    if (content.length() == 0) {
      assertThat(binary, is(instanceOf(EmptyBinaryValue.class)));
    } else if (content.length() < MIN_BINARY_SIZE) {
      assertThat(binary, is(instanceOf(InMemoryBinaryValue.class)));
    } else {
      assertThat(binary, is(instanceOf(StoredBinaryValue.class)));
    }
    assertThat(binary.getHexHash(), is(sha1));
    String binaryContent = IoUtil.read(binary.getStream());
    assertThat(binaryContent, is(content));
    return binary;
  }
  @FixFor("MODE-1358")
  @Test
  public void shouldCopyFilesUsingStreams() throws Exception {
    // Copy a large file into a temporary file ...
    File tempFile = File.createTempFile("copytest", "pdf");
    RandomAccessFile destinationRaf = null;
    RandomAccessFile originalRaf = null;
    try {
      URL sourceUrl = getClass().getResource("/docs/postgresql-8.4.1-US.pdf");
      assertThat(sourceUrl, is(notNullValue()));
      File sourceFile = new File(sourceUrl.toURI());
      assertThat(sourceFile.exists(), is(true));
      assertThat(sourceFile.canRead(), is(true));
      assertThat(sourceFile.isFile(), is(true));

      boolean useBufferedStream = true;
      final int bufferSize = AbstractBinaryStore.bestBufferSize(sourceFile.length());

      destinationRaf = new RandomAccessFile(tempFile, "rw");
      originalRaf = new RandomAccessFile(sourceFile, "r");

      FileChannel destinationChannel = destinationRaf.getChannel();
      OutputStream output = Channels.newOutputStream(destinationChannel);
      if (useBufferedStream) output = new BufferedOutputStream(output, bufferSize);

      // Create an input stream to the original file ...
      FileChannel originalChannel = originalRaf.getChannel();
      InputStream input = Channels.newInputStream(originalChannel);
      if (useBufferedStream) input = new BufferedInputStream(input, bufferSize);

      // Copy the content ...
      Stopwatch sw = new Stopwatch();
      sw.start();
      IoUtil.write(input, output, bufferSize);
      sw.stop();
      System.out.println(
          "Time to copy \""
              + sourceFile.getName()
              + "\" ("
              + sourceFile.length()
              + " bytes): "
              + sw.getTotalDuration());
    } finally {
      tempFile.delete();
      if (destinationRaf != null) destinationRaf.close();
      if (originalRaf != null) originalRaf.close();
    }
  }
  protected void storeAndCheckResource(
      String resourcePath, String expectedSha1, String desc, long numBytes) throws Exception {
    InputStream content = getClass().getClassLoader().getResourceAsStream(resourcePath);
    assertThat(content, is(notNullValue()));

    Stopwatch sw = new Stopwatch();
    sw.start();
    Binary binary = store.storeValue(content, false);
    sw.stop();
    if (print) System.out.println("Time to store " + desc + ": " + sw.getTotalDuration());

    if (numBytes == 0) {
      assertThat(binary, is(instanceOf(EmptyBinaryValue.class)));
    } else if (numBytes < MIN_BINARY_SIZE) {
      assertThat(binary, is(instanceOf(InMemoryBinaryValue.class)));
    } else {
      assertThat(binary, is(instanceOf(StoredBinaryValue.class)));
    }
    assertThat(binary.getHexHash(), is(expectedSha1));
    assertThat(binary.getSize(), is(numBytes));

    // Now try reading and comparing the two streams ...
    InputStream expected = getClass().getClassLoader().getResourceAsStream(resourcePath);
    InputStream actual = binary.getStream();
    byte[] buffer1 = new byte[1024];
    byte[] buffer2 = new byte[1024];
    int numRead = 0;
    while ((numRead = expected.read(buffer1)) == actual.read(buffer2)) {
      if (numRead == -1) break;
      for (int i = 0; i != numRead; ++i) {
        assertThat(buffer1[i], is(buffer2[i]));
      }
    }

    if (print) {
      // And try measuring how fast we can read the file ...
      sw = new Stopwatch();
      sw.start();
      while (-1 != actual.read(buffer2)) {}
      sw.stop();
      System.out.println("Time to read " + desc + ": " + sw.getTotalDuration());
    }
  }