Package bufferings.ktr.wjr.server.service

Source Code of bufferings.ktr.wjr.server.service.KtrWjrServiceImpl

/*
* Copyright 2010 bufferings[at]gmail.com
*
* Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package bufferings.ktr.wjr.server.service;

import static bufferings.ktr.wjr.shared.util.Preconditions.*;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import bufferings.ktr.wjr.client.service.KtrWjrService;
import bufferings.ktr.wjr.server.logic.WjrConfigLoader;
import bufferings.ktr.wjr.server.logic.WjrGAEDevLogRecorder;
import bufferings.ktr.wjr.server.logic.WjrGAELogRecorder;
import bufferings.ktr.wjr.server.logic.WjrGAEProdLogRecorder;
import bufferings.ktr.wjr.server.logic.WjrGAEQuotaRecorder;
import bufferings.ktr.wjr.server.logic.WjrJUnitLogicFactory;
import bufferings.ktr.wjr.server.logic.WjrMethodRunner;
import bufferings.ktr.wjr.server.logic.WjrParamParser;
import bufferings.ktr.wjr.server.logic.WjrStoreLoader;
import bufferings.ktr.wjr.server.util.AppEngineUtil;
import bufferings.ktr.wjr.shared.model.WjrConfig;
import bufferings.ktr.wjr.shared.model.WjrMethodItem;
import bufferings.ktr.wjr.shared.model.WjrStore;

/**
* KtrWjr service class.
*
* @author bufferings[at]gmail.com
*/
public class KtrWjrServiceImpl implements KtrWjrService {

  private static final Logger logger =
    Logger.getLogger(KtrWjrServiceImpl.class.getName());

  protected static final String CLASSES_DIRECTORY = "WEB-INF/classes";

  /**
   * {@inheritDoc}
   */
  public WjrConfig loadConfig(Map<String, List<String>> parameterMap) {
    String configId = getParamParser().getConfigId(parameterMap);
    WjrConfig config = getConfigLoader().loadWjrConfig(configId);
    logger.info("The configuration is loaded. " + config.toString());
    return config;
  }

  /**
   * {@inheritDoc}
   */
  public WjrStore loadStore(Map<String, List<String>> parameterMap) {
    return getStoreLoader().loadWjrStore(CLASSES_DIRECTORY);
  }

  /**
   * {@inheritDoc}
   */
  public WjrMethodItem runTest(WjrMethodItem methodItem,
      Map<String, List<String>> parameterMap, boolean cpumsEnabled,
      boolean apimsEnabled, boolean logHookEnabled, String logHookTimezone) {
    checkNotNull(methodItem, "The methodItem parameter is null.");

    WjrGAEQuotaRecorder quotaRecorder = null;
    if (cpumsEnabled || apimsEnabled) {
      quotaRecorder = getGAEQuotaRecorder();
    }

    WjrGAELogRecorder logRecorder = null;
    if (logHookEnabled) {
      logRecorder = getGAELogRecorder();
    }

    WjrMethodRunner methodRunner = getMethodRunner();
    try {
      methodItem.clearResult();

      if (logRecorder != null) {
        if (logHookTimezone == null || logHookTimezone.length() == 0) {
          logHookTimezone = WjrConfig.DEFAULT_LOGHOOK_TIMEZONE;
        }

        WjrParamParser paramParser = getParamParser();
        String timeZoneId =
          paramParser.getTimeZoneId(parameterMap, logHookTimezone);
        logRecorder.startRecording(timeZoneId);
      }

      if (quotaRecorder != null) {
        quotaRecorder.startRecording();
      }

      methodItem = methodRunner.runWjrMethod(methodItem);
    } finally {
      if (quotaRecorder != null && quotaRecorder.isRecording()) {
        quotaRecorder.stopRecording();
        if (cpumsEnabled) {
          methodItem.setCpuTime(quotaRecorder.getRecordedCpuTime());
        }
        if (apimsEnabled) {
          methodItem.setApiTime(quotaRecorder.getRecordedApiTime());
        }
      }

      if (logRecorder != null && logRecorder.isRecording()) {
        logRecorder.stopRecording();
        methodItem.setLog(logRecorder.getRecordedLog());
      }
    }
    return methodItem;
  }

  /**
   * Gets the configuration loader.
   */
  protected WjrConfigLoader getConfigLoader() {
    return new WjrConfigLoader();
  }

  /**
   * Gets the store loader which loads WjrStore from classes.
   */
  protected WjrStoreLoader getStoreLoader() {
    return WjrJUnitLogicFactory.getStoreLoader();
  }

  /**
   * Gets the method runner which runs the tests.
   */
  protected WjrMethodRunner getMethodRunner() {
    return WjrJUnitLogicFactory.getMethodRunner();
  }

  /**
   * Gets the parameter map parser.
   */
  protected WjrParamParser getParamParser() {
    return new WjrParamParser();
  }

  /**
   * Gets the GAE log recorder.
   */
  protected WjrGAELogRecorder getGAELogRecorder() {
    if (AppEngineUtil.isProduction()) {
      return new WjrGAEProdLogRecorder();
    } else {
      return new WjrGAEDevLogRecorder();
    }
  }

  /**
   * Gets the GAE quota recorder.
   */
  protected WjrGAEQuotaRecorder getGAEQuotaRecorder() {
    return new WjrGAEQuotaRecorder();
  }
}
TOP

Related Classes of bufferings.ktr.wjr.server.service.KtrWjrServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.