|
| 1 | +package io.quarkus.arc.impl; |
| 2 | + |
| 3 | +import io.quarkus.arc.ArcInvocationContext; |
| 4 | +import java.lang.annotation.Annotation; |
| 5 | +import java.util.AbstractMap; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.Set; |
| 14 | +import javax.interceptor.InvocationContext; |
| 15 | + |
| 16 | +/** |
| 17 | + * This Map implementation is specifically optimised to implement |
| 18 | + * {@link InvocationContext#getContextData()}. |
| 19 | + * We try to leverage the fact that in most use cases, while there is a high |
| 20 | + * chance for integration points to need to read the content of this map, |
| 21 | + * write operations are less likely. So we only allocate an actual underlying |
| 22 | + * HashMap on a write operation. |
| 23 | + * In addition, an {@link ArcInvocationContext} always allows to return the set of |
| 24 | + * interceptor bindings via {@link ArcInvocationContext#KEY_INTERCEPTOR_BINDINGS}; |
| 25 | + * this is implemented while avoiding a write operation on the underlying Map. |
| 26 | + * While one goal is efficiency, another goal is safety: while the CDI specification |
| 27 | + * allows any interceptor to make changes to this Map, we specifically disallow |
| 28 | + * 3rd party code to make changes to the interceptor bindings of the platform. |
| 29 | + */ |
| 30 | +final class ContextDataMap implements Map<String, Object> { |
| 31 | + |
| 32 | + private final Set<Annotation> interceptorBindings; |
| 33 | + private HashMap<String, Object> delegate; //important to lazily initialize this |
| 34 | + |
| 35 | + ContextDataMap(Set<Annotation> interceptorBindings) { |
| 36 | + this.interceptorBindings = Objects.requireNonNull(interceptorBindings); |
| 37 | + } |
| 38 | + |
| 39 | + private Map<String, Object> getDelegateForRead() { |
| 40 | + if (delegate == null) { |
| 41 | + return Collections.EMPTY_MAP; |
| 42 | + } else { |
| 43 | + return delegate; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private Map<String, Object> getDelegateForWrite(final int sizeHint) { |
| 48 | + if (delegate == null) { |
| 49 | + this.delegate = new HashMap<>(sizeHint, 1.0f); |
| 50 | + } |
| 51 | + return delegate; |
| 52 | + } |
| 53 | + |
| 54 | + // ** Implement methods from the Map interface ** |
| 55 | + |
| 56 | + @Override |
| 57 | + public void clear() { |
| 58 | + throw new UnsupportedOperationException("Not allowed to clear the context data map"); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean containsKey(Object key) { |
| 63 | + if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + return getDelegateForRead().containsKey(key); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean containsValue(Object value) { |
| 71 | + if (interceptorBindings.equals(value)) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + return getDelegateForRead().containsValue(value); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Set<Map.Entry<String, Object>> entrySet() { |
| 79 | + final AbstractMap.SimpleImmutableEntry<String, Object> firstEntry = new AbstractMap.SimpleImmutableEntry<>( |
| 80 | + ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS, interceptorBindings); |
| 81 | + if (delegate == null) { |
| 82 | + return Collections.singleton(firstEntry); |
| 83 | + } else { |
| 84 | + HashSet entries = new HashSet(delegate.size() + 1); |
| 85 | + entries.addAll(delegate.entrySet()); |
| 86 | + entries.add(firstEntry); |
| 87 | + return entries; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Object get(Object key) { |
| 93 | + if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { |
| 94 | + return interceptorBindings; |
| 95 | + } |
| 96 | + return getDelegateForRead().get(key); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean isEmpty() { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Set<String> keySet() { |
| 106 | + if (delegate == null) { |
| 107 | + return Collections.singleton(ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS); |
| 108 | + } else { |
| 109 | + HashSet set = new HashSet(delegate.size() + 1); |
| 110 | + set.addAll(delegate.keySet()); |
| 111 | + set.add(ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS); |
| 112 | + return set; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void putAll(Map m) { |
| 118 | + if (m.containsKey(ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS)) { |
| 119 | + throw new IllegalArgumentException( |
| 120 | + "Not allowed to put key '" + ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS + "' in the context data map"); |
| 121 | + } |
| 122 | + getDelegateForWrite(m.size()).putAll(m); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public Object remove(Object key) { |
| 127 | + if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { |
| 128 | + throw new IllegalArgumentException("Not allowed to remove key '" + ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS |
| 129 | + + "' from the context data map"); |
| 130 | + } else if (delegate == null) { |
| 131 | + return null; |
| 132 | + } else { |
| 133 | + return delegate.remove(key); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public int size() { |
| 139 | + return getDelegateForRead().size() + 1; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public Collection values() { |
| 144 | + if (delegate == null) { |
| 145 | + return Collections.singleton(interceptorBindings); |
| 146 | + } else { |
| 147 | + ArrayList list = new ArrayList(delegate.size() + 1); |
| 148 | + list.addAll(delegate.values()); |
| 149 | + list.add(interceptorBindings); |
| 150 | + return list; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Object put(String key, Object value) { |
| 156 | + if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { |
| 157 | + throw new IllegalArgumentException( |
| 158 | + "Not allowed to put key '" + ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS + "' in the context data map"); |
| 159 | + } |
| 160 | + return getDelegateForWrite(1).put(key, value); |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments