-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fb1315
commit 3876eef
Showing
11 changed files
with
220 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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
96 changes: 96 additions & 0 deletions
96
core/src/main/java/com/alipay/sofa/common/utils/ResourceUtil.java
This file contains 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,96 @@ | ||
/* | ||
* 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 com.alipay.sofa.common.utils; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
|
||
/** | ||
* @author qilong.zql | ||
* @author Juergen Hoeller | ||
* @since 1.0.17 | ||
*/ | ||
public class ResourceUtil { | ||
|
||
/** URL protocol for a file in the file system: "file" */ | ||
public static final String URL_PROTOCOL_FILE = "file"; | ||
|
||
/** | ||
* Resolve the given resource URL to a {@code java.io.File}, | ||
* i.e. to a file in the file system. | ||
* @param resourceUrl the resource URL to resolve | ||
* @return a corresponding File object | ||
* @throws FileNotFoundException if the URL cannot be resolved to | ||
* a file in the file system | ||
*/ | ||
public static File getFile(URL resourceUrl) throws FileNotFoundException { | ||
return getFile(resourceUrl, "URL"); | ||
} | ||
|
||
/** | ||
* Resolve the given resource URL to a {@code java.io.File}, | ||
* i.e. to a file in the file system. | ||
* @param resourceUrl the resource URL to resolve | ||
* @param description a description of the original resource that | ||
* the URL was created for (for example, a class path location) | ||
* @return a corresponding File object | ||
* @throws FileNotFoundException if the URL cannot be resolved to | ||
* a file in the file system | ||
*/ | ||
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException { | ||
AssertUtil.notNull(resourceUrl, "Resource URL must not be null"); | ||
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) { | ||
throw new FileNotFoundException(description | ||
+ " cannot be resolved to absolute file path " | ||
+ "because it does not reside in the file system: " | ||
+ resourceUrl); | ||
} | ||
try { | ||
return new File(toURI(resourceUrl).getSchemeSpecificPart()); | ||
} catch (URISyntaxException ex) { | ||
// Fallback for URLs that are not valid URIs (should hardly ever happen). | ||
return new File(resourceUrl.getFile()); | ||
} | ||
} | ||
|
||
/** | ||
* Create a URI instance for the given URL, | ||
* replacing spaces with "%20" URI encoding first. | ||
* @param url the URL to convert into a URI instance | ||
* @return the URI instance | ||
* @throws URISyntaxException if the URL wasn't a valid URI | ||
* @see java.net.URL#toURI() | ||
*/ | ||
public static URI toURI(URL url) throws URISyntaxException { | ||
return toURI(url.toString()); | ||
} | ||
|
||
/** | ||
* Create a URI instance for the given location String, | ||
* replacing spaces with "%20" URI encoding first. | ||
* @param location the location String to convert into a URI instance | ||
* @return the URI instance | ||
* @throws URISyntaxException if the location wasn't a valid URI | ||
*/ | ||
public static URI toURI(String location) throws URISyntaxException { | ||
return new URI(StringUtil.replace(location, " ", "%20")); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
core/src/test/java/com/alipay/sofa/common/utils/ResourceUtilTest.java
This file contains 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,43 @@ | ||
/* | ||
* 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 com.alipay.sofa.common.utils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.net.URL; | ||
import java.util.Properties; | ||
|
||
/** | ||
* @author qilong.zql | ||
* @since 1.0.17 | ||
*/ | ||
public class ResourceUtilTest { | ||
|
||
@Test | ||
public void testGetFile() throws Exception { | ||
URL url = this.getClass().getClassLoader().getResource("test-resource-utils.properties"); | ||
File file = ResourceUtil.getFile(url); | ||
Properties properties = new Properties(); | ||
properties.load(new FileInputStream(file)); | ||
Assert.assertEquals(properties.getProperty("keyA"), "A"); | ||
Assert.assertEquals(properties.getProperty("keyB"), "B"); | ||
} | ||
|
||
} |
This file contains 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,2 @@ | ||
keyA=A | ||
keyB=B |
This file contains 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
35 changes: 35 additions & 0 deletions
35
...tarter/src/main/java/com/alipay/sofa/common/boot/logging/DefaultReInitializerChecker.java
This file contains 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,35 @@ | ||
/* | ||
* 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 com.alipay.sofa.common.boot.logging; | ||
|
||
import com.alipay.sofa.common.log.spi.ReInitializeChecker; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* @author qilong.zql | ||
* @since 1.0.17 | ||
*/ | ||
public class DefaultReInitializerChecker implements ReInitializeChecker { | ||
|
||
public final static AtomicBoolean isReInitialized = new AtomicBoolean(false); | ||
|
||
@Override | ||
public boolean isReInitialize() { | ||
return DefaultReInitializerChecker.isReInitialized.get(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
log-sofa-boot-starter/src/main/java/com/alipay/sofa/common/boot/logging/Mark.java
This file contains 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,24 @@ | ||
/* | ||
* 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 com.alipay.sofa.common.boot.logging; | ||
|
||
/** | ||
* @author qilong.zql | ||
* @since 1.0.17 | ||
*/ | ||
public class Mark { | ||
} |
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
...r/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.ReInitializeChecker
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
com.alipay.sofa.common.boot.logging.CommonLoggingApplicationListener | ||
com.alipay.sofa.common.boot.logging.DefaultReInitializerChecker |