下面列出了怎么用org.apache.commons.lang.SerializationException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test(expected = ExecutionException.class)
public void testMultiget_TranscoderExecption() throws ExecutionException, InterruptedException, TimeoutException {
final Transcoder<Serializable> transcoder = new Transcoder<Serializable>() {
@Override
public Serializable decode(final byte[] b) {
throw new SerializationException("QQQQQ YYYYY");
}
@Override
public byte[] encode(final Serializable t) {
return SerializationUtils.serialize(t);
}
};
final MemcachedClient<Object, Serializable> client = createClient(transcoder);
client.setAsync("meh", "its here").get();
client.getBulkAsync(Lists.newArrayList("meh", "bah")).get(1, TimeUnit.MINUTES);
}
@SuppressWarnings("unchecked")
public static <T extends Message> T getDefaultInstance(final String className) {
final MethodHandle handle = DEFAULT_INSTANCE_METHODS_4J.get(className);
if (handle == null) {
throw new MessageClassNotFoundException(className + " not found");
}
try {
return (T) handle.invoke();
} catch (Throwable t) {
throw new SerializationException(t);
}
}
@SuppressWarnings("unchecked")
public static <T extends Message> T newMessageByJavaClassName(final String className, final byte[] bs) {
final MethodHandle handle = PARSE_METHODS_4J.get(className);
if (handle == null) {
throw new MessageClassNotFoundException(className + " not found");
}
try {
return (T) handle.invoke(bs);
} catch (Throwable t) {
throw new SerializationException(t);
}
}
@SuppressWarnings("unchecked")
public static <T extends Message> T newMessageByProtoClassName(final String className, final byte[] bs) {
final MethodHandle handle = PARSE_METHODS_4PROTO.get(className);
if (handle == null) {
throw new MessageClassNotFoundException(className + " not found");
}
try {
return (T) handle.invoke(bs);
} catch (Throwable t) {
throw new SerializationException(t);
}
}
@Override
@SuppressWarnings("unchecked")
public T decode(final byte[] b) {
final Template<?> template = messagePack.lookup(valueType);
try {
final Value value = messagePack.read(b);
return (T) template.read(new Converter(messagePack, value), null);
} catch (final IOException e) {
throw new SerializationException("Failed to decode to type " + valueType.getTypeName(), e);
}
}
@Override
public byte[] encode(final T t) {
try {
return messagePack.write(t);
} catch (final IOException e) {
throw new SerializationException("Failed to encode input " + t.getClass().getSimpleName() + " to type " + valueType.getTypeName(), e);
}
}
@Override
public T decode(final byte[] b) {
try {
return objectMapper.readValue(b, valueType);
} catch (final IOException e) {
throw new SerializationException("Failed to decode to type " + valueType.getSimpleName(), e);
}
}
@Override
public byte[] encode(final T t) {
try {
return objectMapper.writeValueAsBytes(t);
} catch (final JsonProcessingException e) {
throw new SerializationException("Failed to encode input " + t.getClass().getSimpleName() + " to type " + valueType.getSimpleName(), e);
}
}
private Article deserializeMoreover(String serialized) {
try {
Unmarshaller unmarshaller = articleContext.createUnmarshaller();
return (Article) unmarshaller.unmarshal(new StringReader(serialized));
} catch (JAXBException ex) {
throw new SerializationException("Unable to deserialize Moreover data", ex);
}
}
private ArticlesResponse deserializeMoreoverResponse(String serialized) {
try {
Unmarshaller unmarshaller = articlesContext.createUnmarshaller();
return ((JAXBElement<ArticlesResponse>) unmarshaller.unmarshal(new StringReader(serialized))).getValue();
} catch (JAXBException ex) {
throw new SerializationException("Unable to deserialize Moreover data", ex);
}
}
@Override
public Optional<Throwable> fromBytes(final byte[] bytes) {
try {
if (bytes != null && bytes.length > 0) {
return Optional.of((Throwable) SerializationUtils.deserialize(bytes));
}
} catch (final SerializationException | IllegalArgumentException e) {
LOG.log(Level.WARNING, "Unable to deserialize a Throwable.", e);
}
return Optional.empty();
}
/**
* Constructs a JAX-WS deserializer for the specified class.
*
* @param clazz Class to deserialize.
* @throws SerializationException if unable to construct the deserializer.
*/
public JaxBDeserializer(Class<T> clazz) {
this.clazz = clazz;
try {
this.jaxbContext = JAXBContext.newInstance(clazz);
} catch (JAXBException e) {
throw new SerializationException(
String.format("Could not construct deserializer for class: %s.", clazz), e);
}
}
/**
* Deserializes the object.
*
* @throws SerializationException if we cannot deserialize the object.
*/
public T deserialize(Source source) {
try {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz);
return jaxbElement.getValue();
} catch (JAXBException e) {
throw new SerializationException(
String.format("Could not deserialize %s object from source %s.", clazz, source), e);
}
}
/**
* Constructs a JAX-WS serializer for the specified class.
*
* @param clazz Class to serialize.
* @param qname A QName representing the local name for the class - this will be used as the root
* tag name.
* @throws SerializationException if unable to construct the serializer.
*/
public JaxBSerializer(Class<T> clazz, QName qname) {
this.clazz = clazz;
this.qname = qname;
try {
this.jaxbContext = JAXBContext.newInstance(clazz);
} catch (JAXBException e) {
throw new SerializationException(
String.format(
"Could not construct a serializer for class %s and QName %s.", clazz, qname),
e);
}
}
/**
* Serializes the object with the option to include or exclude the XML declaration.
*
* @throws SerializationException if we cannot serialize the object.
*/
public String serialize(T object, boolean includeXmlDeclaration) {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.valueOf(!includeXmlDeclaration));
JAXBElement<T> element =
new JAXBElement<T>(qname, clazz, object);
StringWriter stringWriter = new StringWriter();
marshaller.marshal(element, stringWriter);
return stringWriter.toString();
} catch (JAXBException e) {
throw new SerializationException(String.format("Could not serialize object: %s.", object), e);
}
}