Skip to content

Commit

Permalink
fix PageContext creation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 19, 2024
1 parent 8f59758 commit 00389e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ synchronized static void load(ConfigServerImpl cs, ConfigImpl config, ConfigWebI
_loadStartupHook(cs, config, root, log);
if (LOG) LogUtil.logGlobal(ThreadLocalPageContext.getConfig(cs == null ? config : cs), Log.LEVEL_DEBUG, ConfigWebFactory.class.getName(), "loaded startup hook");

if (config instanceof ConfigServer) LSPEndpointFactory.init(config, false);
if (config instanceof ConfigServer) LSPEndpointFactory.getInstance(config, false);
}

config.setLoadTime(System.currentTimeMillis());
Expand Down
45 changes: 20 additions & 25 deletions core/src/main/java/lucee/runtime/lsp/LSPEndpointFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lucee.runtime.lsp;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
Expand All @@ -10,9 +10,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.Cookie;
import javax.servlet.ServletException;

import lucee.print;
import lucee.commons.io.DevNullOutputStream;
import lucee.commons.io.SystemUtil;
import lucee.commons.io.log.Log;
import lucee.loader.engine.CFMLEngine;
Expand All @@ -21,10 +22,8 @@
import lucee.runtime.Component;
import lucee.runtime.PageContext;
import lucee.runtime.config.Config;
import lucee.runtime.config.ConfigServer;
import lucee.runtime.config.ConfigWeb;
import lucee.runtime.op.Caster;
import lucee.runtime.thread.ThreadUtil;
import lucee.runtime.thread.SerializableCookie;

public class LSPEndpointFactory {
private static final int DEFAULT_LSP_PORT = 2089; // Common LSP port
Expand All @@ -48,11 +47,11 @@ private LSPEndpointFactory(Config config) {
cfcPath = engine.getCastUtil().toString(SystemUtil.getSystemPropOrEnvVar("lucee.lsp.component", null), DEFAULT_COMPONENT);
if (Util.isEmpty(cfcPath, true)) cfcPath = DEFAULT_COMPONENT;

log.debug("lsp", "LSP server port: " + port);
log.debug("lsp", "LSP server component endpoint: " + cfcPath);
log.info("lsp", "LSP server port: " + port);
log.info("lsp", "LSP server component endpoint: " + cfcPath);
}

public static LSPEndpointFactory init(Config config, boolean forceRestart) throws IOException {
public static LSPEndpointFactory getInstance(Config config, boolean forceRestart) throws IOException {
if (Caster.toBooleanValue(SystemUtil.getSystemPropOrEnvVar("lucee.lsp.enabled", null), false)) {
print.e("---- LSPEndpointFactory ----");
synchronized (SystemUtil.createToken("LSPEndpointFactory", "init")) {
Expand All @@ -75,8 +74,12 @@ public static LSPEndpointFactory init(Config config, boolean forceRestart) throw
return instance;
}

public static LSPEndpointFactory getExistingInstance() {
return instance;
}

private LSPEndpointFactory start() throws IOException {
log.debug("lsp", "starting LSP server");
log.info("lsp", "starting LSP server");
try {
serverSocket = new ServerSocket(port);
}
Expand Down Expand Up @@ -104,7 +107,7 @@ private LSPEndpointFactory start() throws IOException {
listenerThread.setDaemon(true);
listenerThread.start();

log.debug("lsp", "LSP server started");
log.info("lsp", "LSP server started");
return this;
}

Expand All @@ -118,13 +121,9 @@ private void stop() throws IOException {
}
}

public static LSPEndpointFactory getInstance(Config config) {
return instance;
}

private void handleClient(Socket clientSocket) {

log.debug("lsp", "LSP server handle client");
log.info("lsp", "LSP server handle client");
try {
// Get input/output streams
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Expand Down Expand Up @@ -188,10 +187,10 @@ private void handleClient(Socket clientSocket) {
}
}

private String processMessage(String jsonMessage) {
public String processMessage(String jsonMessage) {
try {
log.info("lsp", "Received message: " + jsonMessage);
PageContext pc = createPageContext((ConfigWeb) engine.getThreadConfig());
PageContext pc = createPageContext();
Component cfc = engine.getCreationUtil().createComponentFromName(pc, cfcPath);

String response = engine.getCastUtil().toString(cfc.call(pc, "execute", new Object[] { jsonMessage }));
Expand All @@ -214,12 +213,7 @@ private void error(String type, Exception e) {

public static Log getLog(Config config) {
if (config == null) config = CFMLEngineFactory.getInstance().getThreadConfig();
if (config instanceof ConfigServer) {
// we only log to config Server if there is no web context
Config cw = CFMLEngineFactory.getInstance().getThreadConfig();
if (cw != null) config = cw;
}
if (config == null) return null;

try {
Log log = config.getLog("lsp");
if (log == null) log = config.getLog(DEFAULT_LOG);
Expand All @@ -233,8 +227,9 @@ public static Log getLog(Config config) {
return null;
}

public static PageContext createPageContext(final ConfigWeb cw) {
return ThreadUtil.createPageContext(cw, new ByteArrayOutputStream(), "", "/", "", new Cookie[0], null, null, null, null, true, TIMEOUT);
public static PageContext createPageContext() throws ServletException {
return CFMLEngineFactory.getInstance().createPageContext(new File("."), "localhost", "/", "", SerializableCookie.COOKIES0, null, null, null,
DevNullOutputStream.DEV_NULL_OUTPUT_STREAM, -1, false);
}

public static void releasePageContext(PageContext pc) {
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.1.2.13-SNAPSHOT"/>
<property name="version" value="6.1.2.14-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.1.2.13-SNAPSHOT</version>
<version>6.1.2.14-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 00389e0

Please sign in to comment.