|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign.jaxb; |
| 15 | + |
| 16 | +import jakarta.xml.bind.*; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Map.Entry; |
| 21 | +import java.util.concurrent.ConcurrentHashMap; |
| 22 | + |
| 23 | +/** |
| 24 | + * Creates and caches JAXB contexts as well as creates Marshallers and Unmarshallers for each |
| 25 | + * context. Since JAXB contexts creation can be an expensive task, JAXB context can be preloaded on |
| 26 | + * factory creation otherwise they will be created and cached dynamically when needed. |
| 27 | + */ |
| 28 | +public final class JAXBContextFactory { |
| 29 | + |
| 30 | + private final ConcurrentHashMap<JAXBContextCacheKey, JAXBContext> jaxbContexts = |
| 31 | + new ConcurrentHashMap<>(64); |
| 32 | + private final Map<String, Object> properties; |
| 33 | + private final JAXBContextInstantationMode jaxbContextInstantationMode; |
| 34 | + |
| 35 | + private JAXBContextFactory( |
| 36 | + Map<String, Object> properties, JAXBContextInstantationMode jaxbContextInstantationMode) { |
| 37 | + this.properties = properties; |
| 38 | + this.jaxbContextInstantationMode = jaxbContextInstantationMode; |
| 39 | + } |
| 40 | + |
| 41 | + /** Creates a new {@link jakarta.xml.bind.Unmarshaller} that handles the supplied class. */ |
| 42 | + public Unmarshaller createUnmarshaller(Class<?> clazz) throws JAXBException { |
| 43 | + return getContext(clazz).createUnmarshaller(); |
| 44 | + } |
| 45 | + |
| 46 | + /** Creates a new {@link jakarta.xml.bind.Marshaller} that handles the supplied class. */ |
| 47 | + public Marshaller createMarshaller(Class<?> clazz) throws JAXBException { |
| 48 | + Marshaller marshaller = getContext(clazz).createMarshaller(); |
| 49 | + setMarshallerProperties(marshaller); |
| 50 | + return marshaller; |
| 51 | + } |
| 52 | + |
| 53 | + private void setMarshallerProperties(Marshaller marshaller) throws PropertyException { |
| 54 | + for (Entry<String, Object> en : properties.entrySet()) { |
| 55 | + marshaller.setProperty(en.getKey(), en.getValue()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private JAXBContext getContext(Class<?> clazz) throws JAXBException { |
| 60 | + JAXBContextCacheKey cacheKey = jaxbContextInstantationMode.getJAXBContextCacheKey(clazz); |
| 61 | + JAXBContext jaxbContext = this.jaxbContexts.get(cacheKey); |
| 62 | + |
| 63 | + if (jaxbContext == null) { |
| 64 | + jaxbContext = jaxbContextInstantationMode.getJAXBContext(clazz); |
| 65 | + this.jaxbContexts.putIfAbsent(cacheKey, jaxbContext); |
| 66 | + } |
| 67 | + return jaxbContext; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Will preload factory's cache with JAXBContext for provided classes |
| 72 | + * |
| 73 | + * @param classes |
| 74 | + * @throws jakarta.xml.bind.JAXBException |
| 75 | + */ |
| 76 | + private void preloadContextCache(List<Class<?>> classes) throws JAXBException { |
| 77 | + if (classes != null && !classes.isEmpty()) { |
| 78 | + for (Class<?> clazz : classes) { |
| 79 | + getContext(clazz); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** Creates instances of {@link JAXBContextFactory}. */ |
| 85 | + public static class Builder { |
| 86 | + |
| 87 | + private final Map<String, Object> properties = new HashMap<>(10); |
| 88 | + |
| 89 | + private JAXBContextInstantationMode jaxbContextInstantationMode = |
| 90 | + JAXBContextInstantationMode.CLASS; |
| 91 | + |
| 92 | + /** Sets the jaxb.encoding property of any Marshaller created by this factory. */ |
| 93 | + public Builder withMarshallerJAXBEncoding(String value) { |
| 94 | + properties.put(Marshaller.JAXB_ENCODING, value); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** Sets the jaxb.schemaLocation property of any Marshaller created by this factory. */ |
| 99 | + public Builder withMarshallerSchemaLocation(String value) { |
| 100 | + properties.put(Marshaller.JAXB_SCHEMA_LOCATION, value); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Sets the jaxb.noNamespaceSchemaLocation property of any Marshaller created by this factory. |
| 106 | + */ |
| 107 | + public Builder withMarshallerNoNamespaceSchemaLocation(String value) { |
| 108 | + properties.put(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, value); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /** Sets the jaxb.formatted.output property of any Marshaller created by this factory. */ |
| 113 | + public Builder withMarshallerFormattedOutput(Boolean value) { |
| 114 | + properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, value); |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + /** Sets the jaxb.fragment property of any Marshaller created by this factory. */ |
| 119 | + public Builder withMarshallerFragment(Boolean value) { |
| 120 | + properties.put(Marshaller.JAXB_FRAGMENT, value); |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Sets the given property of any Marshaller created by this factory. |
| 126 | + * |
| 127 | + * <p>Example : <br> |
| 128 | + * <br> |
| 129 | + * <code> |
| 130 | + * new JAXBContextFactory.Builder() |
| 131 | + * .withProperty("com.sun.xml.internal.bind.xmlHeaders", "<!DOCTYPE Example SYSTEM \"example.dtd\">") |
| 132 | + * .build(); |
| 133 | + * </code> |
| 134 | + */ |
| 135 | + public Builder withProperty(String key, Object value) { |
| 136 | + properties.put(key, value); |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Provide an instantiation mode for JAXB Contexts, can be class or package, default is class if |
| 142 | + * this method is not called. |
| 143 | + * |
| 144 | + * <p>Example : <br> |
| 145 | + * <br> |
| 146 | + * <code> |
| 147 | + * new JAXBContextFactory.Builder() |
| 148 | + * .withJAXBContextInstantiationMode(JAXBContextInstantationMode.PACKAGE) |
| 149 | + * .build(); |
| 150 | + * </code> |
| 151 | + */ |
| 152 | + public Builder withJAXBContextInstantiationMode( |
| 153 | + JAXBContextInstantationMode jaxbContextInstantiationMode) { |
| 154 | + this.jaxbContextInstantationMode = jaxbContextInstantiationMode; |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + /** Creates a new {@link JAXBContextFactory} instance with a lazy loading cached context */ |
| 159 | + public JAXBContextFactory build() { |
| 160 | + return new JAXBContextFactory(properties, jaxbContextInstantationMode); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Creates a new {@link JAXBContextFactory} instance. Pre-loads context cache with given classes |
| 165 | + * |
| 166 | + * @param classes |
| 167 | + * @return ContextFactory with a pre-populated JAXBContext cache |
| 168 | + * @throws jakarta.xml.bind.JAXBException if provided classes can't be used for JAXBContext |
| 169 | + * generation most likely due to missing JAXB annotations |
| 170 | + */ |
| 171 | + public JAXBContextFactory build(List<Class<?>> classes) throws JAXBException { |
| 172 | + JAXBContextFactory factory = new JAXBContextFactory(properties, jaxbContextInstantationMode); |
| 173 | + factory.preloadContextCache(classes); |
| 174 | + return factory; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments