Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13a4264488 | ||
|
|
7ffb65cc38 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,3 +10,4 @@
|
|||||||
|
|
||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
hs_err_pid*
|
hs_err_pid*
|
||||||
|
/target/
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<h1>项目介绍</h1>
|
<h1>项目介绍</h1>
|
||||||
<p>
|
<p>
|
||||||
Redkale (中文名: 红菜苔,一种湖北特产蔬菜) 是基于Java 8全新的微服务框架, 包含HTTP、WebSocket、TCP/UDP、数据序列化、数据缓存、依赖注入等功能。 本框架致力于简化集中式和微服务架构的开发,在增强开发敏捷性的同时保持高性能。
|
Redkale (中文名: 红菜苔,一种湖北特产蔬菜) 是基于Java 11全新的微服务框架, 包含HTTP、WebSocket、TCP/UDP、数据序列化、数据缓存、依赖注入等功能。 本框架致力于简化集中式和微服务架构的开发,在增强开发敏捷性的同时保持高性能。
|
||||||
</p>
|
</p>
|
||||||
<strong>RedKale 有如下主要特点:</strong>
|
<strong>RedKale 有如下主要特点:</strong>
|
||||||
<ol>
|
<ol>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ APP_HOME=`dirname "$0"`
|
|||||||
|
|
||||||
if [ ! -f "$APP_HOME"/conf/application.xml ]; then
|
if [ ! -f "$APP_HOME"/conf/application.xml ]; then
|
||||||
APP_HOME="$APP_HOME"/..
|
APP_HOME="$APP_HOME"/..
|
||||||
fi
|
fi
|
||||||
|
|
||||||
lib='.'
|
lib='.'
|
||||||
for jar in `ls $APP_HOME/lib/*.jar`
|
for jar in `ls $APP_HOME/lib/*.jar`
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<application nodeid="10000" port="2121">
|
<application nodeid="10000" port="2020">
|
||||||
|
|
||||||
<!-- 详细配置说明见: http://redkale.org/redkale.html#redkale_confxml -->
|
|
||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
<server protocol="HTTP" port="6060">
|
<server protocol="HTTP" port="5050">
|
||||||
|
|
||||||
<request>
|
<request>
|
||||||
<remoteaddr value="request.headers.X-RemoteAddress"/>
|
<remoteaddr value="request.headers.X-RemoteAddress"/>
|
||||||
@@ -24,7 +22,7 @@
|
|||||||
|
|
||||||
<filters autoload="true"/>
|
<filters autoload="true"/>
|
||||||
|
|
||||||
<rest path="/pipes" /> <!-- base指定的自定义HttpServlet子类必须标记@HttpUserType, 不设置base则视为没有当前用户信息设置 -->
|
<rest path="/pipes" />
|
||||||
|
|
||||||
<servlets path="/pipes" autoload="true" />
|
<servlets path="/pipes" autoload="true" />
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<persistence version="2.0">
|
||||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
|
|
||||||
|
|
||||||
<persistence-unit name="" transaction-type="RESOURCE_LOCAL">
|
<persistence-unit name="" transaction-type="RESOURCE_LOCAL">
|
||||||
<shared-cache-mode>ALL</shared-cache-mode>
|
<shared-cache-mode>ALL</shared-cache-mode>
|
||||||
@@ -25,7 +24,7 @@
|
|||||||
<shared-cache-mode>ALL</shared-cache-mode>
|
<shared-cache-mode>ALL</shared-cache-mode>
|
||||||
<properties>
|
<properties>
|
||||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/center?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"/>
|
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/center?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"/>
|
||||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||||
<property name="javax.persistence.jdbc.password" value="1234"/>
|
<property name="javax.persistence.jdbc.password" value="1234"/>
|
||||||
</properties>
|
</properties>
|
||||||
</persistence-unit>
|
</persistence-unit>
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>jarĬ<EFBFBD>Ϸ<EFBFBD><EFBFBD>ڴ˴<EFBFBD>
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>jarĬ<EFBFBD>Ϸ<EFBFBD><EFBFBD>ڴ˴<EFBFBD>
|
||||||
37
my/pom.xml
37
my/pom.xml
@@ -4,16 +4,33 @@
|
|||||||
<groupId>org.redkale</groupId>
|
<groupId>org.redkale</groupId>
|
||||||
<artifactId>redkale</artifactId>
|
<artifactId>redkale</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
<name>RedkaleProject</name>
|
||||||
<url>http://redkale.org</url>
|
<url>http://redkale.org</url>
|
||||||
<description>redkale -- java framework</description>
|
<description>redkale -- java framework</description>
|
||||||
<version>2.2.0</version>
|
<version>2.5.0</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.7.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
<name>Apache 2</name>
|
<name>Apache 2</name>
|
||||||
<url>http://www.apache.org/licenses/</url>
|
<url>https://www.apache.org/licenses/</url>
|
||||||
<distribution>repo</distribution>
|
<distribution>repo</distribution>
|
||||||
<comments>Apache License</comments>
|
<comments>Apache License</comments>
|
||||||
</license>
|
</license>
|
||||||
</licenses>
|
</licenses>
|
||||||
|
|
||||||
<developers>
|
<developers>
|
||||||
@@ -21,13 +38,13 @@
|
|||||||
<id>Redkale</id>
|
<id>Redkale</id>
|
||||||
<name>redkale</name>
|
<name>redkale</name>
|
||||||
<email>redkale@qq.com</email>
|
<email>redkale@qq.com</email>
|
||||||
<url>http://redkale.org</url>
|
<url>https://redkale.org</url>
|
||||||
<roles>
|
<roles>
|
||||||
<role>Project Manager</role>
|
<role>Project Manager</role>
|
||||||
<role>Architect</role>
|
<role>Architect</role>
|
||||||
</roles>
|
</roles>
|
||||||
<organization>redkale</organization>
|
<organization>redkale</organization>
|
||||||
<organizationUrl>http://redkale.org</organizationUrl>
|
<organizationUrl>https://redkale.org</organizationUrl>
|
||||||
<properties>
|
<properties>
|
||||||
<dept>No</dept>
|
<dept>No</dept>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -35,12 +52,6 @@
|
|||||||
</developer>
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
<name>Redkale</name>
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
<snapshotRepository>
|
<snapshotRepository>
|
||||||
<id>ossrh</id>
|
<id>ossrh</id>
|
||||||
@@ -51,11 +62,13 @@
|
|||||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
<scm>
|
<scm>
|
||||||
<url>https://github.com/redkale/redkale</url>
|
<url>https://github.com/redkale/redkale</url>
|
||||||
<connection>scm:git:git@github.com/redkale/redkale.git</connection>
|
<connection>scm:git:git@github.com/redkale/redkale.git</connection>
|
||||||
<developerConnection>scm:git:git@github.com:redkale/redkale.git</developerConnection>
|
<developerConnection>scm:git:git@github.com:redkale/redkale.git</developerConnection>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@@ -99,6 +112,7 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
@@ -111,6 +125,7 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
|||||||
@@ -20,7 +20,8 @@
|
|||||||
</profile>
|
</profile>
|
||||||
|
|
||||||
<profile>
|
<profile>
|
||||||
<id>release</id>
|
<id>release</id>
|
||||||
|
<!--
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@@ -92,6 +93,7 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
-->
|
||||||
</profile>
|
</profile>
|
||||||
</profiles>
|
</profiles>
|
||||||
</settings>
|
</settings>
|
||||||
138
pom.xml
Normal file
138
pom.xml
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.redkale</groupId>
|
||||||
|
<artifactId>redkale</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>RedkaleProject</name>
|
||||||
|
<url>https://redkale.org</url>
|
||||||
|
<description>redkale -- java framework</description>
|
||||||
|
<version>2.5.0</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
|
||||||
|
<junit.version>5.7.0</junit.version>
|
||||||
|
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
|
||||||
|
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
||||||
|
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
|
||||||
|
<maven-failsafe-plugin.version>3.0.0-M5</maven-failsafe-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>Apache 2</name>
|
||||||
|
<url>https://www.apache.org/licenses/</url>
|
||||||
|
<distribution>repo</distribution>
|
||||||
|
<comments>Apache License</comments>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>central</id>
|
||||||
|
<name>Central Repository</name>
|
||||||
|
<url>https://repo.maven.apache.org/maven2</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>sonatype-nexus-snapshots</id>
|
||||||
|
<name>Sonatype Nexus Snapshots</name>
|
||||||
|
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<developers>
|
||||||
|
<developer>
|
||||||
|
<id>Redkale</id>
|
||||||
|
<name>redkale</name>
|
||||||
|
<email>redkale@qq.com</email>
|
||||||
|
<url>https://redkale.org</url>
|
||||||
|
<roles>
|
||||||
|
<role>Project Manager</role>
|
||||||
|
<role>Architect</role>
|
||||||
|
</roles>
|
||||||
|
<organization>redkale</organization>
|
||||||
|
<organizationUrl>https://redkale.org</organizationUrl>
|
||||||
|
<properties>
|
||||||
|
<dept>No</dept>
|
||||||
|
</properties>
|
||||||
|
<timezone>8</timezone>
|
||||||
|
</developer>
|
||||||
|
</developers>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<url>https://github.com/redkale/redkale</url>
|
||||||
|
<connection>scm:git:git@github.com/redkale/redkale.git</connection>
|
||||||
|
<developerConnection>scm:git:git@github.com:redkale/redkale.git</developerConnection>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>-parameters</arg>
|
||||||
|
</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!-- 需要注释掉, 否则会生成native-image配置信息
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.redkale.maven.plugins</groupId>
|
||||||
|
<artifactId>redkale-maven-plugin</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>redkale-compile</id>
|
||||||
|
<phase>process-classes</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
-->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>${maven-jar-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<addMavenDescriptor>false</addMavenDescriptor>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>org.redkale.boot.Application</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-failsafe-plugin</artifactId>
|
||||||
|
<version>${maven-failsafe-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
@@ -136,12 +136,12 @@
|
|||||||
如果name是mimetype.property.开头的值将会在进程启动时进行MimeType.add("yyyy", "YYYYYY")操作。
|
如果name是mimetype.property.开头的值将会在进程启动时进行MimeType.add("yyyy", "YYYYYY")操作。
|
||||||
load: 加载文件,多个用;隔开。
|
load: 加载文件,多个用;隔开。
|
||||||
默认置入的system.property.的有:
|
默认置入的system.property.的有:
|
||||||
System.setProperty("net.transport.poolmaxconns", "100");
|
System.setProperty("redkale.net.transport.poolmaxconns", "100");
|
||||||
System.setProperty("net.transport.pinginterval", "30");
|
System.setProperty("redkale.net.transport.pinginterval", "30");
|
||||||
System.setProperty("net.transport.checkinterval", "30");
|
System.setProperty("redkale.net.transport.checkinterval", "30");
|
||||||
System.setProperty("convert.tiny", "true");
|
System.setProperty("redkale.convert.tiny", "true");
|
||||||
System.setProperty("convert.pool.size", "128");
|
System.setProperty("redkale.convert.pool.size", "128");
|
||||||
System.setProperty("convert.writer.buffer.defsize", "4096");
|
System.setProperty("redkale.convert.writer.buffer.defsize", "4096");
|
||||||
|
|
||||||
<properties>节点下也可包含非<property>节点.
|
<properties>节点下也可包含非<property>节点.
|
||||||
非<property>其节点可以通过@Resource(name="properties.xxxxxx")进行注入, 被注解的字段类型只能是AnyValue、AnyValue[]
|
非<property>其节点可以通过@Resource(name="properties.xxxxxx")进行注入, 被注解的字段类型只能是AnyValue、AnyValue[]
|
||||||
@@ -164,30 +164,38 @@
|
|||||||
excludelibs: 排除lib.path与excludes中的正则表达式匹配的路径, 多个正则表达式用分号;隔开
|
excludelibs: 排除lib.path与excludes中的正则表达式匹配的路径, 多个正则表达式用分号;隔开
|
||||||
charset: 文本编码, 默认: UTF-8
|
charset: 文本编码, 默认: UTF-8
|
||||||
backlog: 默认10K
|
backlog: 默认10K
|
||||||
threads: 线程数, 默认: CPU核数*2,最小8个【已废弃 @since 2.3.0】
|
threads【已废弃】: 线程数, 默认: CPU核数*2,最小8个【已废弃 @since 2.3.0】
|
||||||
maxconns: 最大连接数, 小于1表示无限制, 默认: 0
|
maxconns: 最大连接数, 小于1表示无限制, 默认: 0
|
||||||
maxbody: request.body最大值, 默认: 64K
|
maxbody: request.body最大值, 默认: 64K
|
||||||
bufferCapacity: ByteBuffer的初始化大小, TCP默认: 32K; (HTTP 2.0、WebSocket,必须要16k以上); UDP默认: 1350B
|
bufferCapacity: ByteBuffer的初始化大小, TCP默认: 32K; (HTTP 2.0、WebSocket,必须要16k以上); UDP默认: 1350B
|
||||||
bufferPoolSize: ByteBuffer池的大小,默认: 线程数*4
|
bufferPoolSize: ByteBuffer池的大小,默认: 线程数*4
|
||||||
responsePoolSize: Response池的大小,默认: 线程数*2
|
responsePoolSize: Response池的大小,默认: 1024
|
||||||
aliveTimeoutSeconds: KeepAlive读操作超时秒数, 默认30, 0表示永久不超时; -1表示禁止KeepAlive
|
aliveTimeoutSeconds: KeepAlive读操作超时秒数, 默认30, 0表示永久不超时; -1表示禁止KeepAlive
|
||||||
readTimeoutSeconds: 读操作超时秒数, 默认0, 表示永久不超时
|
readTimeoutSeconds: 读操作超时秒数, 默认0, 表示永久不超时
|
||||||
writeTimeoutSeconds: 写操作超时秒数, 默认0, 表示永久不超时
|
writeTimeoutSeconds: 写操作超时秒数, 默认0, 表示永久不超时
|
||||||
iogroup: 流线程组AsyncGroup对象,如果值为client,表示和Application.asyncGroup对象共用
|
|
||||||
interceptor: 启动/关闭NodeServer时被调用的拦截器实现类,必须是org.redkale.boot.NodeInterceptor的子类,默认为null
|
interceptor: 启动/关闭NodeServer时被调用的拦截器实现类,必须是org.redkale.boot.NodeInterceptor的子类,默认为null
|
||||||
-->
|
-->
|
||||||
<server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib="">
|
<server protocol="HTTP" host="127.0.0.1" port="6060" root="root" lib="">
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
【节点在<server>中唯一】
|
【节点在<server>中唯一】
|
||||||
value: 创建SSLContext的实现类, 可自定义,必须是org.redkale.net.SSLCreator的子类
|
builder: 创建SSLContext的实现类, 可自定义,必须是org.redkale.net.SSLBuilder的子类
|
||||||
clientauth: true/false/want
|
sslProvider: java.security.Provider自定义的实现类,如第三方: org.conscrypt.OpenSSLProvider、org.bouncycastle.jce.provider.BouncyCastleProvider
|
||||||
keystorepass: KEY密码
|
jsseProvider: java.security.Provider自定义的实现类,如第三方: org.conscrypt.JSSEProvider、 org.bouncycastle.jce.provider.BouncyCastleJsseProvider
|
||||||
keystorefile: KEY文件
|
protocol: TLS版本,默认值: TLS
|
||||||
truststorepass: TRUST密码
|
protocols: 设置setEnabledProtocols, 多个用,隔开 如: TLSv1.2,TLSv1.3
|
||||||
truststorefile: TRUST文件
|
clientAuth: WANT/NEED/NONE, 默认值: NONE
|
||||||
|
ciphers: 设置setEnabledCipherSuites, 多个用,隔开 如: TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256
|
||||||
|
keystorePass: KEY密码
|
||||||
|
keystoreFile: KEY文件 .jks
|
||||||
|
keystoreType: KEY类型, 默认值为JKS
|
||||||
|
keystoreAlgorithm: KEY文件的algorithm, 默认值为SunX509
|
||||||
|
truststorePass: TRUST密码
|
||||||
|
truststoreFile: TRUST文件
|
||||||
|
truststoreType: TRUST类型, 默认值为JKS
|
||||||
|
truststoreAlgorithm: TRUST文件的algorithm, 默认值为SunX509
|
||||||
-->
|
-->
|
||||||
<ssl creator=""/>
|
<ssl builder=""/>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
加载所有的Service服务;
|
加载所有的Service服务;
|
||||||
@@ -296,7 +304,7 @@
|
|||||||
period>0表示定时获取时间; 设置1000表示每秒刷新Date时间
|
period>0表示定时获取时间; 设置1000表示每秒刷新Date时间
|
||||||
-->
|
-->
|
||||||
<response>
|
<response>
|
||||||
<contenttype plain="text/plain; charset=utf-8" json="application/json; charset=utf-8"/>
|
<content-type plain="text/plain; charset=utf-8" json="application/json; charset=utf-8"/>
|
||||||
<defcookie domain="" path=""/>
|
<defcookie domain="" path=""/>
|
||||||
<addheader name="Access-Control-Allow-Origin" value="request.headers.Origin" />
|
<addheader name="Access-Control-Allow-Origin" value="request.headers.Origin" />
|
||||||
<setheader name="Access-Control-Allow-Headers" value="request.headers.Access-Control-Request-Headers"/>
|
<setheader name="Access-Control-Allow-Headers" value="request.headers.Access-Control-Request-Headers"/>
|
||||||
@@ -12,29 +12,18 @@
|
|||||||
是否开启缓存(标记为@Cacheable的Entity类),值目前只支持两种: ALL: 所有开启缓存。 NONE: 关闭所有缓存, 非NONE字样统一视为ALL
|
是否开启缓存(标记为@Cacheable的Entity类),值目前只支持两种: ALL: 所有开启缓存。 NONE: 关闭所有缓存, 非NONE字样统一视为ALL
|
||||||
-->
|
-->
|
||||||
<property name="javax.persistence.cachemode" value="ALL"/>
|
<property name="javax.persistence.cachemode" value="ALL"/>
|
||||||
|
<!--
|
||||||
|
是否自动建表当表不存在的时候, 目前只支持mysql、postgres, 默认为false
|
||||||
|
-->
|
||||||
|
<property name="javax.persistence.table.autoddl" value="false"/>
|
||||||
|
|
||||||
<!-- 多个URL用;隔开,如分布式SearchSource需要配多个URL -->
|
<!-- 多个URL用;隔开,如分布式SearchSource需要配多个URL -->
|
||||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/dbuser?characterEncoding=utf8"/>
|
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/dbuser?characterEncoding=utf8"/>
|
||||||
<!--
|
|
||||||
javax.persistence.jdbc.driver在JPA的值是JDBC驱动,Redkale有所不同,值应该是javax.sql.DataSource的子类。
|
|
||||||
为了兼容用户习惯,Redkale内置常见JDBC驱动到javax.sql.DataSource的映射关系:
|
|
||||||
org.mariadb.jdbc.Driver —————— org.mariadb.jdbc.MySQLDataSource
|
|
||||||
org.postgresql.Driver —————— org.postgresql.ds.PGConnectionPoolDataSource
|
|
||||||
com.mysql.jdbc.Driver —————— com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
|
|
||||||
com.mysql.cj.jdbc.Driver —————— com.mysql.cj.jdbc.MysqlConnectionPoolDataSource
|
|
||||||
oracle.jdbc.driver.OracleDriver —————— oracle.jdbc.pool.OracleConnectionPoolDataSource
|
|
||||||
com.microsoft.sqlserver.jdbc.SQLServerDriver —————— com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource
|
|
||||||
org.h2.Driver —————— org.h2.jdbcx.JdbcDataSource
|
|
||||||
因此 com.mysql.jdbc.Driver 会被自动转换成 com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
|
|
||||||
并且如果JDBC驱动是以上几个版本,javax.persistence.jdbc.driver属性都可以省略,Redkale会根据javax.persistence.jdbc.url的值来识别驱动
|
|
||||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
|
||||||
<property name="javax.persistence.jdbc.source" value="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"/>
|
|
||||||
-->
|
|
||||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||||
<property name="javax.persistence.jdbc.password" value="123456"/>
|
<property name="javax.persistence.jdbc.password" value="123456"/>
|
||||||
|
|
||||||
<!-- 最大连接数,默认值:CPU数*16 -->
|
<!-- 最大连接数,默认值:CPU数 -->
|
||||||
<property name="javax.persistence.connections.limit" value="32"/>
|
<property name="javax.persistence.connections.limit" value="12"/>
|
||||||
|
|
||||||
<!-- 包含的SQL模板,相当于反向LIKE,不同的JDBC驱动的SQL语句不一样,Redkale内置了MySQL的语句 -->
|
<!-- 包含的SQL模板,相当于反向LIKE,不同的JDBC驱动的SQL语句不一样,Redkale内置了MySQL的语句 -->
|
||||||
<property name="javax.persistence.contain.sqltemplate" value="LOCATE(${keystr}, ${column}) > 0"/>
|
<property name="javax.persistence.contain.sqltemplate" value="LOCATE(${keystr}, ${column}) > 0"/>
|
||||||
@@ -42,7 +31,7 @@
|
|||||||
|
|
||||||
<!-- 复制表结构的SQL模板,Redkale内置了MySQL的语句 -->
|
<!-- 复制表结构的SQL模板,Redkale内置了MySQL的语句 -->
|
||||||
<property name="javax.persistence.tablenotexist.sqlstates" value="42000;42S02"/>
|
<property name="javax.persistence.tablenotexist.sqlstates" value="42000;42S02"/>
|
||||||
<property name="javax.persistence.tablecopy.sqltemplate" value="CREATE TABLE ${newtable} LIKE ${oldtable}"/>
|
<property name="javax.persistence.tablecopy.sqltemplate" value="CREATE TABLE IF NOT EXISTS ${newtable} LIKE ${oldtable}"/>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
</persistence-unit>
|
</persistence-unit>
|
||||||
@@ -23,11 +23,17 @@ import java.lang.annotation.Target;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 值越大,优先级越高
|
* 值越大,优先级越高
|
||||||
*
|
*
|
||||||
* @since Common Annotations 1.2
|
* @since Common Annotations 1.2
|
||||||
*/
|
*/
|
||||||
@Target({ElementType.TYPE})
|
@Target({ElementType.TYPE})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Priority {
|
public @interface Priority {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 优先级值
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
int value();
|
int value();
|
||||||
}
|
}
|
||||||
@@ -16,17 +16,68 @@ import java.lang.annotation.Target;
|
|||||||
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
|
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Resource {
|
public @interface Resource {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AuthenticationType
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public enum AuthenticationType {
|
public enum AuthenticationType {
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
CONTAINER,
|
CONTAINER,
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
APPLICATION
|
APPLICATION
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资源名称
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
public String name() default "";
|
public String name() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 依赖注入的类型
|
||||||
|
*
|
||||||
|
* @return Class
|
||||||
|
*/
|
||||||
public Class<?> type() default Object.class;
|
public Class<?> type() default Object.class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return AuthenticationType
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
|
public AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public boolean shareable() default true;
|
public boolean shareable() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public String description() default "";
|
public String description() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public String mappedName() default "";
|
public String mappedName() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public String lookup() default "";
|
public String lookup() default "";
|
||||||
}
|
}
|
||||||
@@ -81,12 +81,19 @@ public @interface Column {
|
|||||||
boolean unique() default false;
|
boolean unique() default false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (Optional) Whether the database column is nullable.
|
* (Optional) Whether the database column is required.
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
boolean nullable() default true;
|
boolean nullable() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* for OpenAPI Specification 3
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
String example() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (Optional) Whether the column is included in SQL INSERT
|
* (Optional) Whether the column is included in SQL INSERT
|
||||||
* statements generated by the persistence provider.
|
* statements generated by the persistence provider.
|
||||||
@@ -109,11 +116,13 @@ public @interface Column {
|
|||||||
*
|
*
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
String table() default "";
|
String table() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (Optional) The column length. (Applies only if a
|
* (Optional) The column length. (Applies only if a
|
||||||
* string-valued column is used.)
|
* string-valued column is used.)
|
||||||
|
* if type==String and length == 65535 then sqltype is text
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
@@ -15,9 +15,7 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package javax.persistence;
|
package javax.persistence;
|
||||||
|
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.*;
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.Documented;
|
|
||||||
import static java.lang.annotation.ElementType.TYPE;
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
@@ -27,6 +25,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|||||||
*
|
*
|
||||||
* @since Java Persistence 1.0
|
* @since Java Persistence 1.0
|
||||||
*/
|
*/
|
||||||
|
@Inherited
|
||||||
@Documented
|
@Documented
|
||||||
@Target(TYPE)
|
@Target(TYPE)
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
@@ -82,6 +82,11 @@ public @interface Table {
|
|||||||
*/
|
*/
|
||||||
Index[] indexes() default {};
|
Index[] indexes() default {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* comment
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
String comment() default "";
|
String comment() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,20 +4,22 @@
|
|||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*
|
*
|
||||||
module org.redkale {
|
*/
|
||||||
|
module redkale {
|
||||||
|
|
||||||
requires java.base;
|
requires java.base;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
requires java.xml;
|
requires java.net.http;
|
||||||
requires java.sql;
|
requires java.sql;
|
||||||
|
|
||||||
requires jdk.unsupported; //sun.misc.Unsafe
|
requires jdk.unsupported; //sun.misc.Unsafe
|
||||||
|
|
||||||
exports javax.annotation;
|
exports javax.annotation;
|
||||||
exports javax.persistence;
|
exports javax.persistence;
|
||||||
|
|
||||||
|
exports org.redkale.asm;
|
||||||
exports org.redkale.boot;
|
exports org.redkale.boot;
|
||||||
exports org.redkale.boot.watch;
|
exports org.redkale.boot.watch;
|
||||||
|
exports org.redkale.cluster;
|
||||||
exports org.redkale.convert;
|
exports org.redkale.convert;
|
||||||
exports org.redkale.convert.bson;
|
exports org.redkale.convert.bson;
|
||||||
exports org.redkale.convert.ext;
|
exports org.redkale.convert.ext;
|
||||||
@@ -32,10 +34,11 @@ module org.redkale {
|
|||||||
exports org.redkale.util;
|
exports org.redkale.util;
|
||||||
exports org.redkale.watch;
|
exports org.redkale.watch;
|
||||||
|
|
||||||
uses org.redkale.cluster.ClusterAgent;
|
|
||||||
uses org.redkale.mq.MessageAgent;
|
uses org.redkale.mq.MessageAgent;
|
||||||
uses org.redkale.source.CacheSource;
|
uses org.redkale.cluster.ClusterAgent;
|
||||||
uses org.redkale.source.SourceLoader;
|
uses org.redkale.convert.ConvertProvider;
|
||||||
|
uses org.redkale.source.CacheSourceProvider;
|
||||||
|
uses org.redkale.source.DataSourceProvider;
|
||||||
uses org.redkale.util.ResourceInjectLoader;
|
uses org.redkale.util.ResourceInjectLoader;
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
@@ -102,9 +102,6 @@ public abstract class AnnotationVisitor {
|
|||||||
* method calls. May be null.
|
* method calls. May be null.
|
||||||
*/
|
*/
|
||||||
public AnnotationVisitor(final int api, final AnnotationVisitor av) {
|
public AnnotationVisitor(final int api, final AnnotationVisitor av) {
|
||||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.av = av;
|
this.av = av;
|
||||||
}
|
}
|
||||||
@@ -286,8 +286,13 @@ public class Attribute {
|
|||||||
|
|
||||||
//The stuff below is temporary - once proper support for nestmate attribute has been added, it can be safely removed.
|
//The stuff below is temporary - once proper support for nestmate attribute has been added, it can be safely removed.
|
||||||
//see also changes in ClassReader.accept.
|
//see also changes in ClassReader.accept.
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public static class NestMembers extends Attribute {
|
public static class NestMembers extends Attribute {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public NestMembers() {
|
public NestMembers() {
|
||||||
super("NestMembers");
|
super("NestMembers");
|
||||||
}
|
}
|
||||||
@@ -321,11 +326,16 @@ public class Attribute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public static class NestHost extends Attribute {
|
public static class NestHost extends Attribute {
|
||||||
|
|
||||||
byte[] bytes;
|
byte[] bytes;
|
||||||
String clazz;
|
String clazz;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public NestHost() {
|
public NestHost() {
|
||||||
super("NestHost");
|
super("NestHost");
|
||||||
}
|
}
|
||||||
@@ -104,9 +104,6 @@ public abstract class ClassVisitor {
|
|||||||
* calls. May be null.
|
* calls. May be null.
|
||||||
*/
|
*/
|
||||||
public ClassVisitor(final int api, final ClassVisitor cv) {
|
public ClassVisitor(final int api, final ClassVisitor cv) {
|
||||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.cv = cv;
|
this.cv = cv;
|
||||||
}
|
}
|
||||||
@@ -244,9 +241,6 @@ public abstract class ClassVisitor {
|
|||||||
*/
|
*/
|
||||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||||
TypePath typePath, String desc, boolean visible) {
|
TypePath typePath, String desc, boolean visible) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if (cv != null) {
|
if (cv != null) {
|
||||||
return cv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
return cv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
||||||
}
|
}
|
||||||
@@ -1297,38 +1297,6 @@ public class ClassWriter extends ClassVisitor {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a handle to the constant pool of the class being build. Does nothing
|
|
||||||
* if the constant pool already contains a similar item. <i>This method is
|
|
||||||
* intended for {@link Attribute} sub classes, and is normally not needed by
|
|
||||||
* class generators or adapters.</i>
|
|
||||||
*
|
|
||||||
* @param tag
|
|
||||||
* the kind of this handle. Must be {@link Opcodes#H_GETFIELD},
|
|
||||||
* {@link Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD},
|
|
||||||
* {@link Opcodes#H_PUTSTATIC}, {@link Opcodes#H_INVOKEVIRTUAL},
|
|
||||||
* {@link Opcodes#H_INVOKESTATIC},
|
|
||||||
* {@link Opcodes#H_INVOKESPECIAL},
|
|
||||||
* {@link Opcodes#H_NEWINVOKESPECIAL} or
|
|
||||||
* {@link Opcodes#H_INVOKEINTERFACE}.
|
|
||||||
* @param owner
|
|
||||||
* the internal name of the field or method owner class.
|
|
||||||
* @param name
|
|
||||||
* the name of the field or method.
|
|
||||||
* @param desc
|
|
||||||
* the descriptor of the field or method.
|
|
||||||
* @return the index of a new or already existing method type reference
|
|
||||||
* item.
|
|
||||||
*
|
|
||||||
* @deprecated this method is superseded by
|
|
||||||
* {@link #newHandle(int, String, String, String, boolean)}.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public int newHandle(final int tag, final String owner, final String name,
|
|
||||||
final String desc) {
|
|
||||||
return newHandle(tag, owner, name, desc, tag == Opcodes.H_INVOKEINTERFACE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a handle to the constant pool of the class being build. Does nothing
|
* Adds a handle to the constant pool of the class being build. Does nothing
|
||||||
* if the constant pool already contains a similar item. <i>This method is
|
* if the constant pool already contains a similar item. <i>This method is
|
||||||
@@ -101,9 +101,6 @@ public abstract class FieldVisitor {
|
|||||||
* calls. May be null.
|
* calls. May be null.
|
||||||
*/
|
*/
|
||||||
public FieldVisitor(final int api, final FieldVisitor fv) {
|
public FieldVisitor(final int api, final FieldVisitor fv) {
|
||||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.fv = fv;
|
this.fv = fv;
|
||||||
}
|
}
|
||||||
@@ -145,9 +142,6 @@ public abstract class FieldVisitor {
|
|||||||
*/
|
*/
|
||||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||||
TypePath typePath, String desc, boolean visible) {
|
TypePath typePath, String desc, boolean visible) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if (fv != null) {
|
if (fv != null) {
|
||||||
return fv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
return fv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
||||||
}
|
}
|
||||||
@@ -99,35 +99,6 @@ public final class Handle {
|
|||||||
*/
|
*/
|
||||||
final boolean itf;
|
final boolean itf;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new field or method handle.
|
|
||||||
*
|
|
||||||
* @param tag
|
|
||||||
* the kind of field or method designated by this Handle. Must be
|
|
||||||
* {@link Opcodes#H_GETFIELD}, {@link Opcodes#H_GETSTATIC},
|
|
||||||
* {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC},
|
|
||||||
* {@link Opcodes#H_INVOKEVIRTUAL},
|
|
||||||
* {@link Opcodes#H_INVOKESTATIC},
|
|
||||||
* {@link Opcodes#H_INVOKESPECIAL},
|
|
||||||
* {@link Opcodes#H_NEWINVOKESPECIAL} or
|
|
||||||
* {@link Opcodes#H_INVOKEINTERFACE}.
|
|
||||||
* @param owner
|
|
||||||
* the internal name of the class that owns the field or method
|
|
||||||
* designated by this handle.
|
|
||||||
* @param name
|
|
||||||
* the name of the field or method designated by this handle.
|
|
||||||
* @param desc
|
|
||||||
* the descriptor of the field or method designated by this
|
|
||||||
* handle.
|
|
||||||
*
|
|
||||||
* @deprecated this constructor has been superseded
|
|
||||||
* by {@link #Handle(int, String, String, String, boolean)}.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public Handle(int tag, String owner, String name, String desc) {
|
|
||||||
this(tag, owner, name, desc, tag == Opcodes.H_INVOKEINTERFACE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new field or method handle.
|
* Constructs a new field or method handle.
|
||||||
*
|
*
|
||||||
@@ -5,7 +5,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.asm;
|
package org.redkale.asm;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import static org.redkale.asm.Opcodes.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MethodVisitor 的调试类
|
* MethodVisitor 的调试类
|
||||||
@@ -54,11 +57,20 @@ public class MethodDebugVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param visitor MethodVisitor
|
||||||
|
*/
|
||||||
public MethodDebugVisitor(MethodVisitor visitor) {
|
public MethodDebugVisitor(MethodVisitor visitor) {
|
||||||
//super(Opcodes.ASM5, visitor);
|
//super(Opcodes.ASM5, visitor);
|
||||||
this.visitor = visitor;
|
this.visitor = visitor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
|
||||||
|
visitor.visitTryCatchBlock(start, end, handler, type);
|
||||||
|
if (debug) System.out.println("mv.visitTryCatchBlock(label0, label1, label2, \"" + type + "\");");
|
||||||
|
}
|
||||||
|
|
||||||
public AnnotationVisitor visitParameterAnnotation(int i, String string, boolean bln) {
|
public AnnotationVisitor visitParameterAnnotation(int i, String string, boolean bln) {
|
||||||
AnnotationVisitor av = visitor.visitParameterAnnotation(i, string, bln);
|
AnnotationVisitor av = visitor.visitParameterAnnotation(i, string, bln);
|
||||||
if (debug) System.out.println("mv.visitParameterAnnotation(" + i + ", \"" + string + "\", " + bln + ");");
|
if (debug) System.out.println("mv.visitParameterAnnotation(" + i + ", \"" + string + "\", " + bln + ");");
|
||||||
@@ -189,4 +201,74 @@ public class MethodDebugVisitor {
|
|||||||
visitor.visitEnd();
|
visitor.visitEnd();
|
||||||
if (debug) System.out.println("mv.visitEnd();\r\n\r\n\r\n");
|
if (debug) System.out.println("mv.visitEnd();\r\n\r\n\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void pushInt(MethodDebugVisitor mv, int num) {
|
||||||
|
if (num < 6) {
|
||||||
|
mv.visitInsn(ICONST_0 + num);
|
||||||
|
} else if (num <= Byte.MAX_VALUE) {
|
||||||
|
mv.visitIntInsn(BIPUSH, num);
|
||||||
|
} else if (num <= Short.MAX_VALUE) {
|
||||||
|
mv.visitIntInsn(SIPUSH, num);
|
||||||
|
} else {
|
||||||
|
mv.visitLdcInsn(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void pushInt(MethodVisitor mv, int num) {
|
||||||
|
if (num < 6) {
|
||||||
|
mv.visitInsn(ICONST_0 + num);
|
||||||
|
} else if (num <= Byte.MAX_VALUE) {
|
||||||
|
mv.visitIntInsn(BIPUSH, num);
|
||||||
|
} else if (num <= Short.MAX_VALUE) {
|
||||||
|
mv.visitIntInsn(SIPUSH, num);
|
||||||
|
} else {
|
||||||
|
mv.visitLdcInsn(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void visitAnnotation(final AnnotationVisitor av, final Annotation ann) {
|
||||||
|
try {
|
||||||
|
for (Method anm : ann.annotationType().getMethods()) {
|
||||||
|
final String mname = anm.getName();
|
||||||
|
if ("equals".equals(mname) || "hashCode".equals(mname) || "toString".equals(mname) || "annotationType".equals(mname)) continue;
|
||||||
|
final Object r = anm.invoke(ann);
|
||||||
|
if (r instanceof String[]) {
|
||||||
|
AnnotationVisitor av1 = av.visitArray(mname);
|
||||||
|
for (String item : (String[]) r) {
|
||||||
|
av1.visit(null, item);
|
||||||
|
}
|
||||||
|
av1.visitEnd();
|
||||||
|
} else if (r instanceof Class[]) {
|
||||||
|
AnnotationVisitor av1 = av.visitArray(mname);
|
||||||
|
for (Class item : (Class[]) r) {
|
||||||
|
av1.visit(null, Type.getType(item));
|
||||||
|
}
|
||||||
|
av1.visitEnd();
|
||||||
|
} else if (r instanceof Enum[]) {
|
||||||
|
AnnotationVisitor av1 = av.visitArray(mname);
|
||||||
|
for (Enum item : (Enum[]) r) {
|
||||||
|
av1.visitEnum(null, Type.getDescriptor(item.getClass()), ((Enum) item).name());
|
||||||
|
}
|
||||||
|
av1.visitEnd();
|
||||||
|
} else if (r instanceof Annotation[]) {
|
||||||
|
AnnotationVisitor av1 = av.visitArray(mname);
|
||||||
|
for (Annotation item : (Annotation[]) r) {
|
||||||
|
visitAnnotation(av1.visitAnnotation(null, Type.getDescriptor(((Annotation) item).annotationType())), item);
|
||||||
|
}
|
||||||
|
av1.visitEnd();
|
||||||
|
} else if (r instanceof Class) {
|
||||||
|
av.visit(mname, Type.getType((Class) r));
|
||||||
|
} else if (r instanceof Enum) {
|
||||||
|
av.visitEnum(mname, Type.getDescriptor(r.getClass()), ((Enum) r).name());
|
||||||
|
} else if (r instanceof Annotation) {
|
||||||
|
visitAnnotation(av.visitAnnotation(null, Type.getDescriptor(((Annotation) r).annotationType())), (Annotation) r);
|
||||||
|
} else {
|
||||||
|
av.visit(mname, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
av.visitEnd();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -118,9 +118,6 @@ public abstract class MethodVisitor {
|
|||||||
* calls. May be null.
|
* calls. May be null.
|
||||||
*/
|
*/
|
||||||
public MethodVisitor(final int api, final MethodVisitor mv) {
|
public MethodVisitor(final int api, final MethodVisitor mv) {
|
||||||
if (api < Opcodes.ASM4 || api > Opcodes.ASM6) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.mv = mv;
|
this.mv = mv;
|
||||||
}
|
}
|
||||||
@@ -140,9 +137,6 @@ public abstract class MethodVisitor {
|
|||||||
* allowed (see {@link Opcodes}).
|
* allowed (see {@link Opcodes}).
|
||||||
*/
|
*/
|
||||||
public void visitParameter(String name, int access) {
|
public void visitParameter(String name, int access) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if (mv != null) {
|
if (mv != null) {
|
||||||
mv.visitParameter(name, access);
|
mv.visitParameter(name, access);
|
||||||
}
|
}
|
||||||
@@ -209,9 +203,6 @@ public abstract class MethodVisitor {
|
|||||||
*/
|
*/
|
||||||
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
public AnnotationVisitor visitTypeAnnotation(int typeRef,
|
||||||
TypePath typePath, String desc, boolean visible) {
|
TypePath typePath, String desc, boolean visible) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if (mv != null) {
|
if (mv != null) {
|
||||||
return mv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
return mv.visitTypeAnnotation(typeRef, typePath, desc, visible);
|
||||||
}
|
}
|
||||||
@@ -453,35 +444,6 @@ public abstract class MethodVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Visits a method instruction. A method instruction is an instruction that
|
|
||||||
* invokes a method.
|
|
||||||
*
|
|
||||||
* @param opcode
|
|
||||||
* the opcode of the type instruction to be visited. This opcode
|
|
||||||
* is either INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC or
|
|
||||||
* INVOKEINTERFACE.
|
|
||||||
* @param owner
|
|
||||||
* the internal name of the method's owner class (see
|
|
||||||
* {@link Type#getInternalName() getInternalName}).
|
|
||||||
* @param name
|
|
||||||
* the method's name.
|
|
||||||
* @param desc
|
|
||||||
* the method's descriptor (see {@link Type Type}).
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public void visitMethodInsn(int opcode, String owner, String name,
|
|
||||||
String desc) {
|
|
||||||
if (api >= Opcodes.ASM5) {
|
|
||||||
boolean itf = opcode == Opcodes.INVOKEINTERFACE;
|
|
||||||
visitMethodInsn(opcode, owner, name, desc, itf);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (mv != null) {
|
|
||||||
mv.visitMethodInsn(opcode, owner, name, desc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visits a method instruction. A method instruction is an instruction that
|
* Visits a method instruction. A method instruction is an instruction that
|
||||||
* invokes a method.
|
* invokes a method.
|
||||||
@@ -502,14 +464,6 @@ public abstract class MethodVisitor {
|
|||||||
*/
|
*/
|
||||||
public void visitMethodInsn(int opcode, String owner, String name,
|
public void visitMethodInsn(int opcode, String owner, String name,
|
||||||
String desc, boolean itf) {
|
String desc, boolean itf) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
if (itf != (opcode == Opcodes.INVOKEINTERFACE)) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"INVOKESPECIAL/STATIC on interfaces require ASM 5");
|
|
||||||
}
|
|
||||||
visitMethodInsn(opcode, owner, name, desc);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (mv != null) {
|
if (mv != null) {
|
||||||
mv.visitMethodInsn(opcode, owner, name, desc, itf);
|
mv.visitMethodInsn(opcode, owner, name, desc, itf);
|
||||||
}
|
}
|
||||||
@@ -723,9 +677,6 @@ public abstract class MethodVisitor {
|
|||||||
*/
|
*/
|
||||||
public AnnotationVisitor visitInsnAnnotation(int typeRef,
|
public AnnotationVisitor visitInsnAnnotation(int typeRef,
|
||||||
TypePath typePath, String desc, boolean visible) {
|
TypePath typePath, String desc, boolean visible) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if (mv != null) {
|
if (mv != null) {
|
||||||
return mv.visitInsnAnnotation(typeRef, typePath, desc, visible);
|
return mv.visitInsnAnnotation(typeRef, typePath, desc, visible);
|
||||||
}
|
}
|
||||||
@@ -783,9 +734,6 @@ public abstract class MethodVisitor {
|
|||||||
*/
|
*/
|
||||||
public AnnotationVisitor visitTryCatchAnnotation(int typeRef,
|
public AnnotationVisitor visitTryCatchAnnotation(int typeRef,
|
||||||
TypePath typePath, String desc, boolean visible) {
|
TypePath typePath, String desc, boolean visible) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if (mv != null) {
|
if (mv != null) {
|
||||||
return mv.visitTryCatchAnnotation(typeRef, typePath, desc, visible);
|
return mv.visitTryCatchAnnotation(typeRef, typePath, desc, visible);
|
||||||
}
|
}
|
||||||
@@ -854,9 +802,6 @@ public abstract class MethodVisitor {
|
|||||||
public AnnotationVisitor visitLocalVariableAnnotation(int typeRef,
|
public AnnotationVisitor visitLocalVariableAnnotation(int typeRef,
|
||||||
TypePath typePath, Label[] start, Label[] end, int[] index,
|
TypePath typePath, Label[] start, Label[] end, int[] index,
|
||||||
String desc, boolean visible) {
|
String desc, boolean visible) {
|
||||||
if (api < Opcodes.ASM5) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
if (mv != null) {
|
if (mv != null) {
|
||||||
return mv.visitLocalVariableAnnotation(typeRef, typePath, start,
|
return mv.visitLocalVariableAnnotation(typeRef, typePath, start,
|
||||||
end, index, desc, visible);
|
end, index, desc, visible);
|
||||||
@@ -73,10 +73,9 @@ package org.redkale.asm;
|
|||||||
public interface Opcodes {
|
public interface Opcodes {
|
||||||
|
|
||||||
// ASM API versions
|
// ASM API versions
|
||||||
|
int ASM4 = 4 << 16 | 0 << 8;
|
||||||
int ASM4 = 4 << 16 | 0 << 8 | 0;
|
int ASM5 = 5 << 16 | 0 << 8;
|
||||||
int ASM5 = 5 << 16 | 0 << 8 | 0;
|
int ASM6 = 6 << 16 | 0 << 8;
|
||||||
int ASM6 = 6 << 16 | 0 << 8 | 0;
|
|
||||||
|
|
||||||
// versions
|
// versions
|
||||||
|
|
||||||
@@ -161,7 +161,7 @@ public class TypePath {
|
|||||||
* @return the corresponding TypePath object, or null if the path is empty.
|
* @return the corresponding TypePath object, or null if the path is empty.
|
||||||
*/
|
*/
|
||||||
public static TypePath fromString(final String typePath) {
|
public static TypePath fromString(final String typePath) {
|
||||||
if (typePath == null || typePath.length() == 0) {
|
if (typePath == null || typePath.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
int n = typePath.length();
|
int n = typePath.length();
|
||||||
535
src/main/java/org/redkale/boot/ApiDocsService.java
Normal file
535
src/main/java/org/redkale/boot/ApiDocsService.java
Normal file
@@ -0,0 +1,535 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.boot;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.lang.reflect.*;
|
||||||
|
import java.math.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.*;
|
||||||
|
import java.util.logging.*;
|
||||||
|
import javax.persistence.*;
|
||||||
|
import org.redkale.convert.*;
|
||||||
|
import org.redkale.convert.json.*;
|
||||||
|
import org.redkale.mq.MessageMultiConsumer;
|
||||||
|
import org.redkale.net.http.*;
|
||||||
|
import org.redkale.service.RetResult;
|
||||||
|
import org.redkale.source.*;
|
||||||
|
import org.redkale.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API接口文档生成类,作用:生成Application实例中所有HttpServer的可用HttpServlet的API接口方法 <br>
|
||||||
|
* 继承 HttpBaseServlet 是为了获取 HttpMapping 信息 <br>
|
||||||
|
* https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
public final class ApiDocsService {
|
||||||
|
|
||||||
|
private static final java.lang.reflect.Type TYPE_RETRESULT_OBJECT = new TypeToken<RetResult<Object>>() {
|
||||||
|
}.getType();
|
||||||
|
|
||||||
|
private static final java.lang.reflect.Type TYPE_RETRESULT_STRING = new TypeToken<RetResult<String>>() {
|
||||||
|
}.getType();
|
||||||
|
|
||||||
|
private static final java.lang.reflect.Type TYPE_RETRESULT_INTEGER = new TypeToken<RetResult<Integer>>() {
|
||||||
|
}.getType();
|
||||||
|
|
||||||
|
private static final java.lang.reflect.Type TYPE_RETRESULT_LONG = new TypeToken<RetResult<Long>>() {
|
||||||
|
}.getType();
|
||||||
|
|
||||||
|
private final Application app; //Application全局对象
|
||||||
|
|
||||||
|
public ApiDocsService(Application app) {
|
||||||
|
this.app = app;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(String[] args) throws Exception {
|
||||||
|
//是否跳过RPC接口
|
||||||
|
final boolean skipRPC = Arrays.toString(args).toLowerCase().contains("skip-rpc") && !Arrays.toString(args).toLowerCase().contains("skip-rpc=false");
|
||||||
|
|
||||||
|
List<Map> serverList = new ArrayList<>();
|
||||||
|
Field __prefix = HttpServlet.class.getDeclaredField("_prefix");
|
||||||
|
__prefix.setAccessible(true);
|
||||||
|
Map<String, Map<String, Map<String, Object>>> typesMap = new LinkedHashMap<>();
|
||||||
|
//https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md
|
||||||
|
Map<String, Object> swaggerPathsMap = new LinkedHashMap<>();
|
||||||
|
List<Map> swaggerServers = new ArrayList<>();
|
||||||
|
List<Map> swaggerTags = new ArrayList<>();
|
||||||
|
Map<String, Map<String, Object>> swaggerComponentsMap = new LinkedHashMap<>();
|
||||||
|
for (NodeServer node : app.servers) {
|
||||||
|
if (!(node instanceof NodeHttpServer)) continue;
|
||||||
|
final Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
serverList.add(map);
|
||||||
|
HttpServer server = node.getServer();
|
||||||
|
map.put("address", server.getSocketAddress());
|
||||||
|
swaggerServers.add(Utility.ofMap("url", "http://localhost:" + server.getSocketAddress().getPort()));
|
||||||
|
List<Map<String, Object>> servletsList = new ArrayList<>();
|
||||||
|
map.put("servlets", servletsList);
|
||||||
|
String plainContentType = server.getResponseConfig() == null ? "application/json" : server.getResponseConfig().plainContentType;
|
||||||
|
if (plainContentType == null || plainContentType.isEmpty()) plainContentType = "application/json";
|
||||||
|
if (plainContentType.indexOf(';') > 0) plainContentType = plainContentType.substring(0, plainContentType.indexOf(';'));
|
||||||
|
|
||||||
|
for (HttpServlet servlet : server.getPrepareServlet().getServlets()) {
|
||||||
|
if (!(servlet instanceof HttpServlet)) continue;
|
||||||
|
if (servlet instanceof WebSocketServlet) continue;
|
||||||
|
if (servlet.getClass().getAnnotation(MessageMultiConsumer.class) != null) {
|
||||||
|
node.logger.log(Level.INFO, servlet + " be skipped because has @MessageMultiConsumer");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||||
|
if (ws == null) {
|
||||||
|
node.logger.log(Level.WARNING, servlet + " not found @WebServlet");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (ws.name().isEmpty()) {
|
||||||
|
node.logger.log(Level.INFO, servlet + " be skipped because @WebServlet.name is empty");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final String tag = ws.name().isEmpty() ? servlet.getClass().getSimpleName().replace("Servlet", "").toLowerCase() : ws.name();
|
||||||
|
final Map<String, Object> servletMap = new LinkedHashMap<>();
|
||||||
|
String prefix = (String) __prefix.get(servlet);
|
||||||
|
String[] urlregs = ws.value();
|
||||||
|
if (prefix != null && !prefix.isEmpty()) {
|
||||||
|
for (int i = 0; i < urlregs.length; i++) {
|
||||||
|
urlregs[i] = prefix + urlregs[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
servletMap.put("urlregs", urlregs);
|
||||||
|
servletMap.put("moduleid", ws.moduleid());
|
||||||
|
servletMap.put("name", ws.name());
|
||||||
|
servletMap.put("comment", ws.comment());
|
||||||
|
|
||||||
|
List<Map> mappingsList = new ArrayList<>();
|
||||||
|
servletMap.put("mappings", mappingsList);
|
||||||
|
final Class selfClz = servlet.getClass();
|
||||||
|
Class clz = servlet.getClass();
|
||||||
|
HashSet<String> actionUrls = new HashSet<>();
|
||||||
|
do {
|
||||||
|
if (Modifier.isAbstract(clz.getModifiers())) break;
|
||||||
|
for (Method method : clz.getMethods()) {
|
||||||
|
if (method.getParameterCount() != 2) continue;
|
||||||
|
HttpMapping action = method.getAnnotation(HttpMapping.class);
|
||||||
|
if (action == null) continue;
|
||||||
|
if (!action.inherited() && selfClz != clz) continue; //忽略不被继承的方法
|
||||||
|
if (actionUrls.contains(action.url())) continue;
|
||||||
|
if (HttpScope.class.isAssignableFrom(action.result())) continue; //忽略模板引擎的方法
|
||||||
|
if (action.rpconly() && skipRPC) continue; //不生成RPC接口
|
||||||
|
|
||||||
|
final List<Map<String, Object>> swaggerParamsList = new ArrayList<>();
|
||||||
|
|
||||||
|
final Map<String, Object> mappingMap = new LinkedHashMap<>();
|
||||||
|
mappingMap.put("url", prefix + action.url());
|
||||||
|
actionUrls.add(action.url());
|
||||||
|
mappingMap.put("auth", action.auth());
|
||||||
|
mappingMap.put("actionid", action.actionid());
|
||||||
|
mappingMap.put("comment", action.comment());
|
||||||
|
List<Map> paramsList = new ArrayList<>();
|
||||||
|
mappingMap.put("params", paramsList);
|
||||||
|
List<String> results = new ArrayList<>();
|
||||||
|
Type resultType = action.result();
|
||||||
|
if (!action.resultref().isEmpty()) {
|
||||||
|
Field f = servlet.getClass().getDeclaredField(action.resultref());
|
||||||
|
f.setAccessible(true);
|
||||||
|
resultType = (Type) f.get(servlet);
|
||||||
|
}
|
||||||
|
// for (final Class rtype : action.results()) {
|
||||||
|
// results.add(rtype.getName());
|
||||||
|
// if (typesMap.containsKey(rtype.getName())) continue;
|
||||||
|
// if (rtype.getName().startsWith("java.")) continue;
|
||||||
|
// if (rtype.getName().startsWith("javax.")) continue;
|
||||||
|
// final boolean filter = FilterBean.class.isAssignableFrom(rtype);
|
||||||
|
// final Map<String, Map<String, Object>> typeMap = new LinkedHashMap<>();
|
||||||
|
// Class loop = rtype;
|
||||||
|
// do {
|
||||||
|
// if (loop == null || loop.isInterface()) break;
|
||||||
|
// for (Field field : loop.getDeclaredFields()) {
|
||||||
|
// if (Modifier.isFinal(field.getModifiers())) continue;
|
||||||
|
// if (Modifier.isStatic(field.getModifiers())) continue;
|
||||||
|
//
|
||||||
|
// Map<String, Object> fieldmap = new LinkedHashMap<>();
|
||||||
|
// fieldmap.put("type", field.getType().isArray() ? (field.getType().getComponentType().getName() + "[]") : field.getGenericType().getTypeName());
|
||||||
|
//
|
||||||
|
// Comment comment = field.getAnnotation(Comment.class);
|
||||||
|
// Column col = field.getAnnotation(Column.class);
|
||||||
|
// FilterColumn fc = field.getAnnotation(FilterColumn.class);
|
||||||
|
// if (comment != null) {
|
||||||
|
// fieldmap.put("comment", comment.value());
|
||||||
|
// } else if (col != null) {
|
||||||
|
// fieldmap.put("comment", col.comment());
|
||||||
|
// } else if (fc != null) {
|
||||||
|
// fieldmap.put("comment", fc.comment());
|
||||||
|
// }
|
||||||
|
// fieldmap.put("primary", !filter && (field.getAnnotation(Id.class) != null));
|
||||||
|
// fieldmap.put("updatable", (filter || col == null || col.updatable()));
|
||||||
|
// if (servlet.getClass().getAnnotation(Rest.RestDyn.class) != null) {
|
||||||
|
// if (field.getAnnotation(RestAddress.class) != null) continue;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// typeMap.put(field.getName(), fieldmap);
|
||||||
|
// }
|
||||||
|
// } while ((loop = loop.getSuperclass()) != Object.class);
|
||||||
|
// typesMap.put(rtype.getName(), typeMap);
|
||||||
|
// }
|
||||||
|
mappingMap.put("results", results);
|
||||||
|
boolean hasbodyparam = false;
|
||||||
|
Map<String, Object> swaggerRequestBody = new LinkedHashMap<>();
|
||||||
|
for (HttpParam param : method.getAnnotationsByType(HttpParam.class)) {
|
||||||
|
final Map<String, Object> oldapisParamMap = new LinkedHashMap<>();
|
||||||
|
final boolean isarray = param.type().isArray();
|
||||||
|
final Class ptype = isarray ? param.type().getComponentType() : param.type();
|
||||||
|
oldapisParamMap.put("name", param.name());
|
||||||
|
oldapisParamMap.put("radix", param.radix());
|
||||||
|
oldapisParamMap.put("type", ptype.getName() + (isarray ? "[]" : ""));
|
||||||
|
oldapisParamMap.put("style", param.style());
|
||||||
|
oldapisParamMap.put("comment", param.comment());
|
||||||
|
oldapisParamMap.put("required", param.required());
|
||||||
|
paramsList.add(oldapisParamMap);
|
||||||
|
{
|
||||||
|
final Map<String, Object> paramSchemaMap = new LinkedHashMap<>();
|
||||||
|
Type paramGenericType = param.type();
|
||||||
|
if (!param.typeref().isEmpty()) {
|
||||||
|
Field f = servlet.getClass().getDeclaredField(param.typeref());
|
||||||
|
f.setAccessible(true);
|
||||||
|
paramGenericType = (Type) f.get(servlet);
|
||||||
|
}
|
||||||
|
simpleSchemaType(node.getLogger(), swaggerComponentsMap, param.type(), paramGenericType, paramSchemaMap, true);
|
||||||
|
if (param.style() == HttpParam.HttpParameterStyle.BODY) {
|
||||||
|
swaggerRequestBody.put("description", param.comment());
|
||||||
|
swaggerRequestBody.put("content", Utility.ofMap(plainContentType, Utility.ofMap("schema", paramSchemaMap)));
|
||||||
|
} else {
|
||||||
|
final Map<String, Object> swaggerParamMap = new LinkedHashMap<>();
|
||||||
|
swaggerParamMap.put("name", param.name());
|
||||||
|
swaggerParamMap.put("in", param.style().name().toLowerCase());
|
||||||
|
swaggerParamMap.put("description", param.comment());
|
||||||
|
swaggerParamMap.put("required", param.required());
|
||||||
|
if (param.deprecated()) {
|
||||||
|
swaggerParamMap.put("deprecated", param.deprecated());
|
||||||
|
}
|
||||||
|
//https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterStyle
|
||||||
|
swaggerParamMap.put("style", param.style() == HttpParam.HttpParameterStyle.HEADER || param.name().indexOf('#') == 0 ? "simple" : "form");
|
||||||
|
swaggerParamMap.put("explode", true);
|
||||||
|
swaggerParamMap.put("schema", paramSchemaMap);
|
||||||
|
Object example = formatExample(param.example(), param.type(), paramGenericType);
|
||||||
|
if (example != null) swaggerParamMap.put("example", example);
|
||||||
|
if (!param.example().isEmpty()) {
|
||||||
|
swaggerParamMap.put("example", param.example());
|
||||||
|
}
|
||||||
|
swaggerParamsList.add(swaggerParamMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (param.style() == HttpParam.HttpParameterStyle.BODY) hasbodyparam = true;
|
||||||
|
if (ptype.isPrimitive() || ptype == String.class) continue;
|
||||||
|
if (typesMap.containsKey(ptype.getName())) continue;
|
||||||
|
if (ptype.getName().startsWith("java.")) continue;
|
||||||
|
if (ptype.getName().startsWith("javax.")) continue;
|
||||||
|
|
||||||
|
final Map<String, Map<String, Object>> typeMap = new LinkedHashMap<>();
|
||||||
|
Class loop = ptype;
|
||||||
|
final boolean filter = FilterBean.class.isAssignableFrom(loop);
|
||||||
|
do {
|
||||||
|
if (loop == null || loop.isInterface()) break;
|
||||||
|
for (Field field : loop.getDeclaredFields()) {
|
||||||
|
if (Modifier.isFinal(field.getModifiers())) continue;
|
||||||
|
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||||
|
|
||||||
|
Map<String, Object> fieldmap = new LinkedHashMap<>();
|
||||||
|
fieldmap.put("type", field.getType().isArray() ? (field.getType().getComponentType().getName() + "[]") : field.getGenericType().getTypeName());
|
||||||
|
|
||||||
|
Column col = field.getAnnotation(Column.class);
|
||||||
|
FilterColumn fc = field.getAnnotation(FilterColumn.class);
|
||||||
|
Comment comment = field.getAnnotation(Comment.class);
|
||||||
|
if (comment != null) {
|
||||||
|
fieldmap.put("comment", comment.value());
|
||||||
|
} else if (col != null) {
|
||||||
|
fieldmap.put("comment", col.comment());
|
||||||
|
} else if (fc != null) {
|
||||||
|
fieldmap.put("comment", fc.comment());
|
||||||
|
}
|
||||||
|
fieldmap.put("primary", !filter && (field.getAnnotation(Id.class) != null));
|
||||||
|
fieldmap.put("updatable", (filter || col == null || col.updatable()));
|
||||||
|
|
||||||
|
if (servlet.getClass().getAnnotation(Rest.RestDyn.class) != null) {
|
||||||
|
if (field.getAnnotation(RestAddress.class) != null) continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
typeMap.put(field.getName(), fieldmap);
|
||||||
|
}
|
||||||
|
} while ((loop = loop.getSuperclass()) != Object.class);
|
||||||
|
|
||||||
|
typesMap.put(ptype.getName(), typeMap);
|
||||||
|
}
|
||||||
|
mappingMap.put("result", action.result().getSimpleName().replace("void", "Object"));
|
||||||
|
mappingsList.add(mappingMap);
|
||||||
|
|
||||||
|
final Map<String, Object> swaggerOperatMap = new LinkedHashMap<>();
|
||||||
|
swaggerOperatMap.put("tags", new String[]{tag});
|
||||||
|
swaggerOperatMap.put("operationId", action.name());
|
||||||
|
if (method.getAnnotation(Deprecated.class) != null) {
|
||||||
|
swaggerOperatMap.put("deprecated", true);
|
||||||
|
}
|
||||||
|
Map<String, Object> respSchemaMap = new LinkedHashMap<>();
|
||||||
|
simpleSchemaType(node.getLogger(), swaggerComponentsMap, action.result(), resultType, respSchemaMap, true);
|
||||||
|
|
||||||
|
Map<String, Object> respMap = new LinkedHashMap<>();
|
||||||
|
respMap.put("schema", respSchemaMap);
|
||||||
|
Object example = formatExample(action.example(), action.result(), resultType);
|
||||||
|
if (example != null) swaggerOperatMap.put("example", example);
|
||||||
|
if (!swaggerRequestBody.isEmpty()) swaggerOperatMap.put("requestBody", swaggerRequestBody);
|
||||||
|
swaggerOperatMap.put("parameters", swaggerParamsList);
|
||||||
|
String actiondesc = action.comment();
|
||||||
|
if (action.rpconly()) actiondesc = "[Only for RPC API] " + actiondesc;
|
||||||
|
swaggerOperatMap.put("responses", Utility.ofMap("200", Utility.ofMap("description", actiondesc, "content", Utility.ofMap("application/json", respMap))));
|
||||||
|
|
||||||
|
String m = action.methods() == null || action.methods().length == 0 ? null : action.methods()[0].toLowerCase();
|
||||||
|
if (m == null) {
|
||||||
|
m = hasbodyparam || TYPE_RETRESULT_STRING.equals(resultType) || TYPE_RETRESULT_INTEGER.equals(resultType)
|
||||||
|
|| TYPE_RETRESULT_LONG.equals(resultType) || action.name().contains("create") || action.name().contains("insert")
|
||||||
|
|| action.name().contains("update") || action.name().contains("delete") || action.name().contains("send") ? "post" : "get";
|
||||||
|
}
|
||||||
|
swaggerPathsMap.put(prefix + action.url(), Utility.ofMap("description", action.comment(), m, swaggerOperatMap));
|
||||||
|
}
|
||||||
|
} while ((clz = clz.getSuperclass()) != HttpServlet.class);
|
||||||
|
mappingsList.sort((o1, o2) -> ((String) o1.get("url")).compareTo((String) o2.get("url")));
|
||||||
|
servletsList.add(servletMap);
|
||||||
|
if (!actionUrls.isEmpty()) swaggerTags.add(Utility.ofMap("name", tag, "description", ws.comment()));
|
||||||
|
}
|
||||||
|
servletsList.sort((o1, o2) -> {
|
||||||
|
String[] urlregs1 = (String[]) o1.get("urlregs");
|
||||||
|
String[] urlregs2 = (String[]) o2.get("urlregs");
|
||||||
|
return urlregs1.length > 0 ? (urlregs2.length > 0 ? urlregs1[0].compareTo(urlregs2[0]) : 1) : -1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
{ // https://github.com/OAI/OpenAPI-Specification
|
||||||
|
Map<String, Object> swaggerResultMap = new LinkedHashMap<>();
|
||||||
|
swaggerResultMap.put("openapi", "3.0.0");
|
||||||
|
Map<String, Object> infomap = new LinkedHashMap<>();
|
||||||
|
infomap.put("title", "Redkale generate apidoc");
|
||||||
|
infomap.put("version", "1.0.0");
|
||||||
|
swaggerResultMap.put("info", infomap);
|
||||||
|
swaggerResultMap.put("servers", swaggerServers);
|
||||||
|
swaggerResultMap.put("paths", swaggerPathsMap);
|
||||||
|
swaggerResultMap.put("tags", swaggerTags);
|
||||||
|
if (!swaggerComponentsMap.isEmpty()) swaggerResultMap.put("components", Utility.ofMap("schemas", swaggerComponentsMap));
|
||||||
|
final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "openapi-doc.json"));
|
||||||
|
out.write(JsonConvert.root().convertTo(swaggerResultMap).getBytes(StandardCharsets.UTF_8));
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Map<String, Object> oldapisResultMap = new LinkedHashMap<>();
|
||||||
|
oldapisResultMap.put("servers", serverList);
|
||||||
|
oldapisResultMap.put("types", typesMap);
|
||||||
|
final String json = JsonConvert.root().convertTo(oldapisResultMap);
|
||||||
|
final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "apidoc.json"));
|
||||||
|
out.write(json.getBytes(StandardCharsets.UTF_8));
|
||||||
|
out.close();
|
||||||
|
File doctemplate = new File(app.getConfPath().toString(), "apidoc-template.html");
|
||||||
|
InputStream in = null;
|
||||||
|
if (doctemplate.isFile() && doctemplate.canRead()) {
|
||||||
|
in = new FileInputStream(doctemplate);
|
||||||
|
}
|
||||||
|
if (in == null) in = ApiDocsService.class.getResourceAsStream("apidoc-template.html");
|
||||||
|
String content = Utility.read(in).replace("'${content}'", json);
|
||||||
|
in.close();
|
||||||
|
FileOutputStream outhtml = new FileOutputStream(new File(app.getHome(), "apidoc.html"));
|
||||||
|
outhtml.write(content.getBytes(StandardCharsets.UTF_8));
|
||||||
|
outhtml.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void simpleSchemaType(Logger logger, Map<String, Map<String, Object>> componentsMap, Class type, Type genericType, Map<String, Object> schemaMap, boolean recursive) {
|
||||||
|
if (type == int.class || type == Integer.class || type == AtomicInteger.class) {
|
||||||
|
schemaMap.put("type", "integer");
|
||||||
|
schemaMap.put("format", "int32");
|
||||||
|
} else if (type == long.class || type == Long.class
|
||||||
|
|| type == AtomicLong.class || type == LongAdder.class || type == BigInteger.class) {
|
||||||
|
schemaMap.put("type", "integer");
|
||||||
|
schemaMap.put("format", "int64");
|
||||||
|
} else if (type == float.class || type == Float.class) {
|
||||||
|
schemaMap.put("type", "number");
|
||||||
|
schemaMap.put("format", "float");
|
||||||
|
} else if (type == double.class || type == Double.class || type == BigDecimal.class) {
|
||||||
|
schemaMap.put("type", "number");
|
||||||
|
schemaMap.put("format", "double");
|
||||||
|
} else if (type == boolean.class || type == Boolean.class || type == AtomicBoolean.class) {
|
||||||
|
schemaMap.put("type", "boolean");
|
||||||
|
} else if (type.isPrimitive() || Number.class.isAssignableFrom(type)) {
|
||||||
|
schemaMap.put("type", "number");
|
||||||
|
} else if (type == String.class || CharSequence.class.isAssignableFrom(type)) {
|
||||||
|
schemaMap.put("type", "string");
|
||||||
|
} else if (recursive && (type.isArray() || Collection.class.isAssignableFrom(type))) {
|
||||||
|
schemaMap.put("type", "array");
|
||||||
|
Map<String, Object> sbumap = new LinkedHashMap<>();
|
||||||
|
if (type.isArray()) {
|
||||||
|
simpleSchemaType(logger, componentsMap, type.getComponentType(), type.getComponentType(), sbumap, false);
|
||||||
|
} else if (genericType instanceof ParameterizedType) {
|
||||||
|
Type subpt = ((ParameterizedType) genericType).getActualTypeArguments()[0];
|
||||||
|
if (subpt instanceof Class) {
|
||||||
|
simpleSchemaType(logger, componentsMap, (Class) subpt, subpt, sbumap, false);
|
||||||
|
} else if (subpt instanceof ParameterizedType && ((ParameterizedType) subpt).getOwnerType() instanceof Class) {
|
||||||
|
simpleSchemaType(logger, componentsMap, (Class) ((ParameterizedType) subpt).getOwnerType(), subpt, sbumap, false);
|
||||||
|
} else {
|
||||||
|
sbumap.put("type", "object");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sbumap.put("type", "object");
|
||||||
|
}
|
||||||
|
schemaMap.put("items", sbumap);
|
||||||
|
} else if (!type.getName().startsWith("java.") && !type.getName().startsWith("javax.")) {
|
||||||
|
String ct = simpleComponentType(logger, componentsMap, type, genericType);
|
||||||
|
if (ct == null) {
|
||||||
|
schemaMap.put("type", "object");
|
||||||
|
} else {
|
||||||
|
schemaMap.put("$ref", "#/components/schemas/" + ct);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
schemaMap.put("type", "object");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String simpleComponentType(Logger logger, Map<String, Map<String, Object>> componentsMap, Class type, Type genericType) {
|
||||||
|
try {
|
||||||
|
Encodeable encodeable = JsonFactory.root().loadEncoder(genericType);
|
||||||
|
String ct = componentKey(logger, componentsMap, null, encodeable, true);
|
||||||
|
if (ct == null || ct.length() == 0) return null;
|
||||||
|
if (componentsMap.containsKey(ct)) return ct;
|
||||||
|
Map<String, Object> cmap = new LinkedHashMap<>();
|
||||||
|
componentsMap.put(ct, cmap); //必须在调用simpleSchemaType之前put,不然嵌套情况下死循环
|
||||||
|
|
||||||
|
cmap.put("type", "object");
|
||||||
|
List<String> requireds = new ArrayList<>();
|
||||||
|
Map<String, Object> properties = new LinkedHashMap<>();
|
||||||
|
if (encodeable instanceof ObjectEncoder) {
|
||||||
|
for (EnMember member : ((ObjectEncoder) encodeable).getMembers()) {
|
||||||
|
Map<String, Object> schemaMap = new LinkedHashMap<>();
|
||||||
|
simpleSchemaType(logger, componentsMap, TypeToken.typeToClassOrElse(member.getEncoder().getType(), Object.class), member.getEncoder().getType(), schemaMap, true);
|
||||||
|
String desc = "";
|
||||||
|
if (member.getField() != null) {
|
||||||
|
Column col = member.getField().getAnnotation(Column.class);
|
||||||
|
if (col == null) {
|
||||||
|
FilterColumn fcol = member.getField().getAnnotation(FilterColumn.class);
|
||||||
|
if (fcol != null) {
|
||||||
|
desc = fcol.comment();
|
||||||
|
if (fcol.required()) requireds.add(member.getAttribute().field());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
desc = col.comment();
|
||||||
|
if (!col.nullable()) requireds.add(member.getAttribute().field());
|
||||||
|
}
|
||||||
|
if (desc.isEmpty() && member.getField().getAnnotation(Comment.class) != null) {
|
||||||
|
desc = member.getField().getAnnotation(Comment.class).value();
|
||||||
|
}
|
||||||
|
} else if (member.getMethod() != null) {
|
||||||
|
Column col = member.getMethod().getAnnotation(Column.class);
|
||||||
|
if (col == null) {
|
||||||
|
FilterColumn fcol = member.getMethod().getAnnotation(FilterColumn.class);
|
||||||
|
if (fcol != null) {
|
||||||
|
desc = fcol.comment();
|
||||||
|
if (fcol.required()) requireds.add(member.getAttribute().field());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
desc = col.comment();
|
||||||
|
if (!col.nullable()) requireds.add(member.getAttribute().field());
|
||||||
|
}
|
||||||
|
if (desc.isEmpty() && member.getMethod().getAnnotation(Comment.class) != null) {
|
||||||
|
desc = member.getMethod().getAnnotation(Comment.class).value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!desc.isEmpty()) schemaMap.put("description", desc);
|
||||||
|
properties.put(member.getAttribute().field(), schemaMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!requireds.isEmpty()) cmap.put("required", requireds);
|
||||||
|
cmap.put("properties", properties);
|
||||||
|
return ct;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.log(Level.WARNING, genericType + " generate component info error", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String componentKey(Logger logger, Map<String, Map<String, Object>> componentsMap, EnMember field, Encodeable encodeable, boolean first) {
|
||||||
|
if (encodeable instanceof ObjectEncoder) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(((ObjectEncoder) encodeable).getTypeClass().getSimpleName());
|
||||||
|
for (EnMember member : ((ObjectEncoder) encodeable).getMembers()) {
|
||||||
|
if (member.getEncoder() instanceof ArrayEncoder
|
||||||
|
|| member.getEncoder() instanceof CollectionEncoder) {
|
||||||
|
String subsb = componentKey(logger, componentsMap, member, member.getEncoder(), false);
|
||||||
|
if (subsb == null) return null;
|
||||||
|
AccessibleObject real = member.getField() == null ? member.getMethod() : member.getField();
|
||||||
|
if (real == null) continue;
|
||||||
|
Class cz = real instanceof Field ? ((Field) real).getType() : ((Method) real).getReturnType();
|
||||||
|
Type ct = real instanceof Field ? ((Field) real).getGenericType() : ((Method) real).getGenericReturnType();
|
||||||
|
if (cz == ct) continue;
|
||||||
|
if (sb.length() > 0 && subsb.length() > 0) sb.append("_");
|
||||||
|
sb.append(subsb);
|
||||||
|
} else if (member.getEncoder() instanceof ObjectEncoder || member.getEncoder() instanceof SimpledCoder) {
|
||||||
|
AccessibleObject real = member.getField() == null ? member.getMethod() : member.getField();
|
||||||
|
if (real == null) continue;
|
||||||
|
if (member.getEncoder() instanceof SimpledCoder) {
|
||||||
|
simpleSchemaType(logger, componentsMap, ((SimpledCoder) member.getEncoder()).getType(), ((SimpledCoder) member.getEncoder()).getType(), new LinkedHashMap<>(), true);
|
||||||
|
} else {
|
||||||
|
simpleSchemaType(logger, componentsMap, ((ObjectEncoder) member.getEncoder()).getTypeClass(), ((ObjectEncoder) member.getEncoder()).getType(), new LinkedHashMap<>(), true);
|
||||||
|
}
|
||||||
|
Class cz = real instanceof Field ? ((Field) real).getType() : ((Method) real).getReturnType();
|
||||||
|
Type ct = real instanceof Field ? ((Field) real).getGenericType() : ((Method) real).getGenericReturnType();
|
||||||
|
if (cz == ct) continue;
|
||||||
|
String subsb = componentKey(logger, componentsMap, member, member.getEncoder(), false);
|
||||||
|
if (subsb == null) return null;
|
||||||
|
if (sb.length() > 0 && subsb.length() > 0) sb.append("_");
|
||||||
|
sb.append(subsb);
|
||||||
|
} else if (member.getEncoder() instanceof MapEncoder) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
} else if (encodeable instanceof ArrayEncoder || encodeable instanceof CollectionEncoder) {
|
||||||
|
final boolean array = (encodeable instanceof ArrayEncoder);
|
||||||
|
Encodeable subEncodeable = array ? ((ArrayEncoder) encodeable).getComponentEncoder() : ((CollectionEncoder) encodeable).getComponentEncoder();
|
||||||
|
if (subEncodeable instanceof SimpledCoder && field != null) return "";
|
||||||
|
final String sb = componentKey(logger, componentsMap, null, subEncodeable, false);
|
||||||
|
if (sb == null || sb.isEmpty()) return sb;
|
||||||
|
if (field != null && field.getField() != null && field.getField().getDeclaringClass() == Sheet.class) {
|
||||||
|
return sb;
|
||||||
|
}
|
||||||
|
return sb + (array ? "_Array" : "_Collection");
|
||||||
|
} else if (encodeable instanceof SimpledCoder) {
|
||||||
|
Class stype = ((SimpledCoder) encodeable).getType();
|
||||||
|
if (stype.isPrimitive() || stype == Boolean.class || Number.class.isAssignableFrom(stype) || CharSequence.class.isAssignableFrom(stype)) {
|
||||||
|
return stype.getSimpleName();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
} else if (encodeable instanceof MapEncoder) {
|
||||||
|
return first ? null : "";
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Object formatExample(String example, Class type, Type genericType) {
|
||||||
|
if (example == null || example.isEmpty()) return null;
|
||||||
|
if (type == Flipper.class) {
|
||||||
|
return new Flipper();
|
||||||
|
} else if (TYPE_RETRESULT_OBJECT.equals(genericType)) {
|
||||||
|
return RetResult.success();
|
||||||
|
} else if (TYPE_RETRESULT_STRING.equals(genericType)) {
|
||||||
|
return RetResult.success();
|
||||||
|
} else if (TYPE_RETRESULT_INTEGER.equals(genericType)) {
|
||||||
|
return RetResult.success(0);
|
||||||
|
} else if (TYPE_RETRESULT_LONG.equals(genericType)) {
|
||||||
|
return RetResult.success(0L);
|
||||||
|
}
|
||||||
|
return example;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -43,6 +43,22 @@ public interface ApplicationListener {
|
|||||||
default void postStart(Application application) {
|
default void postStart(Application application) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application 在运行Compile前调用
|
||||||
|
*
|
||||||
|
* @param application Application
|
||||||
|
*/
|
||||||
|
default void preCompile(Application application) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application 在运行Compile后调用
|
||||||
|
*
|
||||||
|
* @param application Application
|
||||||
|
*/
|
||||||
|
default void postCompile(Application application) {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application 在运行shutdown前调用
|
* Application 在运行shutdown前调用
|
||||||
*
|
*
|
||||||
@@ -9,6 +9,7 @@ import java.io.*;
|
|||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
@@ -63,11 +64,11 @@ public final class ClassFilter<T> {
|
|||||||
|
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
public ClassFilter(ClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses) {
|
public ClassFilter(RedkaleClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses) {
|
||||||
this(classLoader, annotationClass, superClass, excludeSuperClasses, null);
|
this(classLoader, annotationClass, superClass, excludeSuperClasses, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClassFilter(ClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses, AnyValue conf) {
|
public ClassFilter(RedkaleClassLoader classLoader, Class<? extends Annotation> annotationClass, Class superClass, Class[] excludeSuperClasses, AnyValue conf) {
|
||||||
this.annotationClass = annotationClass;
|
this.annotationClass = annotationClass;
|
||||||
this.superClass = superClass;
|
this.superClass = superClass;
|
||||||
this.excludeSuperClasses = excludeSuperClasses;
|
this.excludeSuperClasses = excludeSuperClasses;
|
||||||
@@ -75,8 +76,8 @@ public final class ClassFilter<T> {
|
|||||||
this.classLoader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
|
this.classLoader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ClassFilter create(Class[] excludeSuperClasses, String includeregs, String excluderegs, Set<String> includeValues, Set<String> excludeValues) {
|
public static ClassFilter create(RedkaleClassLoader classLoader, Class[] excludeSuperClasses, String includeregs, String excluderegs, Set<String> includeValues, Set<String> excludeValues) {
|
||||||
ClassFilter filter = new ClassFilter(null, null, null, excludeSuperClasses);
|
ClassFilter filter = new ClassFilter(classLoader, null, null, excludeSuperClasses);
|
||||||
filter.setIncludePatterns(includeregs == null ? null : includeregs.split(";"));
|
filter.setIncludePatterns(includeregs == null ? null : includeregs.split(";"));
|
||||||
filter.setExcludePatterns(excluderegs == null ? null : excluderegs.split(";"));
|
filter.setExcludePatterns(excluderegs == null ? null : excluderegs.split(";"));
|
||||||
filter.setPrivilegeIncludes(includeValues);
|
filter.setPrivilegeIncludes(includeValues);
|
||||||
@@ -483,20 +484,20 @@ public final class ClassFilter<T> {
|
|||||||
* 加载当前线程的classpath扫描所有class进行过滤
|
* 加载当前线程的classpath扫描所有class进行过滤
|
||||||
*
|
*
|
||||||
* @param excludeFile 不需要扫描的文件夹, 可以为null
|
* @param excludeFile 不需要扫描的文件夹, 可以为null
|
||||||
|
* @param loader RedkaleClassloader, 不可为null
|
||||||
* @param excludeRegs 包含此关键字的文件将被跳过, 可以为null
|
* @param excludeRegs 包含此关键字的文件将被跳过, 可以为null
|
||||||
* @param filters 过滤器
|
* @param filters 过滤器
|
||||||
*
|
*
|
||||||
* @throws IOException 异常
|
* @throws IOException 异常
|
||||||
*/
|
*/
|
||||||
public static void load(final File excludeFile, final String[] excludeRegs, final ClassFilter... filters) throws IOException {
|
public static void load(final File excludeFile, RedkaleClassLoader loader, final String[] excludeRegs, final ClassFilter... filters) throws IOException {
|
||||||
RedkaleClassLoader loader = (RedkaleClassLoader) Thread.currentThread().getContextClassLoader();
|
|
||||||
List<URL> urlfiles = new ArrayList<>(2);
|
List<URL> urlfiles = new ArrayList<>(2);
|
||||||
List<URL> urljares = new ArrayList<>(2);
|
List<URL> urljares = new ArrayList<>(2);
|
||||||
final URL exurl = excludeFile != null ? excludeFile.toURI().toURL() : null;
|
final URL exurl = excludeFile != null ? excludeFile.toURI().toURL() : null;
|
||||||
final Pattern[] excludePatterns = toPattern(excludeRegs);
|
final Pattern[] excludePatterns = toPattern(excludeRegs);
|
||||||
for (URL url : loader.getAllURLs()) {
|
for (URL url : loader.getAllURLs()) {
|
||||||
if (exurl != null && exurl.sameFile(url)) continue;
|
if (exurl != null && exurl.sameFile(url)) continue;
|
||||||
if (excludePatterns != null) {
|
if (excludePatterns != null && url != RedkaleClassLoader.URL_NONE) {
|
||||||
boolean skip = false;
|
boolean skip = false;
|
||||||
for (Pattern p : excludePatterns) {
|
for (Pattern p : excludePatterns) {
|
||||||
if (p.matcher(url.toString()).matches()) {
|
if (p.matcher(url.toString()).matches()) {
|
||||||
@@ -519,7 +520,7 @@ public final class ClassFilter<T> {
|
|||||||
Set<String> classes = cache.get(url);
|
Set<String> classes = cache.get(url);
|
||||||
if (classes == null) {
|
if (classes == null) {
|
||||||
classes = new LinkedHashSet<>();
|
classes = new LinkedHashSet<>();
|
||||||
try (JarFile jar = new JarFile(URLDecoder.decode(url.getFile(), "UTF-8"))) {
|
try (JarFile jar = new JarFile(URLDecoder.decode(url.getFile(), StandardCharsets.UTF_8))) {
|
||||||
Enumeration<JarEntry> it = jar.entries();
|
Enumeration<JarEntry> it = jar.entries();
|
||||||
while (it.hasMoreElements()) {
|
while (it.hasMoreElements()) {
|
||||||
String entryname = it.nextElement().getName().replace('/', '.');
|
String entryname = it.nextElement().getName().replace('/', '.');
|
||||||
@@ -527,6 +528,7 @@ public final class ClassFilter<T> {
|
|||||||
String classname = entryname.substring(0, entryname.length() - 6);
|
String classname = entryname.substring(0, entryname.length() - 6);
|
||||||
if (classname.startsWith("javax.") || classname.startsWith("com.sun.")) continue;
|
if (classname.startsWith("javax.") || classname.startsWith("com.sun.")) continue;
|
||||||
//常见的jar跳过
|
//常见的jar跳过
|
||||||
|
if (classname.startsWith("com.redkaledyn.")) break; //redkale动态生成的类
|
||||||
if (classname.startsWith("com.mysql.")) break;
|
if (classname.startsWith("com.mysql.")) break;
|
||||||
if (classname.startsWith("org.mariadb.")) break;
|
if (classname.startsWith("org.mariadb.")) break;
|
||||||
if (classname.startsWith("oracle.jdbc.")) break;
|
if (classname.startsWith("oracle.jdbc.")) break;
|
||||||
@@ -553,17 +555,27 @@ public final class ClassFilter<T> {
|
|||||||
Set<String> classes = cache.get(url);
|
Set<String> classes = cache.get(url);
|
||||||
if (classes == null) {
|
if (classes == null) {
|
||||||
classes = new LinkedHashSet<>();
|
classes = new LinkedHashSet<>();
|
||||||
files.clear();
|
final Set<String> cs = classes;
|
||||||
File root = new File(url.getFile());
|
if (url == RedkaleClassLoader.URL_NONE) loader.forEachCacheClass(v -> cs.add(v));
|
||||||
String rootpath = root.getPath();
|
if (cs.isEmpty()) {
|
||||||
loadClassFiles(excludeFile, root, files);
|
files.clear();
|
||||||
for (File f : files) {
|
File root = new File(url.getFile());
|
||||||
String classname = f.getPath().substring(rootpath.length() + 1, f.getPath().length() - 6).replace(File.separatorChar, '.');
|
String rootpath = root.getPath();
|
||||||
if (classname.startsWith("javax.") || classname.startsWith("com.sun.")) continue;
|
loadClassFiles(excludeFile, root, files);
|
||||||
classes.add(classname);
|
for (File f : files) {
|
||||||
if (debug) debugstr.append(classname).append("\r\n");
|
String classname = f.getPath().substring(rootpath.length() + 1, f.getPath().length() - 6).replace(File.separatorChar, '.');
|
||||||
for (final ClassFilter filter : filters) {
|
if (classname.startsWith("javax.") || classname.startsWith("com.sun.")) continue;
|
||||||
if (filter != null) filter.filter(null, classname, url);
|
classes.add(classname);
|
||||||
|
if (debug) debugstr.append(classname).append("\r\n");
|
||||||
|
for (final ClassFilter filter : filters) {
|
||||||
|
if (filter != null) filter.filter(null, classname, url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (String classname : classes) {
|
||||||
|
for (final ClassFilter filter : filters) {
|
||||||
|
if (filter != null) filter.filter(null, classname, url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cache.put(url, classes);
|
cache.put(url, classes);
|
||||||
@@ -5,6 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.boot;
|
package org.redkale.boot;
|
||||||
|
|
||||||
|
import org.redkale.util.RedkaleClassLoader;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
import static java.nio.file.StandardCopyOption.*;
|
import static java.nio.file.StandardCopyOption.*;
|
||||||
@@ -24,12 +26,15 @@ import java.util.regex.Pattern;
|
|||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class LogFileHandler extends Handler {
|
public class LoggingFileHandler extends Handler {
|
||||||
|
|
||||||
|
//public static final String FORMATTER_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s%n%5$s%6$s%n";
|
||||||
|
public static final String FORMATTER_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s\r\n%5$s%6$s\r\n";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SNCP的日志输出Handler
|
* SNCP的日志输出Handler
|
||||||
*/
|
*/
|
||||||
public static class SncpLogFileHandler extends LogFileHandler {
|
public static class LoggingSncpFileHandler extends LoggingFileHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPrefix() {
|
public String getPrefix() {
|
||||||
@@ -39,12 +44,11 @@ public class LogFileHandler extends Handler {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认的日志时间格式化类
|
* 默认的日志时间格式化类
|
||||||
|
* 与SimpleFormatter的区别在于level不使用本地化
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static class LoggingFormater extends Formatter {
|
public static class LoggingFormater extends Formatter {
|
||||||
|
|
||||||
private static final String format = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s\r\n%5$s%6$s\r\n";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(LogRecord log) {
|
public String format(LogRecord log) {
|
||||||
String source;
|
String source;
|
||||||
@@ -71,7 +75,7 @@ public class LogFileHandler extends Handler {
|
|||||||
pw.close();
|
pw.close();
|
||||||
throwable = sw.toString();
|
throwable = sw.toString();
|
||||||
}
|
}
|
||||||
return String.format(format,
|
return String.format(FORMATTER_FORMAT,
|
||||||
System.currentTimeMillis(),
|
System.currentTimeMillis(),
|
||||||
source,
|
source,
|
||||||
log.getLoggerName(),
|
log.getLoggerName(),
|
||||||
@@ -90,9 +94,9 @@ public class LogFileHandler extends Handler {
|
|||||||
ps.println("jdk.level = INFO");
|
ps.println("jdk.level = INFO");
|
||||||
ps.println("sun.level = INFO");
|
ps.println("sun.level = INFO");
|
||||||
ps.println("com.sun.level = INFO");
|
ps.println("com.sun.level = INFO");
|
||||||
ps.println("javax.level = INFO");
|
ps.println("javax.level = INFO");
|
||||||
ps.println("java.util.logging.ConsoleHandler.level = FINEST");
|
ps.println("java.util.logging.ConsoleHandler.level = FINEST");
|
||||||
ps.println("java.util.logging.ConsoleHandler.formatter = " + LogFileHandler.LoggingFormater.class.getName());
|
ps.println("java.util.logging.ConsoleHandler.formatter = " + LoggingFileHandler.LoggingFormater.class.getName());
|
||||||
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(out.toByteArray()));
|
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(out.toByteArray()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
@@ -130,7 +134,7 @@ public class LogFileHandler extends Handler {
|
|||||||
|
|
||||||
private OutputStream logunusualstream;
|
private OutputStream logunusualstream;
|
||||||
|
|
||||||
public LogFileHandler() {
|
public LoggingFileHandler() {
|
||||||
updateTomorrow();
|
updateTomorrow();
|
||||||
configure();
|
configure();
|
||||||
open();
|
open();
|
||||||
@@ -234,7 +238,7 @@ public class LogFileHandler extends Handler {
|
|||||||
|
|
||||||
private void configure() {
|
private void configure() {
|
||||||
LogManager manager = LogManager.getLogManager();
|
LogManager manager = LogManager.getLogManager();
|
||||||
String cname = LogFileHandler.class.getName();
|
String cname = LoggingFileHandler.class.getName();
|
||||||
this.pattern = manager.getProperty(cname + ".pattern");
|
this.pattern = manager.getProperty(cname + ".pattern");
|
||||||
if (this.pattern == null) {
|
if (this.pattern == null) {
|
||||||
this.pattern = "logs-%m/" + getPrefix() + "log-%d.log";
|
this.pattern = "logs-%m/" + getPrefix() + "log-%d.log";
|
||||||
@@ -296,6 +300,7 @@ public class LogFileHandler extends Handler {
|
|||||||
try {
|
try {
|
||||||
if (filterstr != null) {
|
if (filterstr != null) {
|
||||||
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(filterstr);
|
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(filterstr);
|
||||||
|
RedkaleClassLoader.putReflectionDeclaredConstructors(clz, clz.getName());
|
||||||
setFilter((Filter) clz.getDeclaredConstructor().newInstance());
|
setFilter((Filter) clz.getDeclaredConstructor().newInstance());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -304,6 +309,7 @@ public class LogFileHandler extends Handler {
|
|||||||
try {
|
try {
|
||||||
if (formatterstr != null) {
|
if (formatterstr != null) {
|
||||||
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(formatterstr);
|
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(formatterstr);
|
||||||
|
RedkaleClassLoader.putReflectionDeclaredConstructors(clz, clz.getName());
|
||||||
setFormatter((Formatter) clz.getDeclaredConstructor().newInstance());
|
setFormatter((Formatter) clz.getDeclaredConstructor().newInstance());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -1,399 +1,446 @@
|
|||||||
/*
|
/*
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
* To change this template file, choose Tools | Templates
|
* To change this template file, choose Tools | Templates
|
||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
package org.redkale.boot;
|
package org.redkale.boot;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import javax.annotation.*;
|
import java.util.stream.Stream;
|
||||||
import static org.redkale.boot.Application.RESNAME_SNCP_ADDR;
|
import javax.annotation.*;
|
||||||
import org.redkale.boot.ClassFilter.FilterEntry;
|
import static org.redkale.boot.Application.RESNAME_SNCP_ADDR;
|
||||||
import org.redkale.cluster.ClusterAgent;
|
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||||
import org.redkale.mq.MessageAgent;
|
import org.redkale.cluster.ClusterAgent;
|
||||||
import org.redkale.net.*;
|
import org.redkale.mq.MessageAgent;
|
||||||
import org.redkale.net.http.*;
|
import org.redkale.net.*;
|
||||||
import org.redkale.net.sncp.Sncp;
|
import org.redkale.net.http.*;
|
||||||
import org.redkale.service.*;
|
import org.redkale.net.sncp.Sncp;
|
||||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
import org.redkale.service.*;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||||
import org.redkale.watch.*;
|
import org.redkale.util.*;
|
||||||
|
import org.redkale.watch.*;
|
||||||
/**
|
|
||||||
* HTTP Server节点的配置Server
|
/**
|
||||||
*
|
* HTTP Server节点的配置Server
|
||||||
* <p>
|
*
|
||||||
* 详情见: https://redkale.org
|
* <p>
|
||||||
*
|
* 详情见: https://redkale.org
|
||||||
* @author zhangjx
|
*
|
||||||
*/
|
* @author zhangjx
|
||||||
@NodeProtocol("HTTP")
|
*/
|
||||||
public class NodeHttpServer extends NodeServer {
|
@NodeProtocol("HTTP")
|
||||||
|
public class NodeHttpServer extends NodeServer {
|
||||||
protected final boolean rest; //是否加载REST服务, 为true加载rest节点信息并将所有可REST化的Service生成RestServlet
|
|
||||||
|
protected final boolean rest; //是否加载REST服务, 为true加载rest节点信息并将所有可REST化的Service生成RestServlet
|
||||||
protected final HttpServer httpServer;
|
|
||||||
|
protected final HttpServer httpServer;
|
||||||
public NodeHttpServer(Application application, AnyValue serconf) {
|
|
||||||
super(application, createServer(application, serconf));
|
public NodeHttpServer(Application application, AnyValue serconf) {
|
||||||
this.httpServer = (HttpServer) server;
|
super(application, createServer(application, serconf));
|
||||||
this.rest = serconf == null ? false : serconf.getAnyValue("rest") != null;
|
this.httpServer = (HttpServer) server;
|
||||||
}
|
this.rest = serconf == null ? false : serconf.getAnyValue("rest") != null;
|
||||||
|
}
|
||||||
private static Server createServer(Application application, AnyValue serconf) {
|
|
||||||
return new HttpServer(application, application.getStartTime(), application.getResourceFactory().createChild());
|
private static Server createServer(Application application, AnyValue serconf) {
|
||||||
}
|
return new HttpServer(application, application.getStartTime(), application.getResourceFactory().createChild());
|
||||||
|
}
|
||||||
public HttpServer getHttpServer() {
|
|
||||||
return httpServer;
|
public HttpServer getHttpServer() {
|
||||||
}
|
return httpServer;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public InetSocketAddress getSocketAddress() {
|
@Override
|
||||||
return httpServer == null ? null : httpServer.getSocketAddress();
|
public InetSocketAddress getSocketAddress() {
|
||||||
}
|
return httpServer == null ? null : httpServer.getSocketAddress();
|
||||||
|
}
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
@Override
|
||||||
protected ClassFilter<Service> createServiceClassFilter() {
|
@SuppressWarnings("unchecked")
|
||||||
return createClassFilter(this.sncpGroup, null, Service.class, new Class[]{org.redkale.watch.WatchService.class}, Annotation.class, "services", "service");
|
protected ClassFilter<Service> createServiceClassFilter() {
|
||||||
}
|
return createClassFilter(this.sncpGroup, null, Service.class, new Class[]{org.redkale.watch.WatchService.class}, Annotation.class, "services", "service");
|
||||||
|
}
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
@Override
|
||||||
protected ClassFilter<Filter> createFilterClassFilter() {
|
@SuppressWarnings("unchecked")
|
||||||
return createClassFilter(null, null, HttpFilter.class, new Class[]{WatchFilter.class}, null, "filters", "filter");
|
protected ClassFilter<Filter> createFilterClassFilter() {
|
||||||
}
|
return createClassFilter(null, null, HttpFilter.class, new Class[]{WatchFilter.class}, null, "filters", "filter");
|
||||||
|
}
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
@Override
|
||||||
protected ClassFilter<Servlet> createServletClassFilter() {
|
@SuppressWarnings("unchecked")
|
||||||
return createClassFilter(null, WebServlet.class, HttpServlet.class, new Class[]{WatchServlet.class}, null, "servlets", "servlet");
|
protected ClassFilter<Servlet> createServletClassFilter() {
|
||||||
}
|
return createClassFilter(null, WebServlet.class, HttpServlet.class, new Class[]{WatchServlet.class}, null, "servlets", "servlet");
|
||||||
|
}
|
||||||
@Override
|
|
||||||
protected ClassFilter createOtherClassFilter() {
|
@Override
|
||||||
return createClassFilter(null, RestWebSocket.class, WebSocket.class, null, null, "rest", "websocket");
|
protected ClassFilter createOtherClassFilter() {
|
||||||
}
|
return createClassFilter(null, RestWebSocket.class, WebSocket.class, null, null, "rest", "websocket");
|
||||||
|
}
|
||||||
@Override
|
|
||||||
protected void loadService(ClassFilter<? extends Service> serviceFilter, ClassFilter otherFilter) throws Exception {
|
@Override
|
||||||
super.loadService(serviceFilter, otherFilter);
|
protected void loadService(ClassFilter<? extends Service> serviceFilter, ClassFilter otherFilter) throws Exception {
|
||||||
initWebSocketService();
|
super.loadService(serviceFilter, otherFilter);
|
||||||
}
|
initWebSocketService();
|
||||||
|
}
|
||||||
@Override
|
|
||||||
protected void loadFilter(ClassFilter<? extends Filter> filterFilter, ClassFilter otherFilter) throws Exception {
|
@Override
|
||||||
if (httpServer != null) loadHttpFilter(this.serverConf.getAnyValue("filters"), filterFilter);
|
protected void loadFilter(ClassFilter<? extends Filter> filterFilter, ClassFilter otherFilter) throws Exception {
|
||||||
}
|
if (httpServer != null) loadHttpFilter(this.serverConf.getAnyValue("filters"), filterFilter);
|
||||||
|
}
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
@Override
|
||||||
protected void loadServlet(ClassFilter<? extends Servlet> servletFilter, ClassFilter otherFilter) throws Exception {
|
@SuppressWarnings("unchecked")
|
||||||
if (httpServer != null) loadHttpServlet(servletFilter, otherFilter);
|
protected void loadServlet(ClassFilter<? extends Servlet> servletFilter, ClassFilter otherFilter) throws Exception {
|
||||||
}
|
if (httpServer != null) loadHttpServlet(servletFilter, otherFilter);
|
||||||
|
}
|
||||||
private void initWebSocketService() {
|
|
||||||
final NodeServer self = this;
|
private void initWebSocketService() {
|
||||||
final ResourceFactory regFactory = application.getResourceFactory();
|
final NodeServer self = this;
|
||||||
resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, Object attachment) -> { //主要用于单点的服务
|
final ResourceFactory regFactory = application.getResourceFactory();
|
||||||
try {
|
resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, Object attachment) -> { //主要用于单点的服务
|
||||||
if (field.getAnnotation(Resource.class) == null) return;
|
try {
|
||||||
if (!(src instanceof WebSocketServlet)) return;
|
if (field.getAnnotation(Resource.class) == null) return;
|
||||||
ResourceFactory.ResourceLoader loader = null;
|
if (!(src instanceof WebSocketServlet)) return;
|
||||||
ResourceFactory sncpResFactory = null;
|
ResourceFactory.ResourceLoader loader = null;
|
||||||
for (NodeServer ns : application.servers) {
|
ResourceFactory sncpResFactory = null;
|
||||||
if (!ns.isSNCP()) continue;
|
for (NodeServer ns : application.servers) {
|
||||||
sncpResFactory = ns.resourceFactory;
|
if (!ns.isSNCP()) continue;
|
||||||
loader = sncpResFactory.findLoader(WebSocketNode.class, field);
|
sncpResFactory = ns.resourceFactory;
|
||||||
if (loader != null) break;
|
loader = sncpResFactory.findLoader(WebSocketNode.class, field);
|
||||||
}
|
if (loader != null) break;
|
||||||
if (loader != null) loader.load(sncpResFactory, src, resourceName, field, attachment);
|
}
|
||||||
synchronized (regFactory) {
|
if (loader != null) loader.load(sncpResFactory, src, resourceName, field, attachment);
|
||||||
Service nodeService = (Service) rf.find(resourceName, WebSocketNode.class);
|
synchronized (regFactory) {
|
||||||
if (sncpResFactory != null && resourceFactory.find(RESNAME_SNCP_ADDR, String.class) == null) {
|
Service nodeService = (Service) rf.find(resourceName, WebSocketNode.class);
|
||||||
resourceFactory.register(RESNAME_SNCP_ADDR, InetSocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, InetSocketAddress.class));
|
if (sncpResFactory != null && resourceFactory.find(RESNAME_SNCP_ADDR, String.class) == null) {
|
||||||
resourceFactory.register(RESNAME_SNCP_ADDR, SocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, SocketAddress.class));
|
resourceFactory.register(RESNAME_SNCP_ADDR, InetSocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, InetSocketAddress.class));
|
||||||
resourceFactory.register(RESNAME_SNCP_ADDR, String.class, sncpResFactory.find(RESNAME_SNCP_ADDR, String.class));
|
resourceFactory.register(RESNAME_SNCP_ADDR, SocketAddress.class, sncpResFactory.find(RESNAME_SNCP_ADDR, SocketAddress.class));
|
||||||
}
|
resourceFactory.register(RESNAME_SNCP_ADDR, String.class, sncpResFactory.find(RESNAME_SNCP_ADDR, String.class));
|
||||||
if (nodeService == null) {
|
}
|
||||||
MessageAgent messageAgent = null;
|
if (nodeService == null) {
|
||||||
try {
|
MessageAgent messageAgent = null;
|
||||||
Field c = WebSocketServlet.class.getDeclaredField("messageAgent");
|
try {
|
||||||
c.setAccessible(true);
|
Field c = WebSocketServlet.class.getDeclaredField("messageAgent");
|
||||||
messageAgent = (MessageAgent) c.get(src);
|
c.setAccessible(true);
|
||||||
} catch (Exception ex) {
|
messageAgent = (MessageAgent) c.get(src);
|
||||||
logger.log(Level.WARNING, "WebSocketServlet getMessageAgent error", ex);
|
} catch (Exception ex) {
|
||||||
}
|
logger.log(Level.WARNING, "WebSocketServlet getMessageAgent error", ex);
|
||||||
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, messageAgent, application.getResourceFactory(), application.getSncpTransportFactory(), (InetSocketAddress) null, (Set<String>) null, (AnyValue) null);
|
}
|
||||||
regFactory.register(resourceName, WebSocketNode.class, nodeService);
|
nodeService = Sncp.createLocalService(serverClassLoader, resourceName, WebSocketNodeService.class, messageAgent, application.getResourceFactory(), application.getSncpTransportFactory(), (InetSocketAddress) null, (Set<String>) null, (AnyValue) null);
|
||||||
}
|
regFactory.register(resourceName, WebSocketNode.class, nodeService);
|
||||||
resourceFactory.inject(nodeService, self);
|
}
|
||||||
field.set(src, nodeService);
|
resourceFactory.inject(nodeService, self);
|
||||||
logger.fine("[" + Thread.currentThread().getName() + "] Load Service " + nodeService);
|
field.set(src, nodeService);
|
||||||
}
|
logger.fine("[" + Thread.currentThread().getName() + "] Load Service " + nodeService);
|
||||||
} catch (Exception e) {
|
}
|
||||||
logger.log(Level.SEVERE, "WebSocketNode inject error", e);
|
} catch (Exception e) {
|
||||||
}
|
logger.log(Level.SEVERE, "WebSocketNode inject error", e);
|
||||||
}, WebSocketNode.class);
|
}
|
||||||
}
|
}, WebSocketNode.class);
|
||||||
|
}
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
protected void loadHttpFilter(final AnyValue filtersConf, final ClassFilter<? extends Filter> classFilter) throws Exception {
|
@SuppressWarnings("unchecked")
|
||||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
protected void loadHttpFilter(final AnyValue filtersConf, final ClassFilter<? extends Filter> classFilter) throws Exception {
|
||||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||||
List<FilterEntry<? extends Filter>> list = new ArrayList(classFilter.getFilterEntrys());
|
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||||
for (FilterEntry<? extends Filter> en : list) {
|
List<FilterEntry<? extends Filter>> list = new ArrayList(classFilter.getFilterEntrys());
|
||||||
Class<HttpFilter> clazz = (Class<HttpFilter>) en.getType();
|
for (FilterEntry<? extends Filter> en : list) {
|
||||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
Class<HttpFilter> clazz = (Class<HttpFilter>) en.getType();
|
||||||
final HttpFilter filter = clazz.getDeclaredConstructor().newInstance();
|
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||||
resourceFactory.inject(filter, this);
|
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName());
|
||||||
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
final HttpFilter filter = clazz.getDeclaredConstructor().newInstance();
|
||||||
this.httpServer.addHttpFilter(filter, filterConf);
|
resourceFactory.inject(filter, this);
|
||||||
if (sb != null) sb.append(localThreadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
||||||
}
|
this.httpServer.addHttpFilter(filter, filterConf);
|
||||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
if (sb != null) sb.append(localThreadName).append(" Load ").append(clazz.getName()).append(LINE_SEPARATOR);
|
||||||
}
|
}
|
||||||
|
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString());
|
||||||
@SuppressWarnings("unchecked")
|
}
|
||||||
protected void loadHttpServlet(final ClassFilter<? extends Servlet> servletFilter, ClassFilter<? extends WebSocket> webSocketFilter) throws Exception {
|
|
||||||
final AnyValue servletsConf = this.serverConf.getAnyValue("servlets");
|
@SuppressWarnings("unchecked")
|
||||||
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
protected void loadHttpServlet(final ClassFilter<? extends Servlet> servletFilter, ClassFilter<? extends WebSocket> webSocketFilter) throws Exception {
|
||||||
String prefix0 = servletsConf == null ? "" : servletsConf.getValue("path", "");
|
RedkaleClassLoader.putReflectionPublicClasses(HttpServlet.class.getName());
|
||||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
RedkaleClassLoader.putReflectionPublicClasses(HttpPrepareServlet.class.getName());
|
||||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
RedkaleClassLoader.putReflectionDeclaredConstructors(HttpResourceServlet.class, HttpResourceServlet.class.getName());
|
||||||
final String prefix = prefix0;
|
final AnyValue servletsConf = this.serverConf.getAnyValue("servlets");
|
||||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null;
|
||||||
List<FilterEntry<? extends Servlet>> list = new ArrayList(servletFilter.getFilterEntrys());
|
String prefix0 = servletsConf == null ? "" : servletsConf.getValue("path", "");
|
||||||
list.sort((FilterEntry<? extends Servlet> o1, FilterEntry<? extends Servlet> o2) -> { //必须保证WebSocketServlet优先加载, 因为要确保其他的HttpServlet可以注入本地模式的WebSocketNode
|
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||||
boolean ws1 = WebSocketServlet.class.isAssignableFrom(o1.getType());
|
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||||
boolean ws2 = WebSocketServlet.class.isAssignableFrom(o2.getType());
|
final String prefix = prefix0;
|
||||||
if (ws1 == ws2) {
|
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||||
Priority p1 = o1.getType().getAnnotation(Priority.class);
|
List<FilterEntry<? extends Servlet>> list = new ArrayList(servletFilter.getFilterEntrys());
|
||||||
Priority p2 = o2.getType().getAnnotation(Priority.class);
|
list.sort((FilterEntry<? extends Servlet> o1, FilterEntry<? extends Servlet> o2) -> { //必须保证WebSocketServlet优先加载, 因为要确保其他的HttpServlet可以注入本地模式的WebSocketNode
|
||||||
int v = (p2 == null ? 0 : p2.value()) - (p1 == null ? 0 : p1.value());
|
boolean ws1 = WebSocketServlet.class.isAssignableFrom(o1.getType());
|
||||||
return v == 0 ? o1.getType().getName().compareTo(o2.getType().getName()) : 0;
|
boolean ws2 = WebSocketServlet.class.isAssignableFrom(o2.getType());
|
||||||
}
|
if (ws1 == ws2) {
|
||||||
return ws1 ? -1 : 1;
|
Priority p1 = o1.getType().getAnnotation(Priority.class);
|
||||||
});
|
Priority p2 = o2.getType().getAnnotation(Priority.class);
|
||||||
final List<AbstractMap.SimpleEntry<String, String[]>> ss = sb == null ? null : new ArrayList<>();
|
int v = (p2 == null ? 0 : p2.value()) - (p1 == null ? 0 : p1.value());
|
||||||
for (FilterEntry<? extends Servlet> en : list) {
|
return v == 0 ? o1.getType().getName().compareTo(o2.getType().getName()) : 0;
|
||||||
Class<HttpServlet> clazz = (Class<HttpServlet>) en.getType();
|
}
|
||||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
return ws1 ? -1 : 1;
|
||||||
WebServlet ws = clazz.getAnnotation(WebServlet.class);
|
});
|
||||||
if (ws == null) continue;
|
final long starts = System.currentTimeMillis();
|
||||||
if (ws.value().length == 0) {
|
final List<AbstractMap.SimpleEntry<String, String[]>> ss = sb == null ? null : new ArrayList<>();
|
||||||
logger.log(Level.INFO, "not found @WebServlet.value in " + clazz.getName());
|
for (FilterEntry<? extends Servlet> en : list) {
|
||||||
continue;
|
Class<HttpServlet> clazz = (Class<HttpServlet>) en.getType();
|
||||||
}
|
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||||
final HttpServlet servlet = clazz.getDeclaredConstructor().newInstance();
|
if (clazz.getAnnotation(Rest.RestDyn.class) != null) continue; //动态生成的跳过
|
||||||
resourceFactory.inject(servlet, this);
|
WebServlet ws = clazz.getAnnotation(WebServlet.class);
|
||||||
final String[] mappings = ws.value();
|
if (ws == null) continue;
|
||||||
String pref = ws.repair() ? prefix : "";
|
if (ws.value().length == 0) {
|
||||||
DefaultAnyValue servletConf = (DefaultAnyValue) en.getProperty();
|
logger.log(Level.INFO, "not found @WebServlet.value in " + clazz.getName());
|
||||||
this.httpServer.addHttpServlet(servlet, pref, servletConf, mappings);
|
continue;
|
||||||
if (ss != null) {
|
}
|
||||||
for (int i = 0; i < mappings.length; i++) {
|
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName());
|
||||||
mappings[i] = pref + mappings[i];
|
final HttpServlet servlet = clazz.getDeclaredConstructor().newInstance();
|
||||||
}
|
resourceFactory.inject(servlet, this);
|
||||||
ss.add(new AbstractMap.SimpleEntry<>(clazz.getName(), mappings));
|
final String[] mappings = ws.value();
|
||||||
}
|
String pref = ws.repair() ? prefix : "";
|
||||||
}
|
DefaultAnyValue servletConf = (DefaultAnyValue) en.getProperty();
|
||||||
int max = 0;
|
this.httpServer.addHttpServlet(servlet, pref, servletConf, mappings);
|
||||||
if (ss != null && sb != null) {
|
if (ss != null) {
|
||||||
ss.sort((AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));
|
for (int i = 0; i < mappings.length; i++) {
|
||||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
mappings[i] = pref + mappings[i];
|
||||||
if (as.getKey().length() > max) max = as.getKey().length();
|
}
|
||||||
}
|
ss.add(new AbstractMap.SimpleEntry<>("HttpServlet (type=" + clazz.getName() + ")", mappings));
|
||||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
}
|
||||||
sb.append(localThreadName).append(" Load ").append(as.getKey());
|
}
|
||||||
for (int i = 0; i < max - as.getKey().length(); i++) {
|
final List<AbstractMap.SimpleEntry<String, String[]>> rests = sb == null ? null : new ArrayList<>();
|
||||||
sb.append(' ');
|
final List<AbstractMap.SimpleEntry<String, String[]>> webss = sb == null ? null : new ArrayList<>();
|
||||||
}
|
if (rest && serverConf != null) {
|
||||||
sb.append(" mapping to ").append(Arrays.toString(as.getValue())).append(LINE_SEPARATOR);
|
final List<Object> restedObjects = new ArrayList<>();
|
||||||
}
|
for (AnyValue restConf : serverConf.getAnyValues("rest")) {
|
||||||
}
|
loadRestServlet(webSocketFilter, restConf, restedObjects, sb, rests, webss);
|
||||||
if (rest && serverConf != null) {
|
}
|
||||||
final List<Object> restedObjects = new ArrayList<>();
|
}
|
||||||
for (AnyValue restConf : serverConf.getAnyValues("rest")) {
|
int max = 0;
|
||||||
loadRestServlet(webSocketFilter, restConf, restedObjects, sb);
|
if (ss != null && sb != null) {
|
||||||
}
|
int maxTypeLength = 0;
|
||||||
}
|
int maxNameLength = 0;
|
||||||
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString().trim());
|
if (rests != null) {
|
||||||
}
|
for (AbstractMap.SimpleEntry<String, String[]> en : rests) {
|
||||||
|
int pos = en.getKey().indexOf('#');
|
||||||
@SuppressWarnings("unchecked")
|
if (pos > maxTypeLength) maxTypeLength = pos;
|
||||||
protected void loadRestServlet(final ClassFilter<? extends WebSocket> webSocketFilter, final AnyValue restConf, final List<Object> restedObjects, final StringBuilder sb) throws Exception {
|
int len = en.getKey().length() - pos - 1;
|
||||||
if (!rest) return;
|
if (len > maxNameLength) maxNameLength = len;
|
||||||
if (restConf == null) return; //不存在REST服务
|
}
|
||||||
|
}
|
||||||
final long starts = System.currentTimeMillis();
|
if (webss != null) {
|
||||||
String prefix0 = restConf.getValue("path", "");
|
for (AbstractMap.SimpleEntry<String, String[]> en : webss) {
|
||||||
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
int pos = en.getKey().indexOf('#');
|
||||||
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
if (pos > maxTypeLength) maxTypeLength = pos;
|
||||||
|
int len = en.getKey().length() - pos - 1;
|
||||||
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
if (len > maxNameLength) maxNameLength = len;
|
||||||
final List<AbstractMap.SimpleEntry<String, String[]>> ss = sb == null ? null : new ArrayList<>();
|
}
|
||||||
String mqname = restConf.getValue("mq");
|
}
|
||||||
MessageAgent agent0 = null;
|
if (rests != null) {
|
||||||
if (mqname != null) {
|
for (AbstractMap.SimpleEntry<String, String[]> en : rests) {
|
||||||
agent0 = application.getMessageAgent(mqname);
|
StringBuilder sub = new StringBuilder();
|
||||||
if (agent0 == null) throw new RuntimeException("not found " + MessageAgent.class.getSimpleName() + " config for (name=" + mqname + ")");
|
int pos = en.getKey().indexOf('#');
|
||||||
}
|
sub.append("RestDynServlet (type=").append(en.getKey().substring(0, pos));
|
||||||
final MessageAgent messageAgent = agent0;
|
for (int i = 0; i < maxTypeLength - pos; i++) {
|
||||||
if (messageAgent != null) prefix0 = ""; //开启MQ时,prefix字段失效
|
sub.append(' ');
|
||||||
final String prefix = prefix0;
|
}
|
||||||
final boolean autoload = restConf.getBoolValue("autoload", true);
|
sub.append(", name='").append(en.getKey().substring(pos + 1));
|
||||||
{ //加载RestService
|
for (int i = 0; i < maxNameLength - pos; i++) {
|
||||||
String userTypeStr = restConf.getValue("usertype");
|
sub.append(' ');
|
||||||
final Class userType = userTypeStr == null ? null : this.serverClassLoader.loadClass(userTypeStr);
|
}
|
||||||
|
sub.append("')");
|
||||||
final Class baseServletType = this.serverClassLoader.loadClass(restConf.getValue("base", HttpServlet.class.getName()));
|
ss.add(new AbstractMap.SimpleEntry<>(sub.toString(), en.getValue()));
|
||||||
final Set<String> includeValues = new HashSet<>();
|
}
|
||||||
final Set<String> excludeValues = new HashSet<>();
|
}
|
||||||
for (AnyValue item : restConf.getAnyValues("service")) {
|
if (webss != null) {
|
||||||
if (item.getBoolValue("ignore", false)) {
|
for (AbstractMap.SimpleEntry<String, String[]> en : webss) {
|
||||||
excludeValues.add(item.getValue("value", ""));
|
StringBuilder sub = new StringBuilder();
|
||||||
} else {
|
int pos = en.getKey().indexOf('#');
|
||||||
includeValues.add(item.getValue("value", ""));
|
sub.append("RestWebSocket (type=").append(en.getKey().substring(0, pos));
|
||||||
}
|
for (int i = 0; i < maxTypeLength - pos; i++) {
|
||||||
}
|
sub.append(' ');
|
||||||
|
}
|
||||||
final ClassFilter restFilter = ClassFilter.create(null, restConf.getValue("includes", ""), restConf.getValue("excludes", ""), includeValues, excludeValues);
|
sub.append(", name='").append(en.getKey().substring(pos + 1));
|
||||||
final boolean finest = logger.isLoggable(Level.FINEST);
|
for (int i = 0; i < maxNameLength - pos; i++) {
|
||||||
final CountDownLatch scdl = new CountDownLatch(super.interceptorServices.size());
|
sub.append(' ');
|
||||||
super.interceptorServices.stream().parallel().forEach((service) -> {
|
}
|
||||||
try {
|
sub.append("')");
|
||||||
final Class stype = Sncp.getServiceType(service);
|
ss.add(new AbstractMap.SimpleEntry<>(sub.toString(), en.getValue()));
|
||||||
final String name = Sncp.getResourceName(service);
|
}
|
||||||
RestService rs = (RestService) stype.getAnnotation(RestService.class);
|
}
|
||||||
if (rs == null || rs.ignore()) return;
|
ss.sort((AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));
|
||||||
|
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||||
final String stypename = stype.getName();
|
if (as.getKey().length() > max) max = as.getKey().length();
|
||||||
if (!autoload && !includeValues.contains(stypename)) return;
|
}
|
||||||
if (!restFilter.accept(stypename)) return;
|
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
||||||
synchronized (restedObjects) {
|
sb.append(localThreadName).append("Load ").append(as.getKey());
|
||||||
if (restedObjects.contains(service)) {
|
for (int i = 0; i < max - as.getKey().length(); i++) {
|
||||||
logger.log(Level.WARNING, stype.getName() + " repeat create rest servlet, so ignore");
|
sb.append(' ');
|
||||||
return;
|
}
|
||||||
}
|
sb.append(" mapping to ").append(Arrays.toString(as.getValue())).append(LINE_SEPARATOR);
|
||||||
restedObjects.add(service); //避免重复创建Rest对象
|
}
|
||||||
}
|
sb.append(localThreadName).append("All HttpServlets load cost ").append(System.currentTimeMillis() - starts).append(" ms").append(LINE_SEPARATOR);
|
||||||
HttpServlet servlet = httpServer.addRestServlet(serverClassLoader, service, userType, baseServletType, prefix);
|
}
|
||||||
if (servlet == null) return; //没有HttpMapping方法的HttpServlet调用Rest.createRestServlet就会返回null
|
if (sb != null && sb.length() > 0) logger.log(Level.INFO, sb.toString().trim());
|
||||||
String prefix2 = prefix;
|
}
|
||||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
|
||||||
if (ws != null && !ws.repair()) prefix2 = "";
|
@SuppressWarnings("unchecked")
|
||||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
protected void loadRestServlet(final ClassFilter<? extends WebSocket> webSocketFilter, final AnyValue restConf, final List<Object> restedObjects, final StringBuilder sb,
|
||||||
dynServletMap.put(service, servlet);
|
final List<AbstractMap.SimpleEntry<String, String[]>> rests, final List<AbstractMap.SimpleEntry<String, String[]>> webss) throws Exception {
|
||||||
if (messageAgent != null) messageAgent.putService(this, service, servlet);
|
if (!rest) return;
|
||||||
//if (finest) logger.finest(localThreadName + " Create RestServlet(resource.name='" + name + "') = " + servlet);
|
if (restConf == null) return; //不存在REST服务
|
||||||
if (ss != null) {
|
|
||||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
String prefix0 = restConf.getValue("path", "");
|
||||||
for (int i = 0; i < mappings.length; i++) {
|
if (!prefix0.isEmpty() && prefix0.charAt(prefix0.length() - 1) == '/') prefix0 = prefix0.substring(0, prefix0.length() - 1);
|
||||||
mappings[i] = prefix2 + mappings[i];
|
if (!prefix0.isEmpty() && prefix0.charAt(0) != '/') prefix0 = '/' + prefix0;
|
||||||
}
|
|
||||||
synchronized (ss) {
|
final String localThreadName = "[" + Thread.currentThread().getName() + "] ";
|
||||||
ss.add(new AbstractMap.SimpleEntry<>(servlet.getClass().getName() + "(rest.name='" + name + "')", mappings));
|
String mqname = restConf.getValue("mq");
|
||||||
}
|
MessageAgent agent0 = null;
|
||||||
}
|
if (mqname != null) {
|
||||||
} finally {
|
agent0 = application.getMessageAgent(mqname);
|
||||||
scdl.countDown();
|
if (agent0 == null) throw new RuntimeException("not found " + MessageAgent.class.getSimpleName() + " config for (name=" + mqname + ")");
|
||||||
}
|
}
|
||||||
});
|
final MessageAgent messageAgent = agent0;
|
||||||
scdl.await();
|
if (messageAgent != null) prefix0 = ""; //开启MQ时,prefix字段失效
|
||||||
}
|
final String prefix = prefix0;
|
||||||
if (webSocketFilter != null) { //加载RestWebSocket
|
final boolean autoload = restConf.getBoolValue("autoload", true);
|
||||||
final Set<String> includeValues = new HashSet<>();
|
{ //加载RestService
|
||||||
final Set<String> excludeValues = new HashSet<>();
|
String userTypeStr = restConf.getValue("usertype");
|
||||||
for (AnyValue item : restConf.getAnyValues("websocket")) {
|
final Class userType = userTypeStr == null ? null : this.serverClassLoader.loadClass(userTypeStr);
|
||||||
if (item.getBoolValue("ignore", false)) {
|
|
||||||
excludeValues.add(item.getValue("value", ""));
|
final Class baseServletType = this.serverClassLoader.loadClass(restConf.getValue("base", HttpServlet.class.getName()));
|
||||||
} else {
|
final Set<String> includeValues = new HashSet<>();
|
||||||
includeValues.add(item.getValue("value", ""));
|
final Set<String> excludeValues = new HashSet<>();
|
||||||
}
|
for (AnyValue item : restConf.getAnyValues("service")) {
|
||||||
}
|
if (item.getBoolValue("ignore", false)) {
|
||||||
|
excludeValues.add(item.getValue("value", ""));
|
||||||
final ClassFilter restFilter = ClassFilter.create(null, restConf.getValue("includes", ""), restConf.getValue("excludes", ""), includeValues, excludeValues);
|
} else {
|
||||||
final boolean finest = logger.isLoggable(Level.FINEST);
|
includeValues.add(item.getValue("value", ""));
|
||||||
|
}
|
||||||
List<FilterEntry<? extends WebSocket>> list = new ArrayList(webSocketFilter.getFilterEntrys());
|
}
|
||||||
for (FilterEntry<? extends WebSocket> en : list) {
|
|
||||||
Class<WebSocket> clazz = (Class<WebSocket>) en.getType();
|
final ClassFilter restFilter = ClassFilter.create(serverClassLoader, null, application.isCompileMode() ? "" : restConf.getValue("includes", ""), application.isCompileMode() ? "" : restConf.getValue("excludes", ""), includeValues, excludeValues);
|
||||||
if (Modifier.isAbstract(clazz.getModifiers())) {
|
final CountDownLatch scdl = new CountDownLatch(super.interceptorServices.size());
|
||||||
logger.log(Level.FINE, clazz.getName() + " cannot abstract on rest websocket, so ignore");
|
Stream<Service> stream = super.interceptorServices.stream();
|
||||||
continue;
|
if (!application.isCompileMode()) stream = stream.parallel(); //不能并行,否则在maven plugin运行环境下ClassLoader不对
|
||||||
}
|
stream.forEach((service) -> {
|
||||||
if (Modifier.isFinal(clazz.getModifiers())) {
|
try {
|
||||||
logger.log(Level.FINE, clazz.getName() + " cannot final on rest websocket, so ignore");
|
final Class stype = Sncp.getServiceType(service);
|
||||||
continue;
|
final String name = Sncp.getResourceName(service);
|
||||||
}
|
RestService rs = (RestService) stype.getAnnotation(RestService.class);
|
||||||
final Class<? extends WebSocket> stype = en.getType();
|
if (rs == null || rs.ignore()) return;
|
||||||
RestWebSocket rs = stype.getAnnotation(RestWebSocket.class);
|
|
||||||
if (rs == null || rs.ignore()) return;
|
final String stypename = stype.getName();
|
||||||
|
if (!autoload && !includeValues.contains(stypename)) return;
|
||||||
final String stypename = stype.getName();
|
if (!restFilter.accept(stypename)) return;
|
||||||
if (!autoload && !includeValues.contains(stypename)) return;
|
synchronized (restedObjects) {
|
||||||
if (!restFilter.accept(stypename)) return;
|
if (restedObjects.contains(service)) {
|
||||||
if (restedObjects.contains(stype)) {
|
logger.log(Level.WARNING, stype.getName() + " repeat create rest servlet, so ignore");
|
||||||
logger.log(Level.WARNING, stype.getName() + " repeat create rest websocket, so ignore");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
restedObjects.add(service); //避免重复创建Rest对象
|
||||||
restedObjects.add(stype); //避免重复创建Rest对象
|
}
|
||||||
WebSocketServlet servlet = httpServer.addRestWebSocketServlet(serverClassLoader, stype, messageAgent, prefix, en.getProperty());
|
HttpServlet servlet = httpServer.addRestServlet(serverClassLoader, service, userType, baseServletType, prefix);
|
||||||
if (servlet == null) return; //没有RestOnMessage方法的HttpServlet调用Rest.createRestWebSocketServlet就会返回null
|
if (servlet == null) return; //没有HttpMapping方法的HttpServlet调用Rest.createRestServlet就会返回null
|
||||||
String prefix2 = prefix;
|
String prefix2 = prefix;
|
||||||
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||||
if (ws != null && !ws.repair()) prefix2 = "";
|
if (ws != null && !ws.repair()) prefix2 = "";
|
||||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||||
if (finest) logger.finest(localThreadName + " " + stype.getName() + " create a RestWebSocketServlet");
|
dynServletMap.put(service, servlet);
|
||||||
if (ss != null) {
|
if (messageAgent != null) messageAgent.putService(this, service, servlet);
|
||||||
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
//if (finest) logger.finest(localThreadName + " Create RestServlet(resource.name='" + name + "') = " + servlet);
|
||||||
for (int i = 0; i < mappings.length; i++) {
|
if (rests != null) {
|
||||||
mappings[i] = prefix2 + mappings[i];
|
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||||
}
|
for (int i = 0; i < mappings.length; i++) {
|
||||||
ss.add(new AbstractMap.SimpleEntry<>(servlet.getClass().getName(), mappings));
|
mappings[i] = prefix2 + mappings[i];
|
||||||
}
|
}
|
||||||
}
|
synchronized (rests) {
|
||||||
}
|
rests.add(new AbstractMap.SimpleEntry<>(Sncp.getServiceType(service).getName() + "#" + name, mappings));
|
||||||
if (messageAgent != null) this.messageAgents.put(messageAgent.getName(), messageAgent);
|
}
|
||||||
//输出信息
|
}
|
||||||
if (ss != null && !ss.isEmpty() && sb != null) {
|
} finally {
|
||||||
ss.sort((AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));
|
scdl.countDown();
|
||||||
int max = 0;
|
}
|
||||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
});
|
||||||
if (as.getKey().length() > max) max = as.getKey().length();
|
scdl.await();
|
||||||
}
|
}
|
||||||
for (AbstractMap.SimpleEntry<String, String[]> as : ss) {
|
if (webSocketFilter != null) { //加载RestWebSocket
|
||||||
sb.append(localThreadName).append(" Load ").append(as.getKey());
|
final Set<String> includeValues = new HashSet<>();
|
||||||
for (int i = 0; i < max - as.getKey().length(); i++) {
|
final Set<String> excludeValues = new HashSet<>();
|
||||||
sb.append(' ');
|
for (AnyValue item : restConf.getAnyValues("websocket")) {
|
||||||
}
|
if (item.getBoolValue("ignore", false)) {
|
||||||
sb.append(" mapping to ").append(Arrays.toString(as.getValue())).append(LINE_SEPARATOR);
|
excludeValues.add(item.getValue("value", ""));
|
||||||
}
|
} else {
|
||||||
sb.append(localThreadName).append(" All HttpServlets load cost ").append(System.currentTimeMillis() - starts).append(" ms").append(LINE_SEPARATOR);
|
includeValues.add(item.getValue("value", ""));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
final ClassFilter restFilter = ClassFilter.create(serverClassLoader, null, application.isCompileMode() ? "" : restConf.getValue("includes", ""), application.isCompileMode() ? "" : restConf.getValue("excludes", ""), includeValues, excludeValues);
|
||||||
@Override //loadServlet执行之后调用
|
final boolean finest = logger.isLoggable(Level.FINEST);
|
||||||
protected void postLoadServlets() {
|
|
||||||
final ClusterAgent cluster = application.clusterAgent;
|
List<FilterEntry<? extends WebSocket>> list = new ArrayList(webSocketFilter.getFilterEntrys());
|
||||||
if (cluster != null) {
|
for (FilterEntry<? extends WebSocket> en : list) {
|
||||||
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
|
Class<WebSocket> clazz = (Class<WebSocket>) en.getType();
|
||||||
String protocol = pros.value().toUpperCase();
|
if (Modifier.isAbstract(clazz.getModifiers())) {
|
||||||
if (!cluster.containsProtocol(protocol)) return;
|
logger.log(Level.FINE, clazz.getName() + " cannot abstract on rest websocket, so ignore");
|
||||||
if (!cluster.containsPort(server.getSocketAddress().getPort())) return;
|
continue;
|
||||||
cluster.register(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
}
|
||||||
}
|
if (Modifier.isFinal(clazz.getModifiers())) {
|
||||||
}
|
logger.log(Level.FINE, clazz.getName() + " cannot final on rest websocket, so ignore");
|
||||||
|
continue;
|
||||||
@Override
|
}
|
||||||
protected void afterClusterDeregisterOnPreDestroyServices(ClusterAgent cluster, String protocol) {
|
final Class<? extends WebSocket> stype = en.getType();
|
||||||
cluster.deregister(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
if (stype.getAnnotation(Rest.RestDyn.class) != null) continue;
|
||||||
}
|
RestWebSocket rs = stype.getAnnotation(RestWebSocket.class);
|
||||||
}
|
if (rs == null || rs.ignore()) continue;
|
||||||
|
|
||||||
|
final String stypename = stype.getName();
|
||||||
|
if (!autoload && !includeValues.contains(stypename)) continue;
|
||||||
|
if (!restFilter.accept(stypename)) continue;
|
||||||
|
if (restedObjects.contains(stype)) {
|
||||||
|
logger.log(Level.WARNING, stype.getName() + " repeat create rest websocket, so ignore");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
restedObjects.add(stype); //避免重复创建Rest对象
|
||||||
|
WebSocketServlet servlet = httpServer.addRestWebSocketServlet(serverClassLoader, stype, messageAgent, prefix, en.getProperty());
|
||||||
|
if (servlet == null) continue; //没有RestOnMessage方法的HttpServlet调用Rest.createRestWebSocketServlet就会返回null
|
||||||
|
String prefix2 = prefix;
|
||||||
|
WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class);
|
||||||
|
if (ws != null && !ws.repair()) prefix2 = "";
|
||||||
|
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||||
|
if (finest) logger.finest(localThreadName + " " + stype.getName() + " create a RestWebSocketServlet");
|
||||||
|
if (webss != null) {
|
||||||
|
String[] mappings = servlet.getClass().getAnnotation(WebServlet.class).value();
|
||||||
|
for (int i = 0; i < mappings.length; i++) {
|
||||||
|
mappings[i] = prefix2 + mappings[i];
|
||||||
|
}
|
||||||
|
synchronized (webss) {
|
||||||
|
webss.add(new AbstractMap.SimpleEntry<>(stype.getName() + "#" + rs.name(), mappings));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (messageAgent != null) this.messageAgents.put(messageAgent.getName(), messageAgent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override //loadServlet执行之后调用
|
||||||
|
protected void postLoadServlets() {
|
||||||
|
final ClusterAgent cluster = application.clusterAgent;
|
||||||
|
if (!application.isCompileMode() && cluster != null) {
|
||||||
|
NodeProtocol pros = getClass().getAnnotation(NodeProtocol.class);
|
||||||
|
String protocol = pros.value().toUpperCase();
|
||||||
|
if (!cluster.containsProtocol(protocol)) return;
|
||||||
|
if (!cluster.containsPort(server.getSocketAddress().getPort())) return;
|
||||||
|
cluster.register(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void afterClusterDeregisterOnPreDestroyServices(ClusterAgent cluster, String protocol) {
|
||||||
|
cluster.deregister(this, protocol, dynServletMap.keySet(), new HashSet<>());
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -33,8 +33,8 @@ public class NodeSncpServer extends NodeServer {
|
|||||||
private NodeSncpServer(Application application, AnyValue serconf) {
|
private NodeSncpServer(Application application, AnyValue serconf) {
|
||||||
super(application, createServer(application, serconf));
|
super(application, createServer(application, serconf));
|
||||||
this.sncpServer = (SncpServer) this.server;
|
this.sncpServer = (SncpServer) this.server;
|
||||||
this.consumer = sncpServer == null || application.singletonrun ? null : (agent, x) -> {//singleton模式下不生成SncpServlet
|
this.consumer = sncpServer == null || application.isSingletonMode() ? null : (agent, x) -> {//singleton模式下不生成SncpServlet
|
||||||
if (x.getClass().getAnnotation(Local.class) != null) return;
|
if (x.getClass().getAnnotation(Local.class) != null) return; //本地模式的Service不生成SncpServlet
|
||||||
SncpDynServlet servlet = sncpServer.addSncpServlet(x);
|
SncpDynServlet servlet = sncpServer.addSncpServlet(x);
|
||||||
dynServletMap.put(x, servlet);
|
dynServletMap.put(x, servlet);
|
||||||
if (agent != null) agent.putService(this, x, servlet);
|
if (agent != null) agent.putService(this, x, servlet);
|
||||||
@@ -95,6 +95,7 @@ public class NodeSncpServer extends NodeServer {
|
|||||||
for (FilterEntry<? extends Filter> en : list) {
|
for (FilterEntry<? extends Filter> en : list) {
|
||||||
Class<SncpFilter> clazz = (Class<SncpFilter>) en.getType();
|
Class<SncpFilter> clazz = (Class<SncpFilter>) en.getType();
|
||||||
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
if (Modifier.isAbstract(clazz.getModifiers())) continue;
|
||||||
|
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, clazz.getName());
|
||||||
final SncpFilter filter = clazz.getDeclaredConstructor().newInstance();
|
final SncpFilter filter = clazz.getDeclaredConstructor().newInstance();
|
||||||
resourceFactory.inject(filter, this);
|
resourceFactory.inject(filter, this);
|
||||||
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
DefaultAnyValue filterConf = (DefaultAnyValue) en.getProperty();
|
||||||
@@ -106,6 +107,8 @@ public class NodeSncpServer extends NodeServer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadServlet(ClassFilter<? extends Servlet> servletFilter, ClassFilter otherFilter) throws Exception {
|
protected void loadServlet(ClassFilter<? extends Servlet> servletFilter, ClassFilter otherFilter) throws Exception {
|
||||||
|
RedkaleClassLoader.putReflectionPublicClasses(SncpServlet.class.getName());
|
||||||
|
RedkaleClassLoader.putReflectionPublicClasses(SncpDynServlet.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
86
src/main/java/org/redkale/boot/PrepareCompiler.java
Normal file
86
src/main/java/org/redkale/boot/PrepareCompiler.java
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.boot;
|
||||||
|
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import org.redkale.boot.ClassFilter.FilterEntry;
|
||||||
|
import org.redkale.convert.Decodeable;
|
||||||
|
import org.redkale.convert.bson.BsonFactory;
|
||||||
|
import org.redkale.convert.json.*;
|
||||||
|
import org.redkale.source.*;
|
||||||
|
import org.redkale.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行一次Application.run提前获取所有动态类
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
* @since 2.5.0
|
||||||
|
*/
|
||||||
|
public class PrepareCompiler {
|
||||||
|
|
||||||
|
// public static void main(String[] args) throws Exception {
|
||||||
|
// new PrepareCompiler().run();
|
||||||
|
// }
|
||||||
|
public Application run() throws Exception {
|
||||||
|
final Application application = new Application(false, true, Application.loadAppConfig());
|
||||||
|
application.init();
|
||||||
|
for (ApplicationListener listener : application.listeners) {
|
||||||
|
listener.preStart(application);
|
||||||
|
}
|
||||||
|
for (ApplicationListener listener : application.listeners) {
|
||||||
|
listener.preCompile(application);
|
||||||
|
}
|
||||||
|
application.start();
|
||||||
|
final boolean hasSncp = application.getNodeServers().stream().filter(v -> v instanceof NodeSncpServer).findFirst().isPresent();
|
||||||
|
final String[] exlibs = (application.excludelibs != null ? (application.excludelibs + ";") : "").split(";");
|
||||||
|
|
||||||
|
final ClassFilter<?> entityFilter = new ClassFilter(application.getClassLoader(), Entity.class, Object.class, (Class[]) null);
|
||||||
|
final ClassFilter<?> beanFilter = new ClassFilter(application.getClassLoader(), Bean.class, Object.class, (Class[]) null);
|
||||||
|
final ClassFilter<?> filterFilter = new ClassFilter(application.getClassLoader(), null, FilterBean.class, (Class[]) null);
|
||||||
|
|
||||||
|
ClassFilter.Loader.load(application.getHome(), application.getClassLoader(), exlibs, entityFilter, beanFilter, filterFilter);
|
||||||
|
|
||||||
|
for (FilterEntry en : entityFilter.getFilterEntrys()) {
|
||||||
|
Class clz = en.getType();
|
||||||
|
if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers())) continue;
|
||||||
|
try {
|
||||||
|
application.dataSources.forEach(source -> source.compile(clz));
|
||||||
|
JsonFactory.root().loadEncoder(clz);
|
||||||
|
if (hasSncp) BsonFactory.root().loadEncoder(clz);
|
||||||
|
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||||
|
if (hasSncp) BsonFactory.root().loadDecoder(clz);
|
||||||
|
decoder.convertFrom(new JsonReader("{}"));
|
||||||
|
} catch (Exception e) { //JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (FilterEntry en : beanFilter.getFilterEntrys()) {
|
||||||
|
Class clz = en.getType();
|
||||||
|
if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers())) continue;
|
||||||
|
try {
|
||||||
|
JsonFactory.root().loadEncoder(clz);
|
||||||
|
if (hasSncp) BsonFactory.root().loadEncoder(clz);
|
||||||
|
Decodeable decoder = JsonFactory.root().loadDecoder(clz);
|
||||||
|
if (hasSncp) BsonFactory.root().loadDecoder(clz);
|
||||||
|
decoder.convertFrom(new JsonReader("{}"));
|
||||||
|
} catch (Exception e) { //JsonFactory.loadDecoder可能会失败,因为class可能包含抽象类字段,如ColumnValue.value字段
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (FilterEntry en : filterFilter.getFilterEntrys()) {
|
||||||
|
Class clz = en.getType();
|
||||||
|
if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers())) continue;
|
||||||
|
try {
|
||||||
|
FilterNodeBean.load(clz);
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (ApplicationListener listener : application.listeners) {
|
||||||
|
listener.postCompile(application);
|
||||||
|
}
|
||||||
|
application.shutdown();
|
||||||
|
return application;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -55,8 +55,8 @@
|
|||||||
} else {
|
} else {
|
||||||
var w = param.required ? "font-weight:bold;" : "";
|
var w = param.required ? "font-weight:bold;" : "";
|
||||||
var c = ' style="' + w + '"';
|
var c = ' style="' + w + '"';
|
||||||
if (param.src == "HEADER") c = ' style="color:red;' + w + '"';
|
if (param.style == "HEADER") c = ' style="color:red;' + w + '"';
|
||||||
if (param.src == "COOKIE") c = ' style="color:blue;' + w + '"';
|
if (param.style == "COOKIE") c = ' style="color:blue;' + w + '"';
|
||||||
paramshtml.push('<tr><td ' + c + '> ' + param.name + ' </td><td> ' + t + '</td><td> ' + param.comment + '</td></tr>');
|
paramshtml.push('<tr><td ' + c + '> ' + param.name + ' </td><td> ' + t + '</td><td> ' + param.comment + '</td></tr>');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,7 @@ public class CacheClusterAgent extends ClusterAgent implements Resourcable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override //ServiceLoader时判断配置是否符合当前实现类
|
@Override //ServiceLoader时判断配置是否符合当前实现类
|
||||||
public boolean match(AnyValue config) {
|
public boolean acceptsConf(AnyValue config) {
|
||||||
if (config == null) return false;
|
if (config == null) return false;
|
||||||
AnyValue[] properties = config.getAnyValues("property");
|
AnyValue[] properties = config.getAnyValues("property");
|
||||||
if (properties == null || properties.length == 0) return false;
|
if (properties == null || properties.length == 0) return false;
|
||||||
@@ -85,7 +85,7 @@ public abstract class ClusterAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//ServiceLoader时判断配置是否符合当前实现类
|
//ServiceLoader时判断配置是否符合当前实现类
|
||||||
public abstract boolean match(AnyValue config);
|
public abstract boolean acceptsConf(AnyValue config);
|
||||||
|
|
||||||
public boolean containsProtocol(String protocol) {
|
public boolean containsProtocol(String protocol) {
|
||||||
if (protocol == null || protocol.isEmpty()) return false;
|
if (protocol == null || protocol.isEmpty()) return false;
|
||||||
@@ -15,11 +15,11 @@ import org.redkale.util.AnyValue;
|
|||||||
* 详情见: https://redkale.org
|
* 详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @since 2.4.0
|
* @since 2.5.0
|
||||||
*/
|
*/
|
||||||
public interface ClusterAgentLoader {
|
public interface ClusterAgentProvider {
|
||||||
|
|
||||||
public boolean match(AnyValue config);
|
public boolean acceptsConf(AnyValue config);
|
||||||
|
|
||||||
public Class<? extends ClusterAgent> agentClass();
|
public Class<? extends ClusterAgent> agentClass();
|
||||||
}
|
}
|
||||||
40
src/main/java/org/redkale/convert/AnyValueDecoder.java
Normal file
40
src/main/java/org/redkale/convert/AnyValueDecoder.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.convert;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import org.redkale.util.AnyValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AnyValue的Decoder实现
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
* @param <R> Reader输入的子类型
|
||||||
|
*
|
||||||
|
* @since 2.5.0
|
||||||
|
*/
|
||||||
|
public class AnyValueDecoder<R extends Reader> implements Decodeable<R, AnyValue> {
|
||||||
|
|
||||||
|
protected final ConvertFactory factory;
|
||||||
|
|
||||||
|
public AnyValueDecoder(final ConvertFactory factory) {
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AnyValue convertFrom(R in) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return AnyValue.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
33
src/main/java/org/redkale/convert/AnyValueEncoder.java
Normal file
33
src/main/java/org/redkale/convert/AnyValueEncoder.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.convert;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import org.redkale.util.AnyValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AnyValue的Encoder实现
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
* @param <W> Writer输出的子类
|
||||||
|
*
|
||||||
|
* @since 2.5.0
|
||||||
|
*/
|
||||||
|
public class AnyValueEncoder<W extends Writer> implements Encodeable<W, AnyValue> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void convertTo(W out, AnyValue value) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return AnyValue.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -57,6 +57,10 @@ public abstract class Convert<R extends Reader, W extends Writer> {
|
|||||||
|
|
||||||
public abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers);
|
public abstract <T> T convertFrom(final Type type, final ConvertMask mask, final ByteBuffer... buffers);
|
||||||
|
|
||||||
|
public abstract void convertTo(final W writer, final Object value);
|
||||||
|
|
||||||
|
public abstract void convertTo(final W writer, final Type type, final Object value);
|
||||||
|
|
||||||
public abstract byte[] convertToBytes(final Object value);
|
public abstract byte[] convertToBytes(final Object value);
|
||||||
|
|
||||||
public abstract byte[] convertToBytes(final Type type, final Object value);
|
public abstract byte[] convertToBytes(final Type type, final Object value);
|
||||||
@@ -102,12 +102,17 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
||||||
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
|
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
|
||||||
this.register(java.util.Date.class, DateSimpledCoder.instance);
|
this.register(java.util.Date.class, DateSimpledCoder.instance);
|
||||||
|
this.register(java.time.Instant.class, InstantSimpledCoder.instance);
|
||||||
|
this.register(java.time.LocalDate.class, LocalDateSimpledCoder.instance);
|
||||||
|
this.register(java.time.LocalTime.class, LocalTimeSimpledCoder.instance);
|
||||||
|
this.register(java.time.LocalDateTime.class, LocalDateTimeSimpledCoder.instance);
|
||||||
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
|
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
|
||||||
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
|
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
|
||||||
this.register(AtomicLong.class, AtomicLongSimpledCoder.instance);
|
this.register(AtomicLong.class, AtomicLongSimpledCoder.instance);
|
||||||
this.register(BigInteger.class, BigIntegerSimpledCoder.instance);
|
this.register(BigInteger.class, BigIntegerSimpledCoder.instance);
|
||||||
this.register(BigDecimal.class, BigDecimalSimpledCoder.instance);
|
this.register(BigDecimal.class, BigDecimalSimpledCoder.instance);
|
||||||
this.register(InetAddress.class, InetAddressSimpledCoder.instance);
|
this.register(InetAddress.class, InetAddressSimpledCoder.instance);
|
||||||
|
this.register(LongAdder.class, LongAdderSimpledCoder.instance);
|
||||||
this.register(DLong.class, DLongSimpledCoder.instance);
|
this.register(DLong.class, DLongSimpledCoder.instance);
|
||||||
this.register(Class.class, TypeSimpledCoder.instance);
|
this.register(Class.class, TypeSimpledCoder.instance);
|
||||||
this.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressSimpledCoder.instance);
|
this.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressSimpledCoder.instance);
|
||||||
@@ -142,48 +147,51 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
|
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
Class sqldateClass = Class.forName("java.sql.Date");
|
Class sqldateClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date");
|
||||||
this.register(sqldateClass, new SimpledCoder<R, W, java.sql.Date>() {
|
Invoker<Object, Object> sqldateInvoker = Invoker.create(sqldateClass, "valueOf", String.class);
|
||||||
|
this.register(sqldateClass, new SimpledCoder<R, W, Object>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void convertTo(W out, java.sql.Date value) {
|
public void convertTo(W out, Object value) {
|
||||||
out.writeSmallString(value == null ? null : value.toString());
|
out.writeSmallString(value == null ? null : value.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public java.sql.Date convertFrom(R in) {
|
public Object convertFrom(R in) {
|
||||||
String t = in.readSmallString();
|
String t = in.readSmallString();
|
||||||
return t == null ? null : java.sql.Date.valueOf(t);
|
return t == null ? null : sqldateInvoker.invoke(null, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
Class sqltimeClass = Class.forName("java.sql.Time");
|
Class sqltimeClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Time");
|
||||||
this.register(sqltimeClass, new SimpledCoder<R, W, java.sql.Time>() {
|
Invoker<Object, Object> sqltimeInvoker = Invoker.create(sqltimeClass, "valueOf", String.class);
|
||||||
|
this.register(sqltimeClass, new SimpledCoder<R, W, Object>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void convertTo(W out, java.sql.Time value) {
|
public void convertTo(W out, Object value) {
|
||||||
out.writeSmallString(value == null ? null : value.toString());
|
out.writeSmallString(value == null ? null : value.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public java.sql.Time convertFrom(R in) {
|
public Object convertFrom(R in) {
|
||||||
String t = in.readSmallString();
|
String t = in.readSmallString();
|
||||||
return t == null ? null : java.sql.Time.valueOf(t);
|
return t == null ? null : sqltimeInvoker.invoke(null, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
Class timestampClass = Class.forName("java.sql.Timestamp");
|
Class timestampClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Timestamp");
|
||||||
this.register(timestampClass, new SimpledCoder<R, W, java.sql.Timestamp>() {
|
Invoker<Object, Object> timestampInvoker = Invoker.create(timestampClass, "valueOf", String.class);
|
||||||
|
this.register(timestampClass, new SimpledCoder<R, W, Object>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void convertTo(W out, java.sql.Timestamp value) {
|
public void convertTo(W out, Object value) {
|
||||||
out.writeSmallString(value == null ? null : value.toString());
|
out.writeSmallString(value == null ? null : value.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public java.sql.Timestamp convertFrom(R in) {
|
public Object convertFrom(R in) {
|
||||||
String t = in.readSmallString();
|
String t = in.readSmallString();
|
||||||
return t == null ? null : java.sql.Timestamp.valueOf(t);
|
return t == null ? null : timestampInvoker.invoke(null, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -205,9 +213,11 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
}
|
}
|
||||||
synchronized (loaderInited) {
|
synchronized (loaderInited) {
|
||||||
if (!loaderInited.get()) {
|
if (!loaderInited.get()) {
|
||||||
Iterator<ConvertLoader> it = ServiceLoader.load(ConvertLoader.class).iterator();
|
Iterator<ConvertProvider> it = ServiceLoader.load(ConvertProvider.class).iterator();
|
||||||
|
RedkaleClassLoader.putServiceLoader(ConvertProvider.class);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
ConvertLoader cl = it.next();
|
ConvertProvider cl = it.next();
|
||||||
|
RedkaleClassLoader.putReflectionPublicConstructors(cl.getClass(), cl.getClass().getName());
|
||||||
if (cl.type() == ConvertType.PROTOBUF) defProtobufConvert = cl.convert();
|
if (cl.type() == ConvertType.PROTOBUF) defProtobufConvert = cl.convert();
|
||||||
}
|
}
|
||||||
loaderInited.set(true);
|
loaderInited.set(true);
|
||||||
@@ -234,6 +244,24 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
return new EnumSimpledCoder(enumClass);
|
return new EnumSimpledCoder(enumClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Type formatObjectType(Type type) {
|
||||||
|
if (type instanceof Class) {
|
||||||
|
Class<?> clazz = (Class) type;
|
||||||
|
ConvertImpl ci = clazz.getAnnotation(ConvertImpl.class);
|
||||||
|
if (ci != null) {
|
||||||
|
if (!Modifier.isAbstract(clazz.getModifiers()) && !Modifier.isInterface(clazz.getModifiers())) {
|
||||||
|
throw new ConvertException("@" + ConvertImpl.class.getSimpleName() + " must at interface or abstract class, but " + clazz + " not");
|
||||||
|
}
|
||||||
|
Class impl = ci.value();
|
||||||
|
if (Modifier.isAbstract(impl.getModifiers()) || Modifier.isInterface(impl.getModifiers())) {
|
||||||
|
throw new ConvertException("@" + ConvertImpl.class.getSimpleName() + " at class " + impl + " cannot be interface or abstract class");
|
||||||
|
}
|
||||||
|
return impl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
protected <E> Encodeable<W, E> createDyncEncoder(Type type) {
|
protected <E> Encodeable<W, E> createDyncEncoder(Type type) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -360,10 +388,20 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Field readGetSetField(Method method) {
|
||||||
|
String name = readGetSetFieldName(method);
|
||||||
|
if (name == null) return null;
|
||||||
|
try {
|
||||||
|
return method.getDeclaringClass().getDeclaredField(name);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static String readGetSetFieldName(Method method) {
|
static String readGetSetFieldName(Method method) {
|
||||||
if (method == null) return null;
|
if (method == null) return null;
|
||||||
String fname = method.getName();
|
String fname = method.getName();
|
||||||
if (!fname.startsWith("is") && !fname.startsWith("get") && !fname.startsWith("set")) return fname;
|
if (!fname.startsWith("is") && !fname.startsWith("get") && !fname.startsWith("set")) return fname; //record类会直接用field名作为method名
|
||||||
fname = fname.substring(fname.startsWith("is") ? 2 : 3);
|
fname = fname.substring(fname.startsWith("is") ? 2 : 3);
|
||||||
if (fname.length() > 1 && !(fname.charAt(1) >= 'A' && fname.charAt(1) <= 'Z')) {
|
if (fname.length() > 1 && !(fname.charAt(1) >= 'A' && fname.charAt(1) <= 'Z')) {
|
||||||
fname = Character.toLowerCase(fname.charAt(0)) + fname.substring(1);
|
fname = Character.toLowerCase(fname.charAt(0)) + fname.substring(1);
|
||||||
@@ -727,12 +765,15 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
try {
|
try {
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
simpleCoder = (Decodeable) method.invoke(null, this);
|
simpleCoder = (Decodeable) method.invoke(null, this);
|
||||||
|
RedkaleClassLoader.putReflectionDeclaredMethods(clazz.getName());
|
||||||
|
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
|
||||||
break;
|
break;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (simpleCoder == null) {
|
if (simpleCoder == null) {
|
||||||
od = createObjectDecoder(type);
|
Type impl = formatObjectType(type);
|
||||||
|
od = createObjectDecoder(impl);
|
||||||
decoder = od;
|
decoder = od;
|
||||||
} else {
|
} else {
|
||||||
decoder = simpleCoder;
|
decoder = simpleCoder;
|
||||||
@@ -811,14 +852,17 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
try {
|
try {
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
simpleCoder = (Encodeable) method.invoke(null, this);
|
simpleCoder = (Encodeable) method.invoke(null, this);
|
||||||
|
RedkaleClassLoader.putReflectionDeclaredMethods(clazz.getName());
|
||||||
|
RedkaleClassLoader.putReflectionMethod(clazz.getName(), method);
|
||||||
break;
|
break;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (simpleCoder == null) {
|
if (simpleCoder == null) {
|
||||||
encoder = createDyncEncoder(type);
|
Type impl = formatObjectType(type);
|
||||||
|
encoder = createDyncEncoder(impl);
|
||||||
if (encoder == null) {
|
if (encoder == null) {
|
||||||
oe = createObjectEncoder(type);
|
oe = createObjectEncoder(impl);
|
||||||
encoder = oe;
|
encoder = oe;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
53
src/main/java/org/redkale/convert/ConvertImpl.java
Normal file
53
src/main/java/org/redkale/convert/ConvertImpl.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.convert;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于序列化时接口或抽象类的默认实现类, 被标记的类必须是接口或抽象类 <br>
|
||||||
|
* 使用场景: <br>
|
||||||
|
*
|
||||||
|
* <blockquote><pre>
|
||||||
|
* @ConvertImpl(OneImpl.class)
|
||||||
|
* public interface OneEntity {
|
||||||
|
* public String getName();
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* public class OneImpl implements OneEntity {
|
||||||
|
* private String name;
|
||||||
|
* public String getName(){return name;}
|
||||||
|
* public void setName(String name){this.name=name;}
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* String json = "{'name':'hello'}";
|
||||||
|
* OneEntity one = JsonConvert.root.convertFrom(OneEntity.class, json);
|
||||||
|
* //one instanceof OneImpl
|
||||||
|
*
|
||||||
|
* </pre></blockquote>
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
* @since 2.5.0
|
||||||
|
*/
|
||||||
|
@Inherited
|
||||||
|
@Documented
|
||||||
|
@Target({TYPE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface ConvertImpl {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认的实现类
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
Class value();
|
||||||
|
}
|
||||||
@@ -13,9 +13,9 @@ package org.redkale.convert;
|
|||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*
|
*
|
||||||
* @since 2.2.0
|
* @since 2.5.0
|
||||||
*/
|
*/
|
||||||
public interface ConvertLoader {
|
public interface ConvertProvider {
|
||||||
|
|
||||||
public ConvertType type();
|
public ConvertType type();
|
||||||
|
|
||||||
@@ -22,6 +22,10 @@ import org.redkale.util.Attribute;
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public final class DeMember<R extends Reader, T, F> {
|
public final class DeMember<R extends Reader, T, F> {
|
||||||
|
|
||||||
|
final Field field; //对应类成员的Field, 也可能为null
|
||||||
|
|
||||||
|
final Method method; //对应类成员的Method也可能为null
|
||||||
|
|
||||||
protected int index;
|
protected int index;
|
||||||
|
|
||||||
protected int position; //从1开始
|
protected int position; //从1开始
|
||||||
@@ -32,33 +36,40 @@ public final class DeMember<R extends Reader, T, F> {
|
|||||||
|
|
||||||
protected Decodeable<R, F> decoder;
|
protected Decodeable<R, F> decoder;
|
||||||
|
|
||||||
public DeMember(final Attribute<T, F> attribute) {
|
public DeMember(final Attribute<T, F> attribute, Field field, Method method) {
|
||||||
this.attribute = attribute;
|
this.attribute = attribute;
|
||||||
|
this.field = field;
|
||||||
|
this.method = method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DeMember(Attribute<T, F> attribute, Decodeable<R, F> decoder) {
|
public DeMember(Attribute<T, F> attribute, Decodeable<R, F> decoder, Field field, Method method) {
|
||||||
this(attribute);
|
this(attribute, field, method);
|
||||||
this.decoder = decoder;
|
this.decoder = decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <R extends Reader, T, F> DeMember<R, T, F> create(final ConvertFactory factory, final Class<T> clazz, final String fieldname) {
|
public static <R extends Reader, T, F> DeMember<R, T, F> create(final ConvertFactory factory, final Class<T> clazz, final String fieldname) {
|
||||||
try {
|
try {
|
||||||
Field field = clazz.getDeclaredField(fieldname);
|
Field field = clazz.getDeclaredField(fieldname);
|
||||||
return new DeMember<>(Attribute.create(field), factory.loadDecoder(field.getGenericType()));
|
return new DeMember<>(Attribute.create(field), factory.loadDecoder(field.getGenericType()), field, null);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <R extends Reader, T, F> DeMember<R, T, F> create(final ConvertFactory factory, final Class<T> clazz, final String fieldname, final Class<F> fieldtype) {
|
public static <R extends Reader, T, F> DeMember<R, T, F> create(final ConvertFactory factory, final Class<T> clazz, final String fieldname, final Class<F> fieldtype) {
|
||||||
return new DeMember<>(Attribute.create(clazz, fieldname, fieldtype), factory.loadDecoder(fieldtype));
|
try {
|
||||||
|
Field field = clazz.getDeclaredField(fieldname);
|
||||||
|
return new DeMember<>(Attribute.create(clazz, fieldname, fieldtype), factory.loadDecoder(fieldtype), field, null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <R extends Reader, T, F> DeMember<R, T, F> create(final Attribute<T, F> attribute, final ConvertFactory factory, final Class<F> fieldtype) {
|
public static <R extends Reader, T, F> DeMember<R, T, F> create(final Attribute<T, F> attribute, final ConvertFactory factory, final Class<F> fieldtype) {
|
||||||
return new DeMember<>(attribute, factory.loadDecoder(fieldtype));
|
return new DeMember<>(attribute, factory.loadDecoder(fieldtype), null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final boolean match(String name) {
|
public final boolean accepts(String name) {
|
||||||
return attribute.field().equals(name);
|
return attribute.field().equals(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +89,14 @@ public final class DeMember<R extends Reader, T, F> {
|
|||||||
return decoder;
|
return decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Field getField() {
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Method getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
public int getIndex() {
|
public int getIndex() {
|
||||||
return this.index;
|
return this.index;
|
||||||
}
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user