diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java index c8edf0b1..583ccbce 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java @@ -10,7 +10,6 @@ import org.apache.http.config.SocketConfig; import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.X509HostnameVerifier; -import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HttpProcessor; @@ -31,7 +30,6 @@ import com.foxinmy.weixin4j.util.Consts; */ public class HttpComponent4_2Factory extends HttpClientFactory { - private volatile CloseableHttpClient httpClient; private final HttpClientBuilder clientBuilder; public HttpComponent4_2Factory() { diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/xml/XmlStream.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/xml/XmlStream.java index 5fc07be7..b7cc350d 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/xml/XmlStream.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/xml/XmlStream.java @@ -44,12 +44,7 @@ import com.foxinmy.weixin4j.util.StringUtil; public final class XmlStream { private final static String ROOT_ELEMENT_XML = "xml"; private final static String XML_VERSION = "1.0"; - private final static Map, Unmarshaller> messageUnmarshaller; - private final static Map, Marshaller> messageMarshaller; - static { - messageUnmarshaller = new ConcurrentHashMap, Unmarshaller>(); - messageMarshaller = new ConcurrentHashMap, Marshaller>(); - } + private final static ConcurrentHashMap, JAXBContext> jaxbContexts = new ConcurrentHashMap, JAXBContext>();; /** * Xml2Bean @@ -62,28 +57,28 @@ public final class XmlStream { */ @SuppressWarnings("unchecked") public static T fromXML(InputStream content, Class clazz) { - Unmarshaller unmarshaller = messageUnmarshaller.get(clazz); - if (unmarshaller == null) { - try { - JAXBContext jaxbContext = JAXBContext.newInstance(clazz); - unmarshaller = jaxbContext.createUnmarshaller(); - messageUnmarshaller.put(clazz, unmarshaller); - } catch (JAXBException e) { - throw new RuntimeException(e); - } - } + JAXBContext jaxbContext = getJaxbContext(clazz); try { + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Source source = new StreamSource(content); - XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class); + XmlRootElement rootElement = clazz + .getAnnotation(XmlRootElement.class); if (rootElement == null - || rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) { - JAXBElement jaxbElement = unmarshaller.unmarshal(source, clazz); + || rootElement.name().equals( + XmlRootElement.class.getMethod("name") + .getDefaultValue().toString())) { + JAXBElement jaxbElement = unmarshaller.unmarshal(source, + clazz); return jaxbElement.getValue(); } else { return (T) unmarshaller.unmarshal(source); } - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JAXBException ex) { + throw new RuntimeException("Could not unmarshaller class [" + clazz + + "]: " + ex.getMessage(), ex); + } catch (NoSuchMethodException ex) { + throw new RuntimeException("Could not unmarshaller class [" + clazz + + "]: " + ex.getMessage(), ex); } finally { if (content != null) { try { @@ -105,7 +100,8 @@ public final class XmlStream { * @return */ public static T fromXML(String content, Class clazz) { - return fromXML(new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz); + return fromXML( + new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz); } /** @@ -118,7 +114,8 @@ public final class XmlStream { public static String map2xml(Map map) { StringWriter sw = new StringWriter(); try { - XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); + XMLStreamWriter xw = XMLOutputFactory.newInstance() + .createXMLStreamWriter(sw); xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION); xw.writeStartElement(ROOT_ELEMENT_XML); for (Entry entry : map.entrySet()) { @@ -154,7 +151,8 @@ public final class XmlStream { public static String map2xml(JSONObject json) { StringWriter sw = new StringWriter(); try { - XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); + XMLStreamWriter xw = XMLOutputFactory.newInstance() + .createXMLStreamWriter(sw); xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION); xw.writeStartElement(ROOT_ELEMENT_XML); for (Entry entry : json.entrySet()) { @@ -191,7 +189,8 @@ public final class XmlStream { Map map = new HashMap(); StringReader sr = new StringReader(content); try { - XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(sr); + XMLStreamReader xr = XMLInputFactory.newInstance() + .createXMLStreamReader(sr); while (true) { int event = xr.next(); if (event == XMLStreamConstants.END_DOCUMENT) { @@ -244,27 +243,28 @@ public final class XmlStream { @SuppressWarnings("unchecked") public static void toXML(T t, OutputStream os) { Class clazz = (Class) t.getClass(); - Marshaller marshaller = messageMarshaller.get(clazz); - if (marshaller == null) { - try { - JAXBContext jaxbContext = JAXBContext.newInstance(clazz); - marshaller = jaxbContext.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_ENCODING, Consts.UTF_8.name()); - messageMarshaller.put(clazz, marshaller); - } catch (JAXBException e) { - throw new IllegalArgumentException(e); - } - } + JAXBContext jaxbContext = getJaxbContext(clazz); try { - XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class); + Marshaller marshaller = jaxbContext.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_ENCODING, + Consts.UTF_8.name()); + XmlRootElement rootElement = clazz + .getAnnotation(XmlRootElement.class); if (rootElement == null - || rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) { - marshaller.marshal(new JAXBElement(new QName(ROOT_ELEMENT_XML), clazz, t), os); + || rootElement.name().equals( + XmlRootElement.class.getMethod("name") + .getDefaultValue().toString())) { + marshaller.marshal(new JAXBElement(new QName( + ROOT_ELEMENT_XML), clazz, t), os); } else { marshaller.marshal(t, os); } - } catch (Exception e) { - throw new IllegalArgumentException(e); + } catch (JAXBException ex) { + throw new RuntimeException("Could not marshal class [" + clazz + + "]: " + ex.getMessage(), ex); + } catch (NoSuchMethodException ex) { + throw new RuntimeException("Could not marshaller class [" + clazz + + "]: " + ex.getMessage(), ex); } finally { if (os != null) { try { @@ -275,4 +275,19 @@ public final class XmlStream { } } } + + private static JAXBContext getJaxbContext(Class clazz) { + JAXBContext jaxbContext = jaxbContexts.get(clazz); + if (jaxbContext == null) { + try { + jaxbContext = JAXBContext.newInstance(clazz); + jaxbContexts.putIfAbsent(clazz, jaxbContext); + } catch (JAXBException ex) { + throw new RuntimeException( + "Could not instantiate JAXBContext for class [" + clazz + + "]: " + ex.getMessage(), ex); + } + } + return jaxbContext; + } }