Catalog alignment
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / migration / scanner / ClassScanner.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.migration.scanner;
22
23 import org.apache.commons.io.FileUtils;
24 import org.openecomp.sdc.asdctool.migration.core.MigrationException;
25
26 import java.io.File;
27 import java.lang.reflect.Modifier;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Optional;
34
35 /**
36  * scan and instantiate classes of given type in the class path
37  */
38 public class ClassScanner {
39
40
41     private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
42
43     public <T> List<T> getAllClassesOfType(String basePackage, Class<T> ofType) {
44         Collection<File> allClassesInPackage = getAllClassesInPackage(basePackage);
45         List<T> loadedClasses = new ArrayList<>();
46         for (File clazzFile : allClassesInPackage) {
47             Optional<T> instance = loadAndInstantiateClass(getClassReference(clazzFile), ofType);
48             instance.ifPresent(loadedClasses::add);
49         }
50         return loadedClasses;
51     }
52
53     private <T> Optional<T> loadAndInstantiateClass(String classReference, Class<T> ofType)  {
54         try {
55             return instantiateClassOfType(classReference, ofType);
56         }catch (ClassNotFoundException e) {
57             //log
58             throw new MigrationException(String.format("could not find class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e.getMessage()));
59         } catch (IllegalAccessException e1) {
60             //log
61             throw new MigrationException(String.format("could not instantiate class %s of type %s. class is not accessible. cause: %s", classReference, ofType.toGenericString(), e1.getMessage()));
62         } catch (InstantiationException e2) {
63             //log
64             throw new MigrationException(String.format("could not instantiate class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e2.getMessage()));
65         }
66     }
67
68     private <T> Optional<T> instantiateClassOfType(String classReference, Class<T> ofType) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
69         String className = classReference.replaceAll(".class$", "").replaceAll(".class", "");
70         Class<?> aClass = classLoader.loadClass(className);
71         if (ofType.isAssignableFrom(aClass) && isInstantiateAbleClass(aClass)){
72             return Optional.of((T) aClass.newInstance());
73         }
74         return Optional.empty();
75     }
76
77     private boolean isInstantiateAbleClass(Class<?> clazz) {
78         return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isEnum() && !clazz.isAnonymousClass() && !clazz.isInterface();
79     }
80
81     private Collection<File> getAllClassesInPackage(String fromPackage) {
82         String path = fromPackage.replace(".", "/");
83         URL resource = classLoader.getResource(path);
84         if (noMigrationTasks(resource)) {
85             return Collections.emptyList();
86         }
87         return FileUtils.listFiles(new File(resource.getFile()), new String[]{"class"}, true);
88     }
89
90     private boolean noMigrationTasks(URL resource) {
91         return resource == null;
92     }
93
94     private String getClassReference(File classFile) {
95         String asPackage = classFile.getPath().replace(File.separator, ".");
96         String classes = "classes.";
97         return asPackage.substring(asPackage.indexOf(classes) + classes.length());
98     }
99 }