Commits

Juan Carlos Picado Herrera committed 2f20afc

ENCUESTAME-109 notification comet service prototype

Comments (0)

Files changed (11)

encuestame-mvc/src/main/java/org/encuestame/comet/server/CometBayeuxInitializer.java

+package org.encuestame.comet.server;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.servlet.ServletContext;
+
+import org.cometd.bayeux.server.BayeuxServer;
+import org.cometd.java.annotation.ServerAnnotationProcessor;
+import org.cometd.server.BayeuxServerImpl;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
+import org.springframework.context.annotation.Bean;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.ServletContextAware;
+
+/**
+ * Comet Bayeux Server Initializer.
+ * @author Picado, Juan juanATencuestame.org
+ * @since Mar 4, 2011
+ */
+@Component
+public class CometBayeuxInitializer implements DestructionAwareBeanPostProcessor,
+        ServletContextAware {
+
+    /**
+     * Bayeux Server.
+     */
+    private BayeuxServer bayeuxServer;
+
+    /**
+     * Server Annotation Procesor.
+     */
+    private ServerAnnotationProcessor processor;
+
+    /**
+     * Set {@link BayeuxServer}.
+     * @param bayeuxServer
+     */
+    @SuppressWarnings("unused")
+    @Inject
+    private void setBayeuxServer(BayeuxServer bayeuxServer) {
+        this.bayeuxServer = bayeuxServer;
+    }
+
+    /**
+     * Post Construct method.
+     */
+    @SuppressWarnings("unused")
+    @PostConstruct
+    private void init() {
+        this.processor = new ServerAnnotationProcessor(bayeuxServer);
+    }
+
+    /**
+     * Destroy bean.
+     */
+    @SuppressWarnings("unused")
+    @PreDestroy
+    private void destroy() {
+    }
+
+    /**
+     * Post process before initialization.
+     */
+    public Object postProcessBeforeInitialization(Object bean, String name)
+            throws BeansException {
+        processor.processDependencies(bean);
+        processor.processConfigurations(bean);
+        processor.processCallbacks(bean);
+        return bean;
+    }
+
+    /**
+     * Post process after initialization.
+     */
+    public Object postProcessAfterInitialization(Object bean, String name)
+            throws BeansException {
+        return bean;
+    }
+
+    /**
+     * Post process before destruction.
+     */
+    public void postProcessBeforeDestruction(Object bean, String name)
+            throws BeansException {
+        processor.deprocessCallbacks(bean);
+    }
+
+    /**
+     * Bayeux Server.
+     * @return {@link BayeuxServer}.
+     */
+    @Bean(initMethod = "start", destroyMethod = "stop")
+    public BayeuxServer bayeuxServer() {
+        BayeuxServerImpl bean = new BayeuxServerImpl();
+        bean.setOption(BayeuxServerImpl.LOG_LEVEL, "3");
+        return bean;
+    }
+
+    /**
+     * Set spring servlet context.
+     */
+    public void setServletContext(ServletContext servletContext) {
+        servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
+    }
+}

encuestame-mvc/src/main/java/org/encuestame/comet/service/BayeuxInitializer.java

-package org.encuestame.comet.service;
-
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
-import javax.inject.Inject;
-import javax.servlet.ServletContext;
-
-import org.cometd.bayeux.server.BayeuxServer;
-import org.cometd.java.annotation.ServerAnnotationProcessor;
-import org.cometd.server.BayeuxServerImpl;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
-import org.springframework.context.annotation.Bean;
-import org.springframework.stereotype.Component;
-import org.springframework.web.context.ServletContextAware;
-
-@Component
-public class BayeuxInitializer implements DestructionAwareBeanPostProcessor, ServletContextAware
-{
-    private BayeuxServer bayeuxServer;
-    private ServerAnnotationProcessor processor;
-
-    @Inject
-    private void setBayeuxServer(BayeuxServer bayeuxServer)
-    {
-        this.bayeuxServer = bayeuxServer;
-    }
-
-    @PostConstruct
-    private void init()
-    {
-        this.processor = new ServerAnnotationProcessor(bayeuxServer);
-    }
-
-    @PreDestroy
-    private void destroy()
-    {
-    }
-
-    public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
-    {
-        processor.processDependencies(bean);
-        processor.processConfigurations(bean);
-        processor.processCallbacks(bean);
-        return bean;
-    }
-
-    public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
-    {
-        return bean;
-    }
-
-    public void postProcessBeforeDestruction(Object bean, String name) throws BeansException
-    {
-        processor.deprocessCallbacks(bean);
-    }
-
-    @Bean(initMethod = "start", destroyMethod = "stop")
-    public BayeuxServer bayeuxServer()
-    {
-        BayeuxServerImpl bean = new BayeuxServerImpl();
-        bean.setOption(BayeuxServerImpl.LOG_LEVEL, "3");
-        return bean;
-    }
-
-    public void setServletContext(ServletContext servletContext)
-    {
-        servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
-    }
-}

encuestame-mvc/src/main/java/org/encuestame/comet/service/HelloService.java

-package org.encuestame.comet.service;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.inject.Singleton;
-
-import org.cometd.bayeux.server.BayeuxServer;
-import org.cometd.bayeux.server.ServerMessage;
-import org.cometd.bayeux.server.ServerSession;
-import org.cometd.java.annotation.Listener;
-import org.cometd.java.annotation.Service;
-import org.cometd.java.annotation.Session;
-
-@Named
-@Singleton
-@Service("helloService")
-public class HelloService
-{
-    @Inject
-    private BayeuxServer bayeux;
-    @Session
-    private ServerSession serverSession;
-
-    @PostConstruct
-    public void init()
-    {
-    }
-
-    @Listener("/service/hello")
-    public void processHello(ServerSession remote, ServerMessage.Mutable message)
-    {
-        Map<String, Object> input = message.getDataAsMap();
-        String name = (String)input.get("name");
-
-        Map<String, Object> output = new HashMap<String, Object>();
-        output.put("greeting", "Hello, " + name);
-        remote.deliver(serverSession, "/hello", output, null);
-    }
-}

encuestame-mvc/src/main/java/org/encuestame/comet/services/AbstractCometService.java

+
+package org.encuestame.comet.services;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+
+import org.apache.log4j.Logger;
+import org.cometd.bayeux.server.BayeuxServer;
+import org.cometd.bayeux.server.ServerSession;
+import org.cometd.java.annotation.Session;
+import org.encuestame.business.security.AbstractSecurityContext;
+import org.encuestame.business.service.ServiceManager;
+import org.encuestame.business.service.imp.IServiceManager;
+import org.encuestame.persistence.domain.security.UserAccount;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * Base Case to all comet service.
+ * @author Picado, Juan juanATencuestame.org
+ * @since Mar 4, 2011
+ */
+public abstract class AbstractCometService extends AbstractSecurityContext {
+
+
+    protected Logger log = Logger.getLogger(this.getClass());
+
+    /**
+     * {@link ServiceManager}.
+     */
+    @Autowired
+    private IServiceManager serviceManager;
+
+    @Inject
+    private BayeuxServer bayeux;
+    @Session
+    protected ServerSession serverSession;
+
+    @PostConstruct
+    public void init() {
+    }
+
+    /**
+     * @return the serviceManager
+     */
+    public IServiceManager getServiceManager() {
+        return serviceManager;
+    }
+
+    /**
+     * Get By Username.
+     * @param username username
+     * @return
+     */
+    public UserAccount getByUsername(final String username){
+        return getServiceManager().getApplicationServices().getSecurityService().findUserByUserName(username);
+    }
+
+    /**
+     * @param serviceManager
+     *            the serviceManager to set
+     */
+    public void setServiceManager(IServiceManager serviceManager) {
+        this.serviceManager = serviceManager;
+    }
+
+}

encuestame-mvc/src/main/java/org/encuestame/comet/services/HelloService.java

+package org.encuestame.comet.services;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.cometd.bayeux.server.BayeuxServer;
+import org.cometd.bayeux.server.ServerMessage;
+import org.cometd.bayeux.server.ServerSession;
+import org.cometd.java.annotation.Listener;
+import org.cometd.java.annotation.Service;
+import org.cometd.java.annotation.Session;
+
+@Named
+@Singleton
+@Service("helloService")
+public class HelloService {
+    @Inject
+    private BayeuxServer bayeux;
+    @Session
+    private ServerSession serverSession;
+
+    @PostConstruct
+    public void init() {
+    }
+
+    @Listener("/service/hello")
+    public void processHello(ServerSession remote, ServerMessage.Mutable message) {
+        Map<String, Object> input = message.getDataAsMap();
+        String name = (String) input.get("name");
+
+        Map<String, Object> output = new HashMap<String, Object>();
+        output.put("greeting", "Hello, " + name);
+        remote.deliver(serverSession, "/hello", output, null);
+    }
+}

encuestame-mvc/src/main/java/org/encuestame/comet/services/NotificationCometService.java

+
+package org.encuestame.comet.services;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.cometd.bayeux.server.ServerMessage;
+import org.cometd.bayeux.server.ServerSession;
+import org.cometd.java.annotation.Listener;
+import org.cometd.java.annotation.Service;
+import org.encuestame.persistence.dao.INotification;
+import org.encuestame.persistence.dao.imp.NotificationDao;
+import org.encuestame.persistence.domain.security.UserAccount;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * Notification comet service.
+ * @author Picado, Juan juanATencuestame.org
+ * @since Mar 4, 2011
+ */
+@Named
+@Singleton
+@Service("notificationService")
+public class NotificationCometService extends AbstractCometService {
+
+    /** {@link NotificationDao}. **/
+    @Autowired
+    private INotification notificationDao;
+
+    /**
+     * Notificiaton process.
+     * @param remote
+     * @param message
+     */
+    @Listener("/service/notification")
+    public void processNotification(final ServerSession remote, final ServerMessage.Mutable message) {
+        final Map<String, Object> input = message.getDataAsMap();
+        log.debug("Notification Input "+input);
+        String name = (String) input.get("name");
+        final Map<String, Object> output = new HashMap<String, Object>();
+        final UserAccount userAccount = getByUsername(getUserPrincipalUsername());
+        if (userAccount != null) {
+            final Long totalNot = getNotificationDao().retrieveTotalNotificationStatus(userAccount.getAccount());
+            log.debug("totalNot "+totalNot);
+            final Long totalNewNot = getNotificationDao().retrieveTotalNotReadedNotificationStatus(userAccount.getAccount());
+            log.debug("totalNewNot "+totalNewNot);
+            output.put("totalNot", totalNot);
+            output.put("totalNot", totalNewNot);
+        } else {
+            log.error("Error username ");
+        }
+        output.put("greeting", "Hello, " + name);
+        remote.deliver(serverSession, "/not", output, null);
+    }
+
+    /**
+     * @return the notificationDao
+     */
+    public INotification getNotificationDao() {
+        return notificationDao;
+    }
+
+    /**
+     * @param notificationDao the notificationDao to set
+     */
+    public void setNotificationDao(INotification notificationDao) {
+        this.notificationDao = notificationDao;
+    }
+}

encuestame-mvc/src/main/java/org/encuestame/mvc/interceptor/DefaultEnMeInterceptor.java

             final String cookieName,
             final HttpServletResponse response,
             final String value){
-        Cookie cookie = new Cookie(cookieName, value);
+        //Cookie cookie = new Cookie(cookieName, value);
         //cookie.setMaxAge(expiry)
-        response.addCookie(cookie);
+        //response.addCookie(cookie);
     }
 
     /* (non-Javadoc)

encuestame-mvc/src/main/resources/encuestame-comet-context.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans
+                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+                        http://www.springframework.org/schema/context
+                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+    <!-- Commet @Components -->
+    <context:component-scan base-package="org.encuestame.comet" />
+
+</beans>

encuestame-mvc/src/main/resources/encuestame-controller-context.xml

   <!-- Scans for application @Components to deploy -->
   <context:component-scan base-package="org.encuestame.mvc" />
 
-   <context:component-scan base-package="org.encuestame.comet" />
-
   <!--
     Enables annotated @Controllers; responsible for invoking an annotated
     POJO @Controller when one is mapped.

encuestame-war/src/main/webapp/WEB-INF/views/dashboard/web/dashboard.jsp

 <script type="text/javascript">
 dojo.addOnLoad(function()
 {
+
+    //More Info: http://cometd.org/node/49
     var cometd = dojox.cometd;
     console.debug("comentD",cometd);
     var config = {
     {
         if (handshake.successful === true)
         {
-            cometd.batch(function()
-            {
-                cometd.subscribe('/hello', function(message)
-                {
+            cometd.batch(function(){
+                cometd.subscribe('/hello', function(message){
                     dojo.byId('body').innerHTML += '<div>Server Says: ' + message.data.greeting + '</div>';
                 });
                 // Publish on a service channel since the message is for the server only
                 cometd.publish('/service/hello', { name: 'World' });
+
+                cometd.subscribe('/not', function(message){
+                    console.debug("NOTF", message);
+                });
+                // Publish on a service channel since the message is for the server only
+                cometd.publish('/service/notification', { name: 'World2' });
             });
         }
     }

encuestame-war/src/main/webapp/WEB-INF/web.xml

             classpath:encuestame-quartz-context.xml
             classpath:encuestame-rss-context.xml,
             classpath:encuestame-mvc-context.xml,
+            classpath:encuestame-comet-context.xml,
             classpath:encuestame-web-flow-context.xml,
             classpath:encuestame-controller-context.xml,
             classpath:encuestame-json-context.xml,