|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | +* @Title: AdvancePackageScanner.java |
| 20 | +* @Package com.openthinks.easyweb.annotation.process.core.scaner |
| 21 | +* @Description: TODO |
| 22 | +* @author dailey.yet@outlook.com |
| 23 | +* @date Sep 29, 2016 |
| 24 | +* @version V1.0 |
| 25 | +*/ |
| 26 | +package com.openthinks.easyweb.annotation.process.core.scaner; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +import org.reflections.Reflections; |
| 34 | + |
| 35 | +import com.openthinks.easyweb.WebStatic; |
| 36 | +import com.openthinks.easyweb.annotation.Controller; |
| 37 | +import com.openthinks.easyweb.annotation.Filter; |
| 38 | +import com.openthinks.easyweb.annotation.process.core.WebControllerProcesser; |
| 39 | +import com.openthinks.easyweb.annotation.process.core.WebFilterProcesser; |
| 40 | +import com.openthinks.easyweb.annotation.process.objects.WebContainer; |
| 41 | +import com.openthinks.easyweb.annotation.process.objects.WebController; |
| 42 | +import com.openthinks.easyweb.annotation.process.objects.WebFilter; |
| 43 | +import com.openthinks.easyweb.context.WebContexts; |
| 44 | +import com.openthinks.libs.utilities.logger.ProcessLogger; |
| 45 | + |
| 46 | +/** |
| 47 | + * use {@link Reflections} to scan packager |
| 48 | + * @author dailey.yet@outlook.com |
| 49 | + * |
| 50 | + */ |
| 51 | +public class AdvancePackageScanner implements PackageScanner { |
| 52 | + |
| 53 | + @Override |
| 54 | + public void scan(WebContainer container, Set<String> packages) { |
| 55 | + Set<Class<?>> allControllerClazzs = new HashSet<>(); |
| 56 | + Set<Class<?>> allFilterClazzs = new HashSet<>(); |
| 57 | + for (String packageStr : packages) { |
| 58 | + Reflections reflections = new Reflections(packageStr); |
| 59 | + Set<Class<?>> controllerClazzs = reflections.getTypesAnnotatedWith(Controller.class); |
| 60 | + allControllerClazzs.addAll(controllerClazzs); |
| 61 | + Set<Class<?>> filterClazzs = reflections.getTypesAnnotatedWith(Filter.class); |
| 62 | + allFilterClazzs.addAll(filterClazzs); |
| 63 | + } |
| 64 | + processController(container, allControllerClazzs); |
| 65 | + processFilters(container, allFilterClazzs); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param container |
| 70 | + * @param allFilterClazzs |
| 71 | + */ |
| 72 | + protected void processFilters(WebContainer container, Set<Class<?>> allFilterClazzs) { |
| 73 | + List<Object> webFilters = getInstances(allFilterClazzs); |
| 74 | + WebFilterProcesser filterProcesser = new WebFilterProcesser(); |
| 75 | + for (Object filter : webFilters) { |
| 76 | + filterProcesser.setPropertie(WebStatic.WEB_FILTER, filter); |
| 77 | + WebFilter child = filterProcesser.process(); |
| 78 | + container.add(child); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param container |
| 84 | + * @param allControllerClazzs |
| 85 | + */ |
| 86 | + protected void processController(WebContainer container, Set<Class<?>> allControllerClazzs) { |
| 87 | + List<Object> webControllers = getInstances(allControllerClazzs); |
| 88 | + WebControllerProcesser controllerProcesser = new WebControllerProcesser(); |
| 89 | + for (Object controller : webControllers) { |
| 90 | + controllerProcesser.setPropertie(WebStatic.WEB_CONTROLLER, controller); |
| 91 | + WebController child = controllerProcesser.process(); |
| 92 | + container.add(child); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private List<Object> getInstances(Set<Class<?>> webInstancerClasses) { |
| 97 | + List<Object> allInstance = new ArrayList<>(); |
| 98 | + for (Class<?> clazz : webInstancerClasses) { |
| 99 | + try { |
| 100 | + Object obj = WebContexts.get().lookup(clazz); |
| 101 | + allInstance.add(obj); |
| 102 | + } catch (Exception e) { |
| 103 | + ProcessLogger.warn(e); |
| 104 | + } |
| 105 | + } |
| 106 | + return allInstance; |
| 107 | + } |
| 108 | +} |
0 commit comments