Skip to content

Commit 4ec7dac

Browse files
committed
Fix resolving of shapes files against local file system
Note that this is only relevant during tests, since resolution in bamm-cli or library usage of the SDK will always resolve the shapes files from a jar.
1 parent 023692f commit 4ec7dac

File tree

1 file changed

+35
-5
lines changed
  • core/sds-aspect-meta-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/services

1 file changed

+35
-5
lines changed

core/sds-aspect-meta-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/services/MetaModelUrls.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
33
*
44
* See the AUTHORS file(s) distributed with this work for additional
5-
* information regarding authorship.
5+
* information regarding authorship.
66
*
77
* This Source Code Form is subject to the terms of the Mozilla Public
88
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -13,12 +13,16 @@
1313

1414
package io.openmanufacturing.sds.aspectmodel.resolver.services;
1515

16+
import java.io.IOException;
1617
import java.net.URL;
18+
import java.util.List;
1719
import java.util.Optional;
1820

1921
import org.slf4j.Logger;
2022
import org.slf4j.LoggerFactory;
2123

24+
import com.google.common.collect.ImmutableList;
25+
2226
import io.openmanufacturing.sds.aspectmetamodel.KnownVersion;
2327

2428
/**
@@ -40,10 +44,36 @@ private MetaModelUrls() {
4044
*/
4145
public static Optional<URL> url( final String part, final KnownVersion version, final String fileName ) {
4246
final String spec = String.format( "bamm/%s/%s/%s", part, version.toVersionString(), fileName );
43-
final URL result = MetaModelUrls.class.getClassLoader().getResource( spec );
44-
if ( result == null ) {
45-
LOG.warn( "Could not resolve meta model resource {}", spec );
47+
try {
48+
final List<URL> urls = ImmutableList.copyOf( MetaModelUrls.class.getClassLoader().getResources( spec ).asIterator() );
49+
if ( urls.size() == 1 ) {
50+
return Optional.of( urls.get( 0 ) );
51+
}
52+
if ( urls.isEmpty() ) {
53+
return nothingFound( spec );
54+
}
55+
56+
// If multiple resources with the given spec are found:
57+
// - return the one from the file system, if it exists
58+
// - otherwise, the one from jar
59+
// - otherwise, any of the found resources
60+
URL jarUrl = null;
61+
for ( final URL url : urls ) {
62+
if ( url.getProtocol().equals( "file" ) ) {
63+
return Optional.of( url );
64+
}
65+
if ( url.getProtocol().equals( "jar" ) ) {
66+
jarUrl = url;
67+
}
68+
}
69+
return Optional.of( jarUrl == null ? urls.get( 0 ) : jarUrl );
70+
} catch ( final IOException e ) {
71+
return nothingFound( spec );
4672
}
47-
return Optional.ofNullable( result );
73+
}
74+
75+
private static Optional<URL> nothingFound( final String spec ) {
76+
LOG.warn( "Could not resolve meta model resource {}", spec );
77+
return Optional.empty();
4878
}
4979
}

0 commit comments

Comments
 (0)