public static void setGroovyHome(JavaParameters params, @NotNull String groovyHome) {
   params.getVMParametersList().add("-Dgroovy.home=" + groovyHome);
   if (groovyHome.contains("grails")) { // a bit of a hack
     params.getVMParametersList().add("-Dgrails.home=" + groovyHome);
   }
   if (groovyHome.contains("griffon")) { // a bit of a hack
     params.getVMParametersList().add("-Dgriffon.home=" + groovyHome);
   }
 }
  @Override
  public void patchJavaParameters(@Nullable final Module module, JavaParameters javaParameters) {
    if (module != null
        && PsiUtil.isIdeaProject(module.getProject())
        && !javaParameters.getVMParametersList().hasParameter(SYSTEM_CL_PROPERTY)) {
      final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(module.getProject());
      final String qualifiedName = UrlClassLoader.class.getName();
      final PsiClass urlLoaderClass =
          ApplicationManager.getApplication()
              .runReadAction(
                  new Computable<PsiClass>() {
                    @Override
                    public PsiClass compute() {
                      return psiFacade.findClass(
                          qualifiedName,
                          GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module));
                    }
                  });
      if (urlLoaderClass != null) {
        javaParameters.getVMParametersList().addProperty(SYSTEM_CL_PROPERTY, qualifiedName);
      }
    }

    Sdk jdk = javaParameters.getJdk();
    jdk = IdeaJdk.findIdeaJdk(jdk);
    if (jdk == null) return;

    String libPath = jdk.getHomePath() + File.separator + "lib";

    final ParametersList vm = javaParameters.getVMParametersList();
    vm.add("-Xbootclasspath/a:" + libPath + File.separator + "boot.jar");
    if (!vm.hasProperty("idea.load.plugins.id")
        && module != null
        && PluginModuleType.isOfType(module)) {
      final String id = DescriptorUtil.getPluginId(module);
      if (id != null) {
        vm.defineProperty("idea.load.plugins.id", id);
      }
    }

    final File sandboxHome = getSandboxPath(jdk);
    if (sandboxHome != null) {
      if (!vm.hasProperty("idea.home.path")) {
        File homeDir = new File(sandboxHome, "test");
        FileUtil.createDirectory(homeDir);
        vm.defineProperty("idea.home.path", homeDir.getAbsolutePath());
      }
      if (!vm.hasProperty("idea.plugins.path")) {
        vm.defineProperty("idea.plugins.path", new File(sandboxHome, "plugins").getAbsolutePath());
      }
    }

    javaParameters.getClassPath().addFirst(libPath + File.separator + "idea.jar");
    javaParameters.getClassPath().addFirst(libPath + File.separator + "resources.jar");
    javaParameters.getClassPath().addFirst(((JavaSdkType) jdk.getSdkType()).getToolsPath(jdk));
  }
 public void updateJavaParameters(
     RunConfigurationBase configuration, JavaParameters params, RunnerSettings runnerSettings) {
   if (runnerSettings != null || !isApplicableFor(configuration)) {
     return;
   }
   StringBuilder argument = new StringBuilder("-javaagent:");
   final String agentPath = PathUtil.getJarPathForClass(ProjectData.class); // todo spaces
   argument.append(agentPath);
   params.getVMParametersList().add(argument.toString());
   params.getClassPath().add(agentPath);
   params
       .getVMParametersList()
       .addProperty(ProjectData.TRACE_DIR, getTracesDirectory(configuration));
 }
  protected void appendForkInfo(Executor executor) throws ExecutionException {
    final String forkMode = getForkMode();
    if (Comparing.strEqual(forkMode, "none")) {
      if (forkPerModule()) {
        if (isExecutorDisabledInForkedMode()) {
          final String actionName = UIUtil.removeMnemonic(executor.getStartActionText());
          throw new CantRunException(
              "'"
                  + actionName
                  + "' is disabled when per-module working directory is configured.<br/>"
                  + "Please specify single working directory, or change test scope to single module.");
        }
      } else {
        return;
      }
    } else if (isExecutorDisabledInForkedMode()) {
      final String actionName = executor.getActionName();
      throw new CantRunException(
          actionName
              + " is disabled in fork mode.<br/>Please change fork mode to &lt;none&gt; to "
              + actionName.toLowerCase(Locale.ENGLISH)
              + ".");
    }

    final JavaParameters javaParameters = getJavaParameters();
    final Sdk jdk = javaParameters.getJdk();
    if (jdk == null) {
      throw new ExecutionException(
          ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
    }

    try {
      final File tempFile = FileUtil.createTempFile("command.line", "", true);
      final PrintWriter writer = new PrintWriter(tempFile, CharsetToolkit.UTF8);
      try {
        if (JdkUtil.useDynamicClasspath(getConfiguration().getProject())) {
          String classpath = PathUtil.getJarPathForClass(CommandLineWrapper.class);
          final String utilRtPath = PathUtil.getJarPathForClass(StringUtilRt.class);
          if (!classpath.equals(utilRtPath)) {
            classpath += File.pathSeparator + utilRtPath;
          }
          writer.println(classpath);
        } else {
          writer.println("");
        }

        writer.println(((JavaSdkType) jdk.getSdkType()).getVMExecutablePath(jdk));
        for (String vmParameter : javaParameters.getVMParametersList().getList()) {
          writer.println(vmParameter);
        }
      } finally {
        writer.close();
      }

      passForkMode(forkMode, tempFile);
    } catch (Exception e) {
      LOG.error(e);
    }
  }
 protected static void setToolsJar(JavaParameters params) {
   Sdk jdk = params.getJdk();
   if (jdk != null && jdk.getSdkType() instanceof JavaSdkType) {
     String toolsPath = ((JavaSdkType) jdk.getSdkType()).getToolsPath(jdk);
     if (toolsPath != null) {
       params.getVMParametersList().add("-Dtools.jar=" + toolsPath);
     }
   }
 }
 public void testRunningJUnit() throws ExecutionException {
   PsiClass testA = findTestA(getModule1());
   JUnitConfiguration configuration = createConfiguration(testA);
   JavaParameters parameters = checkCanRun(configuration);
   assertEmpty(parameters.getVMParametersList().getList());
   final SegmentedOutputStream notifications = new SegmentedOutputStream(System.out);
   assertTrue(
       JUnitStarter.checkVersion(
           parameters.getProgramParametersList().getArray(), new PrintStream(notifications)));
   assertTrue(parameters.getProgramParametersList().getList().contains(testA.getQualifiedName()));
   assertEquals(JUnitStarter.class.getName(), parameters.getMainClass());
   assertEquals(myJdk.getHomeDirectory().getPresentableUrl(), parameters.getJdkPath());
 }
Ejemplo n.º 7
0
  private void appendForkInfo(Executor executor) throws ExecutionException {
    final String forkMode = myConfiguration.getForkMode();
    if (Comparing.strEqual(forkMode, "none")) {
      return;
    }

    if (getRunnerSettings().getData() != null) {
      final String actionName = executor.getActionName();
      throw new CantRunException(
          actionName
              + " is disabled in fork mode.<br/>Please change fork mode to &lt;none&gt; to "
              + actionName.toLowerCase()
              + ".");
    }

    final JavaParameters javaParameters = getJavaParameters();
    final Sdk jdk = javaParameters.getJdk();
    if (jdk == null) {
      throw new ExecutionException(
          ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
    }

    try {
      final File tempFile = FileUtil.createTempFile("command.line", "", true);
      final PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
      try {
        writer.println(((JavaSdkType) jdk.getSdkType()).getVMExecutablePath(jdk));
        for (String vmParameter : javaParameters.getVMParametersList().getList()) {
          writer.println(vmParameter);
        }
        writer.println("-classpath");
        writer.println(javaParameters.getClassPath().getPathsString());
      } finally {
        writer.close();
      }

      myJavaParameters
          .getProgramParametersList()
          .add("@@@" + forkMode + ',' + tempFile.getAbsolutePath());
    } catch (Exception e) {
      LOG.error(e);
    }
  }
  protected DebuggerSession createRemoteProcess(
      final int transport, final boolean serverMode, JavaParameters javaParameters)
      throws ExecutionException, InterruptedException, InvocationTargetException {
    boolean useSockets = transport == DebuggerSettings.SOCKET_TRANSPORT;

    RemoteConnection remoteConnection =
        new RemoteConnection(useSockets, "127.0.0.1", String.valueOf(DEFAULT_ADDRESS), serverMode);

    String launchCommandLine = remoteConnection.getLaunchCommandLine();

    launchCommandLine = StringUtil.replace(launchCommandLine, RemoteConnection.ONTHROW, "");
    launchCommandLine = StringUtil.replace(launchCommandLine, RemoteConnection.ONUNCAUGHT, "");

    launchCommandLine = StringUtil.replace(launchCommandLine, "suspend=n", "suspend=y");

    println(launchCommandLine, ProcessOutputTypes.SYSTEM);

    for (StringTokenizer tokenizer = new StringTokenizer(launchCommandLine);
        tokenizer.hasMoreTokens(); ) {
      String token = tokenizer.nextToken();
      javaParameters.getVMParametersList().add(token);
    }

    GeneralCommandLine commandLine = CommandLineBuilder.createFromJavaParameters(javaParameters);

    DebuggerSession debuggerSession;

    if (serverMode) {
      debuggerSession = attachVM(remoteConnection, false);
      commandLine.createProcess();
    } else {
      commandLine.createProcess();
      debuggerSession = attachVM(remoteConnection, true);
    }

    ProcessHandler processHandler = debuggerSession.getProcess().getProcessHandler();
    DebugProcessImpl process =
        (DebugProcessImpl)
            DebuggerManagerEx.getInstanceEx(myProject).getDebugProcess(processHandler);

    assertNotNull(process);
    return debuggerSession;
  }
  @Override
  public JavaParameters createJavaParameters(
      @NotNull Module module,
      boolean forCreation,
      boolean forTests,
      boolean classpathFromDependencies,
      @Nullable String jvmParams,
      @NotNull MvcCommand command)
      throws ExecutionException {
    JavaParameters params = new JavaParameters();

    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();

    params.setJdk(sdk);
    final VirtualFile sdkRoot = getSdkRoot(module);
    if (sdkRoot == null) {
      return params;
    }

    Map<String, String> env = params.getEnv();
    if (env == null) {
      env = new HashMap<String, String>();
      params.setEnv(env);
    }
    env.put(getSdkHomePropertyName(), FileUtil.toSystemDependentName(sdkRoot.getPath()));

    final VirtualFile lib = sdkRoot.findChild("lib");
    if (lib != null) {
      for (final VirtualFile child : lib.getChildren()) {
        final String name = child.getName();
        if (name.startsWith("groovy-all-") && name.endsWith(".jar")) {
          params.getClassPath().add(child);
        }
      }
    }
    final VirtualFile dist = sdkRoot.findChild("dist");
    if (dist != null) {
      for (final VirtualFile child : dist.getChildren()) {
        final String name = child.getName();
        if (name.endsWith(".jar")) {
          if (name.startsWith("griffon-cli-")
              || name.startsWith("griffon-rt-")
              || name.startsWith("griffon-resources-")) {
            params.getClassPath().add(child);
          }
        }
      }
    }

    /////////////////////////////////////////////////////////////

    params.setMainClass("org.codehaus.griffon.cli.support.GriffonStarter");

    final VirtualFile rootFile;

    if (forCreation) {
      VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
      if (roots.length != 1) {
        throw new ExecutionException(
            "Failed to initialize griffon module: module "
                + module.getName()
                + " contains more than one root");
      }

      command.getArgs().add(0, roots[0].getName());

      rootFile = roots[0].getParent();
    } else {
      rootFile = findAppRoot(module);
      if (rootFile == null) {
        throw new ExecutionException(
            "Failed to run griffon command: module "
                + module.getName()
                + " is not a Griffon module");
      }
    }

    String workDir = VfsUtilCore.virtualToIoFile(rootFile).getAbsolutePath();

    if (jvmParams != null) {
      params.getVMParametersList().addParametersString(jvmParams);
    }

    if (!params.getVMParametersList().getParametersString().contains(XMX_JVM_PARAMETER)) {
      params.getVMParametersList().add("-Xmx256M");
    }

    final String griffonHomePath = FileUtil.toSystemDependentName(sdkRoot.getPath());
    params.getVMParametersList().add("-Dgriffon.home=" + griffonHomePath);
    params.getVMParametersList().add("-Dbase.dir=" + workDir);

    assert sdk != null;
    params
        .getVMParametersList()
        .add("-Dtools.jar=" + ((JavaSdkType) sdk.getSdkType()).getToolsPath(sdk));

    final String confpath = griffonHomePath + GROOVY_STARTER_CONF;
    params.getVMParametersList().add("-Dgroovy.starter.conf=" + confpath);

    params
        .getVMParametersList()
        .add(
            "-Dgroovy.sanitized.stacktraces=\"groovy., org.codehaus.groovy., java., javax., sun., gjdk.groovy., gant., org.codehaus.gant.\"");

    params.getProgramParametersList().add("--main");
    params.getProgramParametersList().add("org.codehaus.griffon.cli.GriffonScriptRunner");
    params.getProgramParametersList().add("--conf");
    params.getProgramParametersList().add(confpath);
    if (!forCreation && classpathFromDependencies) {
      final String path = getApplicationClassPath(module).getPathsString();
      if (StringUtil.isNotEmpty(path)) {
        params.getProgramParametersList().add("--classpath");
        params.getProgramParametersList().add(path);
      }
    }

    params.setWorkingDirectory(workDir);

    ParametersList paramList = new ParametersList();
    command.addToParametersList(paramList);
    params.getProgramParametersList().add(paramList.getParametersString());

    params.setDefaultCharset(module.getProject());

    return params;
  }
  /**
   * @param project
   * @param parameters
   * @param coreSettings
   * @param runnerSettings
   * @param runConfiguration used to creation fix if maven home not found
   * @return
   * @throws ExecutionException
   */
  public static JavaParameters createJavaParameters(
      @Nullable final Project project,
      @NotNull final MavenRunnerParameters parameters,
      @Nullable MavenGeneralSettings coreSettings,
      @Nullable MavenRunnerSettings runnerSettings,
      @Nullable MavenRunConfiguration runConfiguration)
      throws ExecutionException {
    final JavaParameters params = new JavaParameters();

    ApplicationManager.getApplication().assertReadAccessAllowed();

    if (coreSettings == null) {
      coreSettings =
          project == null
              ? new MavenGeneralSettings()
              : MavenProjectsManager.getInstance(project).getGeneralSettings();
    }
    if (runnerSettings == null) {
      runnerSettings =
          project == null ? new MavenRunnerSettings() : MavenRunner.getInstance(project).getState();
    }

    params.setWorkingDirectory(parameters.getWorkingDirFile());

    params.setJdk(
        getJdk(
            project,
            runnerSettings,
            project != null && MavenRunner.getInstance(project).getState() == runnerSettings));

    final String mavenHome = resolveMavenHome(coreSettings, project, runConfiguration);

    addVMParameters(params.getVMParametersList(), mavenHome, runnerSettings);

    File confFile = MavenUtil.getMavenConfFile(new File(mavenHome));
    if (!confFile.isFile()) {
      throw new ExecutionException(
          "Configuration file is not exists in maven home: " + confFile.getAbsolutePath());
    }

    if (project != null && parameters.isResolveToWorkspace()) {
      try {
        String resolverJar = getArtifactResolverJar(MavenUtil.isMaven3(mavenHome));
        confFile = patchConfFile(confFile, resolverJar);

        File modulesPathsFile = dumpModulesPaths(project);
        params
            .getVMParametersList()
            .addProperty(MavenModuleMap.PATHS_FILE_PROPERTY, modulesPathsFile.getAbsolutePath());
      } catch (IOException e) {
        LOG.error(e);
        throw new ExecutionException("Failed to run maven configuration", e);
      }
    }

    params.getVMParametersList().addProperty("classworlds.conf", confFile.getPath());

    for (String path : getMavenClasspathEntries(mavenHome)) {
      params.getClassPath().add(path);
    }

    params.setMainClass(MAVEN_LAUNCHER_CLASS);
    EncodingManager encodingManager =
        project == null
            ? EncodingProjectManager.getInstance()
            : EncodingProjectManager.getInstance(project);
    params.setCharset(encodingManager.getDefaultCharset());

    addMavenParameters(
        params.getProgramParametersList(), mavenHome, coreSettings, runnerSettings, parameters);

    return params;
  }
  protected JavaParameters createJavaParameters() throws ExecutionException {

    log.debug("createJavaParameters");

    SubstepsRunConfiguration runConfig =
        (SubstepsRunConfiguration) this.getEnvironment().getRunProfile();

    SubstepsRunnerConfigurationModel model = runConfig.getModel();

    JavaParameters params = model.getJavaParameters();

    params.setWorkingDirectory(model.getWorkingDir());

    if (params.getJdk() == null) {

      log.debug("params on the jdk is null");

      params.setJdk(
          JavaSdkImpl.getInstance().createJdk(model.getVersionString(), model.getHomePath()));

      params.getClassPath().add(model.getClassPathString());
    }

    params.getProgramParametersList().add("prog-args-env", "prg-localhost");

    ParametersList vmParametersList = params.getVMParametersList();

    vmParametersList.addParametersString("-Dfile.encoding=UTF-8");
    vmParametersList.addParametersString("-Dcom.sun.management.jmxremote.port=" + jmxPort);
    vmParametersList.addParametersString("-Dcom.sun.management.jmxremote.authenticate=false");
    vmParametersList.addParametersString("-Dcom.sun.management.jmxremote.ssl=false");
    vmParametersList.addParametersString("-Djava.rmi.server.hostname=localhost");

    //        String rmiClasspathString = "\"file://" +
    // model.getClassPathString().replaceAll(File.pathSeparator, " file://") + "\"";

    //        log.debug("rmi classpath: " + rmiClasspathString);

    //        vmParametersList.addParametersString("-Djava.rmi.server.codebase=" +
    // rmiClasspathString);

    vmParametersList.addParametersString("-Dsun.io.serialization.extendedDebugInfo=true");

    //        System.setProperty("java.rmi.server.codebase", rmiClasspathString);

    //        createServerSocket(params);
    //
    //
    //        if (myServerSocket != null) {
    //            params.getProgramParametersList().add(ForkedDebuggerHelper.DEBUG_SOCKET +
    // myServerSocket.getLocalPort());
    //        }
    //

    log.debug(
        "launching substeps runner with classpath: "
            + params.getClassPath().getPathsString()
            + "\njvm info: "
            + model.getHomePath()
            + " version: "
            + model.getVersionString());

    return params;
  }