Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

211-fixed-the-issue-that-the-war-application-name-cannot-be-automatically-recognized #212

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
import com.jd.live.agent.bootstrap.logger.LoggerFactory;
import com.jd.live.agent.core.bootstrap.EnvSupplier;
import com.jd.live.agent.core.bootstrap.env.config.ConfigEnvSupplier;
import com.jd.live.agent.core.bootstrap.resource.BootResourcer;
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.parser.ConfigParser;
import com.jd.live.agent.core.util.Close;
import com.jd.live.agent.core.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -46,6 +45,9 @@ public abstract class AbstractEnvSupplier implements EnvSupplier {
@Inject
private Map<String, ConfigParser> parsers;

@Inject
private List<BootResourcer> resourcers;

/**
* Constructs an AbstractEnvSupplier with the specified resources.
*
Expand Down Expand Up @@ -89,31 +91,35 @@ protected Map<String, Object> loadConfigs(String resource, String prefix) {
String ext = pos > 0 ? resource.substring(pos + 1) : "";
ConfigParser parser = parsers.get(ext);
if (parser != null) {
URL url = getResource(resource);
if (url != null) {
InputStream stream = getResource(resource);
if (stream != null) {
try {
Map<String, Object> result = parse(url.openStream(), parser);
logger.info("[LiveAgent] Successfully load config from " + url);
Map<String, Object> result = parse(stream, parser);
logger.info("[LiveAgent] Successfully load config from " + resource);
return result;
} catch (Throwable e) {
logger.warn("[LiveAgent] Failed to load config from " + url, e);
logger.warn("[LiveAgent] Failed to load config from " + resource, e);
return new HashMap<>();
} finally {
Close.instance().close(stream);
}
}
}
return null;
}

protected URL getResource(String resource) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
URL url = contextClassLoader.getResource(resource);
if (url == null) {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
if (systemClassLoader != contextClassLoader) {
url = systemClassLoader.getResource(resource);
protected InputStream getResource(String resource) {
for (BootResourcer resourcer : resourcers) {
try {
InputStream stream = resourcer.getResource(resource);
if (stream != null) {
return stream;
}
} catch (IOException e) {
return null;
}
}
return url;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed 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 com.jd.live.agent.core.bootstrap.resource;

import com.jd.live.agent.core.extension.annotation.Extensible;

import java.io.IOException;
import java.io.InputStream;

/**
* An interface for finding resources.
*/
@Extensible("BootResourcer")
public interface BootResourcer {

int ORDER_CLASSLOADER = 0;

int ORDER_TOMCAT = 10;

/**
* Finds the resource with the given name and returns its input stream.
*
* @param resource The name of the resource to find.
* @return The input stream of the resource, or null if the resource could not be found.
*/
InputStream getResource(String resource) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed 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 com.jd.live.agent.core.bootstrap.resource.loader;

import com.jd.live.agent.core.bootstrap.resource.BootResourcer;
import com.jd.live.agent.core.extension.annotation.Extension;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
* A class that implements the ResourceFinder interface by searching for resources on the classpath.
*/
@Extension(value = "ClassloaderBootResourcer", order = BootResourcer.ORDER_CLASSLOADER)
public class ClassloaderBootResourcer implements BootResourcer {

@Override
public InputStream getResource(String resource) throws IOException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
URL url = contextClassLoader.getResource(resource);
if (url == null) {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
if (systemClassLoader != contextClassLoader) {
url = systemClassLoader.getResource(resource);
}
}
return url == null ? null : url.openStream();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed 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 com.jd.live.agent.core.bootstrap.resource.tomcat;

import com.jd.live.agent.core.bootstrap.resource.BootResourcer;
import com.jd.live.agent.core.extension.annotation.Extension;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
* A class that implements the ResourceFinder interface by searching for resources in a Tomcat web application.
*/
@Extension(value = "TomcatBootResourcer", order = BootResourcer.ORDER_TOMCAT)
public class TomcatBootResourcer implements BootResourcer {

@Override
public InputStream getResource(String resource) throws IOException {
File workingDirectory = new File(System.getProperty("user.dir"));
if (workingDirectory.getName().equals("bin")) {
workingDirectory = workingDirectory.getParentFile();
}
File webappDirectory = new File(workingDirectory, "webapps/");
if (!webappDirectory.exists()) {
String home = System.getProperty("catalina.home");
if (home != null) {
webappDirectory = new File(home, "webapps/");
}
}
if (webappDirectory.exists()) {
String name = "WEB-INF/classes/" + resource;
File[] files = webappDirectory.listFiles();
if (files != null) {
for (File file : files) {
InputStream inputStream = getInputStream(file, name, webappDirectory);
if (inputStream != null) {
return inputStream;
}
}
}
}
return null;
}

/**
* A private helper method that tries to find the resource in a given file.
*
* @param file The file to search in.
* @param name The name of the resource to find.
* @param webappDirectory The web application directory.
* @return The input stream of the resource, or null if the resource could not be found.
* @throws IOException If an I/O error occurs while trying to read the resource.
*/
private InputStream getInputStream(File file, String name, File webappDirectory) throws IOException {
if (file.isFile() && file.getName().endsWith(".war")) {
return getInputStream(file, name);
} else if (file.isDirectory()) {
File resourceFile = new File(file, name);
return !resourceFile.exists() ? null : Files.newInputStream(resourceFile.toPath());
}
return null;
}

/**
* A private helper method that tries to find the resource in a given WAR file.
*
* @param file The WAR file to search in.
* @param name The name of the resource to find.
* @return The input stream of the resource, or null if the resource could not be found.
* @throws IOException If an I/O error occurs while trying to read the resource.
*/
private InputStream getInputStream(File file, String name) throws IOException {
try (JarFile jarFile = new JarFile(file)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().equals(name)) {
return jarFile.getInputStream(entry);
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.jd.live.agent.core.bootstrap.resource.loader.ClassloaderBootResourcer
com.jd.live.agent.core.bootstrap.resource.tomcat.TomcatBootResourcer
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

<build>
<directory>${basedir}/target</directory>
<finalName>joylive-demo-springcloud2021-provider</finalName>
<finalName>joylive-demo-springcloud2021-provider-war</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
server.port=${SERVER_PORT:${random.int[11000,11999]}}
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=${NACOS_ADDR:${AP_SD_INTERNAL_ADDRESS}:${AP_SD_INTERNAL_HTTP_PORT}}
spring.cloud.nacos.discovery.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public void onDiscovery(@NeverNull String typeName,
@MaybeNull JavaModule module,
boolean loaded) {
if (logger.isDebugEnabled()) {
String message = String.format("ByteKit discovery %s [%s, %s, %s, loaded=%b]", typeName, classLoader, module, Thread.currentThread(), loaded);
String message = String.format("ByteKit discovery %s [%s, %s, %s, loaded=%b]",
typeName, getName(classLoader), module, Thread.currentThread(), loaded);
logger.debug(message);
}
}
Expand All @@ -73,7 +74,8 @@ public void onTransformation(@NeverNull TypeDescription typeDescription,
boolean loaded,
@NeverNull DynamicType dynamicType) {
if (logger.isInfoEnabled()) {
String message = String.format("ByteKit transform %s [%s, %s, %s, loaded=%b]", typeDescription.getName(), classLoader, module, Thread.currentThread(), loaded);
String message = String.format("ByteKit transform %s [%s, %s, %s, loaded=%b]",
typeDescription.getName(), getName(classLoader), module, Thread.currentThread(), loaded);
logger.info(message);
}
}
Expand All @@ -84,7 +86,8 @@ public void onIgnored(@NeverNull TypeDescription typeDescription,
@MaybeNull JavaModule module,
boolean loaded) {
if (logger.isDebugEnabled()) {
String message = String.format("ByteKit ignore %s [%s, %s, %s, loaded=%b]", typeDescription.getName(), classLoader, module, Thread.currentThread(), loaded);
String message = String.format("ByteKit ignore %s [%s, %s, %s, loaded=%b]",
typeDescription.getName(), getName(classLoader), module, Thread.currentThread(), loaded);
logger.debug(message);
}
}
Expand All @@ -97,7 +100,8 @@ public void onError(@NeverNull String typeName,
@NeverNull Throwable throwable) {
OutputStream bos = new ByteArrayOutputStream(1024);
PrintStream printStream = new PrintStream(bos);
printStream.printf("ByteKit error %s [%s, %s, %s, loaded=%b]", typeName, classLoader, module, Thread.currentThread(), loaded);
printStream.printf("ByteKit error %s [%s, %s, %s, loaded=%b]",
typeName, getName(classLoader), module, Thread.currentThread(), loaded);
throwable.printStackTrace(printStream);
logger.error(bos.toString());
}
Expand All @@ -108,9 +112,17 @@ public void onComplete(@NeverNull String typeName,
@MaybeNull JavaModule module,
boolean loaded) {
if (logger.isDebugEnabled()) {
String message = String.format("ByteKit complete %s [%s, %s, %s, loaded=%b]", typeName, classLoader, module, Thread.currentThread(), loaded);
String message = String.format("ByteKit complete %s [%s, %s, %s, loaded=%b]",
typeName, getName(classLoader), module, Thread.currentThread(), loaded);
logger.debug(message);
}
}

protected String getName(ClassLoader classLoader) {
if (classLoader == null) {
return "";
}
return classLoader.getClass().getSimpleName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<spring-core.version>5.3.39</spring-core.version>
<spring-boot.version>3.4.0</spring-boot.version>
<spring-cloud-context.version>3.1.9</spring-cloud-context.version>
<nacos-discovery.version>2023.0.3.2</nacos-discovery.version>
</properties>

<dependencies>
Expand All @@ -37,6 +38,12 @@
<version>${spring-cloud-context.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos-discovery.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void subscribe(ConfigCenter configCenter) {
}
}

public ConfigurableApplicationContext getContext() {
return context;
}

/**
* Adds an environment listener to the given Configurator instance.
*
Expand Down
Loading
Loading