All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.elephantdrummer.configuration.DrummerPropertyHolder Maven / Gradle / Ivy

The newest version!
package com.elephantdrummer.configuration;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.elephantdrummer.annotation.DayOfWeek;
import com.elephantdrummer.annotation.DrummerJob;
import com.elephantdrummer.annotation.trigger.After;
import com.elephantdrummer.annotation.trigger.At;
import com.elephantdrummer.annotation.trigger.Before;
import com.elephantdrummer.annotation.trigger.Every;
import com.elephantdrummer.annotation.trigger.Range;
import com.elephantdrummer.annotation.trigger.TimeZoneEnum;
import com.elephantdrummer.container.ContainerElement;
import com.elephantdrummer.dictionary.DictDefaultAfterValue;
import com.elephantdrummer.dictionary.DictDefaultAtValue;
import com.elephantdrummer.dictionary.DictDefaultBeforeValue;
import com.elephantdrummer.dictionary.DictDefaultEveryValue;
import com.elephantdrummer.dictionary.DictDefaultRangeValue;
import com.elephantdrummer.dictionary.DictExpression;
import com.elephantdrummer.parser.ParserDayOfWeekToCalendarDay;
import com.elephantdrummer.parser.ParserString;
import com.elephantdrummer.trigger.DummyTrigger;
import com.elephantdrummer.trigger.base.DrumTrigger;

/**
 * Copyright 2018 Elephant Software Klaudiusz Wojtkowiak e-mail: [email protected]
 * 
 * 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. 
 */
public class DrummerPropertyHolder extends HashMap implements ContainerElement{
	
	public static final String DRUMMER_PROPERTY_PREFIX="drummer.";

	public static final String AT="at.";
	public static final String EVERY="every.";
	public static final String AFTER="after.";
	public static final String BEFORE="before.";
	public static final String SHIFT="shift.";
	public static final String CRON="cron.";
	public static final String DOT=".";

	private int threads=0;
	

	
	private boolean clusterSingleExecution=false;
	private boolean onStartup=false;
	private boolean skipExecutionWhenPreviousJobIsRunning=false;
	private boolean cacheThreads=false;

	private static final long serialVersionUID = 1L;

	
	public void addProperties(Map map){
		if (map==null) return;
		for (String key:map.keySet()){
			put(key,map.get(key));
		}
	}
	
	
	
	
	
	 public DrummerJob getDrummerJob(String name){
		 DrummerJob drummerJob=new DrummerJob() {
			
			@Override
			public Class annotationType() {
				// TODO Auto-generated method stub
				return null;
			}
			

			
			@Override
			public Class trigger() {
				return DummyTrigger.class;
			}
			
			@Override
			public int threads() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(name+DOT,DictExpression.threads),threads);
			}
			

			
			@Override
			public Range shift() {
				return getCustomisedShift(name+DOT);
			}
			
			@Override
			public Every[] every() {
				return getCustomisedEvery(name+DOT,getArrayMarkers(name));
			}
			

			@Override
			public At[] at() {
				return getCustomisedAt(name+DOT,getArrayMarkers(name));
			}

			@Override
			public boolean clusterSingleExecution() {
				return false;//isClusterSingleExecution();
			}

			@Override
			public boolean onStartup() {
				return ParserString.toBooleanSmall(getJobProperty(name+DOT,DictExpression.onStartup),onStartup);
			}

			@Override
			public boolean skipExecutionWhenPreviousJobIsRunning() {
				return ParserString.toBooleanSmall(getJobProperty(name+DOT,DictExpression.skipExecutionWhenPreviousJobIsRunning),skipExecutionWhenPreviousJobIsRunning);
			}

			@Override
			public String name() {
				return name;
			}



			@Override
			public boolean cacheThreads() {
				return ParserString.toBooleanSmall(getJobProperty(name+DOT,DictExpression.cacheThreads),cacheThreads);
			}

			@Override
			public String[] cron() {
				return getCustomisedCron(name+DOT,getArrayMarkers(name));
			}

		};
		
		
		return drummerJob;
		
	 }
	 
	 private String[] getCustomisedCron(String name,Set arrays){
		 List rv=new LinkedList<>();
		 
		 if (arrays.isEmpty()) {
			 rv.add(getJobProperty(name,DictExpression.cron));
		 }else {
			 for (String arraykey:arrays) {
				 rv.add(getJobProperty(name+arraykey+DOT,DictExpression.cron));
			 }
		 }
		 
		 String[] dm=new String[rv.size()];
		 for (int i=0;i getArrayMarkers(String name){
		 
		 String propkey=DRUMMER_PROPERTY_PREFIX+name+DOT;
			
		 
		 Set rv=new HashSet<>();
		 
		 for (String kk:keySet()) {
			 if (kk.startsWith(propkey)) {
				 String keyrest=kk.replace(propkey, "");
				 int dotposition=keyrest.indexOf(DOT);
				 if (dotposition<0) continue;
				 String  arraykey=keyrest.substring(0, dotposition);
				 
				 
				 if (arraykey==null||arraykey.trim().isEmpty() )continue;
				 
				 String withdot=arraykey+DOT;
				 if (withdot.equals(EVERY)
						 ||withdot.equals(AT)
						 ||withdot.equals(AFTER)
						 ||withdot.equals(BEFORE)
						 ||withdot.equals(CRON)
						 ||withdot.equals(SHIFT)
						 ) continue;
				 rv.add(arraykey);
			 }
		 }
		 
		 
			
			return rv;
		 
	 }
	 
	 private At[] getCustomisedAt(String name,Set arrays){
		 
		 List rv=new LinkedList<>();
		 
		 if (arrays.isEmpty()) {
			 rv.add(getCustomisedAt(name));
		 }else {
			 for (String arraykey:arrays) {
				 rv.add(getCustomisedAt(name+arraykey+DOT));
			 }
		 }
		 
		 At[] dm=new At[rv.size()];
		 for (int i=0;i annotationType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public int year() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+AT,DictExpression.year),DictDefaultAtValue.DEFAULT_YEAR);
			}

			@Override
			public int month() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+AT,DictExpression.month),DictDefaultAtValue.DEFAULT_MONTH);
			}

			@Override
			public int day() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+AT,DictExpression.day),DictDefaultAtValue.DEFAULT_DAY);
			}

			@Override
			public int hour() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+AT,DictExpression.hour),DictDefaultAtValue.DEFAULT_HOUR);
			}

			@Override
			public int minute() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+AT,DictExpression.minute),DictDefaultAtValue.DEFAULT_MINUTE);
			}

			@Override
			public int second() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+AT,DictExpression.second),DictDefaultAtValue.DEFAULT_SECOND);
			}

			@Override
			public DayOfWeek[] dayOfWeek() {
				DayOfWeek[] rv= ParserDayOfWeekToCalendarDay.toDrummerDayOfWeekArray(getJobProperty(prefix+AT,DictExpression.dayOfWeek));
				return rv==null?DictDefaultAtValue.DEFAULT_DAYOFWEEK:rv;
			}

			@Override
			public TimeZoneEnum timezone() {
				//return ParserString.toSmallIntegerWithDefault(getJobProperty("atYear),DictDefaultAtValue.DEFAULT_TIMEZONE);
				return DictDefaultAtValue.DEFAULT_TIMEZONE;
			}

			@Override
			public Before before() {
				return getCustomisedBefore(prefix+AT);
			}

			@Override
			public After after() {
				return getCustomisedAfter(prefix+AT);
			}
			 
		 };
		 
	 }
	 
	 private Every[] getCustomisedEvery(String name,Set arrays){
		 List rv=new LinkedList<>();
		 
		 if (arrays.isEmpty()) {
			 
			 rv.add(getCustomisedEvery(name));
		 }else {
			 for (String arraykey:arrays) {
				 rv.add(getCustomisedEvery(name+arraykey+DOT));
			 }
		 }
		 
		 Every[] dm=new Every[rv.size()];
		 for (int i=0;i annotationType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public int day() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+EVERY,DictExpression.day),DictDefaultEveryValue.DEFAULT_DAY);
			}



			@Override
			public int hour() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+EVERY,DictExpression.hour),DictDefaultEveryValue.DEFAULT_HOUR);
			}

			@Override
			public int minute() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+EVERY,DictExpression.minute),DictDefaultEveryValue.DEFAULT_MINUTE);
			}

			@Override
			public int second() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+EVERY,DictExpression.second),DictDefaultEveryValue.DEFAULT_SECOND);
			}

			@Override
			public int milisecond() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+EVERY,DictExpression.milisecond),DictDefaultEveryValue.DEFAULT_MILISECOND);
			}

			@Override
			public Before before() {
				return getCustomisedBefore(prefix+EVERY);
			}

			@Override
			public After after() {
				return getCustomisedAfter(prefix+EVERY);
			}
			 
		 };
	 }
	 
	 
	 private After getCustomisedAfter(String prefix){
		 return new After(){

			@Override
			public Class annotationType() {
				// TODO Auto-generated method stub
				return null;
			}


			@Override
			public int hour() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,AFTER+DictExpression.hour),DictDefaultAfterValue.DEFAULT_HOUR);
			}

			@Override
			public int minute() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,AFTER+DictExpression.minute),DictDefaultAfterValue.DEFAULT_MINUTE);
			}

			@Override
			public int second() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,AFTER+DictExpression.second),DictDefaultAfterValue.DEFAULT_SECOND);
			}



			@Override
			public int year() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,AFTER+DictExpression.year),DictDefaultAfterValue.DEFAULT_YEAR);
			}

			@Override
			public int month() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,AFTER+DictExpression.month),DictDefaultAfterValue.DEFAULT_MONTH);
			}

			@Override
			public int day() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,AFTER+DictExpression.day),DictDefaultAfterValue.DEFAULT_DAY);
			}

			@Override
			public DayOfWeek[] dayOfWeek() {
				DayOfWeek[] rv= ParserDayOfWeekToCalendarDay.toDrummerDayOfWeekArray(getJobProperty(prefix,AFTER+DictExpression.dayOfWeek));
				return rv==null?DictDefaultAfterValue.DEFAULT_DAYOFWEEK:rv;
			}


			@Override
			public TimeZoneEnum timezone() {
//				return ParserString.toSmallIntegerWithDefault(getJobProperty("everySecond),everySecond);
				return TimeZoneEnum.DEFAULT;//getAfterTimeZone();
			}
			 
		 };
	 }
	 
	 private Before getCustomisedBefore(String prefix){
		 return new Before(){

			@Override
			public Class annotationType() {
				// TODO Auto-generated method stub
				return null;
			}


			@Override
			public int hour() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+BEFORE,DictExpression.hour),DictDefaultBeforeValue.DEFAULT_HOUR);
			}

			@Override
			public int minute() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+BEFORE,DictExpression.minute),DictDefaultBeforeValue.DEFAULT_MINUTE);
			}

			@Override
			public int second() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+BEFORE,DictExpression.second),DictDefaultBeforeValue.DEFAULT_SECOND);
			}



			@Override
			public int year() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+BEFORE,DictExpression.year),DictDefaultBeforeValue.DEFAULT_YEAR);
			}

			@Override
			public int month() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+BEFORE,DictExpression.month),DictDefaultBeforeValue.DEFAULT_MONTH);
			}

			@Override
			public int day() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix+BEFORE,DictExpression.day),DictDefaultBeforeValue.DEFAULT_DAY);
			}

			@Override
			public DayOfWeek[] dayOfWeek() {
				DayOfWeek[] rv= ParserDayOfWeekToCalendarDay.toDrummerDayOfWeekArray(getJobProperty(prefix+BEFORE,DictExpression.dayOfWeek));
				return rv==null?DictDefaultBeforeValue.DEFAULT_DAYOFWEEK:rv;
			}


			@Override
			public TimeZoneEnum timezone() {
//				return ParserString.toSmallIntegerWithDefault(getJobProperty("everySecond),everySecond);
				return TimeZoneEnum.DEFAULT;//getBeforeTimeZone();
			}
			 
		 };
	 }
	 
	 private Range getCustomisedShift(String prefix){
		 return new Range(){

			@Override
			public Class annotationType() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public int day() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,SHIFT+DictExpression.day),DictDefaultRangeValue.DEFAULT_DAY);
			}

			@Override
			public int hour() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,SHIFT+DictExpression.hour),DictDefaultRangeValue.DEFAULT_HOUR);
			}

			@Override
			public int minute() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,SHIFT+DictExpression.minute),DictDefaultRangeValue.DEFAULT_MINUTE);
			}

			@Override
			public int second() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,SHIFT+DictExpression.second),DictDefaultRangeValue.DEFAULT_SECOND);
			}

			@Override
			public int milisecond() {
				return ParserString.toSmallIntegerWithDefault(getJobProperty(prefix,SHIFT+DictExpression.milisecond),DictDefaultRangeValue.DEFAULT_MILISECOND);
			}

			 
		 };
	 }

		private String getJobProperty(String prefix,String key){
			String propkey=DRUMMER_PROPERTY_PREFIX+prefix+key;
			
			if (containsKey(propkey)==false){
				return null;
			}
			String rv= get(propkey);
			return rv;
		}



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy