Skip to content

Commit

Permalink
fix: StreamConnectionProvider instance should not be a singleton (#888)
Browse files Browse the repository at this point in the history
Fixes #888

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed May 25, 2023
1 parent f8825e4 commit a92eff2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public synchronized void start() throws IOException {
final URI rootURI = getRootURI();
this.launcherFuture = new CompletableFuture<>();
this.initializeFuture = CompletableFuture.supplyAsync(() -> {
this.lspStreamProvider = serverDefinition.createConnectionProvider();
this.lspStreamProvider = serverDefinition.createConnectionProvider(initialProject.getProject());
initParams.setInitializationOptions(this.lspStreamProvider.getInitializationOptions(rootURI));
try {
// Starting process...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

/**
* Language server registry.
*
*/
public class LanguageServersRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(LanguageServersRegistry.class);
Expand All @@ -52,7 +51,7 @@ public LanguageServerDefinition(@Nonnull String id, @Nonnull String label, Strin
this.label = label;
this.description = description;
this.isSingleton = isSingleton;
this.lastDocumentDisconnectedTimeout = lastDocumentDisconnectedTimeout != null && lastDocumentDisconnectedTimeout > 0 ? lastDocumentDisconnectedTimeout : DEFAULT_LAST_DOCUMENTED_DISCONNECTED_TIMEOUT;
this.lastDocumentDisconnectedTimeout = lastDocumentDisconnectedTimeout != null && lastDocumentDisconnectedTimeout > 0 ? lastDocumentDisconnectedTimeout : DEFAULT_LAST_DOCUMENTED_DISCONNECTED_TIMEOUT;
this.languageIdMappings = new ConcurrentHashMap<>();
}

Expand All @@ -65,7 +64,7 @@ public String getDisplayName() {
return label != null ? label : id;
}

public abstract StreamConnectionProvider createConnectionProvider();
public abstract StreamConnectionProvider createConnectionProvider(Project project);

public LanguageClientImpl createLanguageClient(Project project) {
return new LanguageClientImpl(project);
Expand All @@ -89,9 +88,15 @@ public ExtensionLanguageServerDefinition(ServerExtensionPointBean element) {
}

@Override
public StreamConnectionProvider createConnectionProvider() {
public StreamConnectionProvider createConnectionProvider(Project project) {
String serverImpl = extension.getImplementationClassName();
if (serverImpl == null || serverImpl.isEmpty()) {
throw new RuntimeException(
"Exception occurred while creating an instance of the stream connection provider, you have to define server/@class attribute in the extension point."); //$NON-NLS-1$
}
try {
return extension.getInstance();
return (StreamConnectionProvider) project.instantiateClass(extension.getServerImpl(),
extension.getPluginDescriptor().getPluginId());
} catch (Exception e) {
throw new RuntimeException(
"Exception occurred while creating an instance of the stream connection provider", e); //$NON-NLS-1$
Expand All @@ -117,17 +122,18 @@ public LanguageClientImpl createLanguageClient(Project project) {
public Class<? extends LanguageServer> getServerInterface() {
String serverInterface = extension.serverInterface;
if (serverInterface != null && !serverInterface.isEmpty()) {
try {
return (Class<? extends LanguageServer>) (Class<?>)extension.getServerInterface();
} catch (ClassNotFoundException exception) {
LOGGER.warn(exception.getLocalizedMessage(), exception);
}
try {
return (Class<? extends LanguageServer>) (Class<?>) extension.getServerInterface();
} catch (ClassNotFoundException exception) {
LOGGER.warn(exception.getLocalizedMessage(), exception);
}
return super.getServerInterface();
}
return super.getServerInterface();
}
}

private static LanguageServersRegistry INSTANCE = null;

public static LanguageServersRegistry getInstance() {
if (INSTANCE == null) {
INSTANCE = new LanguageServersRegistry();
Expand Down Expand Up @@ -203,7 +209,8 @@ public List<ContentTypeToLanguageServerDefinition> getContentTypeToLSPExtensions
return this.connections.stream().filter(mapping -> mapping.getValue() instanceof ExtensionLanguageServerDefinition).collect(Collectors.toList());
}

public @Nullable LanguageServerDefinition getDefinition(@NonNull String languageServerId) {
public @Nullable
LanguageServerDefinition getDefinition(@NonNull String languageServerId) {
for (ContentTypeToLanguageServerDefinition mapping : this.connections) {
if (mapping.getValue().id.equals(languageServerId)) {
return mapping.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class ServerExtensionPointBean extends BaseKeyedLazyInstance<StreamConnec
public String description;

@Attribute("class")
public String clazz;
public String serverImpl;
private Class<?> serverImplClass;

@Attribute("clientImpl")
public String clientImpl;
Expand All @@ -43,6 +44,13 @@ public Class getClientImpl() throws ClassNotFoundException {
return clientClass;
}

public Class getServerImpl() throws ClassNotFoundException {
if (serverImplClass == null) {
serverImplClass = getPluginDescriptor().getPluginClassLoader().loadClass(serverImpl);
}
return serverImplClass;
}

public Class getServerInterface() throws ClassNotFoundException {
if (serverClass == null) {
serverClass = getPluginDescriptor().getPluginClassLoader().loadClass(serverInterface);
Expand All @@ -52,6 +60,6 @@ public Class getServerInterface() throws ClassNotFoundException {

@Override
protected @Nullable String getImplementationClassName() {
return clazz;
return serverImpl;
}
}

0 comments on commit a92eff2

Please sign in to comment.