Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intellij 86 #195

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ buildscript {
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
maven {
url 'https://jitpack.io'
}
}
}

Expand Down Expand Up @@ -63,6 +66,7 @@ checkSourceFormatting {

dependencies {
compile group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "3.4.0"
compile group: "com.github.ballerina-platform", name: "lsp4intellij", version: "0.93.0"
compile group: "commons-configuration", name: "commons-configuration", version: "1.10"
compile group: "org.apache.ant", name: "ant", version: "1.10.1"
compile group: "org.dom4j", name: "dom4j", version: "2.1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.ide.idea.languageserver;

import com.intellij.openapi.application.PreloadingActivity;
import com.intellij.openapi.progress.ProgressIndicator;

import com.liferay.ide.idea.util.FileUtil;
import com.liferay.ide.idea.util.SocketSupport;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.net.JarURLConnection;
import java.net.URL;

import java.nio.file.Files;
import java.nio.file.Paths;

import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;

import org.wso2.lsp4intellij.IntellijLanguageClient;
import org.wso2.lsp4intellij.client.languageserver.serverdefinition.RawCommandServerDefinition;

/**
* @author Dominik Marks
* @author Terry Jia
*/
public class LiferayLanguageServerPreloadingActivity extends PreloadingActivity implements SocketSupport {

@Override
public void preload(@NotNull ProgressIndicator progressIndicator) {
String[] args = {
_computeJavaPath(), "-DliferayLanguageServerStandardIO=true", "-jar",
_computeLiferayPropertiesServerJarPath()
};

RawCommandServerDefinition rawCommandServerDefinition = new RawCommandServerDefinition("properties", args);

IntellijLanguageClient.addServerDefinition(rawCommandServerDefinition);
}

private static String _computeJavaPath() {
String javaPath = "java";

String paths = System.getenv("PATH");

boolean existsInPath = Stream.of(
paths.split(Pattern.quote(File.pathSeparator))
).map(
Paths::get
).anyMatch(
path -> Files.exists(path.resolve("java"))
);

if (!existsInPath) {
String javaHome = System.getProperty("java.home");

File file = new File(javaHome, "bin/java" + (_isWindows() ? ".exe" : ""));

javaPath = file.getAbsolutePath();
}

return javaPath;
}

private static String _computeLiferayPropertiesServerJarPath() {
Properties properties = System.getProperties();

File temp = new File(properties.getProperty("user.home"), ".liferay-intellij-plugin");

File liferayPropertiesServerJar = new File(temp, _PROPERTIES_LSP_JAR_FILE_NAME);

boolean needToCopy = true;

ClassLoader classLoader = LiferayLanguageServerPreloadingActivity.class.getClassLoader();

URL url = classLoader.getResource("/libs/" + _PROPERTIES_LSP_JAR_FILE_NAME);

try (InputStream in = classLoader.getResourceAsStream("/libs/" + _PROPERTIES_LSP_JAR_FILE_NAME)) {
JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();

JarEntry jarEntry = jarURLConnection.getJarEntry();

Long bladeJarTimestamp = jarEntry.getTime();

if (liferayPropertiesServerJar.exists()) {
Long destTimestamp = liferayPropertiesServerJar.lastModified();

if (destTimestamp < bladeJarTimestamp) {
liferayPropertiesServerJar.delete();
}
else {
needToCopy = false;
}
}

if (needToCopy) {
FileUtil.writeFile(liferayPropertiesServerJar, in);
liferayPropertiesServerJar.setLastModified(bladeJarTimestamp);
}
}
catch (IOException ioe) {
}

return liferayPropertiesServerJar.getAbsolutePath();
}

private static boolean _isWindows() {
String osName = System.getProperty("os.name");

osName = osName.toLowerCase();

return osName.contains("windows");
}

private static final String _PROPERTIES_LSP_JAR_FILE_NAME = "liferay-properties-server-all.jar";

}
76 changes: 76 additions & 0 deletions src/main/java/com/liferay/ide/idea/util/SocketSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.ide.idea.util;

import java.io.IOException;

import java.net.ServerSocket;
import java.net.SocketException;

import java.util.Random;

/**
* @author Terry Jia
*/
public interface SocketSupport {

public default int findUnusedPort(int low, int high) {
if (high < low) {
return -1;
}

for (int i = 0; i < 10; ++i) {
int port = getRandomPort(low, high);

if (!isPortInUse(port)) {
return port;
}
}

return -1;
}

public default int getRandomPort(int low, int high) {
return rand.nextInt(high - low) + low;
}

public default boolean isPortInUse(int port) {
ServerSocket serverSocket = null;

try {
serverSocket = new ServerSocket(port, 0, null);
}
catch (SocketException se) {
return true;
}
catch (IOException ioe) {
return true;
}
finally {
if (serverSocket != null) {
try {
serverSocket.close();
}
catch (Exception e) {
}
}
}

return false;
}

public final Random rand = new Random(System.currentTimeMillis());

}
11 changes: 11 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,16 @@
<manifest.parser.provider implementation="com.liferay.ide.idea.bnd.completion.header.OsgiManifestHeaderParsers"/>
<codeInsight.unresolvedReferenceQuickFixProvider implementation="com.liferay.ide.idea.extensions.GradleDependencyQuickFixProvider"/>
<inspectionToolProvider implementation="com.liferay.ide.idea.language.service.LiferayServiceXMLInspectionToolProvider"/>
<preloadingActivity implementation="com.liferay.ide.idea.languageserver.LiferayLanguageServerPreloadingActivity" id="com.liferay.ide.idea.languageserver.LiferayLanguageServerPreloadingActivity" />
<completion.contributor implementationClass="org.wso2.lsp4intellij.contributors.LSPCompletionContributor" id="LSPCompletionContributor" language="any" order="first" />
<externalAnnotator id="LSPAnnotator" language="Properties" implementationClass="org.wso2.lsp4intellij.contributors.annotator.LSPAnnotator"/>
</extensions>

<application-components>
<component>
<implementation-class>org.wso2.lsp4intellij.IntellijLanguageClient</implementation-class>
</component>
</application-components>

</idea-plugin>

Binary file not shown.