通过class定位打包后的resour路径
时间:2021-08-29
本文章向大家介绍通过class定位打包后的resour路径,主要包括通过class定位打包后的resour路径使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
SnifferConfigInitializer
/** * Load the specified config file or default config file * * @return the config file {@link InputStream}, or null if not needEnhance. */ private static InputStreamReader loadConfig() throws AgentPackageNotFoundException, ConfigNotFoundException { String specifiedConfigPath = System.getProperty(SPECIFIED_CONFIG_PATH); File configFile = StringUtil.isEmpty(specifiedConfigPath) ? new File( AgentPackagePath.getPath(), DEFAULT_CONFIG_FILE_NAME) : new File(specifiedConfigPath); if (configFile.exists() && configFile.isFile()) { try { LOGGER.info("Config file found in {}.", configFile); return new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8); } catch (FileNotFoundException e) { throw new ConfigNotFoundException("Failed to load agent.config", e); } } throw new ConfigNotFoundException("Failed to load agent.config."); }
AgentPackagePath
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.apache.skywalking.apm.agent.core.boot; import java.io.File; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; /** * AgentPackagePath is a flag and finder to locate the SkyWalking agent.jar. It gets the absolute path of the agent jar. * The path is the required metadata for agent core looking up the plugins and toolkit activations. If the lookup * mechanism fails, the agent will exit directly. */ public class AgentPackagePath { private static final ILog LOGGER = LogManager.getLogger(AgentPackagePath.class); private static File AGENT_PACKAGE_PATH; public static File getPath() throws AgentPackageNotFoundException { if (AGENT_PACKAGE_PATH == null) { AGENT_PACKAGE_PATH = findPath(); } return AGENT_PACKAGE_PATH; } public static boolean isPathFound() { return AGENT_PACKAGE_PATH != null; } private static File findPath() throws AgentPackageNotFoundException { String classResourcePath = AgentPackagePath.class.getName().replaceAll("\\.", "/") + ".class"; URL resource = ClassLoader.getSystemClassLoader().getResource(classResourcePath); if (resource != null) { String urlString = resource.toString(); LOGGER.debug("The beacon class location is {}.", urlString); int insidePathIndex = urlString.indexOf('!'); boolean isInJar = insidePathIndex > -1; if (isInJar) { urlString = urlString.substring(urlString.indexOf("file:"), insidePathIndex); File agentJarFile = null; try { agentJarFile = new File(new URL(urlString).toURI()); } catch (MalformedURLException | URISyntaxException e) { LOGGER.error(e, "Can not locate agent jar file by url:" + urlString); } if (agentJarFile.exists()) { return agentJarFile.getParentFile(); } } else { int prefixLength = "file:".length(); String classLocation = urlString.substring( prefixLength, urlString.length() - classResourcePath.length()); return new File(classLocation); } } LOGGER.error("Can not locate agent jar file."); throw new AgentPackageNotFoundException("Can not locate agent jar file."); } }
原文地址:https://www.cnblogs.com/tonggc1668/p/15203366.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- DeepWalk:图网络与NLP的巧妙融合
- 基于kubeadm搭建k8s高可用集群
- 扑克牌例题与Collections工具类
- 快速了解Kubernetes
- PyQt5 技巧篇-按钮竖排显示方法,Qt Designer设置按钮竖排显示。
- Kafka集群监控、安全机制与最佳实践
- PyQt5 技术篇-设置输入框的placeholder方法,Qt Designer设置Line Edit、Text Edit编辑框的placeholder
- Kafka集群搭建及必知必会
- Python 技术篇-打开指定文件夹、目录、路径方法,运行指定文件演示
- Python 技巧篇-同一个方法多次引用不同效果功能实现,可选参数设置方法
- Kafka核心API——Connect API
- PyQt5 技术篇-设置alignment对齐方式。Qt Designer设置文本对齐方式。居中、左对齐、右对齐、上对齐、下对齐。
- Nginx部署Vue项目以及解决刷新页面404
- Kafka核心API——Stream API
- Kafka核心API——Consumer消费者