Skip to content

Commit b122de1

Browse files
committed
Copy FunctionClassUtils to s-c-f-serverless-web
1 parent 02cb8fc commit b122de1

File tree

2 files changed

+140
-13
lines changed
  • spring-cloud-function-adapters
    • spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web
    • spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web

2 files changed

+140
-13
lines changed

spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/FunctionClassUtils.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,8 @@ private static Class<?> getStartClass(List<URL> list, ClassLoader classLoader) {
121121
Resource resource = resources[i];
122122
String className = packageName + "." + (resource.getFilename().replace("/", ".")).replace(".class", "");
123123
startClass = ClassUtils.forName(className, classLoader);
124-
// if (isSpringBootApplication(startClass)) {
125-
// logger.info("Loaded Main Kotlin Class: " + startClass);
126-
// return startClass;
127-
// }
128124
}
129125
}
130-
// else if (isSpringBootApplication(startClass)) {
131-
// logger.info("Loaded Start Class: " + startClass);
132-
// return startClass;
133-
// }
134126
}
135127
}
136128
finally {
@@ -145,9 +137,4 @@ private static Class<?> getStartClass(List<URL> list, ClassLoader classLoader) {
145137
}
146138
return null;
147139
}
148-
149-
// private static boolean isSpringBootApplication(Class<?> startClass) {
150-
// return startClass.getDeclaredAnnotation(SpringBootApplication.class) != null
151-
// || startClass.getDeclaredAnnotation(SpringBootConfiguration.class) != null;
152-
// }
153140
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright 2019-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.function.serverless.web;
18+
19+
import java.io.InputStream;
20+
import java.net.URL;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.jar.JarFile;
24+
import java.util.jar.Manifest;
25+
26+
import org.apache.commons.logging.Log;
27+
import org.apache.commons.logging.LogFactory;
28+
29+
//import org.springframework.boot.SpringBootConfiguration;
30+
//import org.springframework.boot.autoconfigure.SpringBootApplication;
31+
import org.springframework.core.KotlinDetector;
32+
import org.springframework.core.io.Resource;
33+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
34+
import org.springframework.util.Assert;
35+
import org.springframework.util.ClassUtils;
36+
import org.springframework.util.StringUtils;
37+
38+
/**
39+
* General utility class which aggregates various class-level utility functions
40+
* used by the framework.
41+
*
42+
* @author Oleg Zhurakousky
43+
* @since 3.0.1
44+
*/
45+
public final class FunctionClassUtils {
46+
47+
private static Log logger = LogFactory.getLog(FunctionClassUtils.class);
48+
49+
private FunctionClassUtils() {
50+
51+
}
52+
53+
/**
54+
* Discovers the start class in the currently running application.
55+
* The discover search order is 'MAIN_CLASS' environment property,
56+
* 'MAIN_CLASS' system property, META-INF/MANIFEST.MF:'Start-Class' attribute,
57+
* meta-inf/manifest.mf:'Start-Class' attribute.
58+
*
59+
* @return instance of Class which represent the start class of the application.
60+
*/
61+
public static Class<?> getStartClass() {
62+
ClassLoader classLoader = FunctionClassUtils.class.getClassLoader();
63+
return getStartClass(classLoader);
64+
}
65+
66+
static Class<?> getStartClass(ClassLoader classLoader) {
67+
Class<?> mainClass = null;
68+
if (System.getenv("MAIN_CLASS") != null) {
69+
mainClass = ClassUtils.resolveClassName(System.getenv("MAIN_CLASS"), classLoader);
70+
}
71+
else if (System.getProperty("MAIN_CLASS") != null) {
72+
mainClass = ClassUtils.resolveClassName(System.getProperty("MAIN_CLASS"), classLoader);
73+
}
74+
else {
75+
try {
76+
Class<?> result = getStartClass(
77+
Collections.list(classLoader.getResources(JarFile.MANIFEST_NAME)), classLoader);
78+
if (result == null) {
79+
result = getStartClass(Collections
80+
.list(classLoader.getResources("meta-inf/manifest.mf")), classLoader);
81+
}
82+
Assert.notNull(result, "Failed to locate main class");
83+
mainClass = result;
84+
}
85+
catch (Exception ex) {
86+
throw new IllegalStateException("Failed to discover main class. An attempt was made to discover "
87+
+ "main class as 'MAIN_CLASS' environment variable, system property as well as "
88+
+ "entry in META-INF/MANIFEST.MF (in that order).", ex);
89+
}
90+
}
91+
logger.info("Main class: " + mainClass);
92+
return mainClass;
93+
}
94+
95+
private static Class<?> getStartClass(List<URL> list, ClassLoader classLoader) {
96+
if (logger.isTraceEnabled()) {
97+
logger.trace("Searching manifests: " + list);
98+
}
99+
for (URL url : list) {
100+
try {
101+
InputStream inputStream = null;
102+
Manifest manifest = new Manifest(url.openStream());
103+
logger.info("Searching for start class in manifest: " + url);
104+
if (logger.isDebugEnabled()) {
105+
manifest.write(System.out);
106+
}
107+
try {
108+
String startClassName = manifest.getMainAttributes().getValue("Start-Class");
109+
if (!StringUtils.hasText(startClassName)) {
110+
startClassName = manifest.getMainAttributes().getValue("Main-Class");
111+
}
112+
113+
if (StringUtils.hasText(startClassName)) {
114+
Class<?> startClass = ClassUtils.forName(startClassName, classLoader);
115+
116+
if (KotlinDetector.isKotlinType(startClass)) {
117+
PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver(classLoader);
118+
String packageName = startClass.getPackage().getName();
119+
Resource[] resources = r.getResources("classpath:" + packageName.replace(".", "/") + "/*.class");
120+
for (int i = 0; i < resources.length; i++) {
121+
Resource resource = resources[i];
122+
String className = packageName + "." + (resource.getFilename().replace("/", ".")).replace(".class", "");
123+
startClass = ClassUtils.forName(className, classLoader);
124+
}
125+
}
126+
}
127+
}
128+
finally {
129+
if (inputStream != null) {
130+
inputStream.close();
131+
}
132+
}
133+
}
134+
catch (Exception ex) {
135+
logger.debug("Failed to determine Start-Class in manifest file of " + url, ex);
136+
}
137+
}
138+
return null;
139+
}
140+
}

0 commit comments

Comments
 (0)