Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExternalizableUtil should not ignore provided ClassLoader #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ <h1>
<li>[<a href='https://github.com/igniterealtime/openfire-hazelcast-plugin/issues/71'>Issue #71</a>] - Send member joined notification across the cluster on clustering start</li>
<li>[<a href='https://github.com/igniterealtime/openfire-hazelcast-plugin/issues/74'>Issue #74</a>] - Warn against usage of plugin-provided classes in Hazelcast</li>
<li>[<a href='https://github.com/igniterealtime/openfire-hazelcast-plugin/issues/76'>Issue #76</a>] - Finish leftCluster handling before invoking joinCluster handlers</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>2.5.1</b> -- September 29, 2021.</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2004-2009 Jive Software. All rights reserved.
* Copyright (C) 2004-2009 Jive Software, 2020-2021 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,10 @@

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

import com.hazelcast.core.HazelcastInstance;
import org.jivesoftware.util.cache.ExternalizableUtilStrategy;

import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
Expand All @@ -33,10 +37,6 @@
import java.util.Map;
import java.util.Set;

import org.jivesoftware.util.cache.ExternalizableUtilStrategy;

import com.hazelcast.core.HazelcastInstance;

/**
* Serialization strategy that uses Hazelcast as its underlying mechanism.
*
Expand Down Expand Up @@ -251,7 +251,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 @@ -268,7 +268,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 @@ -309,7 +309,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 @@ -326,7 +326,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 @@ -388,6 +388,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 @@ -413,7 +417,7 @@ public static Object readObject(DataInput in) throws IOException {
int len = in.readInt();
byte[] buf = new byte[len];
in.readFully(buf);
ObjectInputStream oin = newObjectInputStream(new ByteArrayInputStream(buf));
ObjectInputStream oin = newObjectInputStream(classLoader, new ByteArrayInputStream(buf));
try {
return oin.readObject();
} catch (ClassNotFoundException e) {
Expand All @@ -425,7 +429,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 @@ -439,7 +452,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