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.app; 017 018import io.jboot.app.config.JbootConfigManager; 019 020import java.io.File; 021import java.io.UnsupportedEncodingException; 022import java.net.URL; 023import java.net.URLDecoder; 024 025public class ApplicationUtil { 026 027 static JbootApplicationConfig getAppConfig(String[] args) { 028 JbootConfigManager.parseArgs(args); 029 return getConfig(JbootApplicationConfig.class); 030 } 031 032 033 static void printBannerInfo(JbootApplicationConfig appConfig) { 034 if (appConfig.isBannerEnable()) { 035 System.out.println(); 036 System.out.println(Banner.getText(appConfig.getBannerFile())); 037 System.out.println(); 038 } 039 } 040 041 static void printApplicationInfo(JbootApplicationConfig appConfig) { 042 System.out.println(appConfig.toString()); 043 } 044 045 private static Boolean runInFatjar; 046 047 public static boolean runInFatjar() { 048 if (runInFatjar == null) { 049 runInFatjar = buildRunInFatjar(); 050 } 051 return runInFatjar; 052 } 053 054 055 private static boolean buildRunInFatjar() { 056 URL url = Thread.currentThread().getContextClassLoader().getResource(""); 057 if (url == null) { 058 return true; 059 } 060 061 if ("jar".equalsIgnoreCase(url.getProtocol())) { 062 return true; 063 } 064 065 String urlStr = url.toString().toLowerCase(); 066 if (urlStr.endsWith(".jar!/")) { 067 return true; 068 } 069 070 // 在某些情况下 通过 java -jar 运行时,会以 /config/ 结束 071 if (urlStr.endsWith("/config/")) { 072 File urlPath; 073 try { 074 //中文目录乱码的问题 075 urlPath = new File(URLDecoder.decode(url.getFile(), "UTF-8")); 076 } catch (UnsupportedEncodingException e) { 077 urlPath = new File(url.getPath()); 078 } 079 return !urlPath.exists() || !urlPath.isDirectory(); 080 } 081 082 return false; 083 } 084 085 086 static void printClassPath() { 087 try { 088 if (runInFatjar()) { 089 System.out.println("JbootApplication is running in fatjar."); 090 } else { 091 String path = ApplicationUtil.class.getResource("/").getPath(); 092 // 例如: /D:/JAVA/workSpace_idea/... 093 if (path.indexOf(":") == 2) { 094 path = path.substring(1); 095 } 096 System.out.println("JbootApplication classpath: " + path); 097 } 098 } catch (Exception e) { 099 e.printStackTrace(); 100 } 101 } 102 103 104 static <T> T getConfig(Class<T> clazz) { 105 return JbootConfigManager.me().get(clazz); 106 } 107 108 static String getConfigValue(String key) { 109 return JbootConfigManager.me().getConfigValue(key); 110 } 111 112 113 static boolean isDevMode() { 114 return JbootConfigManager.me().isDevMode(); 115 } 116 117 118}