• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.87
/exist-core/src/main/java/org/exist/util/io/FilterInputStreamCacheFactory.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.util.io;
25

26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.lang.invoke.LambdaMetafactory;
29
import java.lang.invoke.MethodHandle;
30
import java.lang.invoke.MethodHandles;
31
import java.util.function.Function;
32

33
import org.apache.logging.log4j.LogManager;
34
import org.apache.logging.log4j.Logger;
35

36
import static java.lang.invoke.MethodType.methodType;
37

38
/**
39
 * Factory to instantiate a cache object
40
 *
41
 * @author <a href="mailto:adam.retter@googlemail.com">Adam Retter</a>
42
 */
43
public class FilterInputStreamCacheFactory {
44

45
    private static final Logger LOG = LogManager.getLogger(FilterInputStreamCacheFactory.class);
1✔
46
    private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
1✔
47

48
    public interface FilterInputStreamCacheConfiguration {
49
        String getCacheClass();
50
    }
51

52
    private FilterInputStreamCacheFactory() {
53
    }
54

55
    /**
56
     * Get a suitable Cache instance.
57
     *
58
     * @param cacheConfiguration the configuration for the cache
59
     * @param is the input stream to cache
60
     *
61
     * @return the cache instance
62
     *
63
     * @throws IOException if an error occurs setting up the cache
64
     */
65
    public static FilterInputStreamCache getCacheInstance(final FilterInputStreamCacheConfiguration cacheConfiguration, final InputStream is) throws IOException {
66
        final FilterInputStreamCache cache = new FilterInputStreamCacheFactory().instantiate(cacheConfiguration, is);
1✔
67
        if (cache == null) {
1!
68
            throw new IOException("Could not load cache for class: " + cacheConfiguration.getCacheClass());
×
69
        }
70
        FilterInputStreamCacheMonitor.getInstance().register(cache);
1✔
71
        return cache;
1✔
72
    }
73

74
    private FilterInputStreamCache instantiate(final FilterInputStreamCacheConfiguration cacheConfiguration, final InputStream is) {
75
        try {
76
            final Class clazz = Class.forName(cacheConfiguration.getCacheClass());
1✔
77

78
            final MethodHandle methodHandle = LOOKUP.findConstructor(clazz, methodType(void.class, InputStream.class));
1✔
79

80
            final Function<InputStream, FilterInputStreamCache> constructor = (Function<InputStream, FilterInputStreamCache>)
1✔
81
                    LambdaMetafactory.metafactory(
1✔
82
                            LOOKUP, "apply", methodType(Function.class),
1✔
83
                            methodHandle.type().erase(), methodHandle, methodHandle.type()).getTarget().invokeExact();
1✔
84
            return constructor.apply(is);
1✔
85
        } catch (final Throwable e) {
×
86
            if (e instanceof InterruptedException) {
×
87
                // NOTE: must set interrupted flag
88
                Thread.currentThread().interrupt();
×
89
            }
90

91
            LOG.error(e.getMessage(), e);
×
92
            return null;
×
93
        }
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc