Advertisement
kolbka_

Untitled

Jul 20th, 2023 (edited)
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1.  
  2. package org.eclipse.buildship.gradleprop.provider;
  3.  
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.net.URL;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Arrays;
  10.  
  11. import org.eclipse.lsp4e.server.ProcessStreamConnectionProvider;
  12. import org.eclipse.lsp4e.server.StreamConnectionProvider;
  13. import org.eclipse.core.runtime.FileLocator;
  14. import org.eclipse.jdt.launching.IVMInstall;
  15. import org.eclipse.jdt.launching.IVMInstallType;
  16. import org.eclipse.jdt.launching.JavaRuntime;
  17. import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
  18. import org.osgi.framework.FrameworkUtil;
  19. import org.osgi.framework.Bundle;
  20. import java.net.URI;
  21. import java.net.URISyntaxException;
  22. import java.nio.file.Path;
  23. import java.nio.file.Paths;
  24. import org.eclipse.swt.SWT;
  25. import org.eclipse.swt.widgets.Display;
  26. import org.eclipse.swt.widgets.MessageBox;
  27. import org.eclipse.swt.widgets.Shell;
  28. import org.eclipse.ui.PlatformUI;
  29.  
  30. public class ConnectionProvider extends ProcessStreamConnectionProvider
  31.     implements StreamConnectionProvider {
  32.  
  33.   public GradlePropertiesConnectionProvider() {
  34.     URL localFileURL;
  35.     Bundle bundle = FrameworkUtil.getBundle(ConnectionProvider.class);
  36.     try {
  37.       localFileURL = FileLocator.toFileURL(bundle.getEntry("/"));
  38.       URI localFileURI = new URI(localFileURL.toExternalForm());
  39.       Path pathToPlugin = Paths.get(localFileURI.getPath());
  40.  
  41.       String pathToServer = pathToPlugin.resolve("libs/kotlin-language-server.jar").toString();
  42.  
  43.       IExecutionEnvironment[] executionEnvironments = JavaRuntime.getExecutionEnvironmentsManager()
  44.           .getExecutionEnvironments();
  45.  
  46.       IExecutionEnvironment java11Environment = null;
  47.  
  48.       for (IExecutionEnvironment environment : executionEnvironments) {
  49.         if (environment.getId().equals("JavaSE-11")) {
  50.           java11Environment = environment;
  51.           break;
  52.         }
  53.       }
  54.  
  55.       ArrayList<IVMInstall> compatibleJVMs = new ArrayList<>(
  56.           Arrays.asList(java11Environment.getCompatibleVMs()));
  57.       IVMInstall defaultVMInstall = JavaRuntime.getDefaultVMInstall();
  58.       IVMInstall javaExecutable = null;
  59.  
  60.       if (!compatibleJVMs.isEmpty()) {
  61.         if (compatibleJVMs.contains(defaultVMInstall)) {
  62.           javaExecutable = defaultVMInstall;
  63.         } else {
  64.           javaExecutable = compatibleJVMs.get(0);
  65.         }
  66.  
  67.         String pathToJavaExecutable = javaExecutable.getInstallLocation().toPath().resolve("bin")
  68.             .resolve("java")
  69.             .toString();
  70.  
  71.         List<String> commands = new ArrayList<>(
  72.             Arrays.asList(pathToJavaExecutable, "-jar", pathToServer));
  73.  
  74.         // add in commands path to bin application of language server
  75.         setCommands(commands);
  76.         setWorkingDirectory(pathToPlugin.toString());
  77.        
  78.       } else {
  79.         PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
  80.           public void run() {
  81.             Shell shell = new Shell();
  82.             MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
  83.             messageBox.setText("Error");
  84.             messageBox.setMessage(
  85.                 "Compatible version of Java isn't found! Install and rerun application.");
  86.             messageBox.open();
  87.             shell.dispose();
  88.           }
  89.         });
  90.       }
  91.  
  92.  
  93.     } catch (IOException | URISyntaxException e) {
  94.       System.err.println("[GradlePropertiesConnectionProvider]:" + e.toString());
  95.       e.printStackTrace();
  96.     }
  97.   }
  98.  
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement