Skip to content

Commit 1a68061

Browse files
committed
add package scan by different way; support those controller/filter in
jar file
1 parent 5be7457 commit 1a68061

16 files changed

Lines changed: 482 additions & 92 deletions

easywebframework/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<artifactId>utilities</artifactId>
2323
<version>1.0-SNAPSHOT</version>
2424
</dependency>
25+
<dependency>
26+
<groupId>org.reflections</groupId>
27+
<artifactId>reflections</artifactId>
28+
<version>0.9.10</version>
29+
</dependency>
2530
<dependency>
2631
<groupId>com.google.code.gson</groupId>
2732
<artifactId>gson</artifactId>

easywebframework/src/main/java/com/openthinks/easyweb/Versions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.openthinks.easyweb.annotation.Controller;
3030
import com.openthinks.easyweb.annotation.Filter;
3131
import com.openthinks.easyweb.annotation.Jsonp;
32+
import com.openthinks.easyweb.annotation.configure.ScanWay;
33+
import com.openthinks.easyweb.annotation.process.core.scaner.PackageScanner;
3234
import com.openthinks.easyweb.annotation.process.filter.path.FilterPathMatchStrategy;
3335
import com.openthinks.easyweb.annotation.process.filter.path.MixedFilterPathMatchStrategy;
3436
import com.openthinks.easyweb.annotation.process.filter.path.RegExrFilterPathMatchStrategy;
@@ -63,6 +65,7 @@ public class Versions extends VersionCenter {
6365
* <li> Add "enableRemote" option for {@link WebProcessMonitor}
6466
* <li> Add {@link FilterPathMatchStrategy} for {@link WebContainer#lookupFilter(String)}
6567
* <li> Add {@link RegExrFilterPathMatchStrategy},{@link MixedFilterPathMatchStrategy}
68+
* <li> Add {@link ScanWay},{@link PackageScanner} and its two implementations; make framework support scan package from jar file
6669
* </ol>
6770
* </ul>
6871
*/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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: ScanWay.java
20+
* @Package com.openthinks.easyweb.annotation.configure
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.configure;
27+
28+
import java.lang.annotation.ElementType;
29+
import java.lang.annotation.Retention;
30+
import java.lang.annotation.RetentionPolicy;
31+
import java.lang.annotation.Target;
32+
33+
/**
34+
* @author dailey.yet@outlook.com
35+
*
36+
*/
37+
@Retention(RetentionPolicy.RUNTIME)
38+
@Target(ElementType.TYPE)
39+
public @interface ScanWay {
40+
ScanWayEnum value() default ScanWayEnum.FILE_PATH;
41+
42+
public enum ScanWayEnum {
43+
/**
44+
* this way will base on file path;it is suit for web application, which deploy as folder or war
45+
*/
46+
FILE_PATH,
47+
/**
48+
* this way will use third party library(reflections);it is support java application, which deploy as jar file
49+
*/
50+
ADVANCE;
51+
}
52+
}
Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
package com.openthinks.easyweb.annotation.process.core;
22

3-
import java.io.File;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
7-
import com.openthinks.easyweb.WebStatic;
8-
import com.openthinks.easyweb.WebUtils;
9-
import com.openthinks.easyweb.annotation.process.filter.file.ControllerFileFilterVistor;
10-
import com.openthinks.easyweb.annotation.process.filter.file.FilterFileFilterVistor;
3+
import com.openthinks.easyweb.annotation.process.core.scaner.PackageScanner;
4+
import com.openthinks.easyweb.annotation.process.core.scaner.Scanners;
115
import com.openthinks.easyweb.annotation.process.objects.WebContainer;
12-
import com.openthinks.easyweb.annotation.process.objects.WebController;
13-
import com.openthinks.easyweb.annotation.process.objects.WebFilter;
146
import com.openthinks.easyweb.context.WebConfigure;
157
import com.openthinks.easyweb.context.WebContexts;
16-
import com.openthinks.libs.utilities.logger.ProcessLogger;
178

189
/**
1910
* Web container processor
@@ -27,87 +18,10 @@ public WebContainer process() {
2718
WebContainer container = WebContexts.get().getWebContainer();
2819
container.reset();
2920
WebConfigure configure = WebContexts.get().getWebConfigure();
30-
for (String pack : configure.getScanPackages()) {
31-
String path = WebUtils.getPackPath(pack);
32-
scanPackageFilter(container, path);
33-
scanPackageController(container, path);
34-
}
21+
PackageScanner packageScanner = Scanners.get(configure.getScanWay());
22+
packageScanner.scan(container, configure.getScanPackages());
3523
container.buildMapping();
3624
return container;
3725
}
3826

39-
/**
40-
* @param container
41-
* @param filterProcesser
42-
* @param path
43-
*/
44-
protected void scanPackageFilter(WebContainer container, String path) {
45-
WebFilterProcesser filterProcesser = new WebFilterProcesser();
46-
List<Class<?>> filterClasses = filterFilterClass(path);
47-
List<Object> filters = getFilterInstances(filterClasses);
48-
for (Object filter : filters) {
49-
filterProcesser.setPropertie(WebStatic.WEB_FILTER, filter);
50-
WebFilter child = filterProcesser.process();
51-
container.add(child);
52-
}
53-
}
54-
55-
/**
56-
* @param container
57-
* @param controllerProcesser
58-
* @param path
59-
*/
60-
protected void scanPackageController(WebContainer container, String path) {
61-
WebControllerProcesser controllerProcesser = new WebControllerProcesser();
62-
List<Class<?>> controllerClasses = filterControllerClass(path);
63-
List<Object> controllers = getControllerInstances(controllerClasses);
64-
for (Object controller : controllers) {
65-
controllerProcesser.setPropertie(WebStatic.WEB_CONTROLLER, controller);
66-
WebController child = controllerProcesser.process();
67-
container.add(child);
68-
}
69-
}
70-
71-
private List<Object> getFilterInstances(List<Class<?>> filterClasses) {
72-
List<Object> filterInstance = new ArrayList<>();
73-
for (Class<?> clazz : filterClasses) {
74-
try {
75-
filterInstance.add(clazz.newInstance());
76-
} catch (InstantiationException e) {
77-
ProcessLogger.warn(e);
78-
} catch (IllegalAccessException e) {
79-
ProcessLogger.warn(e);
80-
}
81-
}
82-
return filterInstance;
83-
}
84-
85-
private List<Class<?>> filterFilterClass(String packPath) {
86-
File dir = new File(packPath);
87-
List<Class<?>> filterClasss = new ArrayList<>();
88-
dir.listFiles(new FilterFileFilterVistor(filterClasss));
89-
return filterClasss;
90-
}
91-
92-
private List<Object> getControllerInstances(List<Class<?>> controllerClasses) {
93-
List<Object> controllerInstance = new ArrayList<>();
94-
for (Class<?> clazz : controllerClasses) {
95-
try {
96-
controllerInstance.add(clazz.newInstance());
97-
} catch (InstantiationException e) {
98-
ProcessLogger.warn(e);
99-
} catch (IllegalAccessException e) {
100-
ProcessLogger.warn(e);
101-
}
102-
}
103-
return controllerInstance;
104-
}
105-
106-
private List<Class<?>> filterControllerClass(String packPath) {
107-
File dir = new File(packPath);
108-
List<Class<?>> controllerClasss = new ArrayList<>();
109-
dir.listFiles(new ControllerFileFilterVistor(controllerClasss));
110-
return controllerClasss;
111-
}
112-
11327
}

easywebframework/src/main/java/com/openthinks/easyweb/annotation/process/core/WebControllerProcesser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Web controller processor
1212
* @author dailey.yet@outlook.com
1313
*/
14-
class WebControllerProcesser extends AbstractProcesser<WebController> {
14+
public class WebControllerProcesser extends AbstractProcesser<WebController> {
1515
@Override
1616
public WebController process() {
1717
Object instance = this.getPropertie(WebStatic.WEB_CONTROLLER);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)