-
Notifications
You must be signed in to change notification settings - Fork 827
JAV-102 find resource root path #56
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
foundation-common/src/main/java/io/servicecomb/foundation/common/utils/FileUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 io.servicecomb.foundation.common.utils; | ||
|
||
import java.io.File; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URL; | ||
import java.net.URLDecoder; | ||
|
||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.ResourceUtils; | ||
|
||
public final class FileUtils { | ||
private FileUtils() { | ||
|
||
} | ||
|
||
// result is path of a directory or a jar | ||
// eg: | ||
// io.servicecomb.foundation.common.utils.ResourceUtils -> ....../target/classes | ||
// org.apache.commons.codec.binary.StringUtils -> ....../commons-codec/commons-codec/1.9/commons-codec-1.9.jar | ||
public static String findRootPath(Class<?> cls) { | ||
String resourceName = | ||
"/" + ClassUtils.convertClassNameToResourcePath(cls.getName()) + ClassUtils.CLASS_FILE_SUFFIX; | ||
URL url = cls.getResource(resourceName); | ||
return findRootPath(url, resourceName); | ||
} | ||
|
||
// ....../target/classes/config/conf.xml -> ....../target/classes/ | ||
public static String findRootPath(URL url, String suffix) { | ||
String path = url.getPath(); | ||
try { | ||
// convert 'a%20b' to 'a b' | ||
path = URLDecoder.decode(path, "utf-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new IllegalArgumentException("Failed to decode url, path=" + path, e); | ||
} | ||
if (!path.endsWith(suffix)) { | ||
throw new IllegalArgumentException(String.format("url '%s' is not end with '%s'.", path, suffix)); | ||
} | ||
|
||
if (ResourceUtils.isJarURL(url)) { | ||
// protocol:/....jar!....suffix | ||
int idx = path.indexOf('!'); | ||
path = path.substring(url.getProtocol().length() + 2, idx); | ||
} else { | ||
path = path.substring(0, path.length() - suffix.length()); | ||
} | ||
|
||
File file = new File(path); | ||
return file.getPath(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
foundation-common/src/test/java/io/servicecomb/foundation/common/utils/TestFileUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 io.servicecomb.foundation.common.utils; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TestFileUtils { | ||
@Test | ||
public void testFileClassRootPath() { | ||
String path = FileUtils.findRootPath(FileUtils.class); | ||
File file = new File(path, FileUtils.class.getName().replace('.', '/') + ".class"); | ||
Assert.assertTrue(file.exists()); | ||
} | ||
|
||
@Test | ||
public void testJarClassRootPath() { | ||
String path = FileUtils.findRootPath(String.class); | ||
File file = new File(path); | ||
Assert.assertTrue(file.exists()); | ||
} | ||
|
||
@Test | ||
public void testFileUrlRootPath() { | ||
String suffix = "config/config.inc.xml"; | ||
URL url = Thread.currentThread().getContextClassLoader().getResource(suffix); | ||
String path = FileUtils.findRootPath(url, suffix); | ||
File file = new File(path, suffix); | ||
Assert.assertTrue(file.exists()); | ||
|
||
String invalidSuffix = suffix + "x"; | ||
try { | ||
FileUtils.findRootPath(url, invalidSuffix); | ||
Assert.fail("must throw exception"); | ||
} catch (IllegalArgumentException e) { | ||
Assert.assertEquals( | ||
String.format("url '%s' is not end with '%s'.", url.getPath(), invalidSuffix), | ||
e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testJarUrlRootPath() { | ||
String suffix = "META-INF/maven/commons-codec/commons-codec/pom.xml"; | ||
URL url = Thread.currentThread().getContextClassLoader().getResource(suffix); | ||
String path = FileUtils.findRootPath(url, suffix); | ||
File file = new File(path); | ||
Assert.assertTrue(file.exists()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the resourceName is null , it's better to throw the exception here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resourceName is null when load additional config.
it's normal, not a exception