Isolate the context class loader if function appears to need it

When a function is created using an isolated class loader it might
want to use that class loader again for its invocations, and a lot
of tools (Spring etc.) use the context class loader for that kind
of thing if they don't have an explicit default value. So we
set the context class loader before, and unset it after, the
function invocation using a convenience wrapper.
This commit is contained in:
Dave Syer
2017-11-25 05:25:29 +09:00
parent e1bce8c292
commit e824fbf6cb
5 changed files with 266 additions and 12 deletions

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.core;
import java.util.function.Consumer;
import org.springframework.util.ClassUtils;
/**
* @author Dave Syer
*
*/
public class IsolatedConsumer<T> implements Consumer<T> {
private final Consumer<T> consumer;
public IsolatedConsumer(Consumer<T> consumer) {
this.consumer = consumer;
}
@Override
public void accept(T item) {
ClassLoader context = ClassUtils
.overrideThreadContextClassLoader(consumer.getClass().getClassLoader());
try {
consumer.accept(item);
}
finally {
ClassUtils.overrideThreadContextClassLoader(context);
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.core;
import java.util.function.Function;
import org.springframework.util.ClassUtils;
/**
* @author Dave Syer
*
*/
public class IsolatedFunction<S, T> implements Function<S, T> {
private final Function<S, T> function;
public IsolatedFunction(Function<S, T> function) {
this.function = function;
}
@Override
public T apply(S item) {
ClassLoader context = ClassUtils
.overrideThreadContextClassLoader(function.getClass().getClassLoader());
try {
return function.apply(item);
}
finally {
ClassUtils.overrideThreadContextClassLoader(context);
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.core;
import java.util.function.Supplier;
import org.springframework.util.ClassUtils;
/**
* @author Dave Syer
*
*/
public class IsolatedSupplier<T> implements Supplier<T> {
private final Supplier<T> supplier;
public IsolatedSupplier(Supplier<T> supplier) {
this.supplier = supplier;
}
@Override
public T get() {
ClassLoader context = ClassUtils
.overrideThreadContextClassLoader(supplier.getClass().getClassLoader());
try {
return supplier.get();
}
finally {
ClassUtils.overrideThreadContextClassLoader(context);
}
}
}