Skip to content

Commit f80b008

Browse files
committed
🎉finish some helpers
0 parents  commit f80b008

File tree

19 files changed

+1002
-0
lines changed

19 files changed

+1002
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ide
2+
.idea
3+
*.iml
4+
5+
# java
6+
target

pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.github.hgaol</groupId>
6+
<artifactId>uiharu</artifactId>
7+
<packaging>war</packaging>
8+
<version>1.0.0</version>
9+
<name>uiharu</name>
10+
11+
<dependencies>
12+
13+
<!-- apache commons -->
14+
<dependency>
15+
<groupId>org.apache.commons</groupId>
16+
<artifactId>commons-lang3</artifactId>
17+
<version>3.3.2</version>
18+
</dependency>
19+
<!-- apache commons -->
20+
21+
<!-- mysql jdbc -->
22+
<dependency>
23+
<groupId>mysql</groupId>
24+
<artifactId>mysql-connector-java</artifactId>
25+
<version>5.1.33</version>
26+
<scope>runtime</scope>
27+
</dependency>
28+
<!-- mysql jdbc -->
29+
30+
<!-- SLF4J -->
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-log4j12</artifactId>
34+
<version>1.7.25</version>
35+
</dependency>
36+
<!-- SLF4J -->
37+
38+
<!-- demo -->
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>RELEASE</version>
43+
<scope>test</scope>
44+
</dependency>
45+
<!-- demo -->
46+
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<!-- Compile -->
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.3</version>
56+
<configuration>
57+
<source>1.8</source>
58+
<target>1.8</target>
59+
</configuration>
60+
</plugin>
61+
<!-- Test -->
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-surefire-plugin</artifactId>
65+
<version>2.18.1</version>
66+
<configuration>
67+
<skipTests>true</skipTests>
68+
</configuration>
69+
</plugin>
70+
<!-- Source -->
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-source-plugin</artifactId>
74+
<version>3.0.1</version>
75+
<executions>
76+
<execution>
77+
<id>attach-sources</id>
78+
<phase>package</phase>
79+
<goals>
80+
<goal>jar-no-fork</goal>
81+
</goals>
82+
</execution>
83+
</executions>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
88+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.hgaol.uiharu.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* @author: gaohan
10+
* @date: 2018-08-21 17:42
11+
**/
12+
@Target(ElementType.TYPE)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface Controller {
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.hgaol.uiharu.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* @author: gaohan
10+
* @date: 2018-08-21 17:41
11+
**/
12+
@Target(ElementType.TYPE)
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface Service {
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.hgaol.uiharu.constant;
2+
3+
/**
4+
* @author: gaohan
5+
* @date: 2018-08-21 15:05
6+
**/
7+
public class ConfigConstants {
8+
9+
public static final String CONFIG_FILE = "uiharu.properties";
10+
11+
public static final String JDBC_DRIVER = "uiharu.framework.jdbc.driver";
12+
public static final String JDBC_URL = "uiharu.framework.jdbc.url";
13+
public static final String JDBC_USERNAME = "uiharu.framework.jdbc.username";
14+
public static final String JDBC_PASSWORD = "uiharu.framework.jdbc.password";
15+
16+
public static final String APP_BASE_PACKAGE = "uiharu.framework.app.base_package";
17+
public static final String APP_JSP_PATH = "uiharu.smart.framework.app.jsp_path";
18+
public static final String APP_ASSET_PATH = "uiharu.framework.app.asset_path";
19+
public static final String APP_UPLOAD_LIMIT = "uiharu.framework.app.upload_limit";
20+
21+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.github.hgaol.uiharu.helper;
2+
3+
import com.github.hgaol.uiharu.util.ReflectionUtils;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
/**
12+
* 获取Bean映射,获取Bean实例,设置Bean实例
13+
*
14+
* @author: gaohan
15+
* @date: 2018-08-21 19:36
16+
**/
17+
public class BeanHelper {
18+
19+
private static final Logger log = LoggerFactory.getLogger(ConfigHelper.class);
20+
21+
private static final Map<Class<?>, Object> BEAN_MAP = new HashMap<>();
22+
23+
/**
24+
* 初始化Bean映射
25+
*/
26+
static {
27+
Set<Class<?>> beanClassSet = ClassHelper.getBeanClassSet();
28+
for (Class<?> cls : beanClassSet) {
29+
BEAN_MAP.put(cls, ReflectionUtils.newInstance(cls));
30+
}
31+
}
32+
33+
/**
34+
* 获取Bean映射
35+
*/
36+
public static Map<Class<?>, Object> getBeanMap() {
37+
return BEAN_MAP;
38+
}
39+
40+
/**
41+
* 获取Bean实例
42+
*/
43+
public static <T> T getBean(Class<T> cls) {
44+
if (!BEAN_MAP.containsKey(cls)) {
45+
throw new Error("can not find bean by class: " + cls);
46+
}
47+
return (T) BEAN_MAP.get(cls);
48+
}
49+
50+
/**
51+
* 设置Bean实例
52+
*/
53+
public static void setBean(Class<?> cls, Object obj) {
54+
BEAN_MAP.put(cls, obj);
55+
}
56+
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.hgaol.uiharu.helper;
2+
3+
import com.github.hgaol.uiharu.annotation.Controller;
4+
import com.github.hgaol.uiharu.annotation.Service;
5+
import com.github.hgaol.uiharu.util.ClassUtils;
6+
7+
import java.lang.annotation.Annotation;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
/**
12+
* @author: gaohan
13+
* @date: 2018-08-21 17:30
14+
**/
15+
public class ClassHelper {
16+
17+
private static final Set<Class<?>> CLASS_SET;
18+
19+
static {
20+
String basePackage = ConfigHelper.getAppBasePackage();
21+
CLASS_SET = ClassUtils.getClassSet(basePackage);
22+
}
23+
24+
public static Set<Class<?>> getClassSet() {
25+
return CLASS_SET;
26+
}
27+
28+
public static Set<Class<?>> getServiceClassSet() {
29+
return getClassSetByAnnotation(Service.class);
30+
}
31+
32+
public static Set<Class<?>> getControllerClassSet() {
33+
return getClassSetByAnnotation(Controller.class);
34+
}
35+
36+
public static Set<Class<?>> getBeanClassSet() {
37+
Set<Class<?>> classSet = new HashSet<>();
38+
classSet.addAll(getServiceClassSet());
39+
classSet.addAll(getControllerClassSet());
40+
return classSet;
41+
}
42+
43+
/**
44+
* 获取scan-path下某父类(or接口)的所有子类(or实现类)
45+
*/
46+
public static Set<Class<?>> getClassSetBySuper(Class<?> superClass) {
47+
Set<Class<?>> classSet = new HashSet<Class<?>>();
48+
for (Class<?> cls : CLASS_SET) {
49+
if (superClass.isAssignableFrom(cls) && !superClass.equals(cls)) {
50+
classSet.add(cls);
51+
}
52+
}
53+
return classSet;
54+
}
55+
56+
/**
57+
* 获取scan-path下带有某种注解的类
58+
*/
59+
private static Set<Class<?>> getClassSetByAnnotation(Class<? extends Annotation> clazz) {
60+
Set<Class<?>> classSet = new HashSet<>();
61+
for (Class<?> cls : CLASS_SET) {
62+
if (cls.isAnnotationPresent(clazz)) {
63+
classSet.add(cls);
64+
}
65+
}
66+
return classSet;
67+
}
68+
69+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.github.hgaol.uiharu.helper;
2+
3+
import com.github.hgaol.uiharu.util.PropUtils;
4+
import com.github.hgaol.uiharu.constant.ConfigConstants;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.Properties;
9+
10+
/**
11+
* @author: gaohan
12+
* @date: 2018-08-21 14:04
13+
**/
14+
public class ConfigHelper {
15+
16+
private static final Logger log = LoggerFactory.getLogger(ConfigHelper.class);
17+
18+
private static final Properties CONFIG_PROPS = PropUtils.loadProps(ConfigConstants.CONFIG_FILE);
19+
20+
/**
21+
* 获取 JDBC 驱动
22+
*/
23+
public static String getJdbcDriver() {
24+
return PropUtils.getString(CONFIG_PROPS, ConfigConstants.JDBC_DRIVER);
25+
}
26+
27+
/**
28+
* 获取 JDBC URL
29+
*/
30+
public static String getJdbcUrl() {
31+
return PropUtils.getString(CONFIG_PROPS, ConfigConstants.JDBC_URL);
32+
}
33+
34+
/**
35+
* 获取 JDBC 用户名
36+
*/
37+
public static String getJdbcUsername() {
38+
return PropUtils.getString(CONFIG_PROPS, ConfigConstants.JDBC_USERNAME);
39+
}
40+
41+
/**
42+
* 获取 JDBC 密码
43+
*/
44+
public static String getJdbcPassword() {
45+
return PropUtils.getString(CONFIG_PROPS, ConfigConstants.JDBC_PASSWORD);
46+
}
47+
48+
/**
49+
* 获取应用基础包名
50+
*/
51+
public static String getAppBasePackage() {
52+
return PropUtils.getString(CONFIG_PROPS, ConfigConstants.APP_BASE_PACKAGE);
53+
}
54+
55+
/**
56+
* 获取应用 JSP 路径
57+
*/
58+
public static String getAppJspPath() {
59+
return PropUtils.getString(CONFIG_PROPS, ConfigConstants.APP_JSP_PATH, "/WEB-INF/view/");
60+
}
61+
62+
/**
63+
* 获取应用静态资源路径
64+
*/
65+
public static String getAppAssetPath() {
66+
return PropUtils.getString(CONFIG_PROPS, ConfigConstants.APP_ASSET_PATH, "/asset/");
67+
}
68+
69+
/**
70+
* 获取应用文件上传限制
71+
*/
72+
public static int getAppUploadLimit() {
73+
return PropUtils.getInt(CONFIG_PROPS, ConfigConstants.APP_UPLOAD_LIMIT, 10);
74+
}
75+
76+
/**
77+
* 根据属性名获取 String 类型的属性值
78+
*/
79+
public static String getString(String key) {
80+
return PropUtils.getString(CONFIG_PROPS, key);
81+
}
82+
83+
/**
84+
* 根据属性名获取 int 类型的属性值
85+
*/
86+
public static int getInt(String key) {
87+
return PropUtils.getInt(CONFIG_PROPS, key);
88+
}
89+
90+
}

0 commit comments

Comments
 (0)