Skip to content

Commit

Permalink
CODENVY-669: Catch exception on init projects for avoiding crash wsag…
Browse files Browse the repository at this point in the history
…ent (#1521)

* CODENVY-669: Catch exeption during init project for avoding crash wsagent
  • Loading branch information
Vitalii Parfonov authored Jun 17, 2016
1 parent 5244878 commit c65e1ad
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.project.ProjectConfig;
import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.core.model.workspace.WorkspaceConfig;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.project.server.RegisteredProject.Problem;
import org.eclipse.che.api.project.server.handlers.ProjectHandlerRegistry;
import org.eclipse.che.api.project.server.handlers.ProjectInitHandler;
import org.eclipse.che.api.project.server.type.BaseProjectType;
import org.eclipse.che.api.project.server.type.ProjectTypeConstraintException;
import org.eclipse.che.api.project.server.type.ProjectTypeRegistry;
import org.eclipse.che.api.project.server.type.ValueStorageException;
import org.eclipse.che.api.vfs.Path;
import org.eclipse.che.api.vfs.VirtualFile;
import org.eclipse.che.api.vfs.VirtualFileSystem;
Expand Down Expand Up @@ -84,7 +85,19 @@ public void initProjects() throws ConflictException, NotFoundException, ServerEx
final String path = projectConfig.getPath();
final VirtualFile vf = vfs.getRoot().getChild(Path.of(path));
final FolderEntry projectFolder = ((vf == null) ? null : new FolderEntry(vf, this));
putProject(projectConfig, projectFolder, false, false);
// need that to make "problematic" project and not break the workspace
try {
putProject(projectConfig, projectFolder, false, false);
} catch (ProjectTypeConstraintException e) {
//in case bad config
projects.put(path, new RegisteredProject(projectFolder, false, false, projectTypeRegistry, new Problem(12, e.getMessage())));
} catch (NotFoundException e) {
//in case project type not found
projects.put(path, new RegisteredProject(projectFolder, false, false, projectTypeRegistry, new Problem(13, e.getMessage())));
} catch (ValueStorageException e) {
//in case can't calculate Attributes
projects.put(path, new RegisteredProject(projectFolder, false, false, projectTypeRegistry, new Problem(14, e.getMessage())));
}
}

initUnconfiguredFolders();
Expand Down Expand Up @@ -198,6 +211,8 @@ RegisteredProject putProject(ProjectConfig config,
return project;
}



/**
* Removes all projects on and under the incoming path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
*******************************************************************************/
package org.eclipse.che.api.project.server;

import com.google.common.collect.ImmutableList;

import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.project.ProjectConfig;
import org.eclipse.che.api.core.model.project.SourceStorage;
import org.eclipse.che.api.core.model.project.type.Attribute;
import org.eclipse.che.api.core.model.project.type.Value;
import org.eclipse.che.api.project.server.type.AttributeValue;
import org.eclipse.che.api.project.server.type.BaseProjectType;
import org.eclipse.che.api.project.server.type.ProjectTypeConstraintException;
import org.eclipse.che.api.project.server.type.ProjectTypeDef;
import org.eclipse.che.api.project.server.type.ProjectTypeRegistry;
Expand Down Expand Up @@ -95,6 +98,39 @@ public class RegisteredProject implements ProjectConfig {
initAttributes();
}


/**
* Either root folder in this case Project not configure well.
* Use it if can't initialize project correct, project type will be BLANK
*
* @param folder
* root local folder or null
* @param updated
* if this object was updated, i.e. no more synchronized with workspace master
* @param detected
* if this project was detected, initialized when "parent" project initialized
* @param problem
* project problem
*/
RegisteredProject(FolderEntry folder,
boolean updated,
boolean detected,
ProjectTypeRegistry projectTypeRegistry,
Problem problem) throws NotFoundException,
ProjectTypeConstraintException,
ServerException,
ValueStorageException {
attributes = new HashMap<>();

this.folder = folder;
this.config = new NewProjectConfig(folder.getPath());
this.updated = updated;
this.detected = detected;
types = new ProjectTypes(folder.getPath().toString(), BaseProjectType.ID, null, projectTypeRegistry);
problems = ImmutableList.of(problem);
}


/**
* Initialize project attributes.
*
Expand Down Expand Up @@ -275,8 +311,8 @@ public Map<String, List<String>> getAttributes() {
return attrs;
}

public class Problem {
private Problem(int code, String message) {
public static class Problem {
Problem(int code, String message) {
this.code = code;
this.message = message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ public void testInit() throws Exception {
}


@Test
public void testInitWithBadProject() throws Exception {

new File(root, "/foo").mkdir();
new File(root, "/bar").mkdir();

workspaceHolder.addProject(DtoFactory.newDto(ProjectConfigDto.class)
.withPath("/foo")
.withName("project1Name")
.withType("notFoundProjectType"));
workspaceHolder.addProject(DtoFactory.newDto(ProjectConfigDto.class)
.withPath("/bar")
.withName("project1Name")
.withType("pt3"));

ProjectTypeRegistry projectTypeRegistry = new ProjectTypeRegistry(new HashSet<>());
projectTypeRegistry.registerProjectType(new PT1());
projectTypeRegistry.registerProjectType(new PT3());

projectRegistry = new ProjectRegistry(workspaceHolder, vfsProvider, projectTypeRegistry, projectHandlerRegistry, eventService);
projectRegistry.initProjects();

assertEquals(6, projectRegistry.getProjects().size());
assertEquals(1, projectRegistry.getProject("/foo").getProblems().size());
assertEquals(13, projectRegistry.getProject("/foo").getProblems().get(0).code);
assertEquals(1, projectRegistry.getProject("/bar").getProblems().size());
assertEquals(12, projectRegistry.getProject("/bar").getProblems().get(0).code);
}


@Test
public void testNormalProject() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public String getWorkspaceId() {

@Override
protected void addProject(ProjectConfig project) throws ServerException {
projects.put(project.getPath(), project);

}

Expand Down

0 comments on commit c65e1ad

Please sign in to comment.