Search code examples
grailsgrails-plugin

Replace Grails plugin listener


I need to extend the listener class of audit-logging plugin, in order to provide different functionality on some scenarios.

For that purpose I have created this class:

class CustomAuditLogListener extends AuditLogListener{

  public CustomAuditLogListener(Datastore datastore) {
    super(datastore)
  }

  @Override
  protected void onPreUpdate(event) {
     . .  .
}
}

Now, in order for the plugin to use this class instead of the default AuditLogListener, within Bootstrap.groovy I try to remove AuditLogListener from the application listeners, and add the custom listener:

 def applicationContext = servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
    log.debug("Application listeners - PRE")
    log.debug(applicationContext.getApplicationListeners())
    // Remove all the listeners registered by the audit plugin.
    applicationContext.getApplicationListeners.each { listener ->
      if (listener.class == AuditLogListener.class) {
        servletContext.getApplicationListeners.remove(listener)
        log.debug("AuditLogListener removed ")
      }
    }

    grailsApplication.mainContext.eventTriggeringInterceptor.datastores.each { key, datastore ->
      // Don't register the listener if we are disabled
      if (!grailsApplication.config.auditLog.disabled && !datastore.config.auditLog.disabled) {
          def listener = new CustomAuditLogListener(datastore)
          listener.with {
             // some options
          }
          applicationContext.addApplicationListener(listener)
          log.debug("Add new listener CustomAuditLogListener")
      }
    }
    log.debug("Application listeners -POST")
    log.debug(applicationContext.getApplicationListeners())

Application listeners - PRE returns empty list

Add new Listener CustomAudtilogListener is logged

Application Listeners - POST returns empty list

The way I register my listener is copied from the way the plugin registers the listener, seen in this class

To sum, what I need is replacing AuditLogListener, with my own CustomAuditLogListener. Currently the above process is not working. Any suggestions?


Solution

  • Hey sorry but the post does seem a little confusing, although I think I understand

    I need to extend the listener class of audit-logging plugin.

    Now, in order for the plugin to use this class instead of the default AuditLogListener,

    so you are trying to override the auditLogListener.

    Your problem maybe that you are not declaring it as an override in your conf/spring/resources.groovy.

    I have done a similar thing for a class rather than listener here:

    https://github.com/vahidhedayati/testwschat/blob/master/test/unit/anythingbut/grails/plugin/wschat/MyOverrideServiceSpec.groovy

    https://github.com/vahidhedayati/testwschat/blob/master/grails-app/conf/spring/resources.groovy

    and something similar here:

    grails kickstart plugin KickstartFilters how to prevent password information on logs

    basically once you declare your bean

    yourAppFilters(KickstartFilters)

    This then tricks your grails-app and any plugins that maybe referring to KickstartFilters to now refer to yourAppFilters as the replacement

    Try a bean like this: (ctrl shift o) to pull in CustomAuditLogListener

    AuditLogListener(CustomAuditLogListener){
     grailsApplication = ref('grailsApplication')
    }