/**
  * Entry point to the Example.
  *
  * @param args unused
  */
 public static void main(String[] args) {
   Stopwatch stopwatch = SimonManager.getStopwatch("stopwatch");
   for (int i = 1; i <= 10; i++) {
     strangeMethod();
     System.out.println("Stopwatch after round " + i + ": " + stopwatch);
   }
   System.out.println("stopwatch.sample() = " + stopwatch.sample());
 }
  protected DocumentModel doCreateLeafNode(DocumentModel parent, SourceNode node)
      throws IOException {
    if (!shouldImportDocument(node)) {
      return null;
    }
    Stopwatch stopwatch = SimonManager.getStopwatch("org.nuxeo.ecm.platform.importer.create_leaf");
    Split split = stopwatch.start();
    DocumentModel leaf = null;
    try {
      leaf = getFactory().createLeafNode(session, parent, node);
    } catch (IOException e) {
      String errMsg =
          "Unable to create leaf document for "
              + node.getSourcePath()
              + ":"
              + e
              + (e.getCause() != null ? e.getCause() : "");
      fslog(errMsg, true);
      log.error(errMsg);
      // Process leaf node creation error and check if the global
      // import task should continue
      boolean shouldImportTaskContinue =
          getFactory().processLeafNodeCreationError(session, parent, node);
      if (!shouldImportTaskContinue) {
        throw new NuxeoException(e);
      }
    } finally {
      split.stop();
    }
    BlobHolder bh = node.getBlobHolder();
    if (leaf != null && bh != null) {
      Blob blob = bh.getBlob();
      if (blob != null) {
        long fileSize = blob.getLength();
        String fileName = blob.getFilename();
        if (fileSize > 0) {
          long kbSize = fileSize / 1024;
          String parentPath = (parent == null) ? "null" : parent.getPathAsString();
          fslog(
              "Created doc "
                  + leaf.getName()
                  + " at "
                  + parentPath
                  + " with file "
                  + fileName
                  + " of size "
                  + kbSize
                  + "KB",
              true);
        }
        uploadedKO += fileSize;
      }

      // save session if needed
      commit();
    }
    return leaf;
  }
 /** Method implementing the code of the thread. */
 public void run() {
   while (true) {
     Stopwatch stopwatch = SimonManager.getStopwatch("sampled-stopwatch");
     System.out.println("\nstopwatch = " + stopwatch);
     System.out.println("Stopwatch sample: " + stopwatch.sample());
     System.out.println(
         "Stopwatch sample - incremental: " + stopwatch.sampleIncrement("myIncrementalKey"));
     try {
       Thread.sleep(10000);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
 }
  protected void recursiveCreateDocumentFromNode(DocumentModel parent, SourceNode node)
      throws IOException {

    if (getFactory().isTargetDocumentModelFolderish(node)) {
      DocumentModel folder;
      Boolean newThread = false;
      if (skipContainerCreation) {
        folder = parent;
        skipContainerCreation = false;
        newThread = true;
      } else {
        folder = doCreateFolderishNode(parent, node);
        if (folder == null) {
          return;
        }
      }

      // get a new TaskImporter if available to start
      // processing the sub-tree
      GenericThreadedImportTask task = null;
      if (!newThread) {
        task = createNewTaskIfNeeded(folder, node);
      }
      if (task != null) {
        // force comit before starting new thread
        commit(true);
        try {
          GenericMultiThreadedImporter.getExecutor().execute(task);
        } catch (RejectedExecutionException e) {
          log.error("Import task rejected", e);
        }

      } else {
        Stopwatch stopwatch =
            SimonManager.getStopwatch("org.nuxeo.ecm.platform.importer.node_get_children");
        Split split = stopwatch.start();
        List<SourceNode> nodes = node.getChildren();
        split.stop();
        if (nodes != null) {
          for (SourceNode child : nodes) {
            recursiveCreateDocumentFromNode(folder, child);
          }
        }
      }
    } else {
      doCreateLeafNode(parent, node);
    }
  }
  protected void commit(boolean force) {
    uploadedFiles++;
    if (uploadedFiles % 10 == 0) {
      GenericMultiThreadedImporter.addCreatedDoc(taskId, uploadedFiles);
    }

    if (uploadedFiles % batchSize == 0 || force) {
      Stopwatch stopwatch =
          SimonManager.getStopwatch("org.nuxeo.ecm.platform.importer.session_save");
      Split split = stopwatch.start();
      fslog("Committing Core Session after " + uploadedFiles + " files", true);
      session.save();
      TransactionHelper.commitOrRollbackTransaction();
      TransactionHelper.startTransaction(transactionTimeout);
      split.stop();
    }
  }
  protected DocumentModel doCreateFolderishNode(DocumentModel parent, SourceNode node) {
    if (!shouldImportDocument(node)) {
      return null;
    }
    Stopwatch stopwatch =
        SimonManager.getStopwatch("org.nuxeo.ecm.platform.importer.create_folder");
    Split split = stopwatch.start();
    DocumentModel folder = null;
    try {
      folder = getFactory().createFolderishNode(session, parent, node);
    } catch (IOException e) {
      String errorMsg =
          "Unable to create folderish document for "
              + node.getSourcePath()
              + ":"
              + e
              + (e.getCause() != null ? e.getCause() : "");
      fslog(errorMsg, true);
      log.error(errorMsg);
      // Process folderish node creation error and check if the global
      // import task should continue
      boolean shouldImportTaskContinue =
          getFactory().processFolderishNodeCreationError(session, parent, node);
      if (!shouldImportTaskContinue) {
        throw new NuxeoException(e);
      }
    } finally {
      split.stop();
    }
    if (folder != null) {
      String parentPath = (parent == null) ? "null" : parent.getPathAsString();
      fslog("Created Folder " + folder.getName() + " at " + parentPath, true);

      // save session if needed
      commit();
    }
    return folder;
  }