Skip to content

Commit fcc1721

Browse files
committed
Use standard generator arch for AspectModelNamespacePackageCreator
1 parent 31addb7 commit fcc1721

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

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

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,78 @@
11
package org.eclipse.esmf.aspectmodel.generator.zip;
22

33
import java.io.BufferedOutputStream;
4-
import java.io.FileOutputStream;
4+
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
66
import java.io.OutputStream;
77
import java.io.OutputStreamWriter;
88
import java.io.Writer;
9+
import java.util.stream.Stream;
910
import java.util.zip.ZipEntry;
1011
import java.util.zip.ZipOutputStream;
1112

1213
import org.eclipse.esmf.aspectmodel.AspectModelFile;
14+
import org.eclipse.esmf.aspectmodel.generator.GenerationException;
15+
import org.eclipse.esmf.aspectmodel.generator.Generator;
1316
import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
1417
import org.eclipse.esmf.metamodel.AspectModel;
1518

16-
import org.apache.commons.lang3.function.TriConsumer;
19+
public class AspectModelNamespacePackageCreator
20+
extends Generator<AspectModel, String, byte[], NamespacePackageGenerationConfig, NamespacePackageArtifact> {
21+
public static final NamespacePackageGenerationConfig DEFAULT_CONFIG = NamespacePackageGenerationConfigBuilder.builder().build();
22+
@Deprecated( forRemoval = true )
23+
public static final AspectModelNamespacePackageCreator INSTANCE = new AspectModelNamespacePackageCreator( null );
24+
private static final String BASE_ARCHIVE_FORMAT_PATH = "aspect-models/";
1725

18-
public class AspectModelNamespacePackageCreator implements TriConsumer<AspectModel, OutputStream, String> {
26+
public AspectModelNamespacePackageCreator( final AspectModel aspectModel ) {
27+
this( aspectModel, DEFAULT_CONFIG );
28+
}
1929

20-
private static final String BASE_ARCHIVE_FORMAT_PATH = "aspect-models/";
30+
public AspectModelNamespacePackageCreator( final AspectModel aspectModel, final NamespacePackageGenerationConfig config ) {
31+
super( aspectModel, config );
32+
}
2133

22-
public static final AspectModelNamespacePackageCreator INSTANCE = new AspectModelNamespacePackageCreator();
34+
private AspectModel aspectModel() {
35+
return focus;
36+
}
2337

24-
private AspectModelNamespacePackageCreator() {
38+
/**
39+
* @deprecated Use {@link #AspectModelNamespacePackageCreator(AspectModel, NamespacePackageGenerationConfig)} instead
40+
*/
41+
@Deprecated( forRemoval = true )
42+
public void accept( final AspectModel aspectModel, final OutputStream outputStream, final String rootPath ) {
43+
final NamespacePackageGenerationConfig config = NamespacePackageGenerationConfigBuilder.builder()
44+
.rootPath( rootPath )
45+
.build();
46+
final NamespacePackageArtifact artifact = new AspectModelNamespacePackageCreator( aspectModel, config ).singleResult();
47+
write( artifact, x -> outputStream );
48+
}
49+
50+
/**
51+
* Figures out a fitting name for the generated namespace package
52+
*
53+
* @return a name for the generated namespace package
54+
*/
55+
private String packageName() {
56+
if ( aspectModel().aspects().size() == 1 ) {
57+
return aspectModel().aspect().getName() + ".zip";
58+
}
59+
return "package" + aspectModel().hashCode() + ".zip"; // 🤷
2560
}
2661

2762
@Override
28-
public void accept( final AspectModel aspectModel, final OutputStream outputStream, final String rootPath ) {
29-
try ( final FileOutputStream fos = (FileOutputStream) outputStream;
30-
final BufferedOutputStream bos = new BufferedOutputStream( fos );
63+
public Stream<NamespacePackageArtifact> generate() {
64+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
65+
try ( final BufferedOutputStream bos = new BufferedOutputStream( out );
3166
final ZipOutputStream zos = new ZipOutputStream( bos ) ) {
32-
33-
for ( final AspectModelFile aspectModelFile : aspectModel.files() ) {
34-
addFileToArchive( aspectModelFile, zos, rootPath );
35-
}
36-
} catch ( final IOException e ) {
37-
try {
38-
throw new IOException( "Error creating zip archive!", e );
39-
} catch ( final IOException ex ) {
40-
throw new RuntimeException( ex );
67+
for ( final AspectModelFile aspectModelFile : aspectModel().files() ) {
68+
addFileToArchive( aspectModelFile, zos, config.rootPath() );
4169
}
70+
out.close();
71+
} catch ( final IOException exception ) {
72+
throw new GenerationException( "Could not creat namespace package for Aspect Model", exception );
4273
}
74+
75+
return Stream.of( new NamespacePackageArtifact( packageName(), out.toByteArray() ) );
4376
}
4477

4578
private static void addFileToArchive( final AspectModelFile file, final ZipOutputStream zos, final String rootPath )
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2024 Bosch Software Innovations GmbH. All rights reserved.
3+
*/
4+
5+
package org.eclipse.esmf.aspectmodel.generator.zip;
6+
7+
import org.eclipse.esmf.aspectmodel.generator.BinaryArtifact;
8+
9+
public class NamespacePackageArtifact extends BinaryArtifact {
10+
public NamespacePackageArtifact( final String id, final byte[] content ) {
11+
super( id, content );
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2024 Bosch Software Innovations GmbH. All rights reserved.
3+
*/
4+
5+
package org.eclipse.esmf.aspectmodel.generator.zip;
6+
7+
import org.eclipse.esmf.aspectmodel.generator.GenerationConfig;
8+
9+
import io.soabase.recordbuilder.core.RecordBuilder;
10+
11+
@RecordBuilder
12+
public record NamespacePackageGenerationConfig(
13+
String rootPath
14+
) implements GenerationConfig {
15+
public NamespacePackageGenerationConfig {
16+
if ( rootPath == null ) {
17+
rootPath = "";
18+
}
19+
}
20+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ void beforeEach() throws IOException {
4242
void testAspectModelArchiveGeneration() throws IOException {
4343
final AspectModel aspectModel = TestResources.load( TestAspect.ASPECT_WITH_PROPERTY );
4444
final String outputFileName = String.format( "%s/%s", outputDirectory.toString(), "/test_zip.zip" );
45-
46-
AspectModelNamespacePackageCreator.INSTANCE.accept( aspectModel, new FileOutputStream( Paths.get( outputFileName ).toFile() ), "" );
45+
try ( final FileOutputStream fileOutputStream = new FileOutputStream( Paths.get( outputFileName ).toFile() ) ) {
46+
new AspectModelNamespacePackageCreator( aspectModel, AspectModelNamespacePackageCreator.DEFAULT_CONFIG )
47+
.generate( x -> fileOutputStream );
48+
}
4749

4850
assertThat( new File( outputFileName ) ).exists();
4951

0 commit comments

Comments
 (0)