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

com.elephantdrummer.tool.reflect.ScannerDrummerJobProvided Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package com.elephantdrummer.tool.reflect;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.elephantdrummer.annotation.DrummerJob;
import com.elephantdrummer.annotation.TestJob;
import com.elephantdrummer.classconfig.JobClassConfig;
import com.elephantdrummer.executor.base.structure.AfterJobExecution;
import com.elephantdrummer.executor.base.structure.BeforeJobExecution;
import com.elephantdrummer.executor.base.structure.DrummerMethodJobWrapper;
import com.elephantdrummer.executor.base.structure.MethodToInvoke;
import com.elephantdrummer.scope.DrummerObservable;

/**
 * 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 interface ScannerDrummerJobProvided {

	  public static List getAnnotatedMethods(Class clazz){
		  List rv=new LinkedList<>();
		  
		  for (Method m:clazz.getMethods()) {
			  if (m.isAnnotationPresent(DrummerJob.class)==false) continue;
			  if (m.getParameterTypes().length>0) {
//				  System.out.println("metoda z parametrami - out");
				  //TODO: warning
				  continue;
			  }
			  rv.add(m);
		  }
		  
		  while (clazz==null==false) {
			  clazz=clazz.getSuperclass();
			  
			  if (clazz==null||clazz.getMethods()==null) continue;
			  for (Method m:clazz.getMethods()) {
				  if (m.isAnnotationPresent(DrummerJob.class)==false) continue;
				  if (m.getParameterTypes().length>0) {
//					  System.out.println("metoda z parametrami - out");
					  //TODO: warning
					  continue;
				  }
				 if (hasMethod(m,rv)==false) rv.add(m);
			  }
			  
		  }
		  
		  return rv;
	  }
	  
	  public static boolean hasMethod(Method m, Collection col) {
		  if (col==null) return false;
		  for (Method c:col) {
			  if (m.getName().equals(c.getName())){
				  return true;
			  }
		  }
		  
		  return false;
	  }
	  

	  public static List getCreatedDrummerJobsFromAnnotations(List singletons,List alreadyRegisteredJobs){
		  
		  List rv=new LinkedList<>();
		  if (alreadyRegisteredJobs==null) {
			  alreadyRegisteredJobs=new LinkedList<>();
		  }
		  
		  Map configuration=new HashMap<>();
		  
		  List afterJobsMethods=new LinkedList<>();
		  List beforeJobsMethods=new LinkedList<>();
		  
		 singletons.forEach(v->{
			 configuration.putAll(ScannerJobConfiguration.getInternalConfiguration(v));
			 afterJobsMethods.addAll(ScannerAfterJobExecution.getAfterJobExecutionMethods(v));
			 beforeJobsMethods.addAll(ScannerBeforeJobExecution.getBeforeJobExecutionMethods(v));
		 });
		 
		 
		  
		  for (DrummerObservable dob:singletons) {
			  
			  boolean jobIsRegisteredAnsShouldBeSkipped=false;
			  for (DrummerMethodJobWrapper alreadyregistered:alreadyRegisteredJobs) {
				  if (alreadyregistered.getDrummerObservable().getClass().getName().equals(dob.getClass().getName())) {
					  jobIsRegisteredAnsShouldBeSkipped=true;
					  System.out.println("znalazlem joba pomijam go");
					  break;
				  }
			  }
			  
			  if (jobIsRegisteredAnsShouldBeSkipped) {
				  System.out.println("pominiety");
				  continue;
			  }
			  
			  List methods=getAnnotatedMethods(dob.getClass());
				 
			  for (Method m:methods) {
				  DrummerJob dj= m.getAnnotation(DrummerJob.class);
				  
				  JobClassConfig djc=configuration.get(dj.name());
				  DrummerMethodJobWrapper djw=null;
				  
				  if (dj.name()==null==false&&djc==null==false) {
					  djw=new DrummerMethodJobWrapper(dob,m,djc.getDrummerJob(),m.getAnnotation(TestJob.class),djc);
				  }else {
					  djw=new DrummerMethodJobWrapper(dob,m,dj,m.getAnnotation(TestJob.class));
				  }
				  
				  
				  
				  Set afterMethodsForJob=MethodCollectorByPriority.getSortedCollection(dj.name(), afterJobsMethods);
				  djw.setAfterJob(new AfterJobExecution() {
					  	@Override
						public void afterDrummerJobExecution() {
							afterMethodsForJob.forEach(a -> a.execute());
						}
					});
			  
				  Set beforeMethodsForJob=MethodCollectorByPriority.getSortedCollection(dj.name(), beforeJobsMethods);
				  djw.setBeforeJob(new BeforeJobExecution() {
					  	@Override
						public void beforeDrummerJobExecution() {
							beforeMethodsForJob.forEach(b -> b.execute());
						}
					});

				  rv.add(djw);
			  }
		  }
		  return rv;
	  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy