Skip to content

Commit f06a691

Browse files
authored
Fix/import export delete problem on windows (#29)
* Update PackageServiceTest.java * Fixing delete problems for import and export * Fix file lock on windows to remove not closed stream from jena loadturtle method * Remove unecessary delete method
1 parent 1e0cff8 commit f06a691

File tree

5 files changed

+54
-43
lines changed

5 files changed

+54
-43
lines changed

src/main/java/org/eclipse/esmf/ame/repository/strategy/utils/LocalFolderResolverUtils.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@
1313

1414
package org.eclipse.esmf.ame.repository.strategy.utils;
1515

16-
import java.io.File;
17-
import java.io.IOException;
18-
import java.nio.channels.FileChannel;
19-
import java.nio.file.StandardOpenOption;
20-
import java.util.Objects;
21-
22-
import javax.annotation.Nonnull;
23-
2416
import org.apache.commons.io.FileUtils;
2517
import org.eclipse.esmf.ame.exceptions.FileNotFoundException;
2618
import org.eclipse.esmf.ame.exceptions.InvalidAspectModelException;
@@ -29,6 +21,11 @@
2921
import org.slf4j.Logger;
3022
import org.slf4j.LoggerFactory;
3123

24+
import javax.annotation.Nonnull;
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.util.Objects;
28+
3229
public class LocalFolderResolverUtils {
3330

3431
private static final Logger LOG = LoggerFactory.getLogger( LocalFolderResolverUtils.class );
@@ -112,14 +109,9 @@ private static void handleFiles( final File storagePath ) throws IOException {
112109
for ( final File file : Objects.requireNonNull( storagePath.listFiles() ) ) {
113110
if ( file.isDirectory() ) {
114111
handleFiles( file );
115-
FileUtils.forceDelete( file );
116-
} else {
117-
file.createNewFile();
118-
final FileChannel channel = FileChannel.open( file.toPath(), StandardOpenOption.WRITE );
119-
channel.lock().release();
120-
channel.close();
121-
FileUtils.forceDelete( file );
122112
}
113+
114+
FileUtils.forceDelete( file );
123115
}
124116
}
125117
}

src/main/java/org/eclipse/esmf/ame/resolver/inmemory/InMemoryStrategy.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,10 @@
1313

1414
package org.eclipse.esmf.ame.resolver.inmemory;
1515

16-
import static org.apache.jena.http.auth.AuthEnv.LOG;
17-
18-
import java.io.ByteArrayInputStream;
19-
import java.io.File;
20-
import java.io.FileNotFoundException;
21-
import java.nio.charset.StandardCharsets;
22-
import java.nio.file.Path;
23-
import java.util.ArrayList;
24-
import java.util.Arrays;
25-
import java.util.List;
26-
import java.util.Optional;
27-
28-
import org.apache.jena.rdf.model.Model;
29-
import org.apache.jena.rdf.model.RDFNode;
30-
import org.apache.jena.rdf.model.Resource;
31-
import org.apache.jena.rdf.model.ResourceFactory;
32-
import org.apache.jena.rdf.model.StmtIterator;
16+
import io.vavr.NotImplementedError;
17+
import io.vavr.control.Try;
18+
import org.apache.commons.io.IOUtils;
19+
import org.apache.jena.rdf.model.*;
3320
import org.apache.jena.riot.RiotException;
3421
import org.apache.jena.vocabulary.RDF;
3522
import org.eclipse.esmf.ame.exceptions.UrnNotFoundException;
@@ -43,8 +30,15 @@
4330
import org.eclipse.esmf.aspectmodel.vocabulary.SAMME;
4431
import org.eclipse.esmf.samm.KnownVersion;
4532

46-
import io.vavr.NotImplementedError;
47-
import io.vavr.control.Try;
33+
import java.io.*;
34+
import java.nio.charset.StandardCharsets;
35+
import java.nio.file.Path;
36+
import java.util.ArrayList;
37+
import java.util.Arrays;
38+
import java.util.List;
39+
import java.util.Optional;
40+
41+
import static org.apache.jena.http.auth.AuthEnv.LOG;
4842

4943
public class InMemoryStrategy extends AbstractResolutionStrategy {
5044
public final Path processingRootPath;
@@ -62,8 +56,11 @@ private Model loadTurtle( final String aspectModel ) {
6256
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
6357
aspectModel.getBytes( StandardCharsets.UTF_8 ) );
6458

65-
return TurtleLoader.loadTurtle( byteArrayInputStream ).getOrElseThrow(
66-
error -> new RiotException( error.getCause().getMessage(), error.getCause() ) );
59+
Model model = TurtleLoader.loadTurtle( byteArrayInputStream ).getOrElseThrow(
60+
error -> new RiotException( error.getCause().getMessage(), error.getCause() ) );
61+
62+
IOUtils.closeQuietly( byteArrayInputStream );
63+
return model;
6764
}
6865

6966
@Override
@@ -109,7 +106,7 @@ protected Try<Model> getModelFromFileSystem( final AspectModelUrn aspectModelUrn
109106

110107
final File namedResourceFile = directory.resolve( aspectModelUrn.getName() + ".ttl" ).toFile();
111108
if ( namedResourceFile.exists() ) {
112-
return loadFromUri( namedResourceFile.toURI() );
109+
return loadTurtle( new File( namedResourceFile.toURI() ) );
113110
}
114111

115112
LOG.warn( "Looking for {}, but no {}.ttl was found. Inspecting files in {}", aspectModelUrn.getName(),
@@ -123,6 +120,16 @@ protected Try<Model> getModelFromFileSystem( final AspectModelUrn aspectModelUrn
123120
"No model file containing " + aspectModelUrn + " could be found in directory: " + directory ) ) );
124121
}
125122

123+
private Try<Model> loadTurtle( final File aspectModel ) {
124+
try ( final InputStream inputStream = new FileInputStream( aspectModel ) ) {
125+
final Try<Model> model = TurtleLoader.loadTurtle( inputStream );
126+
IOUtils.closeQuietly( inputStream );
127+
return model;
128+
} catch ( final IOException exception ) {
129+
return Try.failure( exception );
130+
}
131+
}
132+
126133
public AspectModelUrn getAspectModelUrn() {
127134
return AspectModelUrn.fromUrn(
128135
getEsmfStatements().orElseThrow( () -> new NotImplementedError( "AspectModelUrn cannot be found." ) ).next()

src/main/java/org/eclipse/esmf/ame/services/utils/UnzipUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ private UnzipUtils() {
4747
*/
4848
@SuppressWarnings( { "squid:S135", "squid:S5042" } )
4949
public static void unzipPackageFile( final InputStream zipFile, final Path packagePath ) {
50-
try ( final ZipInputStream zipInputStream = new ZipInputStream( zipFile ) ) {
50+
ZipInputStream zipInputStream = null;
5151

52+
try {
53+
zipInputStream = new ZipInputStream( zipFile );
5254
ZipEntry zipEntry = zipInputStream.getNextEntry();
5355

5456
while ( zipEntry != null ) {
@@ -76,12 +78,15 @@ public static void unzipPackageFile( final InputStream zipFile, final Path packa
7678

7779
zipEntry = zipInputStream.getNextEntry();
7880
}
81+
7982
zipInputStream.closeEntry();
8083

81-
IOUtils.closeQuietly( zipInputStream );
8284
} catch ( final IOException e ) {
8385
LOG.error( "Cannot read file." );
8486
throw new FileReadException( "Error reading the zip file.", e );
87+
} finally {
88+
IOUtils.closeQuietly( zipFile );
89+
IOUtils.closeQuietly( zipInputStream );
8590
}
8691
}
8792

src/main/java/org/eclipse/esmf/ame/services/utils/ZipUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ private ZipUtils() {
4545
public static byte[] createZipFile( final String zipFileName, final String sourceStoragePath ) throws IOException {
4646
final String zipFile = sourceStoragePath + File.separator + zipFileName;
4747

48-
try ( final FileOutputStream fos = new FileOutputStream( zipFile );
49-
final ZipOutputStream zos = new ZipOutputStream( fos ) ) {
48+
FileOutputStream fos = null;
49+
ZipOutputStream zos = null;
50+
51+
try {
52+
fos = new FileOutputStream(zipFile);
53+
zos = new ZipOutputStream(fos);
5054

5155
final List<File> fileList = getFileList( new File( sourceStoragePath ), new ArrayList<>(), sourceStoragePath );
5256

@@ -73,6 +77,9 @@ public static byte[] createZipFile( final String zipFileName, final String sourc
7377
} catch ( final IOException e ) {
7478
LOG.error( "Cannot create zip file." );
7579
throw new CreateFileException( "Error creating the zip file.", e );
80+
}finally {
81+
IOUtils.closeQuietly( fos );
82+
IOUtils.closeQuietly( zos );
7683
}
7784

7885
return Files.readAllBytes( Paths.get( zipFile ) );

src/test/java/org/eclipse/esmf/ame/services/PackageServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PackageServiceTest {
6060

6161
@Test
6262
void testValidateImportAspectModelPackage() throws IOException {
63-
final Path storagePath = Paths.get( resourcesPath.toString(), "test-packages" );
63+
final Path storagePath = Paths.get( resourcesPath.toString(), "test-packages-import" );
6464
final Path zipFilePath = Paths.get( resourcesPath.toString(), "TestArchive.zip" );
6565

6666
final MockMultipartFile mockedZipFile = new MockMultipartFile( "TestArchive.zip",
@@ -78,7 +78,7 @@ void testValidateImportAspectModelPackage() throws IOException {
7878

7979
@Test
8080
void testValidateAspectModels() {
81-
final Path exportedStoragePath = Paths.get( resourcesPath.toString(), "test-packages" );
81+
final Path exportedStoragePath = Paths.get( resourcesPath.toString(), "test-packages-export" );
8282
final List<String> aspectModelFiles = List.of( nameSpaceOne, nameSpaceTwo );
8383

8484
final ValidationProcess validationProcess = Mockito.mock( ValidationProcess.class );

0 commit comments

Comments
 (0)