Sergey Shepelevich pushed to branch master at cms-community / hippo-configuration-management
Commits: 7f67c874 by Sergey Shepelevich at 2017-08-09T10:54:05+02:00 HCM-187 Close opened streams in yaml reader/writer - - - - - 876ff1d2 by Tobias Jeger at 2017-08-09T14:28:51+02:00 HCM-187 Reduce duplicated logic - - - - - 3 changed files: - model/src/main/java/org/onehippo/cm/model/parser/PathConfigurationReader.java - model/src/main/java/org/onehippo/cm/model/serializer/AbstractBaseSerializer.java - model/src/main/java/org/onehippo/cm/model/serializer/AutoExportModuleWriter.java Changes: ===================================== model/src/main/java/org/onehippo/cm/model/parser/PathConfigurationReader.java ===================================== --- a/model/src/main/java/org/onehippo/cm/model/parser/PathConfigurationReader.java +++ b/model/src/main/java/org/onehippo/cm/model/parser/PathConfigurationReader.java @@ -76,14 +76,15 @@ public class PathConfigurationReader { * @throws ParserException */ public ReadResult read(final Path moduleDescriptorPath, final boolean verifyOnly) throws IOException, ParserException { - final InputStream moduleDescriptorInputStream = new BufferedInputStream(Files.newInputStream(moduleDescriptorPath.toRealPath())); - - final ModuleDescriptorParser moduleDescriptorParser = new ModuleDescriptorParser(explicitSequencing); - final ModuleImpl module = moduleDescriptorParser.parse(moduleDescriptorInputStream, moduleDescriptorPath.toAbsolutePath().toString()); - - final ModuleContext moduleContext = new ModuleContext(module, moduleDescriptorPath); - readModule(module, moduleContext, verifyOnly); - return new ReadResult(moduleContext); + try (InputStream inputStream = Files.newInputStream(moduleDescriptorPath.toRealPath()); + final InputStream moduleDescriptorInputStream = new BufferedInputStream(inputStream)) { + + final ModuleDescriptorParser moduleDescriptorParser = new ModuleDescriptorParser(explicitSequencing); + final ModuleImpl module = moduleDescriptorParser.parse(moduleDescriptorInputStream, moduleDescriptorPath.toAbsolutePath().toString()); + final ModuleContext moduleContext = new ModuleContext(module, moduleDescriptorPath); + readModule(module, moduleContext, verifyOnly); + return new ReadResult(moduleContext); + } } /** @@ -108,7 +109,9 @@ public class PathConfigurationReader { final Path actionsDescriptorPath = moduleContext.getActionsDescriptorPath(); if (Files.exists(actionsDescriptorPath)) { final ActionListParser parser = new ActionListParser(); - parser.parse(Files.newInputStream(actionsDescriptorPath), actionsDescriptorPath.toString(), module); + try(InputStream inputStream = Files.newInputStream(actionsDescriptorPath)) { + parser.parse(inputStream, actionsDescriptorPath.toString(), module); + } } } @@ -132,8 +135,9 @@ public class PathConfigurationReader { final List<Pair<Path, String>> contentSourceData = getSourceData(basePath); for (Pair<Path, String> pair : contentSourceData) { try { - sourceParser.parse(new BufferedInputStream(Files.newInputStream(pair.getLeft())), - pair.getRight(), pair.getLeft().toString(), module); + try (final InputStream sourceInputStream = Files.newInputStream(pair.getLeft())) { + sourceParser.parse(new BufferedInputStream(sourceInputStream), pair.getRight(), pair.getLeft().toString(), module); + } } catch (ParserException e) { // add information about the module and source to the parser exception e.setSource("[" + module.getFullName() + ": " + pair.getRight() + "]"); ===================================== model/src/main/java/org/onehippo/cm/model/serializer/AbstractBaseSerializer.java ===================================== --- a/model/src/main/java/org/onehippo/cm/model/serializer/AbstractBaseSerializer.java +++ b/model/src/main/java/org/onehippo/cm/model/serializer/AbstractBaseSerializer.java @@ -43,8 +43,9 @@ public abstract class AbstractBaseSerializer { } public void serializeNode(final OutputStream outputStream, final Node node) throws IOException { - final Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); - serializeNode(writer, node); + try (final Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { + serializeNode(writer, node); + } } public void serializeNode(final Writer writer, final Node node) throws IOException { ===================================== model/src/main/java/org/onehippo/cm/model/serializer/AutoExportModuleWriter.java ===================================== --- a/model/src/main/java/org/onehippo/cm/model/serializer/AutoExportModuleWriter.java +++ b/model/src/main/java/org/onehippo/cm/model/serializer/AutoExportModuleWriter.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Set; import org.onehippo.cm.model.Module; import org.onehippo.cm.model.impl.ModuleImpl; @@ -47,15 +48,16 @@ public class AutoExportModuleWriter extends ModuleWriter { private void removeDeletedResources(final ModuleContext moduleContext) throws IOException { final ModuleImpl module = moduleContext.getModule(); - log.debug("removing config resources: \n\t{}", String.join("\n\t", module.getRemovedConfigResources())); - log.debug("removing content resources: \n\t{}", String.join("\n\t", module.getRemovedContentResources())); - for (final String removed : module.getRemovedConfigResources()) { - final Path removedPath = moduleContext.getConfigOutputProvider().getResourcePath(null, removed); - Files.deleteIfExists(removedPath); - } - for (final String removed : module.getRemovedContentResources()) { - final Path removedPath = moduleContext.getContentOutputProvider().getResourcePath(null, removed); - Files.deleteIfExists(removedPath); + removeResources(module.getRemovedConfigResources(), moduleContext.getConfigOutputProvider()); + removeResources(module.getRemovedContentResources(), moduleContext.getContentOutputProvider()); + } + + private void removeResources(final Set<String> removedResources, final ResourceOutputProvider resourceOutputProvider) + throws IOException { + for (final String removed : removedResources) { + final Path removedPath = resourceOutputProvider.getResourcePath(null, removed); + boolean wasDeleted = Files.deleteIfExists(removedPath); + log.debug("File to be deleted: {}, was deleted: {}", removedPath, wasDeleted); } } View it on GitLab: https://code.onehippo.org/cms-community/hippo-configuration-management/compare/df5959f4858727114332ab1ce037f9da5ece9e26...876ff1d2a26d87b1acf4f1e9825f17f240f42f55 --- View it on GitLab: https://code.onehippo.org/cms-community/hippo-configuration-management/compare/df5959f4858727114332ab1ce037f9da5ece9e26...876ff1d2a26d87b1acf4f1e9825f17f240f42f55 You're receiving this email because of your account on code.onehippo.org.
_______________________________________________ Hippocms-svn mailing list Hippocms-svn@lists.onehippo.org https://lists.onehippo.org/mailman/listinfo/hippocms-svn