From 204a59472552b5327bfeddb45152391527c3eff4 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 28 Feb 2017 13:59:00 +0300 Subject: [PATCH] TomcatSessionManager can't be used in Tomcat if it already has been deployed in Application. #784 --- .../tomcat/RedissonSessionManager.java | 4 +- .../tomcat/RedissonSessionManager.java | 4 +- .../tomcat/RedissonSessionManager.java | 4 +- .../main/java/org/redisson/config/Config.java | 21 +++++++++- .../org/redisson/config/ConfigSupport.java | 38 +++++++++++++++---- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 6399320c0..693f0350b 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -143,11 +143,11 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { public void start() throws LifecycleException { Config config = null; try { - config = Config.fromJSON(new File(configPath)); + config = Config.fromJSON(new File(configPath), getClass().getClassLoader()); } catch (IOException e) { // trying next format try { - config = Config.fromYAML(new File(configPath)); + config = Config.fromYAML(new File(configPath), getClass().getClassLoader()); } catch (IOException e1) { log.error("Can't parse json config " + configPath, e); throw new LifecycleException("Can't parse yaml config " + configPath, e1); diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 87d41f81a..e17e7ae57 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -146,11 +146,11 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { super.startInternal(); Config config = null; try { - config = Config.fromJSON(new File(configPath)); + config = Config.fromJSON(new File(configPath), getClass().getClassLoader()); } catch (IOException e) { // trying next format try { - config = Config.fromYAML(new File(configPath)); + config = Config.fromYAML(new File(configPath), getClass().getClassLoader()); } catch (IOException e1) { log.error("Can't parse json config " + configPath, e); throw new LifecycleException("Can't parse yaml config " + configPath, e1); diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 87d41f81a..e17e7ae57 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -146,11 +146,11 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { super.startInternal(); Config config = null; try { - config = Config.fromJSON(new File(configPath)); + config = Config.fromJSON(new File(configPath), getClass().getClassLoader()); } catch (IOException e) { // trying next format try { - config = Config.fromYAML(new File(configPath)); + config = Config.fromYAML(new File(configPath), getClass().getClassLoader()); } catch (IOException e1) { log.error("Can't parse json config " + configPath, e); throw new LifecycleException("Can't parse yaml config " + configPath, e1); diff --git a/redisson/src/main/java/org/redisson/config/Config.java b/redisson/src/main/java/org/redisson/config/Config.java index 233778dc0..b15d9a794 100644 --- a/redisson/src/main/java/org/redisson/config/Config.java +++ b/redisson/src/main/java/org/redisson/config/Config.java @@ -556,12 +556,24 @@ public class Config { * Read config object stored in JSON format from File * * @param file object + * @param classLoader class loader * @return config * @throws IOException error */ - public static Config fromJSON(File file) throws IOException { + public static Config fromJSON(File file, ClassLoader classLoader) throws IOException { ConfigSupport support = new ConfigSupport(); - return support.fromJSON(file, Config.class); + return support.fromJSON(file, Config.class, classLoader); + } + + /** + * Read config object stored in JSON format from File + * + * @param file object + * @return config + * @throws IOException error + */ + public static Config fromJSON(File file) throws IOException { + return fromJSON(file); } /** @@ -634,6 +646,11 @@ public class Config { ConfigSupport support = new ConfigSupport(); return support.fromYAML(file, Config.class); } + + public static Config fromYAML(File file, ClassLoader classLoader) throws IOException { + ConfigSupport support = new ConfigSupport(); + return support.fromYAML(file, Config.class, classLoader); + } /** * Read config object stored in YAML format from URL diff --git a/redisson/src/main/java/org/redisson/config/ConfigSupport.java b/redisson/src/main/java/org/redisson/config/ConfigSupport.java index 6535cf813..7c55bb5e4 100644 --- a/redisson/src/main/java/org/redisson/config/ConfigSupport.java +++ b/redisson/src/main/java/org/redisson/config/ConfigSupport.java @@ -26,13 +26,16 @@ import java.util.List; import org.redisson.api.RedissonNodeInitializer; import org.redisson.client.codec.Codec; import org.redisson.cluster.ClusterConnectionManager; +import org.redisson.codec.CodecProvider; import org.redisson.connection.ConnectionManager; import org.redisson.connection.ElasticacheConnectionManager; -import org.redisson.connection.ReplicatedConnectionManager; import org.redisson.connection.MasterSlaveConnectionManager; +import org.redisson.connection.ReplicatedConnectionManager; import org.redisson.connection.SentinelConnectionManager; import org.redisson.connection.SingleConnectionManager; import org.redisson.connection.balancer.LoadBalancer; +import org.redisson.liveobject.provider.ResolverProvider; +import org.redisson.misc.URLBuilder; import com.fasterxml.jackson.annotation.JsonFilter; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -45,10 +48,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; +import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import org.redisson.codec.CodecProvider; -import org.redisson.liveobject.provider.ResolverProvider; -import org.redisson.misc.URLBuilder; /** * @@ -118,8 +119,8 @@ public class ConfigSupport { } - private final ObjectMapper jsonMapper = createMapper(null); - private final ObjectMapper yamlMapper = createMapper(new YAMLFactory()); + private ObjectMapper jsonMapper = createMapper(null, null); + private ObjectMapper yamlMapper = createMapper(new YAMLFactory(), null); public T fromJSON(String content, Class configType) throws IOException { URLBuilder.replaceURLFactory(); @@ -131,8 +132,13 @@ public class ConfigSupport { } public T fromJSON(File file, Class configType) throws IOException { + return fromJSON(file, configType, null); + } + + public T fromJSON(File file, Class configType, ClassLoader classLoader) throws IOException { URLBuilder.replaceURLFactory(); try { + jsonMapper = createMapper(null, classLoader); return jsonMapper.readValue(file, configType); } finally { URLBuilder.restoreURLFactory(); @@ -192,6 +198,17 @@ public class ConfigSupport { URLBuilder.restoreURLFactory(); } } + + public T fromYAML(File file, Class configType, ClassLoader classLoader) throws IOException { + URLBuilder.replaceURLFactory(); + try { + yamlMapper = createMapper(new YAMLFactory(), classLoader); + return yamlMapper.readValue(file, configType); + } finally { + URLBuilder.restoreURLFactory(); + } + } + public T fromYAML(URL url, Class configType) throws IOException { URLBuilder.replaceURLFactory(); @@ -271,7 +288,7 @@ public class ConfigSupport { } } - private ObjectMapper createMapper(JsonFactory mapping) { + private ObjectMapper createMapper(JsonFactory mapping, ClassLoader classLoader) { ObjectMapper mapper = new ObjectMapper(mapping); mapper.addMixIn(MasterSlaveServersConfig.class, MasterSlaveServersConfigMixIn.class); mapper.addMixIn(SingleServerConfig.class, SingleSeverConfigMixIn.class); @@ -285,6 +302,13 @@ public class ConfigSupport { .addFilter("classFilter", SimpleBeanPropertyFilter.filterOutAllExcept()); mapper.setFilterProvider(filterProvider); mapper.setSerializationInclusion(Include.NON_NULL); + + if (classLoader != null) { + TypeFactory tf = TypeFactory.defaultInstance() + .withClassLoader(classLoader); + mapper.setTypeFactory(tf); + } + return mapper; }