|
| 1 | +/* |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package feign.jaxb; |
| 17 | + |
| 18 | +import javax.xml.bind.JAXBContext; |
| 19 | +import javax.xml.bind.JAXBException; |
| 20 | +import javax.xml.bind.Marshaller; |
| 21 | +import javax.xml.bind.PropertyException; |
| 22 | +import javax.xml.bind.Unmarshaller; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Iterator; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.ConcurrentHashMap; |
| 27 | + |
| 28 | +/** |
| 29 | + * Creates and caches JAXB contexts as well as creates Marshallers and Unmarshallers for each context. |
| 30 | + */ |
| 31 | +public final class JAXBContextFactory { |
| 32 | + private final ConcurrentHashMap<Class, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class, JAXBContext>(64); |
| 33 | + private final Map<String, Object> properties; |
| 34 | + |
| 35 | + private JAXBContextFactory(Map<String, Object> properties) { |
| 36 | + this.properties = properties; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a new {@link javax.xml.bind.Unmarshaller} that handles the supplied class. |
| 41 | + */ |
| 42 | + public Unmarshaller createUnmarshaller(Class<?> clazz) throws JAXBException { |
| 43 | + JAXBContext ctx = getContext(clazz); |
| 44 | + return ctx.createUnmarshaller(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a new {@link javax.xml.bind.Marshaller} that handles the supplied class. |
| 49 | + */ |
| 50 | + public Marshaller createMarshaller(Class<?> clazz) throws JAXBException { |
| 51 | + JAXBContext ctx = getContext(clazz); |
| 52 | + Marshaller marshaller = ctx.createMarshaller(); |
| 53 | + setMarshallerProperties(marshaller); |
| 54 | + return marshaller; |
| 55 | + } |
| 56 | + |
| 57 | + private void setMarshallerProperties(Marshaller marshaller) throws PropertyException { |
| 58 | + Iterator<String> keys = properties.keySet().iterator(); |
| 59 | + |
| 60 | + while(keys.hasNext()) { |
| 61 | + String key = keys.next(); |
| 62 | + marshaller.setProperty(key, properties.get(key)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private JAXBContext getContext(Class<?> clazz) throws JAXBException { |
| 67 | + JAXBContext jaxbContext = this.jaxbContexts.get(clazz); |
| 68 | + if (jaxbContext == null) { |
| 69 | + jaxbContext = JAXBContext.newInstance(clazz); |
| 70 | + this.jaxbContexts.putIfAbsent(clazz, jaxbContext); |
| 71 | + } |
| 72 | + return jaxbContext; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates instances of {@link feign.jaxb.JAXBContextFactory} |
| 77 | + */ |
| 78 | + public static class Builder { |
| 79 | + private final Map<String, Object> properties = new HashMap<String, Object>(5); |
| 80 | + |
| 81 | + /** |
| 82 | + * Sets the jaxb.encoding property of any Marshaller created by this factory. |
| 83 | + */ |
| 84 | + public Builder withMarshallerJAXBEncoding(String value) { |
| 85 | + properties.put(Marshaller.JAXB_ENCODING, value); |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Sets the jaxb.schemaLocation property of any Marshaller created by this factory. |
| 91 | + */ |
| 92 | + public Builder withMarshallerSchemaLocation(String value) { |
| 93 | + properties.put(Marshaller.JAXB_SCHEMA_LOCATION, value); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Sets the jaxb.noNamespaceSchemaLocation property of any Marshaller created by this factory. |
| 99 | + */ |
| 100 | + public Builder withMarshallerNoNamespaceSchemaLocation(String value) { |
| 101 | + properties.put(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, value); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Sets the jaxb.formatted.output property of any Marshaller created by this factory. |
| 107 | + */ |
| 108 | + public Builder withMarshallerFormattedOutput(Boolean value) { |
| 109 | + properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, value); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Sets the jaxb.fragment property of any Marshaller created by this factory. |
| 115 | + */ |
| 116 | + public Builder withMarshallerFragment(Boolean value) { |
| 117 | + properties.put(Marshaller.JAXB_FRAGMENT, value); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Creates a new {@link feign.jaxb.JAXBContextFactory} instance. |
| 123 | + */ |
| 124 | + public JAXBContextFactory build() { |
| 125 | + return new JAXBContextFactory(properties); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments