Esempio n. 1
0
  public void startBootstrapInstance(MavenPluginContext pluginContext) {
    this.log = pluginContext.getLog();
    this.configuration = pluginContext.getConfiguration();
    List<Stoppable> stoppables = Lists.newArrayList();
    getSetupStepSequence().execute(pluginContext);

    PrintStream originalSystemErr = System.err;

    bootstrap = getBootstrap();
    log.info("Starting Tomcat ...");
    try {
      File catalinaout = new File(configuration.getCatalinaBase(), "/logs/catalina.out");
      CatalinaOutPrintStream catalinaOutputStream =
          new CatalinaOutPrintStream(
              originalSystemErr,
              new FileOutputStream(catalinaout),
              configuration.isSuspendConsoleOutput());
      System.setErr(catalinaOutputStream);
      bootstrap.init();
      final BootstrapShutdownHook shutdownHook = new BootstrapShutdownHook();
      List<Stoppable> stoppableScanner =
          ScannerSetup.configureScanners(shutdownHook, configuration, log);
      stoppables.addAll(stoppableScanner);
      bootstrap.start();
      Runtime.getRuntime().addShutdownHook(shutdownHook);
      log.info("Tomcat started");
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    }
    stoppables.add(new StoppableBootstrapAdapter(bootstrap));
    pluginContext
        .getMojo()
        .getPluginContext()
        .put(AbstractT7BaseMojo.T7_BOOTSTRAP_CONTEXT_ID, stoppables);
  }
 /** @see org.apache.maven.plugin.Mojo#execute() */
 public void execute() throws MojoExecutionException, MojoFailureException {
   getLog().info("Configuring Jetty for project: " + project.getName());
   if (skip) {
     getLog().info("Skipping Jetty start: jetty.skip==true");
     return;
   }
   PluginLog.setLog(getLog());
   Runtime.getRuntime().addShutdownHook(new ShutdownThread());
   random = new Random();
   startJettyRunner();
 }
  /** @throws MojoExecutionException */
  public void startJettyRunner() throws MojoExecutionException {
    try {

      File props = prepareConfiguration();

      List<String> cmd = new ArrayList<String>();
      cmd.add(getJavaBin());

      if (jvmArgs != null) {
        String[] args = jvmArgs.split(" ");
        for (int i = 0; args != null && i < args.length; i++) {
          if (args[i] != null && !"".equals(args[i])) cmd.add(args[i].trim());
        }
      }

      String classPath = getClassPath();
      if (classPath != null && classPath.length() > 0) {
        cmd.add("-cp");
        cmd.add(classPath);
      }
      cmd.add(Starter.class.getCanonicalName());

      if (stopPort > 0 && stopKey != null) {
        cmd.add("--stop-port");
        cmd.add(Integer.toString(stopPort));
        cmd.add("--stop-key");
        cmd.add(stopKey);
      }
      if (jettyXml != null) {
        cmd.add("--jetty-xml");
        cmd.add(jettyXml);
      }

      if (contextXml != null) {
        cmd.add("--context-xml");
        cmd.add(contextXml);
      }

      cmd.add("--props");
      cmd.add(props.getAbsolutePath());

      String token = createToken();
      cmd.add("--token");
      cmd.add(token);

      ProcessBuilder builder = new ProcessBuilder(cmd);
      builder.directory(project.getBasedir());

      if (PluginLog.getLog().isDebugEnabled())
        PluginLog.getLog().debug(Arrays.toString(cmd.toArray()));

      forkedProcess = builder.start();
      PluginLog.getLog().info("Forked process starting");

      if (waitForChild) {
        startPump("STDOUT", forkedProcess.getInputStream());
        startPump("STDERR", forkedProcess.getErrorStream());
        int exitcode = forkedProcess.waitFor();
        PluginLog.getLog().info("Forked execution exit: " + exitcode);
      } else {
        // wait for the child to be ready before terminating.
        // child indicates it has finished starting by printing on stdout the token passed to it
        try {
          LineNumberReader reader =
              new LineNumberReader(new InputStreamReader(forkedProcess.getInputStream()));
          String line = null;
          int attempts = 10; // max lines we'll read trying to get token
          do {
            --attempts;
            line = reader.readLine();
            if (line != null && line.startsWith(token)) break;
          } while (line != null && attempts > 0);
          reader.close();

          if (line != null && line.trim().equals(token))
            PluginLog.getLog().info("Forked process started.");
          else {
            String err = (line == null ? "" : line.substring(token.length()));
            PluginLog.getLog()
                .info("Forked process startup errors " + (!"".equals(err) ? ":" + err : ""));
          }
        } catch (Exception e) {
          throw new MojoExecutionException(
              "Problem determining if forked process is ready: " + e.getMessage());
        }
      }
    } catch (InterruptedException ex) {
      if (forkedProcess != null && waitForChild) forkedProcess.destroy();

      throw new MojoExecutionException("Failed to start Jetty within time limit");
    } catch (Exception ex) {
      if (forkedProcess != null && waitForChild) forkedProcess.destroy();

      throw new MojoExecutionException("Failed to create Jetty process", ex);
    }
  }