From e4ba9eaf099affcafcc521b0a0aed197af837e0f Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Wed, 26 Dec 2018 12:29:02 +0300 Subject: [PATCH] Feature - JndiRedissonFactory and JndiRedissonSessionManager added. #1819 --- .../tomcat/JndiRedissonSessionManager.java | 67 +++++++++++++++ .../tomcat/JndiRedissonSessionManager.java | 67 +++++++++++++++ .../tomcat/JndiRedissonSessionManager.java | 67 +++++++++++++++ .../tomcat/JndiRedissonSessionManager.java | 67 +++++++++++++++ .../org/redisson/JndiRedissonFactory.java | 82 +++++++++++++++++++ 5 files changed, 350 insertions(+) create mode 100644 redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java create mode 100644 redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java create mode 100644 redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java create mode 100644 redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java create mode 100644 redisson/src/main/java/org/redisson/JndiRedissonFactory.java diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java new file mode 100644 index 000000000..69c1f2ed5 --- /dev/null +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java @@ -0,0 +1,67 @@ +/** + * Copyright 2018 Nikita Koksharov + * + * 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.redisson.tomcat; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.catalina.LifecycleException; +import org.redisson.api.RedissonClient; + +/** + * + * @author Nikita Koksharov + * + */ +public class JndiRedissonSessionManager extends RedissonSessionManager { + + private String jndiName; + + @Override + public void setConfigPath(String configPath) { + throw new IllegalArgumentException("configPath is unavaialble for JNDI based manager"); + } + + @Override + protected RedissonClient buildClient() throws LifecycleException { + InitialContext context = null; + try { + context = new InitialContext(); + Context envCtx = (Context) context.lookup("java:comp/env"); + return (RedissonClient) envCtx.lookup(jndiName); + } catch (NamingException e) { + throw new LifecycleException("Unable to locate Redisson instance by name: " + jndiName, e); + } finally { + if (context != null) { + try { + context.close(); + } catch (NamingException e) { + throw new LifecycleException("Unable to close JNDI context", e); + } + } + } + } + + public String getJndiName() { + return jndiName; + } + + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + +} diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java new file mode 100644 index 000000000..69c1f2ed5 --- /dev/null +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java @@ -0,0 +1,67 @@ +/** + * Copyright 2018 Nikita Koksharov + * + * 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.redisson.tomcat; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.catalina.LifecycleException; +import org.redisson.api.RedissonClient; + +/** + * + * @author Nikita Koksharov + * + */ +public class JndiRedissonSessionManager extends RedissonSessionManager { + + private String jndiName; + + @Override + public void setConfigPath(String configPath) { + throw new IllegalArgumentException("configPath is unavaialble for JNDI based manager"); + } + + @Override + protected RedissonClient buildClient() throws LifecycleException { + InitialContext context = null; + try { + context = new InitialContext(); + Context envCtx = (Context) context.lookup("java:comp/env"); + return (RedissonClient) envCtx.lookup(jndiName); + } catch (NamingException e) { + throw new LifecycleException("Unable to locate Redisson instance by name: " + jndiName, e); + } finally { + if (context != null) { + try { + context.close(); + } catch (NamingException e) { + throw new LifecycleException("Unable to close JNDI context", e); + } + } + } + } + + public String getJndiName() { + return jndiName; + } + + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + +} diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java new file mode 100644 index 000000000..69c1f2ed5 --- /dev/null +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java @@ -0,0 +1,67 @@ +/** + * Copyright 2018 Nikita Koksharov + * + * 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.redisson.tomcat; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.catalina.LifecycleException; +import org.redisson.api.RedissonClient; + +/** + * + * @author Nikita Koksharov + * + */ +public class JndiRedissonSessionManager extends RedissonSessionManager { + + private String jndiName; + + @Override + public void setConfigPath(String configPath) { + throw new IllegalArgumentException("configPath is unavaialble for JNDI based manager"); + } + + @Override + protected RedissonClient buildClient() throws LifecycleException { + InitialContext context = null; + try { + context = new InitialContext(); + Context envCtx = (Context) context.lookup("java:comp/env"); + return (RedissonClient) envCtx.lookup(jndiName); + } catch (NamingException e) { + throw new LifecycleException("Unable to locate Redisson instance by name: " + jndiName, e); + } finally { + if (context != null) { + try { + context.close(); + } catch (NamingException e) { + throw new LifecycleException("Unable to close JNDI context", e); + } + } + } + } + + public String getJndiName() { + return jndiName; + } + + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + +} diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java new file mode 100644 index 000000000..69c1f2ed5 --- /dev/null +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/JndiRedissonSessionManager.java @@ -0,0 +1,67 @@ +/** + * Copyright 2018 Nikita Koksharov + * + * 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.redisson.tomcat; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.catalina.LifecycleException; +import org.redisson.api.RedissonClient; + +/** + * + * @author Nikita Koksharov + * + */ +public class JndiRedissonSessionManager extends RedissonSessionManager { + + private String jndiName; + + @Override + public void setConfigPath(String configPath) { + throw new IllegalArgumentException("configPath is unavaialble for JNDI based manager"); + } + + @Override + protected RedissonClient buildClient() throws LifecycleException { + InitialContext context = null; + try { + context = new InitialContext(); + Context envCtx = (Context) context.lookup("java:comp/env"); + return (RedissonClient) envCtx.lookup(jndiName); + } catch (NamingException e) { + throw new LifecycleException("Unable to locate Redisson instance by name: " + jndiName, e); + } finally { + if (context != null) { + try { + context.close(); + } catch (NamingException e) { + throw new LifecycleException("Unable to close JNDI context", e); + } + } + } + } + + public String getJndiName() { + return jndiName; + } + + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + +} diff --git a/redisson/src/main/java/org/redisson/JndiRedissonFactory.java b/redisson/src/main/java/org/redisson/JndiRedissonFactory.java new file mode 100644 index 000000000..10b7586c0 --- /dev/null +++ b/redisson/src/main/java/org/redisson/JndiRedissonFactory.java @@ -0,0 +1,82 @@ +/** + * Copyright 2018 Nikita Koksharov + * + * 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.redisson; + +import java.io.File; +import java.io.IOException; +import java.util.Hashtable; + +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.NamingException; +import javax.naming.RefAddr; +import javax.naming.Reference; +import javax.naming.spi.ObjectFactory; + +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.client.codec.Codec; +import org.redisson.config.Config; + +/** + * + * @author Nikita Koksharov + * + */ +public class JndiRedissonFactory implements ObjectFactory { + + @Override + public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) + throws Exception { + Reference ref = (Reference) obj; + RefAddr addr = ref.get("configPath"); + return buildClient(addr.getContent().toString()); + } + + protected RedissonClient buildClient(String configPath) throws NamingException { + Config config = null; + try { + config = Config.fromJSON(new File(configPath), getClass().getClassLoader()); + } catch (IOException e) { + // trying next format + try { + config = Config.fromYAML(new File(configPath), getClass().getClassLoader()); + } catch (IOException e1) { + NamingException ex = new NamingException("Can't parse yaml config " + configPath); + ex.initCause(e1); + throw ex; + } + } + + try { + try { + Config c = new Config(config); + Codec codec = c.getCodec().getClass().getConstructor(ClassLoader.class) + .newInstance(Thread.currentThread().getContextClassLoader()); + config.setCodec(codec); + } catch (Exception e) { + throw new IllegalStateException("Unable to initialize codec with ClassLoader parameter", e); + } + + return Redisson.create(config); + } catch (Exception e) { + NamingException ex = new NamingException(); + ex.initCause(e); + throw ex; + } + } + +}