1 /* 2 * Copyright (c) 2012, 2019, 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 package jdk.jfr.internal.dcmd; 26 27 import java.io.IOException; 28 import java.nio.file.InvalidPathException; 29 import java.nio.file.Paths; 30 31 import jdk.jfr.Recording; 32 import jdk.jfr.internal.SecuritySupport.SafePath; 33 34 /** 35 * JFR.stop 36 * 37 */ 38 //Instantiated by native 39 final class DCmdStop extends AbstractDCmd { 40 41 /** 42 * Execute JFR.stop 43 * 44 * Requires that either <code>name or <code>id</code> is set. 45 * 46 * @param recordingText name or id of the recording to stop. 47 * 48 * @param textPath file path where data should be written after recording 49 * has been stopped, or <code>null</code> if recording shouldn't be 50 * written to disk. 51 * @return result text 52 * 53 * @throws DCmdException if recording could not be stopped 54 */ 55 public String execute(String recordingText, String textPath) throws DCmdException { 56 try { 57 SafePath path = resolvePath(textPath, "Failed to stop %s"); 58 Recording recording = findRecording(recordingText); 59 if (textPath != null) { 60 try { 61 recording.setDestination(Paths.get(textPath)); 62 } catch (IOException e) { 63 throw new DCmdException("Failed to stop %s. Could not set destination for \"%s\" to file %s", recording.getName(), textPath, e.getMessage()); 64 } 65 } 66 recording.stop(); 67 reportOperationComplete("Stopped", recording, path); 68 recording.close(); 69 return getResult(); 70 } catch (InvalidPathException | DCmdException e) { 71 if (textPath != null) { 72 throw new DCmdException("Could not write recording \"%s\" to file. %s", recordingText, e.getMessage()); 73 } 74 throw new DCmdException(e, "Could not stop recording \"%s\".", recordingText, e.getMessage()); 75 } 76 } 77 }