Skip to content

Commit

Permalink
constant FB
Browse files Browse the repository at this point in the history
more serialization
  • Loading branch information
elrodro83 committed Dec 11, 2024
1 parent 9102137 commit caf9295
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected void doConfigure(MuleContext muleContext) throws Exception {

registerObject(OBJECT_SECURITY_MANAGER, new DefaultMuleSecurityManager(), muleContext);
registerObject(OBJECT_MULE_STREAM_CLOSER_SERVICE, new DefaultStreamCloserService(), muleContext);
registerObject(DEFAULT_OBJECT_SERIALIZER_NAME, new JavaObjectSerializer(), muleContext);
registerObject(DEFAULT_OBJECT_SERIALIZER_NAME, new JavaObjectSerializer(muleContext.getExecutionClassLoader()), muleContext);

final ContributedErrorTypeRepository contributedErrorTypeRepository = new ContributedErrorTypeRepository();
registerObject(ErrorTypeRepository.class.getName(), contributedErrorTypeRepository, muleContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ private Properties getDeploymentProperties() {

private void getObjectSerializer(DefaultMuleContext muleContext) {
if (objectSerializer == null) {
objectSerializer = new JavaObjectSerializer();
}

if (objectSerializer instanceof MuleContextAware) {
((MuleContextAware) objectSerializer).setMuleContext(muleContext);
objectSerializer = new JavaObjectSerializer(muleContext.getExecutionClassLoader());
}

muleContext.setObjectSerializer(objectSerializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@
import static org.mule.runtime.api.el.BindingContextUtils.addEventBindings;

import org.mule.runtime.api.el.BindingContext;
import org.mule.runtime.api.exception.MuleException;
import org.mule.runtime.api.message.Error;
import org.mule.runtime.api.message.ItemSequenceInfo;
import org.mule.runtime.api.message.Message;
import org.mule.runtime.api.metadata.TypedValue;
import org.mule.runtime.api.security.Authentication;
import org.mule.runtime.api.security.SecurityContext;
import org.mule.runtime.api.util.LazyValue;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.context.notification.FlowCallStack;
import org.mule.runtime.core.api.message.GroupCorrelation;
import org.mule.runtime.core.internal.message.EventInternalContext;
import org.mule.runtime.core.internal.store.DeserializationPostInitialisable;
import org.mule.runtime.core.privileged.event.BaseEventContext;
import org.mule.runtime.core.privileged.event.context.FlowProcessMediatorContext;

import java.util.Map;
import java.util.Optional;

abstract class BaseEventDecorator implements InternalEvent, DeserializationPostInitialisable {
abstract class BaseEventDecorator implements InternalEvent {

private static final long serialVersionUID = 2264829044803742047L;

Expand Down Expand Up @@ -181,18 +178,6 @@ public <T extends EventInternalContext> void setOperationPolicyContext(EventInte
event.setOperationPolicyContext(context);
}

/**
* Invoked after deserialization. This is called when the marker interface {@link DeserializationPostInitialisable} is used.
* This will get invoked after the object has been deserialized passing in the current MuleContext.
*
* @param muleContext the current muleContext instance
* @throws MuleException if there is an error initializing
*/
@SuppressWarnings({"unused"})
private void initAfterDeserialisation(MuleContext muleContext) throws MuleException {
bindingContextBuilder = new LazyValue<>(() -> addEventBindings(this, NULL_BINDING_CONTEXT));
}

@Override
public String toString() {
return this.getClass().getSimpleName() + "->" + event.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,22 @@

import static org.mule.runtime.core.api.util.IOUtils.closeQuietly;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

import org.mule.runtime.api.serialization.SerializationException;
import org.mule.runtime.api.serialization.SerializationProtocol;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.internal.store.DeserializationPostInitialisable;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.inject.Inject;

/**
* Base class for implementations of {@link SerializationProtocol} This class implements all the base behavioral contract allowing
* its extensions to only care about the actual serialization/deserialization part.
*/
public abstract class AbstractSerializationProtocol implements SerializationProtocol {

protected MuleContext muleContext;

/**
* Serializes the given object. Should not care about error handling
*
Expand Down Expand Up @@ -87,7 +80,7 @@ public void serialize(Object object, OutputStream out) throws SerializationExcep
*/
@Override
public <T> T deserialize(byte[] bytes) throws SerializationException {
return deserialize(bytes, muleContext.getExecutionClassLoader());
return deserialize(bytes, getExecutionClassLoader());
}

/**
Expand All @@ -105,7 +98,7 @@ public <T> T deserialize(byte[] bytes, ClassLoader classLoader) throws Serializa
@Override
@SuppressWarnings("unchecked")
public <T> T deserialize(InputStream inputStream) throws SerializationException {
return deserialize(inputStream, muleContext.getExecutionClassLoader());
return deserialize(inputStream, getExecutionClassLoader());
}

/**
Expand All @@ -116,31 +109,14 @@ public <T> T deserialize(InputStream inputStream, ClassLoader classLoader) throw
requireNonNull(inputStream, "Cannot deserialize a null stream");
requireNonNull(classLoader, "Cannot deserialize with a null classloader");
try {
return (T) postInitialize(doDeserialize(inputStream, classLoader));
return (T) doDeserialize(inputStream, classLoader);
} catch (Exception e) {
throw new SerializationException("Could not deserialize object", e);
} finally {
closeQuietly(inputStream);
}
}

protected <T> T postInitialize(T object) throws SerializationException {
if (object instanceof DeserializationPostInitialisable) {
try {
DeserializationPostInitialisable.Implementation.init(object, muleContext);
} catch (Exception e) {
throw new SerializationException(format("Could not initialize instance of %s after deserialization",
object.getClass().getName()),
e);
}
}

return object;
}

@Inject
public final void setMuleContext(MuleContext context) {
muleContext = context;
}
protected abstract ClassLoader getExecutionClassLoader();

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
*/
public class JavaExternalSerializerProtocol extends AbstractSerializationProtocol {

private final ClassLoader executionClassLoader;

public JavaExternalSerializerProtocol(ClassLoader executionClassLoader) {
this.executionClassLoader = requireNonNull(executionClassLoader);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -71,13 +77,7 @@ protected <T> T doDeserialize(InputStream inputStream, ClassLoader classLoader)
requireNonNull(inputStream, "Cannot deserialize a null stream");
requireNonNull(classLoader, "Cannot deserialize with a null classloader");

return (T) SerializationUtils.deserialize(inputStream, classLoader, muleContext);
}

@Override
protected <T> T postInitialize(T object) {
// does nothing since SerializationUtils already does this on its own
return object;
return (T) SerializationUtils.deserialize(inputStream, classLoader);
}

private void validateForSerialization(Object object) {
Expand All @@ -86,4 +86,9 @@ private void validateForSerialization(Object object) {
object.getClass().getName()));
}
}

@Override
protected ClassLoader getExecutionClassLoader() {
return executionClassLoader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
*/
package org.mule.runtime.core.internal.serialization;

import static org.mule.runtime.api.util.Preconditions.checkArgument;

import org.mule.runtime.api.serialization.ObjectSerializer;
import org.mule.runtime.api.serialization.SerializationProtocol;
import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.api.context.MuleContextAware;

/**
* Serializes objects using the default Java serialization mechanism provided by writeObject and readObject methods.
*/
public class JavaObjectSerializer implements ObjectSerializer, MuleContextAware {
public class JavaObjectSerializer implements ObjectSerializer {

private volatile JavaExternalSerializerProtocol javaSerializerProtocol;

public JavaObjectSerializer(ClassLoader executionClassLoader) {
checkArgument(executionClassLoader != null, "executionClassLoader cannot be null");

private volatile JavaExternalSerializerProtocol javaSerializerProtocol = new JavaExternalSerializerProtocol();
javaSerializerProtocol = new JavaExternalSerializerProtocol(executionClassLoader);
}

@Override
public SerializationProtocol getInternalProtocol() {
Expand All @@ -28,8 +34,4 @@ public SerializationProtocol getExternalProtocol() {
return javaSerializerProtocol;
}

@Override
public void setMuleContext(MuleContext context) {
javaSerializerProtocol.setMuleContext(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
*/
package org.mule.runtime.core.internal.serialization;

import org.mule.runtime.core.api.MuleContext;
import org.mule.runtime.core.internal.store.DeserializationPostInitialisable;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -19,20 +16,6 @@

public class SerializationUtils {

public static Object deserialize(InputStream inputStream, MuleContext muleContext) {
if (muleContext == null) {
throw new IllegalArgumentException("The MuleContext must not be null");
}
return deserialize(inputStream, muleContext.getExecutionClassLoader(), muleContext);
}

public static Object deserialize(byte[] objectData, MuleContext muleContext) {
if (muleContext == null) {
throw new IllegalArgumentException("The MuleContext must not be null");
}
return deserialize(objectData, muleContext.getExecutionClassLoader(), muleContext);
}

/**
* <p>
* Deserializes an <code>Object</code> from the specified stream.
Expand All @@ -54,7 +37,7 @@ public static Object deserialize(byte[] objectData, MuleContext muleContext) {
* @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
* @throws org.apache.commons.lang3.SerializationException (runtime) if the serialization fails
*/
public static Object deserialize(InputStream inputStream, ClassLoader cl, MuleContext muleContext) {
public static Object deserialize(InputStream inputStream, ClassLoader cl) {
if (inputStream == null) {
throw new IllegalArgumentException("The InputStream must not be null");
}
Expand All @@ -66,9 +49,6 @@ public static Object deserialize(InputStream inputStream, ClassLoader cl, MuleCo
// stream closed in the finally
in = new ClassLoaderObjectInputStream(cl, inputStream);
Object obj = in.readObject();
if (obj instanceof DeserializationPostInitialisable) {
DeserializationPostInitialisable.Implementation.init(obj, muleContext);
}
return obj;
} catch (ClassNotFoundException ex) {
throw new SerializationException(ex);
Expand Down Expand Up @@ -98,11 +78,11 @@ public static Object deserialize(InputStream inputStream, ClassLoader cl, MuleCo
* @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
private static Object deserialize(byte[] objectData, ClassLoader cl, MuleContext muleContext) {
private static Object deserialize(byte[] objectData, ClassLoader cl) {
if (objectData == null) {
throw new IllegalArgumentException("The byte[] must not be null");
}
ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
return deserialize(bais, cl, muleContext);
return deserialize(bais, cl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
*/
package org.mule.runtime.core.internal.util.store;

import static org.mule.runtime.api.store.ObjectStoreManager.BASE_PERSISTENT_OBJECT_STORE_KEY;
import static org.mule.runtime.core.api.config.i18n.CoreMessages.propertyHasInvalidValue;

import static java.lang.System.currentTimeMillis;
import static java.util.Comparator.comparing;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.stream.Collectors.toMap;
import static org.mule.runtime.api.store.ObjectStoreManager.BASE_PERSISTENT_OBJECT_STORE_KEY;
import static org.mule.runtime.core.api.config.i18n.CoreMessages.propertyHasInvalidValue;

import org.mule.runtime.api.exception.DefaultMuleException;
import org.mule.runtime.api.exception.MuleException;
import org.mule.runtime.api.lifecycle.Disposable;
import org.mule.runtime.api.lifecycle.Initialisable;
import org.mule.runtime.api.lifecycle.InitialisationException;
Expand All @@ -29,7 +28,6 @@
import org.mule.runtime.core.api.context.MuleContextAware;
import org.mule.runtime.core.api.util.UUID;
import org.mule.runtime.core.internal.context.MuleContextWithRegistry;
import org.mule.runtime.core.internal.store.DeserializationPostInitialisable;

import java.io.Serializable;
import java.util.List;
Expand Down Expand Up @@ -245,7 +243,7 @@ public void initialise() throws InitialisationException {
}
}

public static class StoredObject<T> implements Serializable, DeserializationPostInitialisable {
public static class StoredObject<T> implements Serializable {

private static final long serialVersionUID = 8656763235928199259L;
final private T item;
Expand All @@ -271,23 +269,6 @@ public String getKey() {
return key;
}

/**
* Invoked after deserialization. This is called when the marker interface {@link DeserializationPostInitialisable} is used.
* This will get invoked after the object has been deserialized passing in the current MuleContext.
*
* @param muleContext the current muleContext instance
* @throws MuleException if there is an error initializing
*/
@SuppressWarnings({"unused"})
private void initAfterDeserialisation(MuleContext muleContext) throws MuleException {
if (item instanceof DeserializationPostInitialisable) {
try {
DeserializationPostInitialisable.Implementation.init(item, muleContext);
} catch (Exception e) {
throw new DefaultMuleException(e);
}
}
}
}

}
Loading

0 comments on commit caf9295

Please sign in to comment.