Skip to content

Commit

Permalink
More strict DeploymentDescriptorNode - throwing exceptions
Browse files Browse the repository at this point in the history
- validators cover possible issues just partially
- better break deploy asap with more helpful info, especially when it is about
  glassfish issues.

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Mar 19, 2023
1 parent f7a9771 commit 5ab479d
Showing 1 changed file with 41 additions and 35 deletions.
Expand Up @@ -280,12 +280,12 @@ protected void setXMLRootTag(XMLElement element) {
@Override
public XMLNode<?> getHandlerFor(XMLElement element) {
if (handlers == null) {
LOG.log(WARNING, INVALID_DESC_MAPPING, this, "No handler registered");
LOG.log(WARNING, "No handler registered in " + this);
return null;
}
Class<?> c = handlers.get(element.getQName());
if (c == null) {
LOG.log(WARNING, INVALID_DESC_MAPPING, element.getQName(), "No handler registered");
LOG.log(WARNING, "No class registered for " + element.getQName() + " in " + this);
return null;
}
LOG.log(DEBUG, "New Handler requested for {0}", c);
Expand Down Expand Up @@ -422,38 +422,40 @@ private void recordNodeMapping(String subElementName, Class<?> handler) {
*/
@Override
public void setElementValue(XMLElement element, String value) {
LOG.log(DEBUG, "setElementValue(element={0}, value={1})", element, value);
T descriptor = getDescriptor();
if (descriptor == null) {
throw new IllegalStateException("Descriptor not available in " + this);
}
Map<String, String> dispatchTable = getDispatchTable();
if (dispatchTable != null && dispatchTable.containsKey(element.getQName())) {
if (dispatchTable.get(element.getQName()) == null) {
// we just ignore these values from the DDs
LOG.log(DEBUG, "Deprecated element {0} with value {1} is ignored.", element, value);
return;
}
try {
T descriptor = getDescriptor();
if (descriptor == null) {
LOG.log(WARNING, INVALID_DESC_MAPPING, element.getQName(), value);
} else {
setDescriptorInfo(descriptor, dispatchTable.get(element.getQName()), value);
}
return;
} catch (InvocationTargetException e) {
LOG.log(WARNING, INVALID_DESC_MAPPING, dispatchTable.get(element.getQName()),
getDescriptor().getClass());
Throwable t = e.getTargetException();
if (t instanceof IllegalArgumentException) {
// We report the error but we continue loading, this will allow the verifier
// to catch these errors or to register an error handler for notification
LOG.log(WARNING, INVALID_DESC_MAPPING, element, value);
} else {
LOG.log(WARNING, "Error occurred", t);
}
} catch (Throwable t) {
LOG.log(WARNING, "Error occurred", t);
}
if (dispatchTable == null) {
throw new IllegalStateException("Method dispatch table not available in " + this);
}
String qName = element.getQName();
String methodName = dispatchTable.get(qName);
if (methodName == null) {
// we just ignore these values from the DDs
LOG.log(WARNING, "Deprecated element {0} with value {1} is ignored for descriptor {2} of node {3}.",
element, value, descriptor.getClass(), getClass());
LOG.log(DEBUG, "Helpful stacktrace for the previous warning.", new RuntimeException());
return;
}
if (!value.isBlank()) {
LOG.log(WARNING, INVALID_DESC_MAPPING, element.getQName(), value);
try {
setDescriptorInfo(descriptor, methodName, value);
} catch (InvocationTargetException e) {
LOG.log(WARNING, INVALID_DESC_MAPPING, qName, value, descriptor.getClass());
Throwable t = e.getCause();
if (t instanceof IllegalArgumentException) {
// We report the error but we continue loading, this will allow the verifier
// to catch these errors or to register an error handler for notification
LOG.log(WARNING, INVALID_DESC_MAPPING, qName, value, descriptor.getClass());
} else {
throw new IllegalArgumentException(
"Failed " + methodName + " when tried to set '" + value + "' to the descriptor " + descriptor, e);
}
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException(
"Failed " + methodName + " when tried to set '" + value + "' to the descriptor " + descriptor, e);
}
}

Expand All @@ -472,17 +474,21 @@ protected Map<String, String> getDispatchTable() {


/**
* call a setter method on a descriptor with a new value
* Call a setter method on a descriptor with a new value converted automatically
* to a compatible type.
*
* @param target the descriptor to use
* @param methodName the setter method to invoke
* @param value the new value for the field in the descriptor
* @throws ReflectiveOperationException if method failed because of number formatting or method
* doesn't exist.
* @throws InvocationTargetException if the invocation failed for other reason.
* @deprecated guessing element type
*/
@Deprecated
private void setDescriptorInfo(Object target, String methodName, String value) throws ReflectiveOperationException {
LOG.log(Level.DEBUG, "setDescriptorInfo(target.class={0}, methodName={1}, value={2})",
new Object[] {target.getClass(), methodName, value});
LOG.log(Level.DEBUG, "setDescriptorInfo(target.class={0}, methodName={1}, value={2})", target.getClass(),
methodName, value);

ReflectiveOperationException e = new ReflectiveOperationException("Could not find compatible setter.");
for (Entry<Class<?>, Function<String, Object>> entry : ALLOWED_DESCRIPTOR_INFO_CONVERSIONS.entrySet()) {
Expand Down

0 comments on commit 5ab479d

Please sign in to comment.