1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.internal.tool;
  27 
  28 import java.io.FileOutputStream;
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.file.DirectoryStream;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 import java.util.ArrayList;
  37 import java.util.Collections;
  38 import java.util.Deque;
  39 import java.util.List;
  40 
  41 final class Assemble extends Command {
  42 
  43     @Override
  44     public String getName() {
  45         return "assemble";
  46     }
  47 
  48     @Override
  49     public List<String> getOptionSyntax() {
  50         return Collections.singletonList("<repository> <file>");
  51     }
  52 
  53     @Override
  54     public String getDescription() {
  55         return "Assemble leftover chunks from a disk repository into a recording file";
  56     }
  57 
  58     @Override
  59     public void displayOptionUsage(PrintStream stream) {
  60         stream.println("  <repository>   Directory where the repository is located");
  61         stream.println();
  62         stream.println("  <file>         Name of the recording file (.jfr) to create");
  63     }
  64 
  65     @Override
  66     public void execute(Deque<String> options) throws UserSyntaxException, UserDataException {
  67         ensureMinArgumentCount(options, 2);
  68         ensureMaxArgumentCount(options, 2);
  69         Path repository = getDirectory(options.pop());
  70 
  71         Path file = Paths.get(options.pop());
  72         ensureFileDoesNotExist(file);
  73         ensureJFRFile(file);
  74 
  75         try (FileOutputStream fos = new FileOutputStream(file.toFile())) {
  76             List<Path> files = listJFRFiles(repository);
  77             if (files.isEmpty()) {
  78                 throw new UserDataException("no *.jfr files found at " + repository);
  79             }
  80             println();
  81             println("Assembling files... ");
  82             println();
  83             transferTo(files, file, fos.getChannel());
  84             println();
  85             println("Finished.");
  86         } catch (IOException e) {
  87             throw new UserDataException("could not open destination file " + file + ". " + e.getMessage());
  88         }
  89     }
  90 
  91     private List<Path> listJFRFiles(Path path) throws UserDataException {
  92         try {
  93             List<Path> files = new ArrayList<>();
  94             if (Files.isDirectory(path)) {
  95                 try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.jfr")) {
  96                     for (Path p : stream) {
  97                         if (!Files.isDirectory(p) && Files.isReadable(p)) {
  98                             files.add(p);
  99                         }
 100                     }
 101                 }
 102             }
 103             files.sort((u, v) -> u.getFileName().compareTo(v.getFileName()));
 104             return files;
 105         } catch (IOException ioe) {
 106             throw new UserDataException("could not list *.jfr for directory " + path + ". " + ioe.getMessage());
 107         }
 108     }
 109 
 110     private void transferTo(List<Path> sourceFiles, Path output, FileChannel out) throws UserDataException {
 111         long pos = 0;
 112         for (Path p : sourceFiles) {
 113             println(" " + p.toString());
 114             try (FileChannel sourceChannel = FileChannel.open(p)) {
 115                 long rem = Files.size(p);
 116                 while (rem > 0) {
 117                     long n = Math.min(rem, 1024 * 1024);
 118                     long w = out.transferFrom(sourceChannel, pos, n);
 119                     pos += w;
 120                     rem -= w;
 121                 }
 122             } catch (IOException ioe) {
 123                 throw new UserDataException("could not copy recording chunk " + p + " to new file. " + ioe.getMessage());
 124             }
 125         }
 126     }
 127 }