|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.framework.javax; |
| 16 | + |
| 17 | +import apijson.Log; |
| 18 | +import apijson.NotNull; |
| 19 | +import apijson.orm.AbstractFunctionParser; |
| 20 | +import apijson.orm.script.ScriptExecutor; |
| 21 | + |
| 22 | +import java.rmi.ServerException; |
| 23 | + |
| 24 | + |
| 25 | +/**启动入口 Application |
| 26 | + * 右键这个类 > Run As > Java Application |
| 27 | + * @author Lemon |
| 28 | + */ |
| 29 | +public class APIJSONApplication { |
| 30 | + public static final String TAG = "APIJSONApplication"; |
| 31 | + |
| 32 | + @NotNull |
| 33 | + public static APIJSONCreator<? extends Object> DEFAULT_APIJSON_CREATOR; |
| 34 | + static { |
| 35 | + DEFAULT_APIJSON_CREATOR = new APIJSONCreator<>(); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /**初始化,加载所有配置并校验 |
| 40 | + * @return |
| 41 | + * @throws Exception |
| 42 | + */ |
| 43 | + public static void init() throws Exception { |
| 44 | + init(true, DEFAULT_APIJSON_CREATOR); |
| 45 | + } |
| 46 | + /**初始化,加载所有配置并校验 |
| 47 | + * @param shutdownWhenServerError |
| 48 | + * @return |
| 49 | + * @throws Exception |
| 50 | + */ |
| 51 | + public static void init(boolean shutdownWhenServerError) throws Exception { |
| 52 | + init(shutdownWhenServerError, DEFAULT_APIJSON_CREATOR); |
| 53 | + } |
| 54 | + /**初始化,加载所有配置并校验 |
| 55 | + * @param creator |
| 56 | + * @return |
| 57 | + * @throws Exception |
| 58 | + */ |
| 59 | + public static <T extends Object> void init(@NotNull APIJSONCreator<T> creator) throws Exception { |
| 60 | + init(true, creator); |
| 61 | + } |
| 62 | + /**初始化,加载所有配置并校验 |
| 63 | + * @param shutdownWhenServerError |
| 64 | + * @param creator |
| 65 | + * @return |
| 66 | + * @throws Exception |
| 67 | + */ |
| 68 | + public static <T extends Object> void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator<T> creator) throws Exception { |
| 69 | + System.out.println("\n\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 开始启动 >>>>>>>>>>>>>>>>>>>>>>>>\n"); |
| 70 | + DEFAULT_APIJSON_CREATOR = creator; |
| 71 | + |
| 72 | + // 统一用同一个 creator |
| 73 | + APIJSONSQLConfig.APIJSON_CREATOR = creator; |
| 74 | + APIJSONParser.APIJSON_CREATOR = creator; |
| 75 | + APIJSONController.APIJSON_CREATOR = creator; |
| 76 | + |
| 77 | + |
| 78 | + if (APIJSONVerifier.ENABLE_VERIFY_ROLE) { |
| 79 | + System.out.println("\n\n\n开始初始化: Access 权限校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
| 80 | + try { |
| 81 | + APIJSONVerifier.initAccess(shutdownWhenServerError, creator); |
| 82 | + } catch (Throwable e) { |
| 83 | + e.printStackTrace(); |
| 84 | + if (shutdownWhenServerError) { |
| 85 | + onServerError("Access 权限校验配置 初始化失败!", shutdownWhenServerError); |
| 86 | + } |
| 87 | + } |
| 88 | + System.out.println("\n完成初始化: Access 权限校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + if (APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION) { |
| 93 | + System.out.println("\n\n\n开始初始化: Function 远程函数配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
| 94 | + try { |
| 95 | + APIJSONFunctionParser.init(shutdownWhenServerError, creator); |
| 96 | + } catch (Throwable e) { |
| 97 | + e.printStackTrace(); |
| 98 | + if (shutdownWhenServerError) { |
| 99 | + onServerError("Function 远程函数配置 初始化失败!", shutdownWhenServerError); |
| 100 | + } |
| 101 | + } |
| 102 | + System.out.println("\n完成初始化: Function 远程函数配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); |
| 103 | + |
| 104 | + System.out.println("开始测试: Function 远程函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
| 105 | + try { |
| 106 | + APIJSONFunctionParser.test(); |
| 107 | + } catch (Throwable e) { |
| 108 | + e.printStackTrace(); |
| 109 | + if (shutdownWhenServerError) { |
| 110 | + onServerError("Function 远程函数配置 测试失败!", shutdownWhenServerError); |
| 111 | + } |
| 112 | + } |
| 113 | + System.out.println("\n完成测试: Function 远程函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + if (APIJSONVerifier.ENABLE_VERIFY_CONTENT) { |
| 118 | + System.out.println("\n\n\n开始初始化: Request 请求参数校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
| 119 | + try { |
| 120 | + APIJSONVerifier.initRequest(shutdownWhenServerError, creator); |
| 121 | + } catch (Throwable e) { |
| 122 | + e.printStackTrace(); |
| 123 | + if (shutdownWhenServerError) { |
| 124 | + onServerError("Request 请求参数校验配置 初始化失败!", shutdownWhenServerError); |
| 125 | + } |
| 126 | + } |
| 127 | + System.out.println("\n完成初始化: Request 请求参数校验校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); |
| 128 | + |
| 129 | + System.out.println("\n\n\n开始测试: Request 请求参数校验 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
| 130 | + try { |
| 131 | + APIJSONVerifier.testStructure(); |
| 132 | + } catch (Throwable e) { |
| 133 | + e.printStackTrace(); |
| 134 | + if (shutdownWhenServerError) { |
| 135 | + onServerError("Request 请求参数校验 测试失败!", shutdownWhenServerError); |
| 136 | + } |
| 137 | + } |
| 138 | + System.out.println("\n完成测试: Request 请求参数校验 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + System.out.println("官方网站: http://apijson.cn"); |
| 144 | + System.out.println("设计规范: https://github.com/Tencent/APIJSON/blob/master/Document.md#3"); |
| 145 | + System.out.println("测试链接: http://apijson.cn/api?type=JSON&url=http://localhost:8080/get"); |
| 146 | + System.out.println("\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 启动完成,试试调用零代码万能通用 API 吧 ^_^ >>>>>>>>>>>>>>>>>>>>>>>>\n"); |
| 147 | + } |
| 148 | + |
| 149 | + protected static void onServerError(String msg, boolean shutdown) throws ServerException { |
| 150 | + Log.e(TAG, "\n启动时自检测试未通过!原因:\n" + msg); |
| 151 | + |
| 152 | + if (shutdown) { |
| 153 | + System.exit(1); |
| 154 | + } else { |
| 155 | + throw new ServerException(msg); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public static void addScriptExecutor(String language, ScriptExecutor scriptExecutor) { |
| 160 | + scriptExecutor.init(); |
| 161 | + AbstractFunctionParser.SCRIPT_EXECUTOR_MAP.put(language, scriptExecutor); |
| 162 | + } |
| 163 | +} |
0 commit comments