Fixed possible places of failure due to not closing InputStreams(#9780)
Signed-off-by: Dmytro Kulieshov <dkuliesh@redhat.com>6.19.x
parent
ff0ca6ec56
commit
119a61b7bb
|
|
@ -13,6 +13,7 @@ import static org.eclipse.che.plugin.composer.shared.Constants.PACKAGE;
|
|||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
|
@ -20,7 +21,6 @@ import java.nio.file.Paths;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import org.eclipse.che.api.core.ServerException;
|
||||
import org.eclipse.che.api.fs.server.PathTransformer;
|
||||
import org.eclipse.che.api.project.server.type.ReadonlyValueProvider;
|
||||
import org.eclipse.che.api.project.server.type.ValueProvider;
|
||||
|
|
@ -65,13 +65,15 @@ public class ComposerValueProviderFactory implements ValueProviderFactory {
|
|||
}
|
||||
|
||||
return Collections.singletonList(value);
|
||||
} catch (ServerException | IOException e) {
|
||||
} catch (IOException e) {
|
||||
throw new ValueStorageException("Can't read composer.json : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private JsonObject readModel(Path projectFsPath) throws ServerException, IOException {
|
||||
return new Gson().fromJson(Files.newBufferedReader(projectFsPath), JsonObject.class);
|
||||
private JsonObject readModel(Path projectFsPath) throws IOException {
|
||||
try (BufferedReader reader = Files.newBufferedReader(projectFsPath)) {
|
||||
return new Gson().fromJson(reader, JsonObject.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ package org.eclipse.che.plugin.cpp.generator;
|
|||
|
||||
import static org.eclipse.che.api.fs.server.WsPathUtils.resolve;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -41,10 +42,13 @@ public class CProjectGenerator implements CreateProjectHandler {
|
|||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
|
||||
fsManager.createDir(projectWsPath);
|
||||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME);
|
||||
String wsPath = resolve(projectWsPath, FILE_NAME);
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) {
|
||||
fsManager.createDir(projectWsPath);
|
||||
String wsPath = resolve(projectWsPath, FILE_NAME);
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ package org.eclipse.che.plugin.cpp.generator;
|
|||
|
||||
import static org.eclipse.che.api.fs.server.WsPathUtils.resolve;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -40,10 +41,13 @@ public class CppProjectGenerator implements CreateProjectHandler {
|
|||
public void onCreateProject(
|
||||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
fsManager.createDir(projectWsPath);
|
||||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME);
|
||||
String wsPath = resolve(projectWsPath, FILE_NAME);
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) {
|
||||
fsManager.createDir(projectWsPath);
|
||||
String wsPath = resolve(projectWsPath, FILE_NAME);
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -49,10 +49,13 @@ public class CreateNetCoreProjectHandler implements CreateProjectHandler {
|
|||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
|
||||
fsManager.createDir(projectWsPath);
|
||||
InputStream inputStream = new ByteArrayInputStream(getProjectContent());
|
||||
String wsPath = resolve(projectWsPath, PROJECT_FILE_NAME);
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
try (InputStream inputStream = new ByteArrayInputStream(getProjectContent()); ) {
|
||||
fsManager.createDir(projectWsPath);
|
||||
String wsPath = resolve(projectWsPath, PROJECT_FILE_NAME);
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] getProjectContent() {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ import static org.eclipse.che.plugin.java.plain.shared.PlainJavaProjectConstants
|
|||
import static org.eclipse.che.plugin.java.plain.shared.PlainJavaProjectConstants.DEFAULT_SOURCE_FOLDER_VALUE;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.eclipse.che.api.core.ConflictException;
|
||||
|
|
@ -57,31 +59,34 @@ public class PlainJavaProjectGenerator implements CreateProjectHandler {
|
|||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
|
||||
List<String> sourceFolders;
|
||||
if (attributes.containsKey(SOURCE_FOLDER) && !attributes.get(SOURCE_FOLDER).isEmpty()) {
|
||||
sourceFolders = attributes.get(SOURCE_FOLDER).getList();
|
||||
} else {
|
||||
sourceFolders = singletonList(DEFAULT_SOURCE_FOLDER_VALUE);
|
||||
try (InputStream inputStream =
|
||||
getClass().getClassLoader().getResourceAsStream("files/main_class_content")) {
|
||||
List<String> sourceFolders;
|
||||
if (attributes.containsKey(SOURCE_FOLDER) && !attributes.get(SOURCE_FOLDER).isEmpty()) {
|
||||
sourceFolders = attributes.get(SOURCE_FOLDER).getList();
|
||||
} else {
|
||||
sourceFolders = singletonList(DEFAULT_SOURCE_FOLDER_VALUE);
|
||||
}
|
||||
|
||||
fsManager.createDir(projectWsPath);
|
||||
|
||||
String outputDirWsPath = resolve(projectWsPath, DEFAULT_OUTPUT_FOLDER_VALUE);
|
||||
fsManager.createDir(outputDirWsPath);
|
||||
|
||||
String sourceDirWsPath = resolve(projectWsPath, sourceFolders.get(0));
|
||||
fsManager.createDir(sourceDirWsPath);
|
||||
|
||||
String mainJavaWsPath = resolve(sourceDirWsPath, FILE_NAME);
|
||||
fsManager.createFile(mainJavaWsPath, inputStream);
|
||||
|
||||
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectWsPath);
|
||||
IJavaProject javaProject = JavaCore.create(project);
|
||||
|
||||
classpathBuilder.generateClasspath(
|
||||
javaProject, sourceFolders, singletonList(DEFAULT_LIBRARY_FOLDER_VALUE));
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
|
||||
fsManager.createDir(projectWsPath);
|
||||
|
||||
String outputDirWsPath = resolve(projectWsPath, DEFAULT_OUTPUT_FOLDER_VALUE);
|
||||
fsManager.createDir(outputDirWsPath);
|
||||
|
||||
String sourceDirWsPath = resolve(projectWsPath, sourceFolders.get(0));
|
||||
fsManager.createDir(sourceDirWsPath);
|
||||
|
||||
String mainJavaWsPath = resolve(sourceDirWsPath, FILE_NAME);
|
||||
fsManager.createFile(
|
||||
mainJavaWsPath,
|
||||
getClass().getClassLoader().getResourceAsStream("files/main_class_content"));
|
||||
|
||||
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectWsPath);
|
||||
IJavaProject javaProject = JavaCore.create(project);
|
||||
|
||||
classpathBuilder.generateClasspath(
|
||||
javaProject, sourceFolders, singletonList(DEFAULT_LIBRARY_FOLDER_VALUE));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ package org.eclipse.che.plugin.nodejs.generator;
|
|||
import static org.eclipse.che.api.fs.server.WsPathUtils.resolve;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import org.eclipse.che.api.core.ConflictException;
|
||||
|
|
@ -43,11 +44,14 @@ public class NodeJsProjectGenerator implements CreateProjectHandler {
|
|||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
|
||||
fsManager.createDir(projectWsPath);
|
||||
InputStream inputStream =
|
||||
getClass().getClassLoader().getResourceAsStream("files/default_node_content");
|
||||
String wsPath = resolve(projectWsPath, "hello.js");
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
try (InputStream inputStream =
|
||||
getClass().getClassLoader().getResourceAsStream("files/default_node_content")) {
|
||||
fsManager.createDir(projectWsPath);
|
||||
String wsPath = resolve(projectWsPath, "hello.js");
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ package org.eclipse.che.plugin.php.projecttype;
|
|||
import static org.eclipse.che.api.fs.server.WsPathUtils.resolve;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import org.eclipse.che.api.core.ConflictException;
|
||||
|
|
@ -37,11 +38,14 @@ public class PhpProjectGenerator implements CreateProjectHandler {
|
|||
public void onCreateProject(
|
||||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
fsManager.createDir(projectWsPath);
|
||||
InputStream inputStream =
|
||||
getClass().getClassLoader().getResourceAsStream("files/default_php_content");
|
||||
String wsPath = resolve(projectWsPath, "hello.php");
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
try (InputStream inputStream =
|
||||
getClass().getClassLoader().getResourceAsStream("files/default_php_content")) {
|
||||
fsManager.createDir(projectWsPath);
|
||||
String wsPath = resolve(projectWsPath, "hello.php");
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ package org.eclipse.che.plugin.python.generator;
|
|||
|
||||
import static org.eclipse.che.api.fs.server.WsPathUtils.resolve;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -38,11 +39,14 @@ public class PythonProjectGenerator implements CreateProjectHandler {
|
|||
public void onCreateProject(
|
||||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
fsManager.createDir(projectWsPath);
|
||||
InputStream inputStream =
|
||||
getClass().getClassLoader().getResourceAsStream("files/default_python_content");
|
||||
String wsPath = resolve(projectWsPath, "main.py");
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
try (InputStream inputStream =
|
||||
getClass().getClassLoader().getResourceAsStream("files/default_python_content")) {
|
||||
fsManager.createDir(projectWsPath);
|
||||
String wsPath = resolve(projectWsPath, "main.py");
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -55,9 +55,13 @@ public class CreateBaseProjectTypeHandler implements CreateProjectHandler {
|
|||
public void onCreateProject(
|
||||
String projectWsPath, Map<String, AttributeValue> attributes, Map<String, String> options)
|
||||
throws ForbiddenException, ConflictException, ServerException, NotFoundException {
|
||||
fsManager.createDir(projectWsPath, true, true);
|
||||
String wsPath = resolve(projectWsPath, README_FILE_NAME);
|
||||
fsManager.createFile(wsPath, getReadmeContent());
|
||||
try (InputStream inputStream = getReadmeContent()) {
|
||||
fsManager.createDir(projectWsPath, true, true);
|
||||
String wsPath = resolve(projectWsPath, README_FILE_NAME);
|
||||
fsManager.createFile(wsPath, inputStream);
|
||||
} catch (IOException e) {
|
||||
throw new ServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in New Issue