Skip to content

Commit

Permalink
Add a newFile method to handle file paths with spaces. (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchangqing committed Dec 2, 2023
1 parent 584ac7c commit 9c37315
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.bootstrap;

import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.loader.ExecutableArkBizJar;
import com.alipay.sofa.ark.loader.archive.ExplodedArchive;
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
Expand Down Expand Up @@ -66,7 +67,7 @@ protected ExecutableArchive createArchive() throws Exception {
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
File root = new File(path);
File root = FileUtils.file(path);
if (!root.exists()) {
throw new IllegalStateException("Unable to determine code source archive from " + root);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
package com.alipay.sofa.ark.bootstrap;

import com.alipay.sofa.ark.common.util.AssertUtils;
import com.alipay.sofa.ark.common.util.ClassLoaderUtils;
import com.alipay.sofa.ark.common.util.ClassUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.common.util.*;
import com.alipay.sofa.ark.exception.ArkRuntimeException;
import com.alipay.sofa.ark.loader.*;
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
Expand Down Expand Up @@ -115,7 +112,8 @@ protected ContainerArchive getJarContainerArchive() throws Exception {
throw new ArkRuntimeException("Duplicate Container Jar File Found.");
}

return new JarContainerArchive(new JarFileArchive(new File(urlList.get(0).getFile())));
return new JarContainerArchive(new JarFileArchive(FileUtils.file(urlList.get(0)
.getFile())));
}

@Override
Expand All @@ -128,7 +126,8 @@ public List<BizArchive> getBizArchives() throws Exception {
}

for (URL url : urlList) {
bizArchives.add(new JarBizArchive(new JarFileArchive(new File(url.getFile()))));
bizArchives
.add(new JarBizArchive(new JarFileArchive(FileUtils.file(url.getFile()))));
}

return bizArchives;
Expand All @@ -140,8 +139,8 @@ public List<PluginArchive> getPluginArchives() throws Exception {

List<PluginArchive> pluginArchives = new ArrayList<>();
for (URL url : urlList) {
pluginArchives
.add(new JarPluginArchive(new JarFileArchive(new File(url.getFile()))));
pluginArchives.add(new JarPluginArchive(new JarFileArchive(FileUtils.file(url
.getFile()))));
}

return pluginArchives;
Expand Down Expand Up @@ -177,9 +176,10 @@ private File deduceArkConfBaseDir() {
if (classLocation.startsWith("file:")) {
classLocation = classLocation.substring("file:".length());
}
File file = classLocation == null ? null : new File(classLocation);
File file = classLocation == null ? null : FileUtils.file(classLocation);
while (file != null) {
arkConfDir = new File(file.getPath() + File.separator + ARK_CONF_BASE_DIR);
arkConfDir = FileUtils
.file(file.getPath() + File.separator + ARK_CONF_BASE_DIR);
if (arkConfDir.exists() && arkConfDir.isDirectory()) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.ark.loader;

import com.alipay.sofa.ark.bootstrap.ClasspathLauncher;
import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.exception.ArkRuntimeException;
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
import com.alipay.sofa.ark.spi.archive.Archive;
Expand Down Expand Up @@ -73,7 +74,8 @@ public List<BizArchive> getBizArchives() throws Exception {
}

} else {
bizArchives.add(new JarBizArchive(new JarFileArchive(new File(url.getFile()))));
bizArchives
.add(new JarBizArchive(new JarFileArchive(FileUtils.file(url.getFile()))));
}
}
return bizArchives;
Expand All @@ -88,7 +90,7 @@ public List<BizArchive> getBizArchives() throws Exception {
private Archive getArchiveFromJarEntry(URL jarUrl) throws IOException {
String jarPath = jarUrl.getPath().substring(0, jarUrl.getPath().indexOf("!"));
String bizPath = jarUrl.getPath().substring(jarUrl.getPath().indexOf("!") + 2);
List<Archive> nestedArchives = new JarFileArchive(new File(jarPath))
List<Archive> nestedArchives = new JarFileArchive(FileUtils.file(jarPath))
.getNestedArchives(entry -> entry.getName().equals(bizPath));
if (nestedArchives.isEmpty()) {
return null;
Expand Down Expand Up @@ -127,13 +129,13 @@ protected JarFileArchive getUrlJarFileArchive(URL url) throws IOException {
String file = url.getFile();
if (file.contains(FILE_IN_JAR)) {
int pos = file.indexOf(FILE_IN_JAR);
File fatJarFile = new File(file.substring(0, pos));
File fatJarFile = FileUtils.file(file.substring(0, pos));
String nestedJar = file.substring(file.lastIndexOf("/") + 1);
JarFileArchive fatJarFileArchive = new JarFileArchive(fatJarFile);
List<Archive> matched = fatJarFileArchive.getNestedArchives(entry -> entry.getName().contains(nestedJar));
return (JarFileArchive) matched.get(0);
} else {
return new JarFileArchive(new File(file));
return new JarFileArchive(FileUtils.file(file));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.loader.jar.JarFile;
import com.alipay.sofa.ark.loader.data.RandomAccessData.ResourceAccess;
import com.alipay.sofa.ark.spi.archive.Archive;
Expand Down Expand Up @@ -125,7 +126,7 @@ private Archive getUnpackedNestedArchive(JarEntry jarEntry) throws IOException {

private File getTempUnpackFolder() {
if (this.tempUnpackFolder == null) {
File tempFolder = new File(System.getProperty("java.io.tmpdir"));
File tempFolder = FileUtils.file(System.getProperty("java.io.tmpdir"));
this.tempUnpackFolder = createUnpackFolder(tempFolder);
}
return this.tempUnpackFolder;
Expand All @@ -134,7 +135,7 @@ private File getTempUnpackFolder() {
private File createUnpackFolder(File parent) {
int attempts = 0;
while (attempts++ < 1000) {
String fileName = new File(this.jarFile.getName()).getName();
String fileName = FileUtils.file(this.jarFile.getName()).getName();
File unpackFolder = new File(parent, fileName + "-spring-boot-libs-"
+ UUID.randomUUID());
if (unpackFolder.mkdirs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.loader.jar;

import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.common.util.StringUtils;

import java.io.File;
Expand Down Expand Up @@ -289,7 +290,7 @@ private JarFile getRootJarFile(String name) throws IOException {
throw new IllegalStateException("Not a file URL");
}
String path = name.substring(FILE_PROTOCOL.length());
File file = new File(URLDecoder.decode(path, "UTF-8"));
File file = FileUtils.file(path);
Map<File, JarFile> cache = rootFileCache.get();
JarFile result = (cache == null ? null : cache.get(file));
if (result == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.loader.jar;

import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
import com.alipay.sofa.ark.spi.archive.Archive;
Expand Down Expand Up @@ -107,7 +108,7 @@ private static String getArtifactIdFromLocalClassPath(String fileClassPath) {
} else {
// is not from test classpath, for example install uncompressed modules, just return null
// search for pom.properties
File pomPropertiesFile = searchPomProperties(new File(libraryFile));
File pomPropertiesFile = searchPomProperties(FileUtils.file(libraryFile));
if (pomPropertiesFile != null && pomPropertiesFile.exists()) {
pomPropertiesPath = pomPropertiesFile.getAbsolutePath();
} else {
Expand All @@ -118,7 +119,7 @@ private static String getArtifactIdFromLocalClassPath(String fileClassPath) {

String artifactId = null;
if (!StringUtils.isEmpty(pomPropertiesPath)) {
try (InputStream inputStream = Files.newInputStream(new File(pomPropertiesPath)
try (InputStream inputStream = Files.newInputStream(FileUtils.file(pomPropertiesPath)
.toPath())) {
Properties properties = new Properties();
properties.load(inputStream);
Expand Down Expand Up @@ -227,7 +228,7 @@ private static String doGetArtifactIdFromFileName(String jarLocation) {
private static String parseArtifactIdFromJarInJar(String jarLocation) throws IOException {
String rootPath = jarLocation.substring(0, jarLocation.lastIndexOf(JAR_SEPARATOR));
String subNestedPath = jarLocation.substring(jarLocation.lastIndexOf(JAR_SEPARATOR) + 2);
com.alipay.sofa.ark.loader.jar.JarFile jarFile = new com.alipay.sofa.ark.loader.jar.JarFile(new File(rootPath));
com.alipay.sofa.ark.loader.jar.JarFile jarFile = new com.alipay.sofa.ark.loader.jar.JarFile(FileUtils.file(rootPath));
JarFileArchive jarFileArchive = new JarFileArchive(jarFile);
List<Archive> archives = jarFileArchive.getNestedArchives(entry -> !StringUtils.isEmpty(entry.getName()) && entry.getName().equals(subNestedPath));

Expand All @@ -251,7 +252,7 @@ public static com.alipay.sofa.ark.loader.jar.JarFile getTemporaryRootJarFromJarL
// /xxx/xxx/xxx-starter-1.0.0-SNAPSHOT.jar!/BOOT-INF/lib/xxx2-starter-1.1.4-SNAPSHOT-ark-biz.jar!/lib/xxx3-230605-sofa.jar
String[] js = jarLocation.split(JAR_SEPARATOR, -1);
com.alipay.sofa.ark.loader.jar.JarFile rJarFile = new com.alipay.sofa.ark.loader.jar.JarFile(
new File(js[0]));
FileUtils.file(js[0]));
for (int i = 1; i < js.length; i++) {
String jPath = js[i];
if (jPath == null || jPath.isEmpty()) {
Expand All @@ -269,7 +270,7 @@ public static com.alipay.sofa.ark.loader.jar.JarFile getTemporaryRootJarFromJarL
}

private static String parseArtifactIdFromJar(String jarLocation) throws IOException {
jarLocation = URLDecoder.decode(jarLocation, "UTF-8");
jarLocation = FileUtils.decodePath(jarLocation);
try (JarFile jarFile = new JarFile(jarLocation)) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.ark.bootstrap;

import com.alipay.sofa.ark.common.util.ClassLoaderUtils;
import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.loader.EmbedClassPathArchive;
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
import com.alipay.sofa.ark.spi.archive.Archive;
Expand Down Expand Up @@ -52,7 +53,7 @@ public static void setup(){
List<String> mockArguments = new ArrayList<>();
String filePath = ClasspathLauncherTest.class.getClassLoader()
.getResource("SampleClass.class").getPath();
String workingPath = new File(filePath).getParent();
String workingPath = FileUtils.file(filePath).getParent();
mockArguments.add(String.format("-javaagent:%s", workingPath));

RuntimeMXBean runtimeMXBean = Mockito.mock(RuntimeMXBean.class);
Expand Down Expand Up @@ -92,7 +93,7 @@ public void testSpringBootFatJar() throws Exception {
Assert.assertEquals(1, agentUrl.length);

List<URL> urls = new ArrayList<>();
JarFileArchive jarFileArchive = new JarFileArchive(new File(url.getFile()));
JarFileArchive jarFileArchive = new JarFileArchive(FileUtils.file(url.getFile()));
List<Archive> archives = jarFileArchive.getNestedArchives(this::isNestedArchive);
for (Archive archive : archives) {
urls.add(archive.getUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.loader;

import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.spi.archive.BizArchive;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -42,7 +43,7 @@ public class EmbedClassPathArchiveTest {
public void testGetContainerArchive() throws Exception {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL springbootFatJar = cl.getResource("sample-springboot-fat-biz.jar");
JarFileArchive jarFileArchive = new JarFileArchive(new File(springbootFatJar.getFile()));
JarFileArchive jarFileArchive = new JarFileArchive(FileUtils.file(springbootFatJar.getFile()));
Iterator<Archive> archives = jarFileArchive.getNestedArchives(this::isNestedArchive,null);
List<URL> urls = new ArrayList<>();
while (archives.hasNext()){
Expand Down Expand Up @@ -75,7 +76,7 @@ protected boolean isNestedArchive(Archive.Entry entry) {
public void testStaticCombineGetBizArchives() throws Exception {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL springbootFatJar = cl.getResource("static-combine-demo.jar");
JarFileArchive jarFileArchive = new JarFileArchive(new File(springbootFatJar.getFile()));
JarFileArchive jarFileArchive = new JarFileArchive(FileUtils.file(springbootFatJar.getFile()));
Iterator<org.springframework.boot.loader.archive.Archive> archives = jarFileArchive.getNestedArchives(this::isNestedArchive,null);
List<URL> urls = new ArrayList<>();
while (archives.hasNext()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ExplodedBizArchiveTest {
public void testCreate() throws IOException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL arkBizJar = cl.getResource("sample-biz-withjar.jar");
File unpack = FileUtils.unzip(new File(arkBizJar.getFile()), arkBizJar.getFile()
File unpack = FileUtils.unzip(FileUtils.file(arkBizJar.getFile()), arkBizJar.getFile()
+ "-unpack");
ExplodedBizArchive archive = new ExplodedBizArchive(unpack);
Assert.assertNotNull(archive.getManifest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ private static byte[] generateManifest() {

private static byte[] fetchResource(String resourceName) throws IOException {
URL resource = BaseTest.class.getClassLoader().getResource(resourceName);
RandomAccessDataFile dataFile = new RandomAccessDataFile(new File(resource.getFile()));
RandomAccessDataFile dataFile = new RandomAccessDataFile(
com.alipay.sofa.ark.common.util.FileUtils.file(resource.getFile()));
return Bytes.get(dataFile);
}

public static File getTmpDir() {
String tmpPath = System.getProperty("java.io.tmpdir");
return new File(tmpPath);
return com.alipay.sofa.ark.common.util.FileUtils.file(tmpPath);
}

public static File getWorkspace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void testParseArtifactIdFromJarWithBlankPath() throws Exception {
String fullPath = root.getPath() + "space directory";
String jarLocation = fullPath + "/junit-4.12.jar";
FileUtils.mkdir(fullPath);
Files.copy(new File(jar.getFile()), new File(jarLocation));
Files.copy(FileUtils.file(jar.getFile()), FileUtils.file(jarLocation));

URL url = JarUtilsTest.class.getResource("/space directory/junit-4.12.jar");
Method method = JarUtils.class.getDeclaredMethod("parseArtifactIdFromJar", String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alipay.sofa.ark.bootstrap.ClasspathLauncher.ClassPathArchive;
import com.alipay.sofa.ark.common.log.ArkLoggerFactory;
import com.alipay.sofa.ark.common.util.AssertUtils;
import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.container.pipeline.DeployBizStage;
import com.alipay.sofa.ark.container.pipeline.HandleArchiveStage;
Expand Down Expand Up @@ -92,8 +93,7 @@ public static Object main(String[] args) throws ArkRuntimeException {
LaunchCommand launchCommand = LaunchCommand.parse(args);
if (launchCommand.isExecutedByCommandLine()) {
ExecutableArkBizJar executableArchive;
File rootFile = new File(URLDecoder.decode(launchCommand.getExecutableArkBizJar()
.getFile()));
File rootFile = FileUtils.file(launchCommand.getExecutableArkBizJar().getFile());
if (rootFile.isDirectory()) {
executableArchive = new ExecutableArkBizJar(new ExplodedArchive(rootFile));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Biz createBiz(BizArchive bizArchive) throws IOException {
public Biz createBiz(File file) throws IOException {
BizArchive bizArchive;
if (ArkConfigs.isEmbedEnable()) {
File unpackFile = new File(file.getAbsolutePath() + "-unpack");
File unpackFile = FileUtils.file(file.getAbsolutePath() + "-unpack");
if (!unpackFile.exists()) {
unpackFile = FileUtils.unzip(file, file.getAbsolutePath() + "-unpack");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.container;

import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.exception.ArkRuntimeException;
import com.alipay.sofa.ark.loader.ExecutableArkBizJar;
import com.alipay.sofa.ark.loader.archive.JarFileArchive;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void testStart() throws ArkRuntimeException {
@Test
public void testStop() throws Exception {
ArkContainer arkContainer = new ArkContainer(new ExecutableArkBizJar(new JarFileArchive(
new File((jarURL.getFile())))));
FileUtils.file((jarURL.getFile())))));
arkContainer.start();
arkContainer.stop();
Assert.assertFalse(arkContainer.isRunning());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.container;

import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.container.model.BizModel;
import com.alipay.sofa.ark.container.model.PluginModel;
import com.alipay.sofa.ark.container.pipeline.RegisterServiceStage;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void before() {
List<String> mockArguments = new ArrayList<>();
String filePath = this.getClass().getClassLoader()
.getResource("SampleClass.class").getPath();
String workingPath = new File(filePath).getParent();
String workingPath = FileUtils.file(filePath).getParent();
mockArguments.add(String.format("javaaget:%s", workingPath));
mockArguments.add(String.format("-javaagent:%s", workingPath));
mockArguments.add(String.format("-javaagent:%s=xx", workingPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void testUninstallBiz() throws Throwable {
public void testSwitchBiz() throws Throwable {
testUninstallBiz();
// test switch biz
ClientResponse response = ArkClient.installBiz(new File(bizUrl1.getFile()));
ClientResponse response = ArkClient.installBiz(FileUtils.file(bizUrl1.getFile()));
Assert.assertEquals(ResponseCode.SUCCESS, response.getCode());
BizInfo bizInfo = response.getBizInfos().iterator().next();
Assert.assertEquals(BizState.ACTIVATED, bizInfo.getBizState());
Expand Down

0 comments on commit 9c37315

Please sign in to comment.