src/jdk.management.jfr/share/classes/jdk/management/jfr/FlightRecorderMXBeanImpl.java
changeset 58480 8ca46e186a63
parent 50113 caf115bb98ad
equal deleted inserted replaced
58479:35ce0ad5870a 58480:8ca46e186a63
    26 package jdk.management.jfr;
    26 package jdk.management.jfr;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.io.InputStream;
    29 import java.io.InputStream;
    30 import java.io.StringReader;
    30 import java.io.StringReader;
       
    31 import java.nio.file.Path;
    31 import java.nio.file.Paths;
    32 import java.nio.file.Paths;
    32 import java.security.AccessControlContext;
    33 import java.security.AccessControlContext;
    33 import java.security.AccessController;
    34 import java.security.AccessController;
    34 import java.security.PrivilegedAction;
    35 import java.security.PrivilegedAction;
    35 import java.text.ParseException;
    36 import java.text.ParseException;
   103     private static final String OPTION_MAX_AGE = "maxAge";
   104     private static final String OPTION_MAX_AGE = "maxAge";
   104     private static final String OPTION_NAME = "name";
   105     private static final String OPTION_NAME = "name";
   105     private static final String OPTION_DISK = "disk";
   106     private static final String OPTION_DISK = "disk";
   106     private static final String OPTION_DUMP_ON_EXIT = "dumpOnExit";
   107     private static final String OPTION_DUMP_ON_EXIT = "dumpOnExit";
   107     private static final String OPTION_DURATION = "duration";
   108     private static final String OPTION_DURATION = "duration";
   108     private static final List<String> OPTIONS = Arrays.asList(new String[] { OPTION_DUMP_ON_EXIT, OPTION_DURATION, OPTION_NAME, OPTION_MAX_AGE, OPTION_MAX_SIZE, OPTION_DISK, });
   109     private static final String OPTION_DESTINATION = "destination";
       
   110     private static final List<String> OPTIONS = Arrays.asList(new String[] { OPTION_DUMP_ON_EXIT, OPTION_DURATION, OPTION_NAME, OPTION_MAX_AGE, OPTION_MAX_SIZE, OPTION_DISK, OPTION_DESTINATION, });
   109     private final StreamManager streamHandler = new StreamManager();
   111     private final StreamManager streamHandler = new StreamManager();
   110     private final Map<Long, Object> changes = new ConcurrentHashMap<>();
   112     private final Map<Long, Object> changes = new ConcurrentHashMap<>();
   111     private final AtomicLong sequenceNumber = new AtomicLong();
   113     private final AtomicLong sequenceNumber = new AtomicLong();
   112     private final List<MXBeanListener> listeners = new CopyOnWriteArrayList<>();
   114     private final List<MXBeanListener> listeners = new CopyOnWriteArrayList<>();
   113     private FlightRecorder recorder;
   115     private FlightRecorder recorder;
   281         validateOption(ops, OPTION_DISK, MBeanUtils::booleanValue);
   283         validateOption(ops, OPTION_DISK, MBeanUtils::booleanValue);
   282         validateOption(ops, OPTION_NAME, Function.identity());
   284         validateOption(ops, OPTION_NAME, Function.identity());
   283         validateOption(ops, OPTION_MAX_AGE, MBeanUtils::duration);
   285         validateOption(ops, OPTION_MAX_AGE, MBeanUtils::duration);
   284         validateOption(ops, OPTION_MAX_SIZE, MBeanUtils::size);
   286         validateOption(ops, OPTION_MAX_SIZE, MBeanUtils::size);
   285         validateOption(ops, OPTION_DURATION, MBeanUtils::duration);
   287         validateOption(ops, OPTION_DURATION, MBeanUtils::duration);
       
   288         validateOption(ops, OPTION_DESTINATION, x -> MBeanUtils.destination(r, x));
   286 
   289 
   287         // All OK, now set them.atomically
   290         // All OK, now set them.atomically
   288         setOption(ops, OPTION_DUMP_ON_EXIT, "false", MBeanUtils::booleanValue, x -> r.setDumpOnExit(x));
   291         setOption(ops, OPTION_DUMP_ON_EXIT, "false", MBeanUtils::booleanValue, x -> r.setDumpOnExit(x));
   289         setOption(ops, OPTION_DISK, "true", MBeanUtils::booleanValue, x -> r.setToDisk(x));
   292         setOption(ops, OPTION_DISK, "true", MBeanUtils::booleanValue, x -> r.setToDisk(x));
   290         setOption(ops, OPTION_NAME, String.valueOf(r.getId()), Function.identity(), x -> r.setName(x));
   293         setOption(ops, OPTION_NAME, String.valueOf(r.getId()), Function.identity(), x -> r.setName(x));
   291         setOption(ops, OPTION_MAX_AGE, null, MBeanUtils::duration, x -> r.setMaxAge(x));
   294         setOption(ops, OPTION_MAX_AGE, null, MBeanUtils::duration, x -> r.setMaxAge(x));
   292         setOption(ops, OPTION_MAX_SIZE, "0", MBeanUtils::size, x -> r.setMaxSize(x));
   295         setOption(ops, OPTION_MAX_SIZE, "0", MBeanUtils::size, x -> r.setMaxSize(x));
   293         setOption(ops, OPTION_DURATION, null, MBeanUtils::duration, x -> r.setDuration(x));
   296         setOption(ops, OPTION_DURATION, null, MBeanUtils::duration, x -> r.setDuration(x));
       
   297         setOption(ops, OPTION_DESTINATION, null, x -> MBeanUtils.destination(r, x), x -> setOptionDestination(r, x));
   294     }
   298     }
   295 
   299 
   296     @Override
   300     @Override
   297     public Map<String, String> getRecordingOptions(long recording) throws IllegalArgumentException {
   301     public Map<String, String> getRecordingOptions(long recording) throws IllegalArgumentException {
   298         MBeanUtils.checkMonitor();
   302         MBeanUtils.checkMonitor();
   303         options.put(OPTION_NAME, String.valueOf(r.getName()));
   307         options.put(OPTION_NAME, String.valueOf(r.getName()));
   304         options.put(OPTION_MAX_AGE, ManagementSupport.formatTimespan(r.getMaxAge(), " "));
   308         options.put(OPTION_MAX_AGE, ManagementSupport.formatTimespan(r.getMaxAge(), " "));
   305         Long maxSize = r.getMaxSize();
   309         Long maxSize = r.getMaxSize();
   306         options.put(OPTION_MAX_SIZE, String.valueOf(maxSize == null ? "0" : maxSize.toString()));
   310         options.put(OPTION_MAX_SIZE, String.valueOf(maxSize == null ? "0" : maxSize.toString()));
   307         options.put(OPTION_DURATION, ManagementSupport.formatTimespan(r.getDuration(), " "));
   311         options.put(OPTION_DURATION, ManagementSupport.formatTimespan(r.getDuration(), " "));
       
   312         options.put(OPTION_DESTINATION, ManagementSupport.getDestinationOriginalText(r));
   308         return options;
   313         return options;
   309     }
   314     }
   310 
   315 
   311     @Override
   316     @Override
   312     public long cloneRecording(long id, boolean stop) throws IllegalStateException, SecurityException {
   317     public long cloneRecording(long id, boolean stop) throws IllegalStateException, SecurityException {
   344         }
   349         }
   345         try {
   350         try {
   346             setter.accept(converter.apply(v));
   351             setter.accept(converter.apply(v));
   347         } catch (IllegalArgumentException iae) {
   352         } catch (IllegalArgumentException iae) {
   348             throw new IllegalArgumentException("Not a valid value for option '" + name + "'. " + iae.getMessage());
   353             throw new IllegalArgumentException("Not a valid value for option '" + name + "'. " + iae.getMessage());
       
   354         }
       
   355     }
       
   356 
       
   357     private static void setOptionDestination(Recording recording, String destination){
       
   358         try {
       
   359             Path pathDestination = null;
       
   360             if(destination != null){
       
   361                 pathDestination = Paths.get(destination);
       
   362             }
       
   363             recording.setDestination(pathDestination);
       
   364         } catch (IOException e) {
       
   365             IllegalArgumentException iae = new IllegalArgumentException("Not a valid destination " + destination);
       
   366             iae.addSuppressed(e);
       
   367             throw iae;
   349         }
   368         }
   350     }
   369     }
   351 
   370 
   352     private static <T, U> void validateOption(Map<String, String> options, String name, Function<String, U> validator) {
   371     private static <T, U> void validateOption(Map<String, String> options, String name, Function<String, U> validator) {
   353         try {
   372         try {