public static boolean generateHandler(Registry configSystemRegistry, String resourceFullPath)
     throws RegistryException, XMLStreamException {
   RegistryContext registryContext = configSystemRegistry.getRegistryContext();
   if (registryContext == null) {
     return false;
   }
   Resource resource = configSystemRegistry.get(resourceFullPath);
   if (resource != null) {
     String content = null;
     if (resource.getContent() != null) {
       content = RegistryUtils.decodeBytes((byte[]) resource.getContent());
     }
     if (content != null) {
       OMElement handler = AXIOMUtil.stringToOM(content);
       if (handler != null) {
         OMElement dummy = OMAbstractFactory.getOMFactory().createOMElement("dummy", null);
         dummy.addChild(handler);
         try {
           configSystemRegistry.beginTransaction();
           boolean status =
               RegistryConfigurationProcessor.updateHandler(
                   dummy,
                   configSystemRegistry.getRegistryContext(),
                   HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE);
           configSystemRegistry.commitTransaction();
           return status;
         } catch (Exception e) {
           configSystemRegistry.rollbackTransaction();
           throw new RegistryException("Unable to add handler", e);
         }
       }
     }
   }
   return false;
 }
  public static boolean addHandler(Registry configSystemRegistry, String payload)
      throws RegistryException, XMLStreamException {
    String name;
    OMElement element = AXIOMUtil.stringToOM(payload);
    if (element != null) {
      name = element.getAttributeValue(new QName("class"));
    } else return false;

    if (isHandlerNameInUse(name))
      throw new RegistryException("The added handler name is already in use!");

    String path = getContextRoot() + name;
    Resource resource;
    if (!handlerExists(configSystemRegistry, name)) {
      resource = new ResourceImpl();
    } else {
      throw new RegistryException("The added handler name is already in use!");
    }
    resource.setContent(payload);
    try {
      configSystemRegistry.beginTransaction();
      configSystemRegistry.put(path, resource);
      generateHandler(configSystemRegistry, path);
      configSystemRegistry.commitTransaction();
    } catch (Exception e) {
      configSystemRegistry.rollbackTransaction();
      throw new RegistryException("Unable to generate handler", e);
    }
    return true;
  }
  public static boolean removeHandler(Registry configSystemRegistry, String handlerName)
      throws RegistryException, XMLStreamException {
    String handlerConfiguration = getHandlerConfiguration(configSystemRegistry, handlerName);
    if (handlerConfiguration != null) {
      OMElement element = AXIOMUtil.stringToOM(handlerConfiguration);
      try {
        try {
          configSystemRegistry.beginTransaction();
          RegistryConfigurationProcessor.HandlerDefinitionObject handlerDefinitionObject =
              new RegistryConfigurationProcessor.HandlerDefinitionObject(null, element).invoke();
          String[] methods = handlerDefinitionObject.getMethods();
          Filter filter = handlerDefinitionObject.getFilter();
          Handler handler = handlerDefinitionObject.getHandler();
          if (handlerDefinitionObject.getTenantId() != -1) {
            CurrentSession.setCallerTenantId(handlerDefinitionObject.getTenantId());
            // We need to swap the tenant id for this call, if the handler has overriden the
            // default value.
            configSystemRegistry
                .getRegistryContext()
                .getHandlerManager()
                .removeHandler(
                    methods, filter, handler, HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE);
            CurrentSession.removeCallerTenantId();
          } else {
            configSystemRegistry
                .getRegistryContext()
                .getHandlerManager()
                .removeHandler(
                    methods, filter, handler, HandlerLifecycleManager.USER_DEFINED_HANDLER_PHASE);
          }

          configSystemRegistry.commitTransaction();
          return true;
        } catch (Exception e) {
          configSystemRegistry.rollbackTransaction();
          throw e;
        }
      } catch (Exception e) {
        if (e instanceof RegistryException) {
          throw (RegistryException) e;
        } else if (e instanceof XMLStreamException) {
          throw (XMLStreamException) e;
        }
        throw new RegistryException("Unable to build handler configuration", e);
      }
    }
    return false;
  }
  public static boolean deleteHandler(Registry configSystemRegistry, String name)
      throws RegistryException, XMLStreamException {
    if (isHandlerNameInUse(name))
      throw new RegistryException("Handler could not be deleted, since it is already in use!");

    String path = getContextRoot() + name;
    if (configSystemRegistry.resourceExists(path)) {
      try {
        configSystemRegistry.beginTransaction();
        removeHandler(configSystemRegistry, name);
        configSystemRegistry.delete(path);
        configSystemRegistry.commitTransaction();
      } catch (Exception e) {
        configSystemRegistry.rollbackTransaction();
        throw new RegistryException("Unable to remove handler", e);
      }
      return true;
    }
    return false;
  }
  public static boolean updateHandler(Registry configSystemRegistry, String oldName, String payload)
      throws RegistryException, XMLStreamException {
    if (isHandlerNameInUse(oldName))
      throw new RegistryException("Could not update handler since it is already in use!");

    String newName = null;
    OMElement element = AXIOMUtil.stringToOM(payload);
    if (element != null) {
      newName = element.getAttributeValue(new QName("class"));
    }

    if (newName == null || newName.equals("")) return false; // invalid configuration

    if (oldName == null || oldName.equals("")) {
      String path = getContextRoot() + newName;
      Resource resource;
      if (handlerExists(configSystemRegistry, newName)) {
        return false; // we are adding a new handler
      } else {
        resource = new ResourceImpl();
      }
      resource.setContent(payload);
      try {
        configSystemRegistry.beginTransaction();
        configSystemRegistry.put(path, resource);
        generateHandler(configSystemRegistry, path);
        configSystemRegistry.commitTransaction();
      } catch (Exception e) {
        configSystemRegistry.rollbackTransaction();
        throw new RegistryException("Unable to generate handler", e);
      }
      return true;
    }

    if (newName.equals(oldName)) {
      // updating the rest of the content
      String oldPath = getContextRoot() + oldName;
      Resource resource;
      if (handlerExists(configSystemRegistry, oldName)) {
        resource = configSystemRegistry.get(oldPath);
      } else {
        resource = new ResourceImpl(); // will this ever happen?
      }
      resource.setContent(payload);
      try {
        configSystemRegistry.beginTransaction();
        removeHandler(configSystemRegistry, oldName);
        configSystemRegistry.put(oldPath, resource);
        generateHandler(configSystemRegistry, oldPath);
        configSystemRegistry.commitTransaction();
      } catch (Exception e) {
        configSystemRegistry.rollbackTransaction();
        throw new RegistryException("Unable to generate handler", e);
      }
      return true;
    } else {
      String oldPath = getContextRoot() + oldName;
      String newPath = getContextRoot() + newName;

      if (handlerExists(configSystemRegistry, newName)) {
        return false; // we are trying to use the name of a existing handler
      }

      Resource resource;
      if (handlerExists(configSystemRegistry, oldName)) {
        resource = configSystemRegistry.get(oldPath);
      } else {
        resource = new ResourceImpl(); // will this ever happen?
      }

      resource.setContent(payload);
      try {
        configSystemRegistry.beginTransaction();
        configSystemRegistry.put(newPath, resource);
        generateHandler(configSystemRegistry, newPath);
        removeHandler(configSystemRegistry, oldName);
        configSystemRegistry.delete(oldPath);
        configSystemRegistry.commitTransaction();
      } catch (Exception e) {
        configSystemRegistry.rollbackTransaction();
        throw new RegistryException("Unable to renew handler", e);
      }
      return true;
    }
  }