Skip to content

Commit 7d0a100

Browse files
committed
refactoring
1 parent d784480 commit 7d0a100

File tree

1 file changed

+31
-23
lines changed
  • core/sds-aspect-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver

1 file changed

+31
-23
lines changed

core/sds-aspect-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/ClasspathStrategy.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,37 +101,45 @@ protected Stream<String> filesInDirectory( final String directory ) {
101101
final String path = jarIndex > 0 ? url.substring( 0, jarIndex +4 ).replace( "jar:file:", "" ) : url;
102102
final File jarFile = new File(path);
103103
if(jarFile.isFile()) {
104-
final List<String> fileList = new ArrayList<>();
105-
final JarFile jar = new JarFile(jarFile);
106-
final Enumeration<JarEntry> entries = jar.entries();
107-
final String dir = directory.endsWith( "/" ) ? directory : directory + "/";
108-
while(entries.hasMoreElements()) {
109-
final String name = entries.nextElement().getName();
110-
if(name.contains( dir ))
111-
{
112-
final String fileName = name.replace( dir, "" );
113-
if(StringUtils.isNotBlank( fileName ))
114-
{
115-
fileList.add( fileName );
116-
}
117-
}
118-
}
119-
jar.close();
120-
return fileList.stream();
104+
return getFilesFromJar( directory, jarFile );
121105
} else {
122-
final InputStream directoryUrl = getClass().getClassLoader().getResourceAsStream( directory );
123-
if ( directoryUrl == null ) {
124-
LOG.warn( "No such classpath directory {}", directory );
125-
return Stream.empty();
126-
}
127-
return Arrays.stream( IOUtils.toString( directoryUrl, StandardCharsets.UTF_8 ).split( "\\n" ) );
106+
return getFilesFromResources( directory );
128107
}
129108
} catch ( final IOException exception ) {
130109
LOG.warn( "Could not list files in classpath directory {}", directory, exception );
131110
return Stream.empty();
132111
}
133112
}
134113

114+
private Stream<String> getFilesFromResources( String directory ) throws IOException {
115+
final InputStream directoryUrl = getClass().getClassLoader().getResourceAsStream( directory );
116+
if ( directoryUrl == null ) {
117+
LOG.warn( "No such classpath directory {}", directory );
118+
return Stream.empty();
119+
}
120+
return Arrays.stream( IOUtils.toString( directoryUrl, StandardCharsets.UTF_8 ).split( "\\n" ) );
121+
}
122+
123+
private Stream<String> getFilesFromJar( String directory, File jarFile ) throws IOException {
124+
final List<String> fileList = new ArrayList<>();
125+
final JarFile jar = new JarFile( jarFile );
126+
final Enumeration<JarEntry> entries = jar.entries();
127+
final String dir = directory.endsWith( "/" ) ? directory : directory + "/";
128+
while(entries.hasMoreElements()) {
129+
final String name = entries.nextElement().getName();
130+
if(name.contains( dir ))
131+
{
132+
final String fileName = name.replace( dir, "" );
133+
if(StringUtils.isNotBlank( fileName ))
134+
{
135+
fileList.add( fileName );
136+
}
137+
}
138+
}
139+
jar.close();
140+
return fileList.stream();
141+
}
142+
135143
@Override
136144
public Try<Model> apply( final AspectModelUrn aspectModelUrn ) {
137145
final String modelsRootTrailingSlash = modelsRoot.isEmpty() ? "" : "/";

0 commit comments

Comments
 (0)