001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot; 017 018import com.codahale.metrics.MetricRegistry; 019import com.jfinal.aop.Aop; 020import io.jboot.aop.JbootAopFactory; 021import io.jboot.app.config.JbootConfigManager; 022import io.jboot.components.cache.JbootCache; 023import io.jboot.components.cache.JbootCacheManager; 024import io.jboot.components.event.JbootEvent; 025import io.jboot.components.event.JbootEventManager; 026import io.jboot.components.mq.Jbootmq; 027import io.jboot.components.mq.JbootmqManager; 028import io.jboot.components.rpc.JbootrpcManager; 029import io.jboot.components.rpc.JbootrpcReferenceConfig; 030import io.jboot.components.serializer.JbootSerializer; 031import io.jboot.components.serializer.JbootSerializerManager; 032import io.jboot.support.metric.JbootMetricManager; 033import io.jboot.support.redis.JbootRedis; 034import io.jboot.support.redis.JbootRedisManager; 035import io.jboot.utils.StrUtil; 036 037 038public class Jboot { 039 040 /** 041 * 是否是开发模式 042 * 043 * @return 044 */ 045 public static boolean isDevMode() { 046 return JbootConfigManager.me().isDevMode(); 047 } 048 049 050 /** 051 * 获取 缓存 052 * 053 * @return 054 */ 055 public static JbootCache getCache() { 056 return JbootCacheManager.me().getCache(); 057 } 058 059 060 /** 061 * 获取指定的缓存 062 * 063 * @param name 064 * @return 065 */ 066 public static JbootCache getCache(String name) { 067 return JbootCacheManager.me().getCache(name); 068 } 069 070 071 /** 072 * 获取 JbootRedis 工具类,方便操作Redis请求 073 * 074 * @return 075 */ 076 public static JbootRedis getRedis() { 077 return JbootRedisManager.me().getRedis(); 078 } 079 080 /** 081 * 获取指定的 Redis 082 * 083 * @param name 084 * @return 085 */ 086 public static JbootRedis getRedis(String name) { 087 return JbootRedisManager.me().getRedis(name); 088 } 089 090 091 /** 092 * 获取 MetricRegistry 093 * 094 * @return 095 */ 096 public static MetricRegistry getMetric() { 097 return JbootMetricManager.me().metric(); 098 } 099 100 101 /** 102 * 获取 Mq 103 * 104 * @return 105 */ 106 public static Jbootmq getMq() { 107 return JbootmqManager.me().getJbootmq(); 108 } 109 110 /** 111 * 获取指定的 MQ 112 * 113 * @param name 114 * @return 115 */ 116 public static Jbootmq getMq(String name) { 117 return JbootmqManager.me().getJbootmq(name); 118 } 119 120 121 /** 122 * 获取序列化对象 123 * 124 * @return 125 */ 126 public static JbootSerializer getSerializer() { 127 return JbootSerializerManager.me().getSerializer(); 128 } 129 130 131 /** 132 * 获取序列化对象 133 * @param name 134 * @return 135 */ 136 public static JbootSerializer getSerializer(String name) { 137 return JbootSerializerManager.me().getSerializer(name); 138 } 139 140 141 142 /** 143 * 获取配置信息 144 * 145 * @param clazz 146 * @param <T> 147 * @return 148 */ 149 public static <T> T config(Class<T> clazz) { 150 return JbootConfigManager.me().get(clazz); 151 } 152 153 154 /** 155 * 读取配置文件信息 156 * 157 * @param clazz 158 * @param prefix 159 * @param <T> 160 * @return 161 */ 162 public static <T> T config(Class<T> clazz, String prefix) { 163 return JbootConfigManager.me().get(clazz, prefix, null); 164 } 165 166 167 /** 168 * 读取配置文件信息 169 * 170 * @param clazz 171 * @param prefix 172 * @param file 173 * @param <T> 174 * @return 175 */ 176 public static <T> T config(Class<T> clazz, String prefix, String file) { 177 return JbootConfigManager.me().get(clazz, prefix, file); 178 } 179 180 /** 181 * 读取某个配置信息 182 * 183 * @param key 184 * @return 185 */ 186 public static String configValue(String key) { 187 return JbootConfigManager.me().getConfigValue(key); 188 } 189 190 191 /** 192 * 读取某个配置信息 193 * 194 * @param key 195 * @param defaultValue 当获取不到的时候发挥此默认值 196 * @return 197 */ 198 public static String configValue(String key, String defaultValue) { 199 String value = configValue(key); 200 return StrUtil.isNotBlank(value) ? value : defaultValue; 201 } 202 203 204 /** 205 * 获取 RPC 服务 206 * 207 * @param clazz 208 * @param <T> 209 * @return 210 */ 211 public static <T> T service(Class<T> clazz) { 212 return service(clazz, new JbootrpcReferenceConfig()); 213 } 214 215 /** 216 * 获取 RPC 服务 217 * 218 * @param clazz 219 * @param config rpc 配置 220 * @param <T> 221 * @return 222 */ 223 public static <T> T service(Class<T> clazz, JbootrpcReferenceConfig config) { 224 return JbootrpcManager.me().getJbootrpc().serviceObtain(clazz, config); 225 } 226 227 /** 228 * 想本地系统发送一个事件 229 * 230 * @param event 231 */ 232 public static void sendEvent(JbootEvent event) { 233 JbootEventManager.me().publish(event); 234 } 235 236 /** 237 * 向本地系统发送一个事件 238 * 239 * @param action 240 * @param data 241 */ 242 public static void sendEvent(String action, Object data) { 243 sendEvent(new JbootEvent(action, data)); 244 } 245 246 247 /** 248 * 根据类名获取 Aop 下的 Bean 249 * 250 * @param clazz 251 * @param <T> 252 * @return 253 */ 254 public static <T> T getBean(Class<T> clazz) { 255 return Aop.get(clazz); 256 } 257 258 /** 259 * 根据名称获取 Aop 下的 Bean 260 * 261 * @param name 262 * @param <T> 263 * @return 264 */ 265 public static <T> T getBean(String name) { 266 return JbootAopFactory.me().getBean(name); 267 } 268 269}