fixed #20
This commit is contained in:
parent
3d26b8f531
commit
effe886ed4
@ -2,6 +2,8 @@ package com.foxinmy.weixin4j.xml;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -11,6 +13,8 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBElement;
|
import javax.xml.bind.JAXBElement;
|
||||||
@ -30,6 +34,7 @@ import javax.xml.transform.stream.StreamSource;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.foxinmy.weixin4j.model.Consts;
|
import com.foxinmy.weixin4j.model.Consts;
|
||||||
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.util.StringUtil;
|
import com.foxinmy.weixin4j.util.StringUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,12 +49,21 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
|||||||
public final class XmlStream {
|
public final class XmlStream {
|
||||||
private final static String ROOT_ELEMENT_XML = "xml";
|
private final static String ROOT_ELEMENT_XML = "xml";
|
||||||
private final static String XML_VERSION = "1.0";
|
private final static String XML_VERSION = "1.0";
|
||||||
private final static Map<Class<?>, Unmarshaller> messageUnmarshaller;
|
private static ThreadLocal<Map<Class<?>, Unmarshaller>> messageUnmarshaller;
|
||||||
private final static Map<Class<?>, Marshaller> messageMarshaller;
|
private static ThreadLocal<Map<Class<?>, Marshaller>> messageMarshaller;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
messageUnmarshaller = new HashMap<Class<?>, Unmarshaller>();
|
messageUnmarshaller = new ThreadLocal<Map<Class<?>, Unmarshaller>>() {
|
||||||
messageMarshaller = new HashMap<Class<?>, Marshaller>();
|
@Override
|
||||||
|
protected Map<Class<?>, Unmarshaller> initialValue() {
|
||||||
|
return new HashMap<Class<?>, Unmarshaller>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
messageMarshaller = new ThreadLocal<Map<Class<?>, Marshaller>>() {
|
||||||
|
@Override
|
||||||
|
protected Map<Class<?>, Marshaller> initialValue() {
|
||||||
|
return new HashMap<Class<?>, Marshaller>();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,22 +77,26 @@ public final class XmlStream {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
||||||
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
|
Unmarshaller unmarshaller = messageUnmarshaller.get().get(clazz);
|
||||||
if (unmarshaller == null) {
|
if (unmarshaller == null) {
|
||||||
try {
|
try {
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||||
unmarshaller = jaxbContext.createUnmarshaller();
|
unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
messageUnmarshaller.put(clazz, unmarshaller);
|
messageUnmarshaller.get().put(clazz, unmarshaller);
|
||||||
} catch (JAXBException e) {
|
} catch (JAXBException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Source source = new StreamSource(content);
|
Source source = new StreamSource(content);
|
||||||
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
XmlRootElement rootElement = clazz
|
||||||
|
.getAnnotation(XmlRootElement.class);
|
||||||
if (rootElement == null
|
if (rootElement == null
|
||||||
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
|| rootElement.name().equals(
|
||||||
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz);
|
XmlRootElement.class.getMethod("name")
|
||||||
|
.getDefaultValue().toString())) {
|
||||||
|
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source,
|
||||||
|
clazz);
|
||||||
return jaxbElement.getValue();
|
return jaxbElement.getValue();
|
||||||
} else {
|
} else {
|
||||||
return (T) unmarshaller.unmarshal(source);
|
return (T) unmarshaller.unmarshal(source);
|
||||||
@ -106,7 +124,8 @@ public final class XmlStream {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static <T> T fromXML(String content, Class<T> clazz) {
|
public static <T> T fromXML(String content, Class<T> clazz) {
|
||||||
return fromXML(new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
return fromXML(
|
||||||
|
new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,10 +138,12 @@ public final class XmlStream {
|
|||||||
public static String map2xml(Map<String, String> map) {
|
public static String map2xml(Map<String, String> map) {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
try {
|
try {
|
||||||
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
||||||
|
.createXMLStreamWriter(sw);
|
||||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||||
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it.hasNext();) {
|
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it
|
||||||
|
.hasNext();) {
|
||||||
Entry<String, String> entry = it.next();
|
Entry<String, String> entry = it.next();
|
||||||
if (StringUtil.isBlank(entry.getValue())) {
|
if (StringUtil.isBlank(entry.getValue())) {
|
||||||
continue;
|
continue;
|
||||||
@ -156,10 +177,12 @@ public final class XmlStream {
|
|||||||
public static String map2xml(JSONObject json) {
|
public static String map2xml(JSONObject json) {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
try {
|
try {
|
||||||
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
||||||
|
.createXMLStreamWriter(sw);
|
||||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||||
for (Iterator<Entry<String, Object>> it = json.entrySet().iterator(); it.hasNext();) {
|
for (Iterator<Entry<String, Object>> it = json.entrySet()
|
||||||
|
.iterator(); it.hasNext();) {
|
||||||
Entry<String, Object> entry = it.next();
|
Entry<String, Object> entry = it.next();
|
||||||
if (StringUtil.isBlank(json.getString(entry.getKey()))) {
|
if (StringUtil.isBlank(json.getString(entry.getKey()))) {
|
||||||
continue;
|
continue;
|
||||||
@ -194,7 +217,8 @@ public final class XmlStream {
|
|||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
StringReader sr = new StringReader(content);
|
StringReader sr = new StringReader(content);
|
||||||
try {
|
try {
|
||||||
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(sr);
|
XMLStreamReader xr = XMLInputFactory.newInstance()
|
||||||
|
.createXMLStreamReader(sr);
|
||||||
while (true) {
|
while (true) {
|
||||||
int event = xr.next();
|
int event = xr.next();
|
||||||
if (event == XMLStreamConstants.END_DOCUMENT) {
|
if (event == XMLStreamConstants.END_DOCUMENT) {
|
||||||
@ -247,22 +271,27 @@ public final class XmlStream {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> void toXML(T t, OutputStream os) {
|
public static <T> void toXML(T t, OutputStream os) {
|
||||||
Class<T> clazz = (Class<T>) t.getClass();
|
Class<T> clazz = (Class<T>) t.getClass();
|
||||||
Marshaller marshaller = messageMarshaller.get(clazz);
|
Marshaller marshaller = messageMarshaller.get().get(clazz);
|
||||||
if (marshaller == null) {
|
if (marshaller == null) {
|
||||||
try {
|
try {
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||||
marshaller = jaxbContext.createMarshaller();
|
marshaller = jaxbContext.createMarshaller();
|
||||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, Consts.UTF_8.name());
|
marshaller.setProperty(Marshaller.JAXB_ENCODING,
|
||||||
messageMarshaller.put(clazz, marshaller);
|
Consts.UTF_8.name());
|
||||||
|
messageMarshaller.get().put(clazz, marshaller);
|
||||||
} catch (JAXBException e) {
|
} catch (JAXBException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
XmlRootElement rootElement = clazz
|
||||||
|
.getAnnotation(XmlRootElement.class);
|
||||||
if (rootElement == null
|
if (rootElement == null
|
||||||
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
|| rootElement.name().equals(
|
||||||
marshaller.marshal(new JAXBElement<T>(new QName(ROOT_ELEMENT_XML), clazz, t), os);
|
XmlRootElement.class.getMethod("name")
|
||||||
|
.getDefaultValue().toString())) {
|
||||||
|
marshaller.marshal(new JAXBElement<T>(new QName(
|
||||||
|
ROOT_ELEMENT_XML), clazz, t), os);
|
||||||
} else {
|
} else {
|
||||||
marshaller.marshal(t, os);
|
marshaller.marshal(t, os);
|
||||||
}
|
}
|
||||||
@ -278,4 +307,24 @@ public final class XmlStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
executor.submit(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
System.err.println(XmlStream
|
||||||
|
.fromXML(
|
||||||
|
new FileInputStream(
|
||||||
|
new File(
|
||||||
|
"/tmp/weixin4j/token/wx_mp_token_wx4ab8f8de58159a57.xml")),
|
||||||
|
Token.class));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user