ModuleActivator.java :  » UnTagged » moca » org » openmrs » module » usagestatistics » Android Open Source

Android Open Source » UnTagged » moca 
moca » org » openmrs » module » usagestatistics » ModuleActivator.java
/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.usagestatistics;

import java.util.Date;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.context.Context;
import org.openmrs.module.Activator;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.usagestatistics.util.StatsUtils;
import org.openmrs.scheduler.TaskDefinition;

/**
 * This class contains the logic that is run every time this module is either
 * started or shutdown
 */
public class ModuleActivator implements Activator {

  private static final Log log = LogFactory.getLog(ModuleActivator.class);

  /**
   * @see org.openmrs.module.Activator#startup()
   */
  public void startup() {
    log.info("Starting usage statistics module");
  }
  
  /**
   * Not called automatically by the OpenMRS framework, so needs
   * to be manually called from one of the following places:
   *   1. An advice point class constructor
   *   2. A servlet class constructor
   *  as these will be invoked after the context refresh
   *   
   *  In this module its called from TimeChartServlet
   *  @see org.openmrs.module.usagestatistics.web.servlet.TimeChartServlet#TimeChartServlet()
   */
  public void load() {
    log.info("Usage statistics module loaded");
    
    registerAggregationTask();
  }

  /**
   * @see org.openmrs.module.Activator#shutdown()
   */
  public void shutdown() {
    log.info("Shutting down usage statistics module");

    unregisterAggregationTask();
  }
  
  /**
   * Provides a convenient way of calling load() from another
   * class, e.g. 
   * <code>UsageStatsActivator.getInstance().load();</code>
   * @return the instance of this activator created by OpenMRS
   */
  public static ModuleActivator getInstance() {
    return (ModuleActivator)ModuleFactory.getModuleById(Constants.MODULE_ID).getActivator();
  }

  /**
   * Registers the aggregation task if it isn't already registered
   */
  public boolean registerAggregationTask() {
    try {
      Context.addProxyPrivilege("Manage Scheduler");

      TaskDefinition task = Context.getSchedulerService().getTaskByName(Constants.TASK_PROCESS_DATA);
      if (task == null) {
        Date start = StatsUtils.getPreviousMidnight(null);

        task = new TaskDefinition();
        task.setTaskClass(StatsAggregatorTask.class.getCanonicalName());
        task.setRepeatInterval(60 * 60l); // Hourly
        task.setStartOnStartup(true);
        task.setStartTime(start);
        task.setName(Constants.TASK_PROCESS_DATA);
        task.setUuid(UUID.randomUUID().toString());
        task.setDescription("Deletes or aggregates old usage statistics data");
        Context.getSchedulerService().scheduleTask(task);

        log.info("Registered aggregation task with scheduler");
      } else {
        log.info("Aggregation task already registered with scheduler");
        return false;
      }

    } catch (Exception ex) {
      log.warn("Unable to register aggregation task with scheduler", ex);
      return false;
    } finally {
      Context.removeProxyPrivilege("Manage Scheduler");
    }
    return true;
  }

  /**
   * Unregisters the aggregation task if it exists
   */
  private void unregisterAggregationTask() {
    TaskDefinition task = Context.getSchedulerService().getTaskByName(Constants.TASK_PROCESS_DATA);

    if (task != null)
      Context.getSchedulerService().deleteTask(task.getId());
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.