ChannelManagerImpl.java

  1. /*
  2.  * Copyright (C) 2003-2014 eXo Platform SAS.
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU Affero General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU Affero General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Affero General Public License
  15.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.commons.notification.channel;

  18. import groovy.text.GStringTemplateEngine;

  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.exoplatform.commons.api.notification.channel.AbstractChannel;
  25. import org.exoplatform.commons.api.notification.channel.ChannelManager;
  26. import org.exoplatform.commons.api.notification.channel.template.AbstractTemplateBuilder;
  27. import org.exoplatform.commons.api.notification.channel.template.TemplateProvider;
  28. import org.exoplatform.commons.api.notification.lifecycle.AbstractNotificationLifecycle;
  29. import org.exoplatform.commons.api.notification.model.ChannelKey;
  30. import org.exoplatform.commons.api.notification.model.PluginKey;
  31. import org.exoplatform.commons.notification.template.TemplateUtils;
  32. import org.exoplatform.services.log.ExoLogger;
  33. import org.exoplatform.services.log.Log;
  34. import org.picocontainer.Startable;

  35. /**
  36.  * Created by The eXo Platform SAS
  37.  * Author : eXoPlatform
  38.  *          thanhvc@exoplatform.com
  39.  * Dec 12, 2014  
  40.  */
  41. public class ChannelManagerImpl implements ChannelManager, Startable {
  42.   /** logger */
  43.   private static final Log LOG = ExoLogger.getLogger(ChannelManagerImpl.class);
  44.   /** Defines the channels: key = channelId and Channel*/
  45.   private final Map<ChannelKey, AbstractChannel> channels;
  46.   private List<TemplateProvider> providers;
  47.   private GStringTemplateEngine gTemplateEngine;
  48.   public ChannelManagerImpl() {
  49.     channels = new HashMap<ChannelKey, AbstractChannel>();
  50.     providers = new LinkedList<TemplateProvider>();
  51.     gTemplateEngine = new GStringTemplateEngine();
  52.   }
  53.  
  54.   /**
  55.    * Register new channel
  56.    * @param channel
  57.    */
  58.   @Override
  59.   public void register(AbstractChannel channel) {
  60.     channels.put(ChannelKey.key(channel.getId()), channel);
  61.   }

  62.   /**
  63.    * Unregister the specified channel
  64.    *
  65.    * @param channel
  66.    */
  67.   @Override
  68.   public void unregister(AbstractChannel channel) {
  69.     channels.remove(channel.getId());
  70.   }

  71.   /**
  72.    * Register the template provider
  73.    *
  74.    * @param provider
  75.    */
  76.   @Override
  77.   public void registerTemplateProvider(TemplateProvider provider) {
  78.     providers.add(provider);
  79.   }
  80.  
  81.   /**
  82.    * Register and override the template provider
  83.    *
  84.    * @param provider
  85.    */
  86.   @Override
  87.   public void registerOverrideTemplateProvider(TemplateProvider provider) {
  88.     providers.add(provider);
  89.     AbstractChannel channel = channels.get(provider.getChannelKey());
  90.     if (channel != null) {
  91.       channel.registerTemplateProvider(addTemplateEngine(provider));
  92.     } else {
  93.       LOG.warn("Register the new TemplateProvider is unsucessful");
  94.     }
  95.   }

  96.   @Override
  97.   public AbstractChannel getChannel(ChannelKey key) {
  98.     return channels.get(key);
  99.   }
  100.  
  101.   @Override
  102.   public AbstractNotificationLifecycle getLifecycle(ChannelKey key) {
  103.     return getChannel(key).getLifecycle();
  104.   }

  105.   /**
  106.    * Gets size of channels has been registered
  107.    *
  108.    * @return
  109.    */
  110.   @Override
  111.   public int sizeChannels() {
  112.     return channels.size();
  113.   }

  114.   @Override
  115.   public List<AbstractChannel> getChannels() {
  116.     List<AbstractChannel> channels = new LinkedList<AbstractChannel>();
  117.     AbstractChannel emailChannel = getChannel(ChannelKey.key(MailChannel.ID));
  118.     if (emailChannel != null) {
  119.       channels.add(emailChannel);
  120.     }
  121.     for (AbstractChannel channel : this.channels.values()) {
  122.       if (MailChannel.ID.equals(channel.getId())) {
  123.         continue;
  124.       }
  125.       channels.add(channel);
  126.     }
  127.     return Collections.unmodifiableList(channels);
  128.   }

  129.   @Override
  130.   public void start() {
  131.     for (TemplateProvider provider : providers) {
  132.       AbstractChannel channel = channels.get(provider.getChannelKey());
  133.       if (channel != null) {
  134.         channel.registerTemplateProvider(addTemplateEngine(provider));
  135.       } else {
  136.         LOG.warn("Register the new TemplateProvider is unsucessful");
  137.       }
  138.     }
  139.   }

  140.   @Override
  141.   public void stop() {
  142.   }

  143.   /**
  144.    * Makes the template file to Groovy template.
  145.    * It's ready to generate the message.
  146.    *
  147.    * Why needs to do this way?
  148.    * - TemplateBuilder and TemplateProvider can be extensible or override. So only final list to make the template.
  149.    * - Don't waste time to build the template for useless template.
  150.    
  151.    * @param provider
  152.    * @return
  153.    */
  154.   private TemplateProvider addTemplateEngine(TemplateProvider provider) {
  155.     Map<PluginKey, AbstractTemplateBuilder> builders = provider.getTemplateBuilder();
  156.     Map<PluginKey, String> configs = provider.getTemplateFilePathConfigs();
  157.     for (PluginKey plugin : configs.keySet()) {
  158.       String templatePath = configs.get(plugin);
  159.       if (templatePath != null && templatePath.length() > 0) {
  160.         try {
  161.           AbstractTemplateBuilder builder = builders.get(plugin);
  162.           if (builder != null) {
  163.             String template = TemplateUtils.loadGroovyTemplate(templatePath);
  164.             builder.setTemplateEngine(gTemplateEngine.createTemplate(template));
  165.           }
  166.         } catch (Exception e) {
  167.           LOG.warn("Failed to build groovy template engine for: " + plugin.getId() + " templatePath: " + templatePath, e);
  168.         }
  169.       }
  170.     }
  171.     return provider;
  172.   }
  173. }