Skip to content

Commit d99717c

Browse files
committed
DefaultDeserializer and DeserializingConverter allow for specifying a ClassLoader
Issue: SPR-13409
1 parent d4a23b8 commit d99717c

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

spring-core/src/main/java/org/springframework/core/serializer/DefaultDeserializer.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,23 +20,50 @@
2020
import java.io.InputStream;
2121
import java.io.ObjectInputStream;
2222

23+
import org.springframework.core.ConfigurableObjectInputStream;
2324
import org.springframework.core.NestedIOException;
2425

2526
/**
26-
* Deserializer that reads an input stream using Java Serialization.
27+
* A default {@link Deserializer} implementation that reads an input stream
28+
* using Java serialization.
2729
*
2830
* @author Gary Russell
2931
* @author Mark Fisher
32+
* @author Juergen Hoeller
3033
* @since 3.0.5
34+
* @see ObjectInputStream
3135
*/
3236
public class DefaultDeserializer implements Deserializer<Object> {
3337

38+
private final ClassLoader classLoader;
39+
40+
41+
/**
42+
* Create a {@code DefaultDeserializer} with default {@link ObjectInputStream}
43+
* configuration, using the "latest user-defined ClassLoader".
44+
*/
45+
public DefaultDeserializer() {
46+
this.classLoader = null;
47+
}
48+
49+
/**
50+
* Create a {@code DefaultDeserializer} for using an {@link ObjectInputStream}
51+
* with the given {@code ClassLoader}.
52+
* @since 4.2.1
53+
* @see ConfigurableObjectInputStream#ConfigurableObjectInputStream(InputStream, ClassLoader)
54+
*/
55+
public DefaultDeserializer(ClassLoader classLoader) {
56+
this.classLoader = classLoader;
57+
}
58+
59+
3460
/**
3561
* Reads the input stream and deserializes into an object.
62+
* @see ObjectInputStream#readObject()
3663
*/
3764
@Override
3865
public Object deserialize(InputStream inputStream) throws IOException {
39-
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
66+
ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, this.classLoader);
4067
try {
4168
return objectInputStream.readObject();
4269
}

spring-core/src/main/java/org/springframework/core/serializer/support/DeserializingConverter.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,11 +24,13 @@
2424
import org.springframework.util.Assert;
2525

2626
/**
27-
* A {@link Converter} that delegates to a {@link org.springframework.core.serializer.Deserializer}
27+
* A {@link Converter} that delegates to a
28+
* {@link org.springframework.core.serializer.Deserializer}
2829
* to convert data in a byte array to an object.
2930
*
3031
* @author Gary Russell
3132
* @author Mark Fisher
33+
* @author Juergen Hoeller
3234
* @since 3.0.5
3335
*/
3436
public class DeserializingConverter implements Converter<byte[], Object> {
@@ -37,14 +39,26 @@ public class DeserializingConverter implements Converter<byte[], Object> {
3739

3840

3941
/**
40-
* Create a default DeserializingConverter that uses standard Java deserialization.
42+
* Create a {@code DeserializingConverter} with default {@link java.io.ObjectInputStream}
43+
* configuration, using the "latest user-defined ClassLoader".
44+
* @see DefaultDeserializer#DefaultDeserializer()
4145
*/
4246
public DeserializingConverter() {
4347
this.deserializer = new DefaultDeserializer();
4448
}
4549

4650
/**
47-
* Create a DeserializingConverter that delegates to the provided {@link Deserializer}.
51+
* Create a {@code DeserializingConverter} for using an {@link java.io.ObjectInputStream}
52+
* with the given {@code ClassLoader}.
53+
* @since 4.2.1
54+
* @see DefaultDeserializer#DefaultDeserializer(ClassLoader)
55+
*/
56+
public DeserializingConverter(ClassLoader classLoader) {
57+
this.deserializer = new DefaultDeserializer(classLoader);
58+
}
59+
60+
/**
61+
* Create a {@code DeserializingConverter} that delegates to the provided {@link Deserializer}.
4862
*/
4963
public DeserializingConverter(Deserializer<Object> deserializer) {
5064
Assert.notNull(deserializer, "Deserializer must not be null");

0 commit comments

Comments
 (0)