Skip to content

Commit

Permalink
Source folders of Plain Java project are not properly shown in Projec…
Browse files Browse the repository at this point in the history
…t Explorer after WS is restarted #10456 (#11003)

Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny authored and tsmaeder committed Oct 17, 2018
1 parent 4b3793f commit b616925
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.plugin.java.plain.server.projecttype;

import static org.eclipse.che.api.fs.server.WsPathUtils.absolutize;
import static org.eclipse.che.api.languageserver.LanguageServiceUtils.removePrefixUri;
import static org.eclipse.che.api.languageserver.util.JsonUtil.convertToJson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.ForbiddenException;
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.project.server.ProjectManager;
import org.eclipse.che.api.project.server.impl.NewProjectConfigImpl;
import org.eclipse.che.api.project.shared.NewProjectConfig;
import org.eclipse.che.api.project.shared.RegisteredProject;
import org.eclipse.che.jdt.ls.extension.api.Notifications;
import org.eclipse.che.plugin.java.languageserver.NotifyJsonRpcTransmitter;
import org.eclipse.lsp4j.ExecuteCommandParams;

public class PlainJavaProjectUpdateUtil {
public static CompletableFuture<Object> updateProjectConfig(
ProjectManager projectManager, String projectWsPath)
throws IOException, ForbiddenException, ConflictException, NotFoundException, ServerException,
BadRequestException {
String wsPath = absolutize(projectWsPath);
RegisteredProject project =
projectManager
.get(wsPath)
.orElseThrow(() -> new NotFoundException("Can't find project: " + projectWsPath));

NewProjectConfig projectConfig =
new NewProjectConfigImpl(
projectWsPath, project.getName(), project.getType(), project.getSource());
RegisteredProject result = projectManager.update(projectConfig);
return CompletableFuture.completedFuture(result.getPath());
}

public static void notifyClientOnProjectUpdate(
NotifyJsonRpcTransmitter notifyTransmitter, String projectPath) {
List<Object> parameters = new ArrayList<>();
parameters.add(removePrefixUri(convertToJson(projectPath).getAsString()));
notifyTransmitter.sendNotification(
new ExecuteCommandParams(Notifications.UPDATE_ON_PROJECT_CLASSPATH_CHANGED, parameters));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@
import static java.util.stream.Collectors.toList;
import static org.eclipse.che.ide.ext.java.shared.Constants.OUTPUT_FOLDER;
import static org.eclipse.che.ide.ext.java.shared.Constants.SOURCE_FOLDER;
import static org.eclipse.che.plugin.java.plain.server.projecttype.PlainJavaProjectUpdateUtil.notifyClientOnProjectUpdate;
import static org.eclipse.che.plugin.java.plain.server.projecttype.PlainJavaProjectUpdateUtil.updateProjectConfig;
import static org.eclipse.che.plugin.java.plain.shared.PlainJavaProjectConstants.DEFAULT_SOURCE_FOLDER_VALUE;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.ForbiddenException;
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.fs.server.PathTransformer;
import org.eclipse.che.api.project.server.ProjectManager;
import org.eclipse.che.api.project.server.notification.ProjectUpdatedEvent;
import org.eclipse.che.api.project.server.type.ReadonlyValueProvider;
import org.eclipse.che.api.project.server.type.ValueProvider;
import org.eclipse.che.api.project.server.type.ValueProviderFactory;
import org.eclipse.che.api.project.server.type.ValueStorageException;
import org.eclipse.che.plugin.java.languageserver.JavaLanguageServerExtensionService;
import org.eclipse.che.plugin.java.languageserver.NotifyJsonRpcTransmitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* {@link ValueProviderFactory} for Plain Java project type. Factory crates a class which provides
Expand All @@ -36,22 +51,52 @@
*/
@Singleton
public class PlainJavaValueProviderFactory implements ValueProviderFactory {
private static final Logger LOG = LoggerFactory.getLogger(PlainJavaValueProviderFactory.class);

private final PathTransformer transformer;
private final JavaLanguageServerExtensionService extensionService;
private final NotifyJsonRpcTransmitter notifyTransmitter;
private final ProjectManager projectManager;

@SuppressWarnings("unused")
private final EventService eventService;

@Inject
public PlainJavaValueProviderFactory(
PathTransformer transformer, JavaLanguageServerExtensionService extensionService) {
PathTransformer transformer,
JavaLanguageServerExtensionService extensionService,
NotifyJsonRpcTransmitter notifyTransmitter,
ProjectManager projectManager,
EventService eventService) {
this.transformer = transformer;
this.extensionService = extensionService;
this.notifyTransmitter = notifyTransmitter;
this.projectManager = projectManager;
this.eventService = eventService;
eventService.subscribe(this::onProjectUpdated, ProjectUpdatedEvent.class);
}

@Override
public ValueProvider newInstance(String wsPath) {
return new PlainJavaValueProvider(wsPath);
}

private void onProjectUpdated(ProjectUpdatedEvent event) {
try {
updateProjectConfig(projectManager, event.getProjectPath()).get();
} catch (BadRequestException
| ConflictException
| ForbiddenException
| IOException
| InterruptedException
| ExecutionException
| ServerException
| NotFoundException e) {
LOG.error(e.getMessage());
}
notifyClientOnProjectUpdate(notifyTransmitter, event.getProjectPath());
}

private class PlainJavaValueProvider extends ReadonlyValueProvider {

private String wsPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
*/
package org.eclipse.che.plugin.java.plain.server.rest;

import static org.eclipse.che.api.fs.server.WsPathUtils.absolutize;
import static org.eclipse.che.api.languageserver.LanguageServiceUtils.prefixURI;
import static org.eclipse.che.api.languageserver.LanguageServiceUtils.removePrefixUri;
import static org.eclipse.che.api.languageserver.util.JsonUtil.convertToJson;
import static org.eclipse.che.plugin.java.plain.server.projecttype.PlainJavaProjectUpdateUtil.notifyClientOnProjectUpdate;
import static org.eclipse.che.plugin.java.plain.server.projecttype.PlainJavaProjectUpdateUtil.updateProjectConfig;

import com.google.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
Expand All @@ -33,14 +30,9 @@
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.project.server.ProjectManager;
import org.eclipse.che.api.project.server.impl.NewProjectConfigImpl;
import org.eclipse.che.api.project.shared.NewProjectConfig;
import org.eclipse.che.api.project.shared.RegisteredProject;
import org.eclipse.che.jdt.ls.extension.api.Notifications;
import org.eclipse.che.jdt.ls.extension.api.dto.ClasspathEntry;
import org.eclipse.che.plugin.java.languageserver.JavaLanguageServerExtensionService;
import org.eclipse.che.plugin.java.languageserver.NotifyJsonRpcTransmitter;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,34 +77,11 @@ public void updateClasspath(
BadRequestException {
try {
extensionService.updateClasspathWithResult(prefixURI(projectPath), entries).get();
updateProjectConfig(projectPath).get();
updateProjectConfig(projectManager, projectPath).get();
} catch (InterruptedException | ExecutionException e) {
LOG.error(e.getMessage());
}

notifyClientOnProjectUpdate(projectPath);
}

private CompletableFuture<Object> updateProjectConfig(String projectWsPath)
throws IOException, ForbiddenException, ConflictException, NotFoundException, ServerException,
BadRequestException {
String wsPath = absolutize(projectWsPath);
RegisteredProject project =
projectManager
.get(wsPath)
.orElseThrow(() -> new NotFoundException("Can't find project: " + projectWsPath));

NewProjectConfig projectConfig =
new NewProjectConfigImpl(
projectWsPath, project.getName(), project.getType(), project.getSource());
RegisteredProject result = projectManager.update(projectConfig);
return CompletableFuture.completedFuture(result.getPath());
}

private void notifyClientOnProjectUpdate(String projectPath) {
List<Object> parameters = new ArrayList<>();
parameters.add(removePrefixUri(convertToJson(projectPath).getAsString()));
notifyTransmitter.sendNotification(
new ExecuteCommandParams(Notifications.UPDATE_ON_PROJECT_CLASSPATH_CHANGED, parameters));
notifyClientOnProjectUpdate(notifyTransmitter, projectPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ private void handleNotification(ExecuteCommandParams params) {
updateProjectConfig(stringValue(params.getArguments()));
break;
case Notifications.UPDATE_ON_PROJECT_CLASSPATH_CHANGED:
for (Object project : params.getArguments()) {
updateProject(stringValue(project))
.then(
container -> {
eventBus.fireEvent(
new ProjectClasspathChangedEvent(
stringValue(container.getLocation().toString())));
});
}
params
.getArguments()
.forEach(
project -> {
updateProject(stringValue(project))
.then(
container -> {
eventBus.fireEvent(
new ProjectClasspathChangedEvent(
stringValue(container.getLocation().toString())));
});
});
break;
default:
break;
Expand All @@ -118,8 +121,18 @@ private Promise<Container> updateProject(String project) {
optContainer -> {
if (optContainer.isPresent()) {
Container container = optContainer.get();
container.synchronize();
return promises.resolve(container);
return projectService
.getProject(Path.valueOf(project))
.thenPromise(
projectConfigDto -> {
return projectService
.updateProject(projectConfigDto)
.thenPromise(
arg -> {
container.synchronize();
return promises.resolve(container);
});
});
}
return promises.resolve(null);
});
Expand Down

0 comments on commit b616925

Please sign in to comment.