Skip to content

Commit

Permalink
Fixes piranhacloud#4191 - Add ability to disable Herring (piranhaclou…
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Nov 20, 2024
1 parent f5c4f67 commit ab13ae1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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];
Expand All @@ -118,10 +120,10 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
String methodName = "set" + new String(chars);

Optional<Method> 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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
}
}
17 changes: 16 additions & 1 deletion extension/herring/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@

/**
* This module integrates Manorrock Herring into Piranha.
*
*
* <p>
* The following property can be used to influence the workings of this module.
* </p>
* <table>
* <tr>
* <th>Property</th>
* <th>Notes</th>
* </tr>
* <tr>
* <td>cloud.piranha.extension.herring.HerringExtension.enabled</td>
* <td>true to enable (default), false to disable</td>
* </tr>
* <caption>Configurable properties</caption>
* </table>
*
* @author Manfred Riem ([email protected])
*/
module cloud.piranha.extension.herring {
Expand Down

0 comments on commit ab13ae1

Please sign in to comment.