001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.contentanalysis;
017
018import java.util.Properties;
019
020import org.apache.commons.lang3.BooleanUtils;
021import org.apache.commons.lang3.StringUtils;
022import org.apache.log4j.Logger;
023
024import core.tut.pori.properties.SystemProperty;
025
026/**
027 * The properties for Content Analysis service.
028 * 
029 */
030public class CAProperties extends SystemProperty {
031  /** 
032   * value used for max task size when the limit is disabled.
033   * 
034   * @see #getMaxTaskSize()
035   */
036  public static final int MAX_TASK_SIZE_DISABLED = -1;
037  /** 
038   * value used for max task delay when the schedule delay is disabled.
039   * 
040   * @see #getScheduleTaskDelay()
041   */
042  public static final int TASK_DELAY_DISABLED = -1;
043  /* properties */
044  private static final String PROPERTY_SERVICE_TUT_PORI_CA = PROPERTY_SERVICE_PORI+".contentanalysis";
045  private static final String PROPERTY_SERVICE_TUT_PORI_CA_MAX_TASK_SIZE = PROPERTY_SERVICE_TUT_PORI_CA+".max_task_size";
046  private static final String PROPERTY_SERVICE_TUT_PORI_CA_RESOLVE_FRIENDLY_KEYWORDS = PROPERTY_SERVICE_TUT_PORI_CA+".resolve_friendly_keywords";
047  private static final String PROPERTY_SERVICE_TUT_PORI_CA_SCHEDULE_TASK_DELAY = PROPERTY_SERVICE_TUT_PORI_CA+".schedule_task_delay";
048  private int _maxTaskSize = -1;
049  private boolean _resolveFriendlyKeywords = false;
050  private long _scheduleTaskDelay = -1;
051  
052  @Override
053  public void initialize(Properties properties) throws IllegalArgumentException {
054    String property = properties.getProperty(PROPERTY_SERVICE_TUT_PORI_CA_RESOLVE_FRIENDLY_KEYWORDS);
055    if(StringUtils.isBlank(property)){
056      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_TUT_PORI_CA_RESOLVE_FRIENDLY_KEYWORDS);
057    }
058    _resolveFriendlyKeywords = BooleanUtils.toBoolean(property);
059    
060    try{
061      _maxTaskSize = Integer.parseInt(properties.getProperty(PROPERTY_SERVICE_TUT_PORI_CA_MAX_TASK_SIZE));
062    } catch (NumberFormatException ex){
063      Logger.getLogger(getClass()).warn(ex, ex);
064      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_TUT_PORI_CA_MAX_TASK_SIZE);
065    }
066    if(_maxTaskSize != MAX_TASK_SIZE_DISABLED && _maxTaskSize < 1){
067      Logger.getLogger(getClass()).debug("Max task size was < 1, reseting to default disabled value: "+MAX_TASK_SIZE_DISABLED);
068      _maxTaskSize = MAX_TASK_SIZE_DISABLED;
069    }
070    
071    try{
072      _scheduleTaskDelay = Long.parseLong(properties.getProperty(PROPERTY_SERVICE_TUT_PORI_CA_SCHEDULE_TASK_DELAY));
073    } catch (NumberFormatException ex){
074      Logger.getLogger(getClass()).warn(ex, ex);
075      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_TUT_PORI_CA_SCHEDULE_TASK_DELAY);
076    }
077    if(_scheduleTaskDelay != TASK_DELAY_DISABLED && _scheduleTaskDelay < 1){
078      Logger.getLogger(getClass()).debug("Task schedule delay was < 1, reseting to default disabled value: "+TASK_DELAY_DISABLED);
079      _scheduleTaskDelay = TASK_DELAY_DISABLED;
080    }
081  }
082
083  /**
084   * @return the resolveFriendlyKeywords
085   */
086  public boolean isResolveFriendlyKeywords() {
087    return _resolveFriendlyKeywords;
088  }
089
090  /**
091   * The recommended maximum photo count in a single task. Value is never 0, and less than 0 if the limit is disabled.
092   * 
093   * @return the maxTaskSize
094   */
095  public int getMaxTaskSize() {
096    return _maxTaskSize;
097  }
098
099  /**
100   * The default task schedule delay. The tasks should be scheduled to start immediately if the value is less than 1.
101   * 
102   * @return the scheduleTaskDelay
103   */
104  public long getScheduleTaskDelay() {
105    return _scheduleTaskDelay;
106  }
107  
108  @Override
109  public String getPropertyFilePath() {
110    return CONFIGURATION_FILE_PATH+Definitions.PROPERTY_FILE;
111  }
112}