Skip to content

Commit 810bbfa

Browse files
committed
Update PR with
1 parent 9767359 commit 810bbfa

File tree

6 files changed

+188
-72
lines changed

6 files changed

+188
-72
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoader.java

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
import static java.util.stream.Collectors.toSet;
1717

18+
import java.io.ByteArrayInputStream;
19+
import java.io.ByteArrayOutputStream;
1820
import java.io.File;
21+
import java.io.FileInputStream;
1922
import java.io.FileNotFoundException;
2023
import java.io.IOException;
2124
import java.io.InputStream;
@@ -24,7 +27,6 @@
2427
import java.util.ArrayList;
2528
import java.util.Collection;
2629
import java.util.Deque;
27-
import java.util.Enumeration;
2830
import java.util.HashSet;
2931
import java.util.List;
3032
import java.util.Map;
@@ -34,7 +36,7 @@
3436
import java.util.function.Supplier;
3537
import java.util.stream.Stream;
3638
import java.util.zip.ZipEntry;
37-
import java.util.zip.ZipFile;
39+
import java.util.zip.ZipInputStream;
3840

3941
import org.eclipse.esmf.aspectmodel.AspectModelFile;
4042
import org.eclipse.esmf.aspectmodel.resolver.AspectModelFileLoader;
@@ -71,6 +73,7 @@
7173
*/
7274
public class AspectModelLoader implements ResolutionStrategySupport {
7375
private static final Logger LOG = LoggerFactory.getLogger( AspectModelLoader.class );
76+
private static final String ASPECT_MODELS_FOLDER = "aspect-models";
7477

7578
public static final Supplier<ResolutionStrategy> DEFAULT_STRATEGY = () -> {
7679
final Path currentDirectory = Path.of( System.getProperty( "user.dir" ) );
@@ -178,37 +181,89 @@ public AspectModel load( final InputStream inputStream ) {
178181
return buildAspectModel( loaderContext.loadedFiles() );
179182
}
180183

181-
public AspectModel loadFromArchive( final String pathToArchive ) {
182-
File zipFile = new File( pathToArchive );
184+
@FunctionalInterface
185+
public interface InputStreamProvider {
186+
InputStream get() throws IOException;
187+
}
183188

184-
if ( !zipFile.exists() || !zipFile.isFile() ) {
189+
/**
190+
* Load Namespace Package (Archive) an Aspect Model from a File
191+
*
192+
* @param namespacePackage the archive file
193+
* @return the Aspect Model
194+
*/
195+
public AspectModel loadNamespacePackage( final File namespacePackage ) {
196+
if ( !namespacePackage.exists() || !namespacePackage.isFile() ) {
185197
throw new RuntimeException( new FileNotFoundException( "The specified file does not exist or is not a file." ) );
186198
}
187199

200+
try ( InputStream inputStream = new FileInputStream( namespacePackage ) ) {
201+
return loadNamespacePackage( inputStream );
202+
} catch ( IOException e ) {
203+
LOG.error( "Error reading the file: {}", namespacePackage.getAbsolutePath(), e );
204+
throw new RuntimeException( "Error reading the file: " + namespacePackage.getAbsolutePath(), e );
205+
}
206+
}
207+
208+
/**
209+
* Load Namespace Package (Archive) an Aspect Model from an InputStream
210+
*
211+
* @param inputStream the input stream
212+
* @return the Aspect Model
213+
*/
214+
public AspectModel loadNamespacePackage( final InputStream inputStream ) {
215+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
216+
try {
217+
inputStream.transferTo( baos );
218+
} catch ( IOException e ) {
219+
throw new RuntimeException( e );
220+
}
221+
final boolean hasAspectModelsFolder = containsFolderInNamespacePackage( new ByteArrayInputStream( baos.toByteArray() ) );
222+
return loadNamespacePackageFromStream( new ByteArrayInputStream( baos.toByteArray() ), hasAspectModelsFolder );
223+
}
224+
225+
private AspectModel loadNamespacePackageFromStream( final InputStream inputStream, final boolean hasAspectModelsFolder ) {
188226
List<AspectModelFile> aspectModelFiles = new ArrayList<>();
189227

190-
try ( ZipFile zip = new ZipFile( zipFile ) ) {
191-
Enumeration<? extends ZipEntry> entries = zip.entries();
228+
try ( ZipInputStream zis = new ZipInputStream( inputStream ) ) {
229+
ZipEntry entry;
192230

193-
while ( entries.hasMoreElements() ) {
194-
ZipEntry entry = entries.nextElement();
231+
while ( (entry = zis.getNextEntry()) != null ) {
232+
boolean isRelevantEntry =
233+
(hasAspectModelsFolder && entry.getName().startsWith( ASPECT_MODELS_FOLDER ) && entry.getName().endsWith( ".ttl" )) ||
234+
(!hasAspectModelsFolder && entry.getName().endsWith( ".ttl" ));
195235

196-
if ( entry.getName().endsWith( ".ttl" ) ) {
197-
try ( InputStream inputStream = zip.getInputStream( entry ) ) {
198-
AspectModelFile aspectModelFile = AspectModelFileLoader.load( inputStream );
199-
aspectModelFiles.add( aspectModelFile );
200-
} catch ( IOException e ) {
201-
LOG.error( String.format( "Error reading entry: %s - %s", entry.getName(), e.getMessage() ) );
202-
}
236+
if ( isRelevantEntry ) {
237+
AspectModelFile aspectModelFile = migrate( AspectModelFileLoader.load( zis ) );
238+
aspectModelFiles.add( aspectModelFile );
203239
}
204240
}
241+
242+
zis.closeEntry();
205243
} catch ( IOException e ) {
206-
LOG.error( String.format( "Error reading the ZIP file: %s", e.getMessage() ) );
244+
LOG.error( "Error reading the Archive input stream", e );
245+
throw new RuntimeException( "Error reading the Archive input stream", e );
207246
}
208247

248+
LoaderContext loaderContext = new LoaderContext();
249+
resolve( aspectModelFiles, loaderContext );
209250
return buildAspectModel( aspectModelFiles );
210251
}
211252

253+
private boolean containsFolderInNamespacePackage( final InputStream inputStream ) {
254+
try ( ZipInputStream zis = new ZipInputStream( inputStream ) ) {
255+
ZipEntry entry;
256+
while ( (entry = zis.getNextEntry()) != null ) {
257+
if ( entry.isDirectory() && entry.getName().equals( ASPECT_MODELS_FOLDER ) ) {
258+
return true;
259+
}
260+
}
261+
} catch ( IOException e ) {
262+
throw new RuntimeException( e );
263+
}
264+
return false;
265+
}
266+
212267
private AspectModelFile migrate( final AspectModelFile file ) {
213268
return MetaModelVersionMigrator.INSTANCE.apply( file );
214269
}

core/esmf-aspect-meta-model-java/src/test/java/org/eclipse/esmf/aspectmodel/loader/AspectModelLoaderTest.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
1717

18+
import java.io.File;
19+
import java.io.FileInputStream;
20+
import java.io.FileNotFoundException;
1821
import java.net.URISyntaxException;
1922
import java.nio.file.Path;
2023
import java.nio.file.Paths;
@@ -34,7 +37,7 @@
3437

3538
class AspectModelLoaderTest {
3639
@Test
37-
public void testOfAbstractEntityCyclomaticCreation() {
40+
void testOfAbstractEntityCyclomaticCreation() {
3841
final Map<String, ComplexType> entities = TestResources.load( TestAspect.ASPECT_WITH_MULTIPLE_ENTITIES_SAME_EXTEND ).elements()
3942
.stream().filter( ComplexType.class::isInstance ).map( ComplexType.class::cast )
4043
.collect( Collectors.toMap( ComplexType::getName, Function.identity() ) );
@@ -48,10 +51,10 @@ public void testOfAbstractEntityCyclomaticCreation() {
4851
}
4952

5053
@Test
51-
public void testLoadAspectModelFromZipArchive() throws URISyntaxException {
54+
void testLoadAspectModelFromZipArchiveFile() throws URISyntaxException {
5255
final ClassLoader classLoader = getClass().getClassLoader();
5356
final Path archivePath = Paths.get( classLoader.getResource( "namespaces.zip" ).toURI() );
54-
final AspectModel aspectModel = new AspectModelLoader().loadFromArchive( archivePath.toString() );
57+
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new File( archivePath.toString() ) );
5558

5659
assertThat( aspectModel.namespaces() ).hasSize( 2 );
5760
assertThat( aspectModel.namespaces().get( 0 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.1.0" );
@@ -65,4 +68,45 @@ public void testLoadAspectModelFromZipArchive() throws URISyntaxException {
6568
assertThat( aspectsNames ).contains( aspectModelFile.aspect().getName() );
6669
} );
6770
}
71+
72+
@Test
73+
void testLoadAspectModelFromZipArchiveInputStream() throws URISyntaxException, FileNotFoundException {
74+
final ClassLoader classLoader = getClass().getClassLoader();
75+
final Path archivePath = Paths.get( classLoader.getResource( "namespaces.zip" ).toURI() );
76+
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new FileInputStream( archivePath.toString() ) );
77+
78+
assertThat( aspectModel.namespaces() ).hasSize( 2 );
79+
assertThat( aspectModel.namespaces().get( 0 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.1.0" );
80+
assertThat( aspectModel.namespaces().get( 1 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.0.0" );
81+
82+
final List<String> aspectsNames = List.of( "Movement2", "Movement3", "Movement", "SimpleAspect" );
83+
84+
assertThat( aspectModel.files() ).hasSize( 4 );
85+
assertThat( aspectModel.files() )
86+
.anySatisfy( aspectModelFile -> {
87+
assertThat( aspectsNames ).contains( aspectModelFile.aspect().getName() );
88+
} );
89+
}
90+
91+
/**
92+
* Test migration to the latest version of Aspect Model in Archive
93+
*/
94+
@Test
95+
void testLoadAspectModelFromZipArchive2_0_0() throws URISyntaxException, FileNotFoundException {
96+
final ClassLoader classLoader = getClass().getClassLoader();
97+
final Path archivePath = Paths.get( classLoader.getResource( "namespaces_with_old_version.zip" ).toURI() );
98+
final AspectModel aspectModel = new AspectModelLoader().loadNamespacePackage( new FileInputStream( archivePath.toString() ) );
99+
100+
assertThat( aspectModel.namespaces() ).hasSize( 2 );
101+
assertThat( aspectModel.namespaces().get( 0 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.1.0" );
102+
assertThat( aspectModel.namespaces().get( 1 ).getName() ).contains( "urn:samm:org.eclipse.examples:1.0.0" );
103+
104+
final List<String> aspectsNames = List.of( "Movement2", "Movement3", "Movement4", "Movement", "SimpleAspect" );
105+
106+
assertThat( aspectModel.files() ).hasSize( 5 );
107+
assertThat( aspectModel.files() )
108+
.anySatisfy( aspectModelFile -> {
109+
assertThat( aspectsNames ).contains( aspectModelFile.aspect().getName() );
110+
} );
111+
}
68112
}
5.93 KB
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.eclipse.esmf.aspectmodel.generator.zip;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.io.OutputStreamWriter;
8+
import java.io.Writer;
9+
import java.util.zip.ZipEntry;
10+
import java.util.zip.ZipOutputStream;
11+
12+
import org.eclipse.esmf.aspectmodel.AspectModelFile;
13+
import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
14+
import org.eclipse.esmf.metamodel.AspectModel;
15+
16+
import org.apache.commons.lang3.function.TriConsumer;
17+
18+
public class AspectModelNamespacePackageCreator implements TriConsumer<AspectModel, OutputStream, Boolean> {
19+
20+
private static final String AASX_ARCHIVE_FORMAT_PATH = "aasx/aspect-models/";
21+
private static final String BASE_ARCHIVE_FORMAT_PATH = "aspect-models/";
22+
23+
public static final AspectModelNamespacePackageCreator INSTANCE = new AspectModelNamespacePackageCreator();
24+
25+
private AspectModelNamespacePackageCreator() {
26+
}
27+
28+
@Override
29+
public void accept( final AspectModel aspectModel, final OutputStream outputStream, final Boolean isAasxArchiveFormat ) {
30+
try ( FileOutputStream fos = (FileOutputStream) outputStream;
31+
BufferedOutputStream bos = new BufferedOutputStream( fos );
32+
ZipOutputStream zos = new ZipOutputStream( bos ) ) {
33+
34+
for ( final AspectModelFile aspectModelFile : aspectModel.files() ) {
35+
addFileToArchive( aspectModelFile, zos, isAasxArchiveFormat );
36+
}
37+
} catch ( IOException e ) {
38+
try {
39+
throw new IOException( "Error creating zip archive!", e );
40+
} catch ( IOException ex ) {
41+
throw new RuntimeException( ex );
42+
}
43+
}
44+
}
45+
46+
private static void addFileToArchive( final AspectModelFile file, final ZipOutputStream zos, final boolean isAasxZipFormat )
47+
throws IOException {
48+
final String aspectString = AspectSerializer.INSTANCE.apply( file.aspect() );
49+
final String fileName = String.format( "%s/%s/%s/%s.ttl",
50+
isAasxZipFormat ? AASX_ARCHIVE_FORMAT_PATH : BASE_ARCHIVE_FORMAT_PATH,
51+
file.aspect().urn().getNamespace(),
52+
file.aspect().urn().getVersion(),
53+
file.aspect().getName() );
54+
final ZipEntry zipEntry = new ZipEntry( fileName );
55+
zos.putNextEntry( zipEntry );
56+
57+
Writer writer = new OutputStreamWriter( zos );
58+
writer.write( aspectString );
59+
writer.flush();
60+
61+
zos.closeEntry();
62+
}
63+
}

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/zip/AspectModelZipGenerator.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/zip/AspectModelZipGeneratorTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import static org.assertj.core.api.Assertions.assertThat;
1717

1818
import java.io.File;
19+
import java.io.FileOutputStream;
1920
import java.io.IOException;
2021
import java.nio.file.Files;
2122
import java.nio.file.Path;
23+
import java.nio.file.Paths;
2224

2325
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
2426
import org.eclipse.esmf.metamodel.AspectModel;
@@ -38,15 +40,16 @@ void beforeEach() throws IOException {
3840
}
3941

4042
@Test
41-
void testAspectModelZipGeneration() throws IOException {
43+
void testAspectModelArchiveGeneration() throws IOException {
4244
final AspectModel aspectModel = TestResources.load( TestAspect.ASPECT_WITH_PROPERTY );
4345
final String outputFileName = String.format( "%s/%s", outputDirectory.toString(), "/test_zip.zip" );
4446

45-
AspectModelZipGenerator.generateZipAspectModelArchive( aspectModel, outputFileName );
47+
AspectModelNamespacePackageCreator.INSTANCE.accept( aspectModel, new FileOutputStream( Paths.get( outputFileName ).toFile() ),
48+
false );
4649

4750
assertThat( new File( outputFileName ) ).exists();
4851

49-
final AspectModel aspectModelResult = new AspectModelLoader().loadFromArchive( outputFileName );
52+
final AspectModel aspectModelResult = new AspectModelLoader().loadNamespacePackage( new File( outputFileName ) );
5053
assertThat( aspectModelResult.namespaces() ).hasSize( 1 );
5154
assertThat( aspectModelResult.files() ).hasSize( 1 );
5255
assertThat( aspectModelResult.files().get( 0 ).aspect() ).isEqualTo( aspectModel.aspect() );

0 commit comments

Comments
 (0)