Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
isochrone
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Institut für Informatik
dbis
dbis-isochrone
isochrone
Commits
74e6b3e0
Commit
74e6b3e0
authored
10 years ago
by
Nikolaus Krismer
Browse files
Options
Downloads
Patches
Plain Diff
reworked config file retrieval for dataset configurations
parent
980fb4c5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/it/unibz/inf/isochrone/config/ConfigDataset.java
+45
-7
45 additions, 7 deletions
...ain/java/it/unibz/inf/isochrone/config/ConfigDataset.java
src/main/java/it/unibz/inf/isochrone/util/ResourceHelper.java
+53
-66
53 additions, 66 deletions
...main/java/it/unibz/inf/isochrone/util/ResourceHelper.java
with
98 additions
and
73 deletions
src/main/java/it/unibz/inf/isochrone/config/ConfigDataset.java
+
45
−
7
View file @
74e6b3e0
...
@@ -17,6 +17,7 @@ import it.unibz.inf.isochrone.util.ResourceHelper;
...
@@ -17,6 +17,7 @@ import it.unibz.inf.isochrone.util.ResourceHelper;
import
java.io.File
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.net.URL
;
import
java.text.DateFormat
;
import
java.text.DateFormat
;
import
java.text.ParseException
;
import
java.text.ParseException
;
...
@@ -26,6 +27,7 @@ import java.util.Arrays;
...
@@ -26,6 +27,7 @@ import java.util.Arrays;
import
java.util.Calendar
;
import
java.util.Calendar
;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -35,7 +37,9 @@ import java.util.regex.Pattern;
...
@@ -35,7 +37,9 @@ import java.util.regex.Pattern;
import
javax.validation.constraints.NotNull
;
import
javax.validation.constraints.NotNull
;
import
org.apache.commons.io.Charsets
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -77,7 +81,8 @@ public class ConfigDataset {
...
@@ -77,7 +81,8 @@ public class ConfigDataset {
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ConfigDataset
.
class
);
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ConfigDataset
.
class
);
private
static
final
Collection
<
String
>
ALL_DATASETS
=
getAvailableDatasets
(
true
);
private
static
final
Collection
<
String
>
ALL_DATASETS
=
getAvailableDatasets
(
true
);
private
static
final
String
DS_CONFIG_PATTERN
=
".*config\\_(.*)\\.xml"
;
private
static
final
String
DS_CONFIG_EXTENSION
=
"xml"
;
private
static
final
String
DS_CONFIG_PATTERN
=
".*config\\_(.*)\\."
+
DS_CONFIG_EXTENSION
;
private
static
Map
<
String
,
ConfigDataset
>
instanceMap
=
new
HashMap
<>();
private
static
Map
<
String
,
ConfigDataset
>
instanceMap
=
new
HashMap
<>();
@PropertyValue
(
"client.tArrival"
)
@PropertyValue
(
"client.tArrival"
)
...
@@ -361,23 +366,56 @@ public class ConfigDataset {
...
@@ -361,23 +366,56 @@ public class ConfigDataset {
protected
static
Collection
<
String
>
getAvailableDatasets
(
final
boolean
filterInvalid
)
{
protected
static
Collection
<
String
>
getAvailableDatasets
(
final
boolean
filterInvalid
)
{
LOGGER
.
info
(
"Getting available datasets"
);
LOGGER
.
info
(
"Getting available datasets"
);
final
ClassLoader
cLoader
=
Thread
.
currentThread
().
getContextClassLoader
();
final
Pattern
dsConfigPattern
=
Pattern
.
compile
(
DS_CONFIG_PATTERN
);
final
Pattern
dsConfigPattern
=
Pattern
.
compile
(
DS_CONFIG_PATTERN
);
URL
r
=
null
;
InputStream
r2
=
null
;
Collection
<
String
>
resources
=
null
;
Collection
<
String
>
resources
=
null
;
try
{
try
{
final
URL
r
=
Thread
.
currentThread
().
getContextClassLoader
().
getResource
(
"/"
);
// find resources on disk and add to available ones
if
(
r
==
null
)
{
r
=
cLoader
.
getResource
(
"/"
);
resources
=
ResourceHelper
.
getResources
(
dsConfigPattern
);
if
(
r
!=
null
)
{
}
else
{
final
Collection
<
File
>
files
=
FileUtils
.
listFiles
(
new
File
(
r
.
getFile
()),
new
String
[]
{
DS_CONFIG_EXTENSION
},
true
);
final
Collection
<
File
>
files
=
FileUtils
.
listFiles
(
new
File
(
r
.
getFile
()),
null
,
false
);
resources
=
new
ArrayList
<>(
files
.
size
());
resources
=
new
ArrayList
<>(
files
.
size
());
for
(
File
file
:
files
)
{
for
(
File
file
:
files
)
{
resources
.
add
(
file
.
getName
());
final
String
filename
=
file
.
getName
();
if
(
dsConfigPattern
.
matcher
(
filename
).
matches
())
{
resources
.
add
(
filename
);
}
}
}
// find resources on classpath (in jar files...) and add to available ones
r2
=
cLoader
.
getResourceAsStream
(
"/"
);
if
(
r2
!=
null
)
{
final
Collection
<
String
>
files
=
IOUtils
.
readLines
(
r2
,
Charsets
.
UTF_8
);
for
(
String
filename
:
files
)
{
if
(
dsConfigPattern
.
matcher
(
filename
).
matches
())
{
resources
.
add
(
filename
);
}
}
}
}
}
// find resources on webapp resources (in tomcat lib folder, in WEB-INF...) and add to available ones
final
Enumeration
<
URL
>
e
=
cLoader
.
getResources
(
"../lib"
);
if
(
e
!=
null
)
{
final
Collection
<
URL
>
webappResources
=
Collections
.
list
(
e
);
final
Collection
<
String
>
files
=
ResourceHelper
.
findFilesInResources
(
webappResources
,
dsConfigPattern
);
resources
.
addAll
(
files
);
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
resources
=
null
;
resources
=
null
;
LOGGER
.
warn
(
"Datasets could not be read. There might be no configured datasets available!"
);
LOGGER
.
warn
(
"Datasets could not be read. There might be no configured datasets available!"
);
}
finally
{
if
(
r2
!=
null
)
{
try
{
r2
.
close
();
}
catch
(
IOException
e
)
{
LOGGER
.
debug
(
"Could not close input stream after searching for config files in classpath"
);
}
}
}
}
if
(
resources
==
null
||
resources
.
size
()
<=
0
)
{
if
(
resources
==
null
||
resources
.
size
()
<=
0
)
{
...
...
This diff is collapsed.
Click to expand it.
src/main/java/it/unibz/inf/isochrone/util/ResourceHelper.java
+
53
−
66
View file @
74e6b3e0
...
@@ -2,103 +2,90 @@ package it.unibz.inf.isochrone.util;
...
@@ -2,103 +2,90 @@ package it.unibz.inf.isochrone.util;
import
java.io.File
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.
util.ArrayList
;
import
java.
net.URL
;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.Enumeration
;
import
java.util.Enumeration
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.jar.JarEntry
;
import
java.util.jar.JarFile
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipFile
;
/**
* Gets available resources (from the classpath) by a regular expression pattern.
*/
public
final
class
ResourceHelper
{
public
final
class
ResourceHelper
{
private
static
final
String
EXTENSION_JAR
=
".jar"
;
private
static
final
String
PROPERTY_CP
=
"java.class.path"
;
// Constructor
private
ResourceHelper
()
{
}
private
ResourceHelper
()
{}
// Public static methods
/**
/**
* Gets all resources matching the given pattern.
* Gets all resources matching the given pattern
from the path elements given
.
*
*
* @param
p
attern the pattern to match against
(as string)
* @param
fileNameP
attern the pattern to match against
* @return the resources in the order they are found
* @return the resources in the order they are found
* @throws IOException if an I/O error occurs
* @throws IOException if an I/O error occurs
*/
*/
public
static
Collection
<
String
>
getResources
(
final
String
pattern
)
throws
IOException
{
public
static
Set
<
String
>
findFilesInResources
(
final
Collection
<
URL
>
classPathElements
,
final
Pattern
fileNamePattern
)
throws
IOException
{
return
getResources
(
Pattern
.
compile
(
pattern
));
final
String
[]
retval
=
new
String
[
classPathElements
.
size
()];
}
/**
int
i
=
0
;
* Gets all resources matching the given pattern.
for
(
final
URL
element
:
classPathElements
)
{
*
retval
[
i
++]
=
element
.
getFile
();
* @param pattern the pattern to match against
* @return the resources in the order they are found
* @throws IOException if an I/O error occurs
*/
public
static
Collection
<
String
>
getResources
(
final
Pattern
pattern
)
throws
IOException
{
final
Collection
<
String
>
retval
=
new
ArrayList
<>();
final
String
classPath
=
System
.
getProperty
(
"java.class.path"
,
"."
);
final
String
[]
classPathElements
=
classPath
.
split
(
File
.
pathSeparator
);
for
(
final
String
element
:
classPathElements
)
{
retval
.
addAll
(
getResources
(
element
,
pattern
));
}
}
return
retval
;
return
findFilesInResources
(
retval
,
fileNamePattern
)
;
}
}
// Private static methods
public
static
Set
<
String
>
findFilesInResources
(
final
String
[]
pathElements
,
final
Pattern
fileNamePattern
)
throws
IOException
{
Set
<
String
>
result
=
new
HashSet
<
String
>();
for
(
String
element
:
pathElements
)
{
final
File
newFile
=
new
File
(
element
);
if
(
newFile
.
isDirectory
())
{
result
.
addAll
(
findResourceInDirectory
(
newFile
,
fileNamePattern
));
}
else
{
result
.
addAll
(
findResourceInFile
(
newFile
,
fileNamePattern
));
}
}
private
static
Collection
<
String
>
getResources
(
final
String
element
,
final
Pattern
pattern
)
throws
IOException
{
return
result
;
final
Collection
<
String
>
retval
=
new
ArrayList
<>();
}
final
File
file
=
new
File
(
element
);
public
static
Set
<
String
>
findFilesInClassPath
(
final
Pattern
fileNamePattern
)
throws
IOException
{
if
(
file
.
isDirectory
())
{
final
String
classPath
=
System
.
getProperty
(
PROPERTY_CP
);
retval
.
addAll
(
getResourcesFromDirectory
(
file
,
pattern
));
final
String
[]
pathElements
=
classPath
.
split
(
File
.
pathSeparator
);
}
else
{
retval
.
addAll
(
getResourcesFromJarFile
(
file
,
pattern
));
}
return
retval
;
return
findFilesInResources
(
pathElements
,
fileNamePattern
)
;
}
}
private
static
Collection
<
String
>
get
Resource
sFromJar
File
(
final
File
f
ile
,
final
Pattern
p
attern
)
throws
IOException
{
private
static
Collection
<
String
>
find
Resource
In
File
(
final
File
resourceF
ile
,
final
Pattern
fileNameP
attern
)
throws
IOException
{
final
Collection
<
String
>
re
tval
=
new
ArrayList
<
>();
final
Set
<
String
>
re
sult
=
new
HashSet
<
String
>();
try
(
final
ZipFile
zf
=
new
ZipFile
(
file
))
{
if
(
resourceFile
.
canRead
()
&&
resourceFile
.
getAbsolutePath
().
endsWith
(
EXTENSION_JAR
))
{
final
Enumeration
<?
extends
ZipEntry
>
e
=
zf
.
entries
();
try
(
final
JarFile
jarFile
=
new
JarFile
(
resourceFile
))
{
while
(
e
.
hasMoreE
le
m
ent
s
())
{
final
Enumeration
<
JarEntry
>
entries
=
jarFi
le
.
ent
ries
();
final
ZipEntry
ze
=
e
.
next
Element
()
;
while
(
entries
.
hasMore
Element
s
()
)
{
final
String
fileName
=
ze
.
getName
();
final
JarEntry
singleEntry
=
entries
.
nextElement
();
final
boolean
accept
=
pattern
.
matcher
(
file
Name
).
matches
()
;
if
(
fileNamePattern
.
matcher
(
singleEntry
.
get
Name
()
).
matches
()
)
{
if
(
accept
)
{
result
.
add
(
jarFile
.
getName
()
+
File
.
separatorChar
+
singleEntry
.
getName
());
retval
.
add
(
fileName
);
}
}
}
}
}
}
catch
(
final
IOException
ioe
)
{
throw
ioe
;
}
}
return
re
tval
;
return
re
sult
;
}
}
private
static
Collection
<
String
>
getResourcesFromDirectory
(
final
File
directory
,
final
Pattern
pattern
)
throws
IOException
{
private
static
Set
<
String
>
findResourceInDirectory
(
final
File
directory
,
final
Pattern
fileNamePattern
)
throws
IOException
{
final
Collection
<
String
>
retval
=
new
ArrayList
<>();
final
Set
<
String
>
result
=
new
HashSet
<
String
>();
final
File
[]
files
=
directory
.
listFiles
();
final
File
[]
fileList
=
directory
.
listFiles
();
for
(
final
File
currentFile
:
files
)
{
for
(
final
File
file
:
fileList
)
{
if
(
fileNamePattern
.
matcher
(
currentFile
.
getAbsolutePath
()).
matches
())
{
if
(
file
.
isDirectory
())
{
result
.
add
(
currentFile
.
getAbsolutePath
());
retval
.
addAll
(
getResourcesFromDirectory
(
file
,
pattern
));
}
else
if
(
currentFile
.
isDirectory
())
{
result
.
addAll
(
findResourceInDirectory
(
currentFile
,
fileNamePattern
));
}
else
{
}
else
{
final
String
fileName
=
file
.
getCanonicalPath
();
result
.
addAll
(
findResourceInFile
(
currentFile
,
fileNamePattern
));
final
boolean
accept
=
pattern
.
matcher
(
fileName
).
matches
();
if
(
accept
)
{
retval
.
add
(
fileName
);
}
}
}
}
}
return
re
tval
;
return
re
sult
;
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment