|
| 1 | +/** |
| 2 | + * Copyright (c) 2018 Angelo ZERR, Daniel Dekany. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation |
| 10 | + */ |
| 11 | +package org.eclipse.lsp4e.freemarker; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.OutputStream; |
| 16 | +import java.lang.reflect.InvocationTargetException; |
| 17 | +import java.lang.reflect.Method; |
| 18 | +import java.net.URL; |
| 19 | +import java.net.URLClassLoader; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.concurrent.Future; |
| 22 | + |
| 23 | +import org.eclipse.core.runtime.Platform; |
| 24 | +import org.osgi.framework.Bundle; |
| 25 | + |
| 26 | +/** |
| 27 | + * Starts the FreeMarker LSP server inside the current JVM, and connects to it. |
| 28 | + */ |
| 29 | +public class FreemarkerStreamConnectionProvider extends LocalStreamConnectionProvider { |
| 30 | + |
| 31 | + public FreemarkerStreamConnectionProvider() { |
| 32 | + super(FreemarkerPlugin.getDefault().getLog(), FreemarkerPlugin.getPluginId()); |
| 33 | + } |
| 34 | + |
| 35 | + private static final String LANGUAGE_SERVER_JAR_ENTRY_NAME = "server/freemarker-languageserver-all.jar"; //$NON-NLS-1$ |
| 36 | + private static final String LANGUAGE_SERVER_LAUNCHER_CLASS_NAME = "freemarker.ext.languageserver.FreemarkerServerLauncher"; //$NON-NLS-1$ |
| 37 | + |
| 38 | + @Override |
| 39 | + protected LocalServer launchServer(InputStream clientToServerStream, OutputStream serverToClientStream) |
| 40 | + throws IOException { |
| 41 | + URL[] classPath = getFreemarkerLanguageServerClassPath(); |
| 42 | + logInfo("Using class path: " + Arrays.toString(classPath)); |
| 43 | + |
| 44 | + URLClassLoader dynamicJarClassLoader = new URLClassLoader(classPath); |
| 45 | + |
| 46 | + Method launcherMethod; |
| 47 | + try { |
| 48 | + Class<?> launcherClass = dynamicJarClassLoader.loadClass(LANGUAGE_SERVER_LAUNCHER_CLASS_NAME); |
| 49 | + launcherMethod = launcherClass.getMethod("launch", //$NON-NLS-1$ |
| 50 | + new Class<?>[] { InputStream.class, OutputStream.class }); |
| 51 | + } catch (Exception e) { |
| 52 | + throw new RuntimeException( |
| 53 | + "Couldn't get launcher class and method via Java reflection (using class path: " |
| 54 | + + Arrays.toString(classPath) + "); see cause exception", e); //$NON-NLS-2$ |
| 55 | + } |
| 56 | + Future<?> launchedFuture; |
| 57 | + try { |
| 58 | + launchedFuture = (Future<?>) launcherMethod.invoke(null, clientToServerStream, serverToClientStream); |
| 59 | + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
| 60 | + throw new RuntimeException("Error when calling launcher method; see cause exception", e); //$NON-NLS-1$ |
| 61 | + } |
| 62 | + |
| 63 | + return new LocalServer(launchedFuture) { |
| 64 | + @Override |
| 65 | + public void stop() { |
| 66 | + super.stop(); |
| 67 | + try { |
| 68 | + dynamicJarClassLoader.close(); |
| 69 | + } catch (IOException e) { |
| 70 | + logError("Error when closing the dynamic jar class-loader", e); //$NON-NLS-1$ |
| 71 | + } |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + private URL[] getFreemarkerLanguageServerClassPath() { |
| 77 | + Bundle bundle = Platform.getBundle(FreemarkerPlugin.PLUGIN_ID); |
| 78 | + if (bundle == null) { |
| 79 | + throw new RuntimeException("Bundle " + FreemarkerPlugin.PLUGIN_ID + " not found"); //$NON-NLS-1$ |
| 80 | + } |
| 81 | + |
| 82 | + URL languageServerJarURL = bundle.getEntry(LANGUAGE_SERVER_JAR_ENTRY_NAME); |
| 83 | + if (languageServerJarURL == null) { |
| 84 | + throw new RuntimeException( |
| 85 | + "Entity " + LANGUAGE_SERVER_JAR_ENTRY_NAME + " not found in bundle " + FreemarkerPlugin.PLUGIN_ID); //$NON-NLS-1$ |
| 86 | + } |
| 87 | + |
| 88 | + // TODO: Add freemarker.jar from the user project here, if it's found and has |
| 89 | + // high enough version, otherwise add freemarker.jar from this plugin. |
| 90 | + // (Currently, freemarker.jar is bundled into the language server jar.) |
| 91 | + |
| 92 | + return new URL[] { languageServerJarURL }; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments