From ab13ae14a0f5f64779276c5513ca082daad734b7 Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Wed, 20 Nov 2024 17:38:58 -0600 Subject: [PATCH] Fixes #4191 - Add ability to disable Herring (#4234) --- .../extension/herring/HerringExtension.java | 59 ++++++++++--------- .../herring/src/main/java/module-info.java | 17 +++++- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/extension/herring/src/main/java/cloud/piranha/extension/herring/HerringExtension.java b/extension/herring/src/main/java/cloud/piranha/extension/herring/HerringExtension.java index 14d7812f9..7dc310f59 100644 --- a/extension/herring/src/main/java/cloud/piranha/extension/herring/HerringExtension.java +++ b/extension/herring/src/main/java/cloud/piranha/extension/herring/HerringExtension.java @@ -69,21 +69,23 @@ public class HerringExtension implements WebApplicationExtension { */ @Override public void configure(WebApplication webApplication) { - LOGGER.log(DEBUG, "Configuring HerringExtension"); - if (System.getProperty(INITIAL_CONTEXT_FACTORY) == null) { - LOGGER.log(DEBUG, "Setting " + INITIAL_CONTEXT_FACTORY + " to " + HerringInitialContextFactory.class.getName()); - System.setProperty(INITIAL_CONTEXT_FACTORY, HerringInitialContextFactory.class.getName()); - } - if (!System.getProperty(INITIAL_CONTEXT_FACTORY).equals(HerringInitialContextFactory.class.getName())) { - LOGGER.log(WARNING, INITIAL_CONTEXT_FACTORY + " is not set to " + HerringInitialContextFactory.class.getName()); - } - - Context context = new DefaultInitialContext(); - - Context proxyContext = (Context) Proxy.newProxyInstance( - Thread.currentThread().getContextClassLoader(), - new Class[] { Context.class }, - new InvocationHandler() { + LOGGER.log(DEBUG, "Configuring the HerringExtension"); + if (Boolean.parseBoolean(System.getProperty("cloud.piranha.extension.herring.HerringExtension.enabled", "true"))) { + + if (System.getProperty(INITIAL_CONTEXT_FACTORY) == null) { + LOGGER.log(DEBUG, "Setting " + INITIAL_CONTEXT_FACTORY + " to " + HerringInitialContextFactory.class.getName()); + System.setProperty(INITIAL_CONTEXT_FACTORY, HerringInitialContextFactory.class.getName()); + } + if (!System.getProperty(INITIAL_CONTEXT_FACTORY).equals(HerringInitialContextFactory.class.getName())) { + LOGGER.log(WARNING, INITIAL_CONTEXT_FACTORY + " is not set to " + HerringInitialContextFactory.class.getName()); + } + + Context context = new DefaultInitialContext(); + + Context proxyContext = (Context) Proxy.newProxyInstance( + Thread.currentThread().getContextClassLoader(), + new Class[]{Context.class}, + new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { @@ -98,7 +100,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl if (jndiName.startsWith("java:comp/env/")) { String classNameWithField = jndiName.substring("java:comp/env/".length()); - String[] classNameAndField = classNameWithField.split("/"); + String[] classNameAndField = classNameWithField.split("/"); if (classNameAndField.length == 2) { String className = classNameAndField[0]; String fieldName = classNameAndField[1]; @@ -118,10 +120,10 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl String methodName = "set" + new String(chars); Optional optionalMethod = Arrays.stream(beanClass.getDeclaredMethods()) - .filter(m -> m.getName().equals(methodName)) - .filter(m -> m.getParameterCount() == 1) - .filter(m -> m.getAnnotationsByType(Resource.class) != null) - .findFirst(); // ignore overloaded for now + .filter(m -> m.getName().equals(methodName)) + .filter(m -> m.getParameterCount() == 1) + .filter(m -> m.getAnnotationsByType(Resource.class) != null) + .findFirst(); // ignore overloaded for now if (optionalMethod.isPresent()) { resources = optionalMethod.get().getAnnotationsByType(Resource.class); @@ -134,8 +136,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl String lookup = resourceAnnnotation.lookup(); if (!"".equals(lookup)) { - returnValue = method.invoke(context, new Object[] {lookup}); - args = new Object[] {lookup}; + returnValue = method.invoke(context, new Object[]{lookup}); + args = new Object[]{lookup}; invoked = true; } else { throw new IllegalStateException("Cannot find " + type); @@ -145,9 +147,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl } } } catch (Throwable t) { - if (t instanceof InvocationTargetException invocationException && - invocationException.getTargetException() instanceof NamingException namingException) { - throw namingException; + if (t instanceof InvocationTargetException invocationException + && invocationException.getTargetException() instanceof NamingException namingException) { + throw namingException; } e.addSuppressed(t); } @@ -160,7 +162,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl // De-referencing can eventually be moved to DefaultInitialContext if (method.getName().equals("lookup") && returnValue instanceof Reference) { returnValue = NamingManager.getObjectInstance( - returnValue, new CompositeName(args[0].toString()), null, null); + returnValue, new CompositeName(args[0].toString()), null, null); } return returnValue; @@ -170,7 +172,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl } }); - HerringInitialContextFactory.setInitialContext(proxyContext); - webApplication.setAttribute(Context.class.getName(), proxyContext); + HerringInitialContextFactory.setInitialContext(proxyContext); + webApplication.setAttribute(Context.class.getName(), proxyContext); + } } } diff --git a/extension/herring/src/main/java/module-info.java b/extension/herring/src/main/java/module-info.java index 11f49feae..bdfd73ed0 100644 --- a/extension/herring/src/main/java/module-info.java +++ b/extension/herring/src/main/java/module-info.java @@ -28,7 +28,22 @@ /** * This module integrates Manorrock Herring into Piranha. - * + * + *

+ * The following property can be used to influence the workings of this module. + *

+ * + * + * + * + * + * + * + * + * + * + *
PropertyNotes
cloud.piranha.extension.herring.HerringExtension.enabledtrue to enable (default), false to disable
Configurable properties
+ * * @author Manfred Riem (mriem@manorrock.com) */ module cloud.piranha.extension.herring {