PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/jdr/jboss-as-jdr/src/main/java/org/jboss/as/jdr/JdrReportService.java

http://github.com/jbossas/jboss-as
Java | 148 lines | 95 code | 17 blank | 36 comment | 11 complexity | 159877e040312c7536df22951c00907c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.jdr;
  23. import java.io.Console;
  24. import java.net.HttpURLConnection;
  25. import java.net.URL;
  26. import java.security.AccessController;
  27. import java.util.concurrent.ExecutorService;
  28. import java.util.concurrent.Executors;
  29. import java.util.concurrent.ThreadFactory;
  30. import org.jboss.as.controller.ModelController;
  31. import org.jboss.as.controller.OperationFailedException;
  32. import org.jboss.as.controller.ServiceVerificationHandler;
  33. import org.jboss.as.controller.client.ModelControllerClient;
  34. import org.jboss.as.network.NetworkUtils;
  35. import org.jboss.as.server.ServerEnvironment;
  36. import org.jboss.as.server.ServerEnvironmentService;
  37. import org.jboss.as.server.Services;
  38. import org.jboss.msc.service.Service;
  39. import org.jboss.msc.service.ServiceController;
  40. import org.jboss.msc.service.ServiceName;
  41. import org.jboss.msc.service.ServiceTarget;
  42. import org.jboss.msc.service.StartContext;
  43. import org.jboss.msc.service.StartException;
  44. import org.jboss.msc.service.StopContext;
  45. import org.jboss.msc.value.InjectedValue;
  46. import org.jboss.threads.JBossThreadFactory;
  47. /**
  48. * Service that provides a {@link JdrReportCollector}.
  49. *
  50. * @author Brian Stansberry
  51. * @author Mike M. Clark
  52. * @author Jesse Jaggars
  53. */
  54. public class JdrReportService implements JdrReportCollector, Service<JdrReportCollector> {
  55. public static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("jdr", "collector");
  56. public static ServiceController<JdrReportCollector> addService(final ServiceTarget target, final ServiceVerificationHandler verificationHandler) {
  57. JdrReportService service = new JdrReportService();
  58. return target.addService(SERVICE_NAME, service)
  59. .addDependency(ServerEnvironmentService.SERVICE_NAME, ServerEnvironment.class, service.serverEnvironmentValue)
  60. .addDependency(Services.JBOSS_SERVER_CONTROLLER, ModelController.class, service.modelControllerValue)
  61. .addListener(verificationHandler)
  62. .setInitialMode(ServiceController.Mode.ACTIVE)
  63. .install();
  64. }
  65. private final InjectedValue<ServerEnvironment> serverEnvironmentValue = new InjectedValue<ServerEnvironment>();
  66. private final InjectedValue<ModelController> modelControllerValue = new InjectedValue<ModelController>();
  67. private ExecutorService executorService;
  68. private ServerEnvironment serverEnvironment;
  69. private ModelControllerClient controllerClient;
  70. /**
  71. * Collect a JDR report when run outside the Application Server.
  72. */
  73. public JdrReport standaloneCollect(String host, String port) throws OperationFailedException {
  74. Console cons = System.console();
  75. String username = null;
  76. String password = null;
  77. if (host == null) {
  78. host = "localhost";
  79. }
  80. if (port == null) {
  81. port = "9990";
  82. }
  83. // Let's go ahead and see if we need to auth before prompting the user
  84. // for a username and password
  85. boolean must_auth = false;
  86. try {
  87. URL managementApi = new URL("http://" + NetworkUtils.formatPossibleIpv6Address(host) + ":" + port + "/management");
  88. HttpURLConnection conn = (HttpURLConnection) managementApi.openConnection();
  89. int code = conn.getResponseCode();
  90. if (code != 200) {
  91. must_auth = true;
  92. }
  93. } catch (Exception e) {
  94. }
  95. if (must_auth) {
  96. if (cons != null) {
  97. username = cons.readLine("Management username: ");
  98. password = String.valueOf(cons.readPassword("Management password: "));
  99. }
  100. }
  101. SosInterpreter interpreter = new SosInterpreter();
  102. return interpreter.collect(username, password, host, port);
  103. }
  104. /**
  105. * Collect a JDR report.
  106. */
  107. public JdrReport collect() throws OperationFailedException {
  108. SosInterpreter interpreter = new SosInterpreter();
  109. serverEnvironment = serverEnvironmentValue.getValue();
  110. interpreter.setJbossHomeDir(serverEnvironment.getHomeDir().getAbsolutePath());
  111. interpreter.setReportLocationDir(serverEnvironment.getServerTempDir().getAbsolutePath());
  112. interpreter.setControllerClient(controllerClient);
  113. interpreter.setHostControllerName(serverEnvironment.getHostControllerName());
  114. interpreter.setServerName(serverEnvironment.getServerName());
  115. return interpreter.collect();
  116. }
  117. public synchronized void start(StartContext context) throws StartException {
  118. final ThreadFactory threadFactory = new JBossThreadFactory(new ThreadGroup("JdrReportCollector-threads"), Boolean.FALSE, null, "%G - %t", null, null, AccessController.getContext());
  119. executorService = Executors.newCachedThreadPool(threadFactory);
  120. serverEnvironment = serverEnvironmentValue.getValue();
  121. controllerClient = modelControllerValue.getValue().createClient(executorService);
  122. }
  123. public synchronized void stop(StopContext context) {
  124. if (executorService != null) {
  125. executorService.shutdownNow();
  126. }
  127. }
  128. public JdrReportService getValue() throws IllegalStateException, IllegalArgumentException {
  129. return this;
  130. }
  131. }