aboutsummaryrefslogtreecommitdiffstats
path: root/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/CacheControlFeature.java
blob: 1e7c3e7f71555a02477c9696c1d498dda9451ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.onap.aaiclient.client;

import java.io.Closeable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PreDestroy;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.Factory;
import javax.cache.configuration.FactoryBuilder;
import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheWriter;
import javax.cache.spi.CachingProvider;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;
import org.apache.cxf.jaxrs.client.cache.CacheControlClientReaderInterceptor;
import org.apache.cxf.jaxrs.client.cache.CacheControlClientRequestFilter;
import org.apache.cxf.jaxrs.client.cache.Entry;
import org.apache.cxf.jaxrs.client.cache.Key;


@Provider
public class CacheControlFeature implements Feature, Closeable {
    private CachingProvider provider;
    private CacheManager manager;
    private Cache<Key, Entry> cache;
    private boolean cacheResponseInputStream;
    private Factory<ExpiryPolicy> expiryPolicy;

    @Override
    public boolean configure(final FeatureContext context) {
        // TODO: read context properties to exclude some patterns?
        final Cache<Key, Entry> entryCache = createCache(context.getConfiguration().getProperties());
        context.register(new CacheControlClientRequestFilter(entryCache));
        CacheControlClientReaderInterceptor reader = new CacheControlClientReaderInterceptor(entryCache);
        reader.setCacheResponseInputStream(cacheResponseInputStream);
        context.register(reader);
        return true;
    }

    @PreDestroy // TODO: check it is called
    public void close() {
        for (final Closeable c : Arrays.asList(cache, manager, provider)) {
            try {
                if (c != null) {
                    c.close();
                }
            } catch (final Exception e) {
                // no-op
            }
        }
    }

    private Cache<Key, Entry> createCache(final Map<String, Object> properties) {
        final Properties props = new Properties();
        props.putAll(properties);

        final String prefix = this.getClass().getName() + ".";
        final String uri = props.getProperty(prefix + "config-uri");
        final String name = props.getProperty(prefix + "name", this.getClass().getName());

        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

        provider = Caching.getCachingProvider();
        try {
            synchronized (contextClassLoader) {
                manager = provider.getCacheManager(uri == null ? provider.getDefaultURI() : new URI(uri),
                        contextClassLoader, props);
                if (manager.getCache(name) == null) {
                    final MutableConfiguration<Key, Entry> configuration = new MutableConfiguration<Key, Entry>()
                            .setReadThrough("true".equalsIgnoreCase(props.getProperty(prefix + "readThrough", "false")))
                            .setWriteThrough(
                                    "true".equalsIgnoreCase(props.getProperty(prefix + "writeThrough", "false")))
                            .setManagementEnabled(
                                    "true".equalsIgnoreCase(props.getProperty(prefix + "managementEnabled", "false")))
                            .setStatisticsEnabled(
                                    "true".equalsIgnoreCase(props.getProperty(prefix + "statisticsEnabled", "false")))
                            .setStoreByValue(
                                    "true".equalsIgnoreCase(props.getProperty(prefix + "storeByValue", "false")));

                    final String loader = props.getProperty(prefix + "loaderFactory");
                    if (loader != null) {
                        @SuppressWarnings("unchecked")
                        Factory<? extends CacheLoader<Key, Entry>> f =
                                newInstance(contextClassLoader, loader, Factory.class);
                        configuration.setCacheLoaderFactory(f);
                    }
                    final String writer = props.getProperty(prefix + "writerFactory");
                    if (writer != null) {
                        @SuppressWarnings("unchecked")
                        Factory<? extends CacheWriter<Key, Entry>> f =
                                newInstance(contextClassLoader, writer, Factory.class);
                        configuration.setCacheWriterFactory(f);
                    }
                    if (expiryPolicy != null) {

                        configuration.setExpiryPolicyFactory(expiryPolicy);
                    }
                    configuration.addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration(
                            FactoryBuilder.factoryOf(new CacheLogger()), null, true, true));

                    cache = manager.createCache(name, configuration);
                } else {
                    cache = manager.getCache(name);
                }
                return cache;
            }
        } catch (final URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @SuppressWarnings("unchecked")
    private static <T> T newInstance(final ClassLoader contextClassLoader, final String clazz, final Class<T> cast) {
        try {
            return (T) contextClassLoader.loadClass(clazz).newInstance();
        } catch (final Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    public void setCacheResponseInputStream(boolean cacheStream) {
        this.cacheResponseInputStream = cacheStream;
    }

    public void setExpiryPolicyFactory(Factory<ExpiryPolicy> f) {
        this.expiryPolicy = f;
    }
}