Skip to content

Commit

Permalink
ExternalizableUtil should not ignore provided ClassLoader
Browse files Browse the repository at this point in the history
When a ClassLoader instance is provided (when reading data), this instance should actually be used.

fixes #81
  • Loading branch information
guusdk committed Nov 5, 2024
1 parent e87bfe2 commit b8dfced
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ <h1>
<li>[<a href='https://github.com/igniterealtime/openfire-hazelcast-plugin/issues/105'>Issue #105</a>] - Maximum cache size (in bytes) incorrectly reported</li>
<li>[<a href='https://github.com/igniterealtime/openfire-hazelcast-plugin/issues/103'>Issue #103</a>] - Fix Cluster initialization race condition</li>
<li>[<a href='https://github.com/igniterealtime/openfire-hazelcast-plugin/issues/102'>Issue #102</a>] - Remove unused code in ClusterListener</li>
<li>[<a href='https://github.com/igniterealtime/openfire-hazelcast-plugin/issues/81'>Issue #81</a>] - ClusterExternalizableUtil should not ignore provided ClassLoader instances</li>
</ul>

<p><b>3.0.0</b> -- September 12, 2024</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2004-2009 Jive Software, 2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2004-2009 Jive Software, 2020-2024 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.jivesoftware.openfire.plugin.util.cache;

import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
Expand Down Expand Up @@ -266,7 +267,7 @@ public void writeSerializableCollection(DataOutput out, Collection<? extends Ser
* @return the number of elements added to the collection.
*/
public int readExternalizableCollection(DataInput in, Collection<? extends Externalizable> value, ClassLoader loader) throws IOException {
Collection<Externalizable> result = (Collection<Externalizable>) readObject(in);
Collection<Externalizable> result = (Collection<Externalizable>) readObject(loader, in);
if (result == null) return 0;
((Collection<Externalizable>)value).addAll(result);
return result.size();
Expand All @@ -283,7 +284,7 @@ public int readExternalizableCollection(DataInput in, Collection<? extends Exter
* @return the number of elements added to the collection.
*/
public int readSerializableCollection(DataInput in, Collection<? extends Serializable> value, ClassLoader loader) throws IOException {
Collection<Serializable> result = (Collection<Serializable>) readObject(in);
Collection<Serializable> result = (Collection<Serializable>) readObject(loader, in);
if (result == null) return 0;
((Collection<Serializable>)value).addAll(result);
return result.size();
Expand Down Expand Up @@ -324,7 +325,7 @@ public void writeSerializableMap(DataOutput out, Map<? extends Serializable, ? e
* @return the number of elements added to the collection.
*/
public int readExternalizableMap(DataInput in, Map<String, ? extends Externalizable> map, ClassLoader loader) throws IOException {
Map<String, Externalizable> result = (Map<String, Externalizable>) readObject(in);
Map<String, Externalizable> result = (Map<String, Externalizable>) readObject(loader, in);
if (result == null) return 0;
((Map<String, Externalizable>)map).putAll(result);
return result.size();
Expand All @@ -341,7 +342,7 @@ public int readExternalizableMap(DataInput in, Map<String, ? extends Externaliza
* @return the number of elements added to the collection.
*/
public int readSerializableMap(DataInput in, Map<? extends Serializable, ? extends Serializable> map, ClassLoader loader) throws IOException {
Map<String, Serializable> result = (Map<String, Serializable>) readObject(in);
Map<String, Serializable> result = (Map<String, Serializable>) readObject(loader, in);
if (result == null) return 0;
((Map<String, Serializable>)map).putAll(result);
return result.size();
Expand Down Expand Up @@ -403,6 +404,10 @@ public static void writeObject(DataOutput out, Object obj) throws IOException {
}

public static Object readObject(DataInput in) throws IOException {
return readObject(null, in);
}

public static Object readObject(ClassLoader classLoader, DataInput in) throws IOException {
byte type = in.readByte();
if (type == 0) {
return null;
Expand All @@ -428,7 +433,7 @@ public static Object readObject(DataInput in) throws IOException {
int len = in.readInt();
byte[] buf = new byte[len];
in.readFully(buf);
try (ObjectInputStream oin = newObjectInputStream(new ByteArrayInputStream(buf))) {
try (ObjectInputStream oin = newObjectInputStream(classLoader, new ByteArrayInputStream(buf))) {
return oin.readObject();
} catch (ClassNotFoundException e) {
throw new IOException(e);
Expand All @@ -437,7 +442,16 @@ public static Object readObject(DataInput in) throws IOException {
throw new IOException("Unknown object type=" + type);
}
}


public static ObjectInputStream newObjectInputStream(@Nullable final ClassLoader classLoader, final InputStream in) throws IOException {
return new ObjectInputStream(in) {
@Override
protected Class<?> resolveClass(final ObjectStreamClass desc) throws ClassNotFoundException {
return loadClass(classLoader, desc.getName());
}
};
}

public static ObjectInputStream newObjectInputStream(final InputStream in) throws IOException {
return new ObjectInputStream(in) {
@Override
Expand All @@ -451,7 +465,7 @@ public static Class<?> loadClass(final String className) throws ClassNotFoundExc
return loadClass(null, className);
}

public static Class<?> loadClass(final ClassLoader classLoader, final String className) throws ClassNotFoundException {
public static Class<?> loadClass(@Nullable final ClassLoader classLoader, final String className) throws ClassNotFoundException {
if (className == null) {
throw new IllegalArgumentException("ClassName cannot be null!");
}
Expand Down

0 comments on commit b8dfced

Please sign in to comment.