You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2020/10/31 13:36:58 UTC
[jackrabbit-filevault] branch master updated: JCRVLT-478 fix Sonar
issues (#101)
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git
The following commit(s) were added to refs/heads/master by this push:
new e58171b JCRVLT-478 fix Sonar issues (#101)
e58171b is described below
commit e58171bde5560c27a5f41e1f6fc98839198029fc
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sat Oct 31 14:36:47 2020 +0100
JCRVLT-478 fix Sonar issues (#101)
WIP
---
.../org/apache/jackrabbit/vault/cli/CmdExport.java | 44 ++++----
.../apache/jackrabbit/vault/cli/CmdExportCli.java | 60 ++++++-----
.../apache/jackrabbit/vault/cli/CmdImportCli.java | 25 ++---
.../vault/util/console/AbstractApplication.java | 6 +-
.../jackrabbit/vault/fs/SerializerArtifact.java | 67 ++++++------
.../vault/packaging/impl/JcrPackageImpl.java | 113 ++++++++++-----------
.../packaging/impl/JcrPackageManagerImpl.java | 32 ++++--
.../packaging/registry/impl/FSPackageRegistry.java | 7 +-
.../registry/impl/JcrPackageRegistry.java | 5 +
.../apache/jackrabbit/vault/sync/impl/SyncLog.java | 6 +-
.../jackrabbit/vault/sync/impl/TreeSync.java | 7 +-
.../org/apache/jackrabbit/vault/vlt/VltFile.java | 3 +-
.../vault/vlt/meta/xml/zip/UpdateableZipFile.java | 78 +++++++-------
13 files changed, 241 insertions(+), 212 deletions(-)
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExport.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExport.java
index d5c18d1..b4d1a3f 100644
--- a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExport.java
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExport.java
@@ -55,27 +55,33 @@ public class CmdExport extends AbstractJcrFsCommand {
}
File localFile = ctx.getVaultFsApp().getPlatformFile(localPath, false);
- AbstractExporter exporter;
- if (type.equals("platform")) {
- if (!localFile.exists()) {
- localFile.mkdirs();
+ AbstractExporter exporter = null;
+ try {
+ if (type.equals("platform")) {
+ if (!localFile.exists()) {
+ localFile.mkdirs();
+ }
+ exporter = new PlatformExporter(localFile);
+ ((PlatformExporter) exporter).setPruneMissing(cl.hasOption(optPrune));
+ } else if (type.equals("jar")) {
+ exporter = new JarExporter(localFile);
+ } else {
+ throw new Exception("Type " + type + " not supported");
+ }
+ if (jcrPath == null || !jcrPath.startsWith("/")) {
+ exporter.setRelativePaths(true);
+ }
+ VaultFsApp.log.info("Exporting {} to {}", vaultFile.getPath(), localFile.getCanonicalPath());
+ if (verbose) {
+ exporter.setVerbose(new DefaultProgressListener());
+ }
+ exporter.export(vaultFile);
+ VaultFsApp.log.info("Exporting done.");
+ } finally {
+ if (exporter != null) {
+ exporter.close();
}
- exporter = new PlatformExporter(localFile);
- ((PlatformExporter) exporter).setPruneMissing(cl.hasOption(optPrune));
- } else if (type.equals("jar")) {
- exporter = new JarExporter(localFile);
- } else {
- throw new Exception("Type " + type + " not supported");
- }
- if (jcrPath == null || !jcrPath.startsWith("/")) {
- exporter.setRelativePaths(true);
- }
- VaultFsApp.log.info("Exporting {} to {}", vaultFile.getPath(), localFile.getCanonicalPath());
- if (verbose) {
- exporter.setVerbose(new DefaultProgressListener());
}
- exporter.export(vaultFile);
- VaultFsApp.log.info("Exporting done.");
}
/**
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExportCli.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExportCli.java
index 21ee6b1..e7354ee 100644
--- a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExportCli.java
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdExportCli.java
@@ -83,35 +83,41 @@ public class CmdExportCli extends AbstractVaultCommand {
}
File localFile = app.getPlatformFile(localPath, false);
- AbstractExporter exporter;
- VltContext vCtx;
- if (type.equals("platform")) {
- if (!localFile.exists()) {
- localFile.mkdirs();
+ AbstractExporter exporter = null;
+ try {
+ VltContext vCtx;
+ if (type.equals("platform")) {
+ if (!localFile.exists()) {
+ localFile.mkdirs();
+ }
+ exporter = new PlatformExporter(localFile);
+ ((PlatformExporter) exporter).setPruneMissing(cl.hasOption(optPrune));
+ vCtx = app.createVaultContext(localFile);
+ } else if (type.equals("jar")) {
+ exporter = new JarExporter(localFile);
+ vCtx = app.createVaultContext(localFile.getParentFile());
+ } else {
+ throw new Exception("Type " + type + " not supported");
+ }
+
+ vCtx.setVerbose(cl.hasOption(OPT_VERBOSE));
+ VaultFile vaultFile = vCtx.getFileSystem(addr).getFile(jcrPath);
+ if (vaultFile == null) {
+ VaultFsApp.log.error("Not such remote file: {}", jcrPath);
+ return;
+ }
+
+ VaultFsApp.log.info("Exporting {} to {}", vaultFile.getPath(), localFile.getCanonicalPath());
+ if (verbose) {
+ exporter.setVerbose(new DefaultProgressListener());
+ }
+ exporter.export(vaultFile);
+ VaultFsApp.log.info("Exporting done.");
+ } finally {
+ if (exporter != null) {
+ exporter.close();
}
- exporter = new PlatformExporter(localFile);
- ((PlatformExporter) exporter).setPruneMissing(cl.hasOption(optPrune));
- vCtx = app.createVaultContext(localFile);
- } else if (type.equals("jar")) {
- exporter = new JarExporter(localFile);
- vCtx = app.createVaultContext(localFile.getParentFile());
- } else {
- throw new Exception("Type " + type + " not supported");
- }
-
- vCtx.setVerbose(cl.hasOption(OPT_VERBOSE));
- VaultFile vaultFile = vCtx.getFileSystem(addr).getFile(jcrPath);
- if (vaultFile == null) {
- VaultFsApp.log.error("Not such remote file: {}", jcrPath);
- return;
- }
-
- VaultFsApp.log.info("Exporting {} to {}", vaultFile.getPath(), localFile.getCanonicalPath());
- if (verbose) {
- exporter.setVerbose(new DefaultProgressListener());
}
- exporter.export(vaultFile);
- VaultFsApp.log.info("Exporting done.");
}
/**
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdImportCli.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdImportCli.java
index 8b93a36..2d4b07a 100644
--- a/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdImportCli.java
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdImportCli.java
@@ -32,7 +32,6 @@ import org.apache.commons.cli2.builder.DefaultOptionBuilder;
import org.apache.commons.cli2.builder.GroupBuilder;
import org.apache.commons.cli2.option.Command;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.vault.fs.api.RepositoryAddress;
import org.apache.jackrabbit.vault.fs.api.VaultFile;
import org.apache.jackrabbit.vault.fs.io.Archive;
@@ -95,17 +94,17 @@ public class CmdImportCli extends AbstractVaultCommand {
VaultFile vaultFile = vCtx.getFileSystem(addr).getFile(jcrPath);
VaultFsApp.log.info("Importing {} to {}", localFile.getCanonicalPath(), vaultFile.getPath());
- Archive archive;
- if (localFile.isFile()) {
- archive = new ZipArchive(localFile);
- } else {
- if (cl.hasOption(optSync)) {
- VaultFsApp.log.warn("--sync is not supported yet");
- }
- archive = new FileArchive(localFile);
- }
- archive.open(false);
+ Archive archive = null;
try {
+ if (localFile.isFile()) {
+ archive = new ZipArchive(localFile);
+ } else {
+ if (cl.hasOption(optSync)) {
+ VaultFsApp.log.warn("--sync is not supported yet");
+ }
+ archive = new FileArchive(localFile);
+ }
+ archive.open(false);
Importer importer = new Importer();
if (verbose) {
importer.getOptions().setListener(new DefaultProgressListener());
@@ -113,7 +112,9 @@ public class CmdImportCli extends AbstractVaultCommand {
Session s = vaultFile.getFileSystem().getAggregateManager().getSession();
importer.run(archive, s, vaultFile.getPath());
} finally {
- archive.close();
+ if (archive != null) {
+ archive.close();
+ }
}
VaultFsApp.log.info("Importing done.");
}
diff --git a/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/AbstractApplication.java b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/AbstractApplication.java
index 894e4e1..c13452b 100644
--- a/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/AbstractApplication.java
+++ b/vault-cli/src/main/java/org/apache/jackrabbit/vault/util/console/AbstractApplication.java
@@ -318,9 +318,9 @@ public abstract class AbstractApplication {
}
props.store(out, "Console Configuration");
*/
- FileOutputStream out = new FileOutputStream(file);
- globalEnv.store(out, "Console Configuration");
- out.close();
+ try (FileOutputStream out = new FileOutputStream(file)) {
+ globalEnv.store(out, "Console Configuration");
+ }
log.info("Configuration saved to {}", file.getCanonicalPath());
}
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/SerializerArtifact.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/SerializerArtifact.java
index c57606e..e780bc2 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/SerializerArtifact.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/SerializerArtifact.java
@@ -18,6 +18,7 @@
package org.apache.jackrabbit.vault.fs;
import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -111,40 +112,44 @@ public class SerializerArtifact extends AbstractArtifact implements ExportArtifa
* {@inheritDoc}
*/
public VaultInputSource getInputSource() throws IOException, RepositoryException {
- final InputStream in;
- final long size;
try (DeferredFileOutputStream out = new DeferredFileOutputStream(8192, "vlttmp", ".tmp", null)) {
spool(out);
-
- if (out.isInMemory()) {
- in = new ByteArrayInputStream(out.getData());
- size = out.getData().length;
- } else {
- in = new TempFileInputStream(out.getFile());
- size = out.getFile().length();
- }
- }
- return new VaultInputSource() {
- @Override
- public String getSystemId() {
- return SerializerArtifact.this.getRelativePath();
- }
-
- @Override
- public InputStream getByteStream() {
- return in;
- }
-
-
- public long getContentLength() {
- return size;
- }
-
- public long getLastModified() {
- return lastModified;
- }
- };
+ return new VaultInputSource() {
+ private InputStream in = null;
+ private long size = -1;
+ @Override
+ public String getSystemId() {
+ return SerializerArtifact.this.getRelativePath();
+ }
+
+ @Override
+ public InputStream getByteStream() {
+ if (in == null) {
+ if (out.isInMemory()) {
+ in = new ByteArrayInputStream(out.getData());
+ size = out.getData().length;
+ } else {
+ try {
+ in = new TempFileInputStream(out.getFile());
+ } catch (FileNotFoundException e) {
+ throw new IllegalStateException("Could not find temp file", e);
+ }
+ size = out.getFile().length();
+ }
+ }
+ return in;
+ }
+
+ public long getContentLength() {
+ return size;
+ }
+
+ public long getLastModified() {
+ return lastModified;
+ }
+ };
+ }
}
/**
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
index f98db07..96006fb 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
@@ -305,9 +305,10 @@ public class JcrPackageImpl implements JcrPackage {
}
if (!forceFileArchive && size >= 0 && size < MAX_MEMORY_ARCHIVE_SIZE) {
MemoryArchive archive = new MemoryArchive(false);
- try (InputStream in = getData().getStream()) {
+ try (InputStream in = getData().getBinary().getStream()) {
archive.run(in);
} catch (Exception e) {
+ archive.close();
throw new IOException("Error while reading stream", e);
}
// workaround for shallow packages that don't have a meta-inf anymore (JCRVLT-188)
@@ -321,15 +322,11 @@ public class JcrPackageImpl implements JcrPackage {
pack = new ZipVaultPackage(archive, true);
} else {
File tmpFile = File.createTempFile("vaultpack", ".zip");
- FileOutputStream out = FileUtils.openOutputStream(tmpFile);
Binary bin = getData().getBinary();
- InputStream in = null;
- try {
- in = bin.getStream();
+ try (FileOutputStream out = FileUtils.openOutputStream(tmpFile);
+ InputStream in = bin.getStream()) {
IOUtils.copy(in, out);
} finally {
- IOUtils.closeQuietly(in);
- IOUtils.closeQuietly(out);
bin.dispose();
}
pack = new ZipVaultPackage(tmpFile, true);
@@ -619,53 +616,12 @@ public class JcrPackageImpl implements JcrPackage {
// process the discovered sub-packages
for (Archive.Entry e: entries) {
VaultInputSource in = a.getInputSource(e);
- InputStream ins = null;
- try {
- ins = in.getByteStream();
- JcrPackageImpl subPackage;
+ try (InputStream ins = in.getByteStream()) {
try {
- PackageId subPid = mgr.register(ins, true);
- JcrRegisteredPackage subPkg = (JcrRegisteredPackage) mgr.open(subPid);
- if (subPkg == null) {
- log.error("Package {}: Newly extracted subpackage is gone: {}", pId, subPid);
- continue;
- } else {
- subPackage = (JcrPackageImpl) subPkg.getJcrPackage();
- }
+ PackageId id = extractSubpackage(pId, ins, opts, processed, hasOwnContent);
+ log.debug("Package {}: Extracted sub-package: {}", pId, id);
} catch (IOException e1) {
- log.error("Package {}: Error while extracting subpackage {}: {}", pId, in.getSystemId());
- continue;
- }
-
- if (hasOwnContent) {
- // add dependency to this package
- Dependency[] oldDeps = subPackage.getDefinition().getDependencies();
- Dependency[] newDeps = DependencyUtil.addExact(oldDeps, pId);
- if (oldDeps != newDeps) {
- subPackage.getDefinition().setDependencies(newDeps, true);
- }
- } else {
- // add parent dependencies to this package
- Dependency[] oldDeps = subPackage.getDefinition().getDependencies();
- Dependency[] newDeps = oldDeps;
- for (Dependency d: getDefinition().getDependencies()) {
- newDeps = DependencyUtil.add(newDeps, d);
- }
- if (oldDeps != newDeps) {
- subPackage.getDefinition().setDependencies(newDeps, true);
- }
- }
-
- PackageId id = subPackage.getDefinition().getId();
- processed.add(id);
- log.debug("Package {}: Extracted sub-package: {}", pId, id);
-
- if (!opts.isNonRecursive()) {
- subPackage.extractSubpackages(opts, processed);
- }
- } finally {
- if (ins != null) {
- ins.close();
+ log.error("Package {}: Error while extracting subpackage {}: {}", pId, in.getSystemId(), e1);
}
}
}
@@ -680,6 +636,46 @@ public class JcrPackageImpl implements JcrPackage {
}
}
+ private PackageId extractSubpackage(@NotNull PackageId containerPackageId, @NotNull InputStream input, @NotNull ImportOptions opts, @NotNull Set<PackageId> processed, boolean hasOwnContent) throws RepositoryException, IOException, PackageException {
+ JcrPackageImpl subPackage;
+
+ PackageId subPid = mgr.register(input, true);
+ try (JcrRegisteredPackage subPkg = (JcrRegisteredPackage) mgr.open(subPid)) {
+ if (subPkg == null) {
+ log.error("Package {}: Newly extracted subpackage is gone: {}", containerPackageId, subPid);
+ return null;
+ } else {
+ subPackage = (JcrPackageImpl) subPkg.getJcrPackage();
+ }
+
+ if (hasOwnContent) {
+ // add dependency to this package
+ Dependency[] oldDeps = subPackage.getDefinition().getDependencies();
+ Dependency[] newDeps = DependencyUtil.addExact(oldDeps, containerPackageId);
+ if (oldDeps != newDeps) {
+ subPackage.getDefinition().setDependencies(newDeps, true);
+ }
+ } else {
+ // add parent dependencies to this package
+ Dependency[] oldDeps = subPackage.getDefinition().getDependencies();
+ Dependency[] newDeps = oldDeps;
+ for (Dependency d: getDefinition().getDependencies()) {
+ newDeps = DependencyUtil.add(newDeps, d);
+ }
+ if (oldDeps != newDeps) {
+ subPackage.getDefinition().setDependencies(newDeps, true);
+ }
+ }
+
+ PackageId id = subPackage.getDefinition().getId();
+ processed.add(id);
+ if (!opts.isNonRecursive()) {
+ subPackage.extractSubpackages(opts, processed);
+ }
+ return id;
+ }
+ }
+
private void findSubPackageEntries(@NotNull List<Archive.Entry> entries, @NotNull Archive.Entry folder) {
for (Archive.Entry e: folder.getChildren()) {
final String name = e.getName();
@@ -1004,12 +1000,13 @@ public class JcrPackageImpl implements JcrPackage {
if (!opts.isNonRecursive() && subPackages.size() > 0) {
JcrPackageManagerImpl packMgr = new JcrPackageManagerImpl(mgr);
for (PackageId id : subPackages) {
- JcrPackage pack = packMgr.open(id);
- if (pack != null) {
- if (pack.getSnapshot() == null) {
- log.warn("Unable to uninstall sub package {}. Snapshot missing.", id);
- } else {
- pack.uninstall(opts);
+ try (JcrPackage pack = packMgr.open(id)) {
+ if (pack != null) {
+ if (pack.getSnapshot() == null) {
+ log.warn("Unable to uninstall sub package {}. Snapshot missing.", id);
+ } else {
+ pack.uninstall(opts);
+ }
}
}
}
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java
index c1a43ba..6b02ab3 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java
@@ -573,19 +573,29 @@ public class JcrPackageManagerImpl extends PackageManagerImpl implements JcrPack
continue;
}
JcrPackageImpl pack = new JcrPackageImpl(registry, child);
- if (pack.isValid()) {
- // skip packages with illegal names
- JcrPackageDefinition jDef = pack.getDefinition();
- if (jDef != null && !jDef.getId().isValid()) {
- continue;
- }
- if (filter == null || filter.contains(child.getPath())) {
- if (!built || pack.getSize() > 0) {
- packages.add(pack);
+ try {
+ if (pack.isValid()) {
+ // skip packages with illegal names
+ JcrPackageDefinition jDef = pack.getDefinition();
+ if (jDef != null && !jDef.getId().isValid()) {
+ pack.close();
+ continue;
+ }
+ if (filter == null || filter.contains(child.getPath())) {
+ if (!built || pack.getSize() > 0) {
+ packages.add(pack);
+ continue;
+ }
}
+ pack.close();
+ } else if (child.hasNodes() && !shallow){
+ listPackages(child, packages, filter, built, false);
+ } else {
+ pack.close();
}
- } else if (child.hasNodes() && !shallow){
- listPackages(child, packages, filter, built, false);
+ } catch (RepositoryException e) {
+ pack.close();
+ throw e;
}
}
}
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java
index 8125436..6fe1bbb 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/FSPackageRegistry.java
@@ -527,8 +527,7 @@ public class FSPackageRegistry extends AbstractPackageRegistry {
@NotNull
@Override
public PackageId register(@NotNull File file, boolean replace) throws IOException, PackageExistsException {
- ZipVaultPackage pack = new ZipVaultPackage(file, false, true);
- try {
+ try (ZipVaultPackage pack = new ZipVaultPackage(file, false, true)) {
File pkgFile = buildPackageFile(pack.getId());
if (pkgFile.exists()) {
if (replace) {
@@ -550,10 +549,6 @@ public class FSPackageRegistry extends AbstractPackageRegistry {
.withExternal(false);
setInstallState(state);
return pack.getId();
- } finally {
- if (!pack.isClosed()) {
- pack.close();
- }
}
}
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java
index 5a62712..843b20a 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java
@@ -296,6 +296,7 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
&& node.hasProperty(JcrConstants.JCR_CONTENT + "/" + JcrConstants.JCR_DATA)) {
return pack;
} else {
+ pack.close();
return null;
}
}
@@ -365,6 +366,7 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
Exception error = pump.getError();
log.error("Error while reading from input stream.", error);
bin.dispose();
+ archive.close();
throw new IOException("Error while reading from input stream", error);
}
@@ -372,6 +374,7 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
String msg = "Stream is not a content package. Missing 'jcr_root'.";
log.error(msg);
bin.dispose();
+ archive.close();
throw new IOException(msg);
}
@@ -384,6 +387,7 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
}
if (!pid.isValid()) {
bin.dispose();
+ archive.close();
throw new RepositoryException("Unable to create package. Illegal package name.");
}
@@ -409,6 +413,7 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
if (replace) {
parent.getNode(name).remove();
} else {
+ archive.close();
throw new PackageExistsException("Package already exists: " + pid).setId(pid);
}
}
diff --git a/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/SyncLog.java b/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/SyncLog.java
index e31699c..51dda20 100644
--- a/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/SyncLog.java
+++ b/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/SyncLog.java
@@ -51,12 +51,10 @@ public class SyncLog {
line.append(dateFmt.format(new Date()));
line.append(msg);
line.append("\n");
- try {
- FileWriter writer = new FileWriter(logFile, true);
+ try (FileWriter writer = new FileWriter(logFile, true)) {
writer.write(line.toString());
- writer.close();
} catch (IOException e) {
- log.error("Unable to update log file: " + e.toString());
+ log.error("Unable to update log file: {}", e);
}
}
}
\ No newline at end of file
diff --git a/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/TreeSync.java b/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/TreeSync.java
index db49c3a..1d84cf3 100644
--- a/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/TreeSync.java
+++ b/vault-sync/src/main/java/org/apache/jackrabbit/vault/sync/impl/TreeSync.java
@@ -416,9 +416,10 @@ public class TreeSync {
if (preserveFileDate) {
cal.setTimeInMillis(e.file.lastModified());
}
- InputStream in = FileUtils.openInputStream(e.file);
- Binary bin = content.getSession().getValueFactory().createBinary(in);
- content.setProperty(Property.JCR_DATA, bin);
+ try (InputStream in = FileUtils.openInputStream(e.file)) {
+ Binary bin = content.getSession().getValueFactory().createBinary(in);
+ content.setProperty(Property.JCR_DATA, bin);
+ }
content.setProperty(Property.JCR_LAST_MODIFIED, cal);
content.setProperty(Property.JCR_MIMETYPE, MimeTypes.getMimeType(e.file.getName(), MimeTypes.APPLICATION_OCTET_STREAM));
syncLog.log("%s jcr://%s", action, ntFile.getPath());
diff --git a/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/VltFile.java b/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/VltFile.java
index d4589cb..d7c970e 100644
--- a/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/VltFile.java
+++ b/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/VltFile.java
@@ -403,8 +403,7 @@ public class VltFile implements DocumentSource {
if (!force) {
// check if the file still contains the diff markers
boolean mayContainMarker = false;
- try {
- BufferedReader in = new BufferedReader(new FileReader(file));
+ try (BufferedReader in = new BufferedReader(new FileReader(file))) {
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith(Hunk3.MARKER_B[0])
diff --git a/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/meta/xml/zip/UpdateableZipFile.java b/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/meta/xml/zip/UpdateableZipFile.java
index be16342..b705397 100644
--- a/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/meta/xml/zip/UpdateableZipFile.java
+++ b/vault-vlt/src/main/java/org/apache/jackrabbit/vault/vlt/meta/xml/zip/UpdateableZipFile.java
@@ -112,14 +112,21 @@ public class UpdateableZipFile {
if (!file.exists()) {
return null;
}
+ ZipInputStream zin = null;
InputStream in = FileUtils.openInputStream(file);
- ZipInputStream zin = new ZipInputStream(in);
- ZipEntry entry = zin.getNextEntry();
- while (entry != null) {
- if (entry.getName().equals(name)) {
- return zin;
+ try {
+ zin = new ZipInputStream(in);
+ ZipEntry entry = zin.getNextEntry();
+ while (entry != null) {
+ if (entry.getName().equals(name)) {
+ return zin;
+ }
+ entry = zin.getNextEntry();
}
- entry = zin.getNextEntry();
+ } catch (IOException e) {
+ IOUtils.closeQuietly(zin);
+ IOUtils.closeQuietly(in);
+ throw e;
}
IOUtils.closeQuietly(zin);
IOUtils.closeQuietly(in);
@@ -149,40 +156,39 @@ public class UpdateableZipFile {
}
// create tmp file
File newZip = File.createTempFile(file.getName(), ".tmp", file.getParentFile());
- ZipOutputStream out = new ZipOutputStream(
- new BufferedOutputStream(new FileOutputStream(newZip)));
- out.setLevel(Deflater.NO_COMPRESSION);
-
- // add new files on top
- for (Map.Entry<String, InputStream> update: toUpdate.entrySet()) {
- ZipEntry entry = new ZipEntry(update.getKey());
- out.putNextEntry(entry);
- InputStream in = update.getValue();
- copy(in, out);
- IOUtils.closeQuietly(in);
- }
- // process existing zip entries
- if (file.exists()) {
- InputStream in = FileUtils.openInputStream(file);
- ZipInputStream zin = new ZipInputStream(in);
- ZipEntry entry = zin.getNextEntry();
- while (entry != null) {
- if (!toUpdate.containsKey(entry.getName()) && !toDelete.contains(entry.getName())) {
- ZipEntry newEntry = entry;
- if (toMove.containsKey(entry.getName())) {
- newEntry = new ZipEntry(toMove.get(entry.getName()));
- newEntry.setTime(entry.getTime());
- //newEntry.setSize(entry.getSize());
+ try (ZipOutputStream out = new ZipOutputStream(
+ new BufferedOutputStream(new FileOutputStream(newZip)))) {
+ out.setLevel(Deflater.NO_COMPRESSION);
+
+ // add new files on top
+ for (Map.Entry<String, InputStream> update: toUpdate.entrySet()) {
+ ZipEntry entry = new ZipEntry(update.getKey());
+ out.putNextEntry(entry);
+ InputStream in = update.getValue();
+ copy(in, out);
+ IOUtils.closeQuietly(in);
+ }
+ // process existing zip entries
+ if (file.exists()) {
+ try (InputStream in = FileUtils.openInputStream(file);
+ ZipInputStream zin = new ZipInputStream(in)) {
+ ZipEntry entry = zin.getNextEntry();
+ while (entry != null) {
+ if (!toUpdate.containsKey(entry.getName()) && !toDelete.contains(entry.getName())) {
+ ZipEntry newEntry = entry;
+ if (toMove.containsKey(entry.getName())) {
+ newEntry = new ZipEntry(toMove.get(entry.getName()));
+ newEntry.setTime(entry.getTime());
+ //newEntry.setSize(entry.getSize());
+ }
+ out.putNextEntry(newEntry);
+ copy(zin, out);
+ }
+ entry = zin.getNextEntry();
}
- out.putNextEntry(newEntry);
- copy(zin, out);
}
- entry = zin.getNextEntry();
}
- IOUtils.closeQuietly(zin);
- IOUtils.closeQuietly(in);
}
- out.close();
toDelete.clear();
toUpdate.clear();