Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
fce7ddb3
Unverified
Commit
fce7ddb3
authored
Oct 28, 2017
by
Jason Song
Committed by
GitHub
Oct 28, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #804 from nobodyiam/refactor-resource-utils
refactor resource utils a little bit
parents
a86dd3e0
dca803c2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
33 deletions
+47
-33
ResourceUtils.java
.../com/ctrip/framework/apollo/core/utils/ResourceUtils.java
+28
-14
log4j2.xml
apollo-demo/src/main/resources/log4j2.xml
+19
-19
No files found.
apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/ResourceUtils.java
View file @
fce7ddb3
package
com
.
ctrip
.
framework
.
apollo
.
core
.
utils
;
package
com
.
ctrip
.
framework
.
apollo
.
core
.
utils
;
import
java.net.URL
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -43,8 +44,8 @@ public class ResourceUtils {
...
@@ -43,8 +44,8 @@ public class ResourceUtils {
if
(
logger
.
isDebugEnabled
())
{
if
(
logger
.
isDebugEnabled
())
{
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
s
ropertyName
:
props
.
stringPropertyNames
())
{
for
(
String
p
ropertyName
:
props
.
stringPropertyNames
())
{
sb
.
append
(
sropertyName
).
append
(
'='
).
append
(
props
.
getProperty
(
s
ropertyName
)).
append
(
'\n'
);
sb
.
append
(
propertyName
).
append
(
'='
).
append
(
props
.
getProperty
(
p
ropertyName
)).
append
(
'\n'
);
}
}
if
(
sb
.
length
()
>
0
)
{
if
(
sb
.
length
()
>
0
)
{
...
@@ -58,6 +59,7 @@ public class ResourceUtils {
...
@@ -58,6 +59,7 @@ public class ResourceUtils {
private
static
InputStream
loadConfigFileFromDefaultSearchLocations
(
String
configPath
)
{
private
static
InputStream
loadConfigFileFromDefaultSearchLocations
(
String
configPath
)
{
try
{
try
{
// load from default search locations
for
(
String
searchLocation
:
DEFAULT_FILE_SEARCH_LOCATIONS
)
{
for
(
String
searchLocation
:
DEFAULT_FILE_SEARCH_LOCATIONS
)
{
File
candidate
=
Paths
.
get
(
searchLocation
,
configPath
).
toFile
();
File
candidate
=
Paths
.
get
(
searchLocation
,
configPath
).
toFile
();
if
(
candidate
.
exists
()
&&
candidate
.
isFile
()
&&
candidate
.
canRead
())
{
if
(
candidate
.
exists
()
&&
candidate
.
isFile
()
&&
candidate
.
canRead
())
{
...
@@ -66,22 +68,35 @@ public class ResourceUtils {
...
@@ -66,22 +68,35 @@ public class ResourceUtils {
}
}
}
}
InputStream
in
=
ClassLoaderUtil
.
getLoader
().
getResourceAsStream
(
configPath
);
// load from classpath
URL
url
=
ClassLoaderUtil
.
getLoader
().
getResource
(
configPath
);
if
(
in
!=
null
)
{
if
(
url
!=
null
)
{
logger
.
debug
(
"Reading config from resource {}"
,
ClassLoaderUtil
.
getLoader
().
getResource
(
configPath
).
getPath
());
InputStream
in
=
getResourceAsStream
(
url
);
return
in
;
}
else
{
if
(
in
!=
null
)
{
// load outside resource under current user path
logger
.
debug
(
"Reading config from resource {}"
,
url
.
getPath
());
File
candidate
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
configPath
);
return
in
;
if
(
candidate
.
exists
()
&&
candidate
.
isFile
()
&&
candidate
.
canRead
())
{
logger
.
debug
(
"Reading config from resource {}"
,
candidate
.
getAbsolutePath
());
return
new
FileInputStream
(
candidate
);
}
}
}
}
// load outside resource under current user path
File
candidate
=
new
File
(
System
.
getProperty
(
"user.dir"
),
configPath
);
if
(
candidate
.
exists
()
&&
candidate
.
isFile
()
&&
candidate
.
canRead
())
{
logger
.
debug
(
"Reading config from resource {}"
,
candidate
.
getAbsolutePath
());
return
new
FileInputStream
(
candidate
);
}
}
catch
(
FileNotFoundException
e
)
{
}
catch
(
FileNotFoundException
e
)
{
//ignore
//ignore
}
}
return
null
;
return
null
;
}
}
}
\ No newline at end of file
private
static
InputStream
getResourceAsStream
(
URL
url
)
{
try
{
return
url
!=
null
?
url
.
openStream
()
:
null
;
}
catch
(
IOException
e
)
{
return
null
;
}
}
}
apollo-demo/src/main/resources/log4j2.xml
View file @
fce7ddb3
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<configuration
monitorInterval=
"60"
>
<configuration
monitorInterval=
"60"
>
<appenders>
<appenders>
<Console
name=
"Console"
target=
"SYSTEM_OUT"
>
<Console
name=
"Console"
target=
"SYSTEM_OUT"
>
<PatternLayout
pattern=
"[apollo-demo][%t]%d %-5p [%c] %m%n"
/>
<PatternLayout
pattern=
"[apollo-demo][%t]%d %-5p [%c] %m%n"
/>
</Console>
</Console>
<Async
name=
"Async"
includeLocation=
"true"
>
<Async
name=
"Async"
includeLocation=
"true"
>
<AppenderRef
ref=
"Console"
/>
<AppenderRef
ref=
"Console"
/>
</Async>
</Async>
</appenders>
</appenders>
<loggers>
<loggers>
<logger
name=
"com.ctrip.framework.apollo"
additivity=
"false"
level=
"trace
"
>
<logger
name=
"com.ctrip.framework.apollo"
additivity=
"false"
level=
"INFO
"
>
<AppenderRef
ref=
"Async"
level=
"INFO
"
/>
<AppenderRef
ref=
"Async
"
/>
</logger>
</logger>
<logger
name=
"com.ctrip.framework.apollo.demo"
additivity=
"false"
level=
"trace
"
>
<logger
name=
"com.ctrip.framework.apollo.demo"
additivity=
"false"
level=
"DEBUG
"
>
<AppenderRef
ref=
"Async"
level=
"DEBUG
"
/>
<AppenderRef
ref=
"Async
"
/>
</logger>
</logger>
<root
level=
"INFO"
>
<root
level=
"INFO"
>
<AppenderRef
ref=
"Async"
/>
<AppenderRef
ref=
"Async"
/>
</root>
</root>
</loggers>
</loggers>
</configuration>
</configuration>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment