SynchronizationTask.java

  1.   /*
  2.    * Copyright (C) 2003-2012 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.settings.chromattic;
  18.  
  19.   import org.exoplatform.commons.chromattic.ChromatticLifeCycle;
  20.   import org.exoplatform.commons.chromattic.SessionContext;
  21.   import org.exoplatform.container.PortalContainer;
  22.   import org.exoplatform.settings.impl.SettingServiceImpl;
  23.  
  24.   /**
  25.    * Created by The eXo Platform SAS
  26.    * Author : thanh_vucong
  27.    *          thanh_vucong@exoplatform.com
  28.    * Dec 10, 2012  
  29.    */
  30.   public abstract class SynchronizationTask<V>
  31.   {
  32.  
  33.      /**
  34.       * Executes a task within a context from the specified life cycle. If an existing context already exists
  35.       * then this context is used otherwise a context is managed for the duration of the {@link #execute(SessionContext)}
  36.       * method.
  37.       *
  38.       * @param lifeCycle the life cycle
  39.       * @return a value
  40.       */
  41.     public final V executeWith(ChromatticLifeCycle lifeCycle) {
  42.         PortalContainer container = PortalContainer.getInstance();
  43.         SettingServiceImpl settingServiceImpl = (SettingServiceImpl) container.getComponentInstanceOfType(SettingServiceImpl.class);
  44.       boolean created = settingServiceImpl.startSynchronization();
  45.       try {
  46.         return execute(lifeCycle.getContext());
  47.       }
  48.       finally {
  49.           settingServiceImpl.stopSynchronization(created);
  50.       }
  51.     }
  52.  
  53.      /**
  54.       * Implementor must provide the task logic here.
  55.       *
  56.       * @param ctx the context
  57.       * @return a value
  58.       */
  59.      protected abstract V execute(SessionContext ctx);
  60.      
  61.      
  62.   }