Skip to content

Commit

Permalink
Merge pull request #1363 from 1c-syntax/feature/closable-spring-context
Browse files Browse the repository at this point in the history
Возможность закрыть и переиспользовать спринговый контекст
  • Loading branch information
nixel2007 authored Sep 18, 2020
2 parents 9e88965 + 1e653e7 commit f8146b6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo;
import lombok.AccessLevel;
import lombok.Getter;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.DefaultResourceLoader;

Expand All @@ -40,16 +42,27 @@
@ComponentScan("com.github._1c_syntax.bsl.languageserver")
public class BSLLSBinding {

@Getter(lazy = true)
private static final ApplicationContext applicationContext = createContext();
@Getter(lazy = true, value = AccessLevel.PRIVATE)
private static final SpringApplication application = createApplication();
@Getter(lazy = true, value = AccessLevel.PRIVATE)
private static final ConfigurableApplicationContext context = createContext();

public BSLLSBinding() {
// public constructor is needed for spring initialization
}

public static ConfigurableApplicationContext getApplicationContext() {
var context = getContext();
if (!context.isActive()) {
context = createContext();
}

return context;
}

@SuppressWarnings("unchecked")
public static Collection<DiagnosticInfo> getDiagnosticInfos() {
return (Collection<DiagnosticInfo>) getApplicationContext().getBean("diagnosticInfos", Collection.class);
return getApplicationContext().getBean("diagnosticInfos", Collection.class);
}

public static LanguageServerConfiguration getLanguageServerConfiguration() {
Expand All @@ -60,7 +73,7 @@ public static ServerContext getServerContext() {
return getApplicationContext().getBean(ServerContext.class);
}

private static ApplicationContext createContext() {
private static SpringApplication createApplication() {
return new SpringApplicationBuilder(BSLLSBinding.class)
.bannerMode(Banner.Mode.OFF)
.web(WebApplicationType.NONE)
Expand All @@ -71,7 +84,10 @@ private static ApplicationContext createContext() {
"app.command.line.runner.enabled", "false",
"app.scheduling.enabled", "false"
))
.build()
.run();
.build();
}

private static ConfigurableApplicationContext createContext() {
return getApplication().run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
package com.github._1c_syntax.bsl.languageserver;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
class BSLLSBindingTest {

@Test
Expand All @@ -37,4 +35,35 @@ void testGetServerContext() {
// then
assertThat(serverContext).isNotNull();
}

@Test
void testGetDiagnosticInfos() {
// when
var diagnosticInfos = BSLLSBinding.getDiagnosticInfos();

// then
assertThat(diagnosticInfos).isNotNull();
}

@Test
void testGetLanguageServerConfiguration() {
// when
var languageServerConfiguration = BSLLSBinding.getLanguageServerConfiguration();

// then
assertThat(languageServerConfiguration).isNotNull();
}

@Test
void testReactivateContext() {
// given
var applicationContext = BSLLSBinding.getApplicationContext();
applicationContext.close();

// when
applicationContext = BSLLSBinding.getApplicationContext();

// then
assertThat(applicationContext.isActive()).isTrue();
}
}

0 comments on commit f8146b6

Please sign in to comment.