376 final String enableThreadContentionMonitoring =
377 configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING);
378
379 if (enableThreadContentionMonitoring != null) {
380 ManagementFactory.getThreadMXBean().
381 setThreadContentionMonitoringEnabled(true);
382 }
383
384 String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT);
385 if (jmxremotePort != null) {
386 jmxServer = ConnectorBootstrap.
387 startRemoteConnectorServer(jmxremotePort, configProps);
388
389 startDiscoveryService(configProps);
390 } else {
391 throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, "No port specified");
392 }
393 } catch (JdpException e) {
394 error(e);
395 } catch (AgentConfigurationError err) {
396 error(err.getError(), err.getParams());
397 }
398 }
399
400 private static synchronized void stopRemoteManagementAgent() throws Exception {
401
402 JdpController.stopDiscoveryService();
403
404 if (jmxServer != null) {
405 ConnectorBootstrap.unexportRegistry();
406 ConnectorAddressLink.unexportRemote();
407
408 // Attempt to stop already stopped agent
409 // Don't cause any errors.
410 jmxServer.stop();
411 jmxServer = null;
412 configProps = null;
413 }
414 }
415
416 private static synchronized String getManagementAgentStatus() throws Exception {
437
438 /*
439 * If the jmxremote.port property is set then we start the
440 * RMIConnectorServer for remote M&M.
441 *
442 * If the jmxremote or jmxremote.port properties are set then
443 * we start a RMIConnectorServer for local M&M. The address
444 * of this "local" server is exported as a counter to the jstat
445 * instrumentation buffer.
446 */
447 if (jmxremote != null || jmxremotePort != null) {
448 if (jmxremotePort != null) {
449 jmxServer = ConnectorBootstrap.
450 startRemoteConnectorServer(jmxremotePort, props);
451 startDiscoveryService(props);
452 }
453 startLocalManagementAgent();
454 }
455
456 } catch (AgentConfigurationError e) {
457 error(e.getError(), e.getParams());
458 } catch (Exception e) {
459 error(e);
460 }
461 }
462
463 private static void startDiscoveryService(Properties props)
464 throws IOException, JdpException {
465 // Start discovery service if requested
466 String discoveryPort = props.getProperty("com.sun.management.jdp.port");
467 String discoveryAddress = props.getProperty("com.sun.management.jdp.address");
468 String discoveryShouldStart = props.getProperty("com.sun.management.jmxremote.autodiscovery");
469
470 // Decide whether we should start autodicovery service.
471 // To start autodiscovery following conditions should be met:
472 // autodiscovery==true OR (autodicovery==null AND jdp.port != NULL)
473
474 boolean shouldStart = false;
475 if (discoveryShouldStart == null){
476 shouldStart = (discoveryPort != null);
477 }
648 error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\"");
649 } catch (NoSuchMethodException ex) {
650 error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\"");
651 } catch (SecurityException ex) {
652 error(AGENT_CLASS_ACCESS_DENIED);
653 } catch (Exception ex) {
654 String msg = (ex.getCause() == null
655 ? ex.getMessage()
656 : ex.getCause().getMessage());
657 error(AGENT_CLASS_FAILED, msg);
658 }
659 }
660 }
661
662 public static void error(String key) {
663 String keyText = getText(key);
664 System.err.print(getText("agent.err.error") + ": " + keyText);
665 throw new RuntimeException(keyText);
666 }
667
668 public static void error(String key, String[] params) {
669 if (params == null || params.length == 0) {
670 error(key);
671 } else {
672 StringBuilder message = new StringBuilder(params[0]);
673 for (int i = 1; i < params.length; i++) {
674 message.append(' ').append(params[i]);
675 }
676 error(key, message.toString());
677 }
678 }
679
680 public static void error(String key, String message) {
681 String keyText = getText(key);
682 System.err.print(getText("agent.err.error") + ": " + keyText);
683 System.err.println(": " + message);
684 throw new RuntimeException(keyText + ": " + message);
685 }
686
687 public static void error(Exception e) {
688 e.printStackTrace();
689 System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString());
690 throw new RuntimeException(e);
691 }
692
693 public static void warning(String key, String message) {
694 System.err.print(getText("agent.err.warning") + ": " + getText(key));
695 System.err.println(": " + message);
696 }
697
698 private static void initResource() {
699 try {
700 messageRB =
701 ResourceBundle.getBundle("jdk.internal.agent.resources.agent");
702 } catch (MissingResourceException e) {
703 throw new Error("Fatal: Resource for management agent is missing");
704 }
705 }
706
707 public static String getText(String key) {
708 if (messageRB == null) {
709 initResource();
|
376 final String enableThreadContentionMonitoring =
377 configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING);
378
379 if (enableThreadContentionMonitoring != null) {
380 ManagementFactory.getThreadMXBean().
381 setThreadContentionMonitoringEnabled(true);
382 }
383
384 String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT);
385 if (jmxremotePort != null) {
386 jmxServer = ConnectorBootstrap.
387 startRemoteConnectorServer(jmxremotePort, configProps);
388
389 startDiscoveryService(configProps);
390 } else {
391 throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, "No port specified");
392 }
393 } catch (JdpException e) {
394 error(e);
395 } catch (AgentConfigurationError err) {
396 error(err);
397 }
398 }
399
400 private static synchronized void stopRemoteManagementAgent() throws Exception {
401
402 JdpController.stopDiscoveryService();
403
404 if (jmxServer != null) {
405 ConnectorBootstrap.unexportRegistry();
406 ConnectorAddressLink.unexportRemote();
407
408 // Attempt to stop already stopped agent
409 // Don't cause any errors.
410 jmxServer.stop();
411 jmxServer = null;
412 configProps = null;
413 }
414 }
415
416 private static synchronized String getManagementAgentStatus() throws Exception {
437
438 /*
439 * If the jmxremote.port property is set then we start the
440 * RMIConnectorServer for remote M&M.
441 *
442 * If the jmxremote or jmxremote.port properties are set then
443 * we start a RMIConnectorServer for local M&M. The address
444 * of this "local" server is exported as a counter to the jstat
445 * instrumentation buffer.
446 */
447 if (jmxremote != null || jmxremotePort != null) {
448 if (jmxremotePort != null) {
449 jmxServer = ConnectorBootstrap.
450 startRemoteConnectorServer(jmxremotePort, props);
451 startDiscoveryService(props);
452 }
453 startLocalManagementAgent();
454 }
455
456 } catch (AgentConfigurationError e) {
457 error(e);
458 } catch (Exception e) {
459 error(e);
460 }
461 }
462
463 private static void startDiscoveryService(Properties props)
464 throws IOException, JdpException {
465 // Start discovery service if requested
466 String discoveryPort = props.getProperty("com.sun.management.jdp.port");
467 String discoveryAddress = props.getProperty("com.sun.management.jdp.address");
468 String discoveryShouldStart = props.getProperty("com.sun.management.jmxremote.autodiscovery");
469
470 // Decide whether we should start autodicovery service.
471 // To start autodiscovery following conditions should be met:
472 // autodiscovery==true OR (autodicovery==null AND jdp.port != NULL)
473
474 boolean shouldStart = false;
475 if (discoveryShouldStart == null){
476 shouldStart = (discoveryPort != null);
477 }
648 error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\"");
649 } catch (NoSuchMethodException ex) {
650 error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\"");
651 } catch (SecurityException ex) {
652 error(AGENT_CLASS_ACCESS_DENIED);
653 } catch (Exception ex) {
654 String msg = (ex.getCause() == null
655 ? ex.getMessage()
656 : ex.getCause().getMessage());
657 error(AGENT_CLASS_FAILED, msg);
658 }
659 }
660 }
661
662 public static void error(String key) {
663 String keyText = getText(key);
664 System.err.print(getText("agent.err.error") + ": " + keyText);
665 throw new RuntimeException(keyText);
666 }
667
668 public static void error(String key, String message) {
669 String keyText = getText(key);
670 System.err.print(getText("agent.err.error") + ": " + keyText);
671 System.err.println(": " + message);
672 throw new RuntimeException(keyText + ": " + message);
673 }
674
675 public static void error(Exception e) {
676 e.printStackTrace();
677 System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString());
678 throw new RuntimeException(e);
679 }
680
681 public static void error(AgentConfigurationError e) {
682 String keyText = getText(e.getError());
683 String[] params = e.getParams();
684
685 System.err.print(getText("agent.err.error") + ": " + keyText);
686
687 if (params != null && params.length != 0) {
688 StringBuffer message = new StringBuffer(params[0]);
689 for (int i = 1; i < params.length; i++) {
690 message.append(" " + params[i]);
691 }
692 System.err.println(": " + message);
693 }
694 e.printStackTrace();
695 throw new RuntimeException(e);
696 }
697
698 public static void warning(String key, String message) {
699 System.err.print(getText("agent.err.warning") + ": " + getText(key));
700 System.err.println(": " + message);
701 }
702
703 private static void initResource() {
704 try {
705 messageRB =
706 ResourceBundle.getBundle("jdk.internal.agent.resources.agent");
707 } catch (MissingResourceException e) {
708 throw new Error("Fatal: Resource for management agent is missing");
709 }
710 }
711
712 public static String getText(String key) {
713 if (messageRB == null) {
714 initResource();
|