1
1
/**
2
2
* Copyright (C) 2000 - 2012 Silverpeas
3
3
*
4
- * This program is free software: you can redistribute it and/or modify
5
- * it under the terms of the GNU Affero General Public License as
6
- * published by the Free Software Foundation, either version 3 of the
7
- * License, or (at your option) any later version.
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the
5
+ * GNU Affero General Public License as published by the Free Software Foundation, either version 3
6
+ * of the License, or (at your option) any later version.
8
7
*
9
- * As a special exception to the terms and conditions of version 3.0 of
10
- * the GPL, you may redistribute this Program in connection with Free/Libre
11
- * Open Source Software ("FLOSS") applications as described in Silverpeas's
12
- * FLOSS exception. You should have received a copy of the text describing
13
- * the FLOSS exception, and it is also available here:
8
+ * As a special exception to the terms and conditions of version 3.0 of the GPL, you may
9
+ * redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
10
+ * applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
11
+ * text describing the FLOSS exception, and it is also available here:
14
12
* "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
15
13
*
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU Affero General Public License for more details.
14
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
+ * Affero General Public License for more details.
20
17
*
21
- * You should have received a copy of the GNU Affero General Public License
22
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ * You should have received a copy of the GNU Affero General Public License along with this program.
19
+ * If not, see <http://www.gnu.org/licenses/>.
23
20
*/
24
-
25
21
package org .silverpeas .dbbuilder .util ;
26
22
27
23
import java .io .File ;
24
+ import java .io .FileFilter ;
28
25
import java .io .FileInputStream ;
29
26
import java .io .IOException ;
30
27
import java .io .InputStream ;
@@ -39,7 +36,6 @@ public class Configuration {
39
36
40
37
private static String dbbuilderHome = null ;
41
38
private static String dbbuilderData = null ;
42
-
43
39
private static final String DATA_KEY = "dbbuilder.data" ;
44
40
private static final String HOME_KEY = "dbbuilder.home" ;
45
41
private static final String DBREPOSITORY_SUBDIR = "dbRepository" ;
@@ -48,29 +44,26 @@ public class Configuration {
48
44
private static final String TEMP_FILES_SUBDIR = "temp" ;
49
45
private static final String DIR_CONTRIBUTIONFILESROOT = Configuration .getHome ()
50
46
+ File .separator + DBREPOSITORY_SUBDIR + File .separator + CONTRIB_FILES_SUBDIR ;
51
- // Répertoire racine des DB Pieces Contribution File
47
+ // Répertoire racine des DB Pieces Contribution File
52
48
private static final String DIR_DBPIECESFILESROOT =
53
49
getHome () + File .separator + DBREPOSITORY_SUBDIR ;
54
- // Répertoire temp
50
+ // Répertoire temp
55
51
private static final String DIR_TEMP = getHome () + File .separator + TEMP_FILES_SUBDIR ;
56
52
57
53
/**
58
54
* Load a properties file from the classpath then from $SILVERPEAS_HOME/properties
59
- * @param propertyName
60
- * @return a java.util.Properties
61
- * @throws IOException
55
+ * @param propertyName the path of the resource in the classpath or relative to the
56
+ * $SILVERPEAS_HOME/properties folder.
57
+ * @return the resource properties
58
+ * @throws IOException if an error occurs while loading the resource content.
62
59
*/
63
60
public static Properties loadResource (String propertyName ) throws IOException {
64
61
Properties properties = new Properties ();
65
62
InputStream in = Configuration .class .getClassLoader ().getResourceAsStream (propertyName );
66
63
try {
67
64
if (in == null ) {
68
- String path = propertyName .replace ('/' , File .separatorChar );
69
- path = path .replace ('\\' , File .separatorChar );
70
- if (!path .startsWith (File .separator )) {
71
- path = File .separatorChar + path ;
72
- }
73
- File configurationFile = new File (getHome () + File .separatorChar + "properties" + path );
65
+ String path = getPropertyPath (propertyName );
66
+ File configurationFile = new File (path );
74
67
if (configurationFile .exists ()) {
75
68
in = new FileInputStream (configurationFile );
76
69
}
@@ -84,7 +77,63 @@ public static Properties loadResource(String propertyName) throws IOException {
84
77
return properties ;
85
78
}
86
79
87
- // Récupère le répertoire racine d'installation
80
+ /**
81
+ * Loads the properties from specified resource.
82
+ * @param resource a resource.
83
+ * @return the resource properties.
84
+ * @throws IOException if an error occurs while loading the resource content.
85
+ */
86
+ public static Properties loadResource (File resource ) throws IOException {
87
+ Properties properties = new Properties ();
88
+ InputStream stream = null ;
89
+ if (resource .exists ()) {
90
+ try {
91
+ stream = new FileInputStream (resource );
92
+ properties .load (stream );
93
+ } finally {
94
+ IOUtils .closeQuietly (stream );
95
+ }
96
+ }
97
+ return properties ;
98
+ }
99
+
100
+ /**
101
+ * Lists the resources in the specified directory and whose the name matches the specified filter.
102
+ * The path of the directory should be relative to the properties folder in the Silverpeas home
103
+ * directory.
104
+ * @param directoryPath the relative path of the directory containing the resources to list.
105
+ * @param filter a filter on the resources to list. If the filter is null or empty, then no
106
+ * filtering is applied on the resources listing in the specified directory.
107
+ * @return an array of File instances, each of them representing a resource in the specified
108
+ * directory and matching the specified filter if any.
109
+ * @throws IOException if the path doesn't refer a directory
110
+ */
111
+ public static File [] listResources (final String directoryPath , final FileFilter filter ) throws
112
+ IOException {
113
+ String path = getPropertyPath (directoryPath );
114
+ File dir = new File (path );
115
+ if (!dir .exists () || !dir .isDirectory ()) {
116
+ throw new IOException ("The path '" + path + "' doesn't refer a directory!" );
117
+ }
118
+ return dir .listFiles (new FileFilter () {
119
+ @ Override
120
+ public boolean accept (File file ) {
121
+ boolean match = (filter != null ? filter .accept (file ) : true );
122
+ return file .isFile () && match ;
123
+ }
124
+ });
125
+ }
126
+
127
+ public static boolean isExist (final String resourcePath ) {
128
+ String path = Configuration .class .getClassLoader ().getResource (resourcePath ).getPath ();
129
+ if (!new File (path ).exists ()) {
130
+ path = getPropertyPath (resourcePath );
131
+ return new File (path ).exists ();
132
+ }
133
+ return true ;
134
+ }
135
+
136
+ // Récupère le répertoire racine d'installation
88
137
public static String getHome () {
89
138
if (dbbuilderHome == null ) {
90
139
if (!System .getProperties ().containsKey (HOME_KEY )) {
@@ -106,7 +155,7 @@ public static String getPiecesFilesDir() {
106
155
return DIR_DBPIECESFILESROOT ;
107
156
}
108
157
109
- // Récupère le répertoire data d'installation
158
+ // Récupère le répertoire data d'installation
110
159
public static String getData () {
111
160
if (dbbuilderData == null ) {
112
161
if (System .getProperties ().containsKey (DATA_KEY )) {
@@ -128,6 +177,15 @@ public static String getLogDir() {
128
177
return getHome () + File .separator + LOG_FILES_SUBDIR ;
129
178
}
130
179
180
+ private static String getPropertyPath (String propertyPath ) {
181
+ String path = propertyPath .replace ('/' , File .separatorChar );
182
+ path = path .replace ('\\' , File .separatorChar );
183
+ if (!path .startsWith (File .separator )) {
184
+ path = File .separatorChar + path ;
185
+ }
186
+ return getHome () + File .separatorChar + "properties" + path ;
187
+ }
188
+
131
189
private Configuration () {
132
190
}
133
191
}
0 commit comments