Highlighted

I don't get any logs messages In my EventListener module

Avatar

Avatar

hadrien303

Avatar

hadrien303

hadrien303

25-04-2018

Hello guys,

I'm actually trying to get JCR events (NODE_ADDED/PROPERTY_CHANGED/PROPERTY_ADDED) from my site but I don't know if its working and I don't think so, because I'm not getting any logs written..


I created a log file thru the log support console, and I attached it on the package "com.adobe.aem.community.core.core.listeners" and "myprojectName" (which is the artifactId of my maven project, and also "apps.myapp" (myapp is the name of the site I created, It creates my logFile, I got some logs in it but not the ones I wrote in my code below..

So I created a AEM project using the archetype 12, and I have this EventListener:

package com.adobe.aem.community.core.core.listeners;

//import com.day.io.file.FileUtils;
import com.day.cq.wcm.api.components.ComponentContext;
import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.event.jobs.JobManager;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.jackrabbit.api.observation.JackrabbitEvent;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.*;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;

import javax.jcr.observation.ObservationManager;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import org.apache.commons.io.FileUtils;

@Component
public class HorizonEventListener implements EventListener {

   private final Logger LOGGER = LoggerFactory.getLogger(HorizonEventListener.class);

   @Reference
   private SlingRepository repository;
  private Session session;
  private ObservationManager observationManager;
  private ResourceResolverFactory resourceResolverFactory;
  private ResourceResolver resourceResolver;

  protected void activate(ComponentContext context) throws Exception {

             //this.session = repository.loginService(null, this.repository.getDefaultWorkspace());
             this.resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
             this.session = this.resourceResolver.adaptTo(Session.class);
            this.session = repository.loginAdministrative(null);
            this.LOGGER.info("SESSION OBJET IS LIVE ? " + session.isLive());

   //this.writeLogFile("### ACTIVATE ###", this.session.getRootNode(), null);

             this.observationManager = session.getWorkspace().getObservationManager();
             this.observationManager.addEventListener(this, Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.NODE_ADDED, "/apps/horizon", true, null,          null, true);
            this.LOGGER.info("### ADDED JCR EVENT LISTENER ###");
}

   protected void deactivate(ComponentContext componentContext) {

             try {

                       if (this.observationManager != null) {

                                 this.observationManager.removeEventListener(this);
                                this.LOGGER.info("### REMOVED JCR EVENT LISTENER ###");
                                this.writeLogFile("### DEACTIVATE ###", this.session.getRootNode(), null);
                       }

            } catch (Exception e) {

                  this.LOGGER.error("### ERROR REMOVING THE JCR EVENT LISTENER ###", e);
             } finally {

                  if (session != null) {

                       session.logout();
                       session = null;
                   }

            }

  }

   @Override
   public void onEvent(EventIterator it) {

             this.LOGGER.info("### new event ###");
             System.console().writer().write("EVENT LOGGED");
           

          while (it.hasNext()) {

                 Event event = it.nextEvent();
                 try {

                       this.writeLogFile("### LOGZZZ ###", this.session.getRootNode(), event);
                      this.LOGGER.info("### new property event: {}", event.getPath());
                       Property changedProperty = this.session.getProperty(event.getPath());
                      if (changedProperty.getName().equalsIgnoreCase("jcr:title") && !changedProperty.getString().endsWith("!")) {

                           changedProperty.setValue(changedProperty.getString() + "!");
                           this.session.save();
                       }

                 } catch (Exception e) {

                       this.LOGGER.error(e.getMessage(), e);
                  }

            }

  }

If anyone could help me it will be awsome

Thanks,
Hadrien