You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Luca Burgazzoli (Jira)" <ji...@apache.org> on 2021/02/01 17:10:00 UTC

[jira] [Updated] (CAMEL-16127) Revisit PackageScanResourceResolver

     [ https://issues.apache.org/jira/browse/CAMEL-16127?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Luca Burgazzoli updated CAMEL-16127:
------------------------------------
    Description: 
The PackageScanResourceResolver has two methods to resolve resources:

{code:java}
Set<InputStream> findResources(String location) throws Exception;
Set<String> findResourceNames(String location) throws Exception;
{code}

The default implementation provided by DefaultPackageScanResourceResolver is:

{code:java}
@Override
public Set<String> findResourceNames(String location) throws Exception {
    Set<KeyValueHolder<String, InputStream>> answer = new LinkedHashSet<>();
    doFindResources(location, answer);
    return answer.stream().map(KeyValueHolder::getKey).collect(Collectors.toSet());
}
@Override
public Set<InputStream> findResources(String location) throws Exception {
    Set<KeyValueHolder<String, InputStream>> answer = new LinkedHashSet<>();
    doFindResources(location, answer);
    return answer.stream().map(KeyValueHolder::getValue).collect(Collectors.toSet());
}
{code}

There are two issues here:
1. findResourceNames leaks resources as the InpuStreams found by doFindResources are never closed.  
2. is is not possible to correlate an InputStream with the related resource name

I'd propose to refactor this interface to something like:

{code:java}
Map<String, ThrowingSupplier<InputStream>> findResources();

default Collection<String> findResourceNames()  throws Exception {
    return findResources().keySet():
}

default Collection<InputStream> findResourceStreams() throws Exception {
    List<InputStream> streams = new ArrayList();
    for (ThrowingSupplier<InputStream> supplier : findResources().values()) {
        streams.add(supplier.get());
    }

    return streams;
}
{code}


  was:
The PackageScanResourceResolver has two methods to resolve resources:

{code:java}
Set<InputStream> findResources(String location) throws Exception;
Set<String> findResourceNames(String location) throws Exception;
{code}

The default implementation provided by DefaultPackageScanResourceResolver is:

@Override
public Set<String> findResourceNames(String location) throws Exception {
    Set<KeyValueHolder<String, InputStream>> answer = new LinkedHashSet<>();
    doFindResources(location, answer);
    return answer.stream().map(KeyValueHolder::getKey).collect(Collectors.toSet());
}
@Override
public Set<InputStream> findResources(String location) throws Exception {
    Set<KeyValueHolder<String, InputStream>> answer = new LinkedHashSet<>();
    doFindResources(location, answer);
    return answer.stream().map(KeyValueHolder::getValue).collect(Collectors.toSet());
}
{code}

There are two issues here:
1. findResourceNames leaks resources as the InpuStreams found by doFindResources are never closed.  
2. is is not possible to correlate an InputStream with the related resource name

I'd propose to refactor this interface to something like:

{code:java}
Map<String, ThrowingSupplier<InputStream>> findResources();

default Collection<String> findResourceNames()  throws Exception {
    return findResources().keySet():
}

default Collection<InputStream> findResourceStreams() throws Exception {
    List<InputStream> streams = new ArrayList();
    for (ThrowingSupplier<InputStream> supplier : findResources().values()) {
        streams.add(supplier.get());
    }

    return streams;
}
{code}



> Revisit PackageScanResourceResolver
> -----------------------------------
>
>                 Key: CAMEL-16127
>                 URL: https://issues.apache.org/jira/browse/CAMEL-16127
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-core, camel-core-api
>            Reporter: Luca Burgazzoli
>            Priority: Minor
>             Fix For: 3.9.0
>
>
> The PackageScanResourceResolver has two methods to resolve resources:
> {code:java}
> Set<InputStream> findResources(String location) throws Exception;
> Set<String> findResourceNames(String location) throws Exception;
> {code}
> The default implementation provided by DefaultPackageScanResourceResolver is:
> {code:java}
> @Override
> public Set<String> findResourceNames(String location) throws Exception {
>     Set<KeyValueHolder<String, InputStream>> answer = new LinkedHashSet<>();
>     doFindResources(location, answer);
>     return answer.stream().map(KeyValueHolder::getKey).collect(Collectors.toSet());
> }
> @Override
> public Set<InputStream> findResources(String location) throws Exception {
>     Set<KeyValueHolder<String, InputStream>> answer = new LinkedHashSet<>();
>     doFindResources(location, answer);
>     return answer.stream().map(KeyValueHolder::getValue).collect(Collectors.toSet());
> }
> {code}
> There are two issues here:
> 1. findResourceNames leaks resources as the InpuStreams found by doFindResources are never closed.  
> 2. is is not possible to correlate an InputStream with the related resource name
> I'd propose to refactor this interface to something like:
> {code:java}
> Map<String, ThrowingSupplier<InputStream>> findResources();
> default Collection<String> findResourceNames()  throws Exception {
>     return findResources().keySet():
> }
> default Collection<InputStream> findResourceStreams() throws Exception {
>     List<InputStream> streams = new ArrayList();
>     for (ThrowingSupplier<InputStream> supplier : findResources().values()) {
>         streams.add(supplier.get());
>     }
>     return streams;
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)