1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.environment.internal; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.nio.file.Files; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Provider; |
28 |
|
|
29 |
|
import org.apache.commons.io.FileUtils; |
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.environment.Environment; |
32 |
|
|
33 |
|
|
34 |
|
@link |
35 |
|
|
36 |
|
@version |
37 |
|
@since |
38 |
|
|
|
|
| 92.3% |
Uncovered Elements: 6 (78) |
Complexity: 26 |
Complexity Density: 0.58 |
|
39 |
|
public abstract class AbstractEnvironment implements Environment |
40 |
|
{ |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
private static final String DEFAULT_TMP_DIRECTORY = System.getProperty("java.io.tmpdir"); |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
private static final String TEMP_NAME = "xwiki-temp"; |
50 |
|
|
51 |
|
@Inject |
52 |
|
protected Logger logger; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@Inject |
60 |
|
private Provider<EnvironmentConfiguration> configurationProvider; |
61 |
|
|
62 |
|
|
63 |
|
@see |
64 |
|
|
65 |
|
private File temporaryDirectory; |
66 |
|
|
67 |
|
|
68 |
|
@see |
69 |
|
|
70 |
|
private File permanentDirectory; |
71 |
|
|
72 |
|
|
73 |
|
@param@link |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
213 |
public void setPermanentDirectory(File permanentDirectory)... |
76 |
|
{ |
77 |
213 |
this.permanentDirectory = permanentDirectory; |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
@param@link |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
211 |
public void setTemporaryDirectory(File temporaryDirectory)... |
84 |
|
{ |
85 |
211 |
this.temporaryDirectory = temporaryDirectory; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
@inheritDoc |
90 |
|
|
91 |
|
@link |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@link |
96 |
|
|
97 |
|
|
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 5 |
Complexity Density: 0.56 |
|
99 |
409 |
@Override... |
100 |
|
public File getPermanentDirectory() |
101 |
|
{ |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
409 |
if (this.permanentDirectory == null) { |
107 |
78 |
String systemProperty = System.getProperty("xwiki.data.dir"); |
108 |
78 |
final String classSpecified = getPermanentDirectoryName(); |
109 |
78 |
final String configured = this.configurationProvider.get().getPermanentDirectoryPath(); |
110 |
|
|
111 |
78 |
final String[] locations = |
112 |
|
new String[] { systemProperty, classSpecified, configured, getTemporaryDirectoryName(), |
113 |
|
DEFAULT_TMP_DIRECTORY }; |
114 |
77 |
this.permanentDirectory = initializeDirectory(locations, false); |
115 |
|
|
116 |
77 |
if (systemProperty == null && classSpecified == null && configured == null) { |
117 |
|
|
118 |
35 |
this.logger.warn("No permanent directory configured. Using temporary directory [{}].", |
119 |
|
this.permanentDirectory); |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
408 |
return this.permanentDirectory; |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
@return |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
78 |
protected String getPermanentDirectoryName()... |
134 |
|
{ |
135 |
78 |
return null; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
@inheritDoc |
140 |
|
|
141 |
|
@link |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@see |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
149 |
806 |
@Override... |
150 |
|
public File getTemporaryDirectory() |
151 |
|
{ |
152 |
806 |
if (this.temporaryDirectory == null) { |
153 |
68 |
final String[] locations = new String[] { getTemporaryDirectoryName(), DEFAULT_TMP_DIRECTORY }; |
154 |
68 |
this.temporaryDirectory = initializeDirectory(locations, true); |
155 |
|
} |
156 |
|
|
157 |
804 |
return this.temporaryDirectory; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
@return |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
167 |
146 |
protected String getTemporaryDirectoryName()... |
168 |
|
{ |
169 |
146 |
return null; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
@param |
174 |
|
|
175 |
|
@param |
176 |
|
@return@link |
177 |
|
|
178 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
179 |
145 |
private File initializeDirectory(final String[] locations, final boolean isTemp)... |
180 |
|
{ |
181 |
145 |
final String tempOrPermanent = (isTemp) ? "temporary" : "permanent"; |
182 |
145 |
boolean first = true; |
183 |
145 |
for (final String location : locations) { |
184 |
328 |
if (location == null) { |
185 |
183 |
continue; |
186 |
|
} |
187 |
145 |
if (!first) { |
188 |
0 |
this.logger.warn("Falling back on [{}] as the {} directory.", location, tempOrPermanent); |
189 |
|
} |
190 |
145 |
first = false; |
191 |
145 |
final File dir = initializeDirectory(location, isTemp, tempOrPermanent); |
192 |
145 |
if (dir != null) { |
193 |
144 |
return dir; |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
1 |
throw new RuntimeException(String.format( |
198 |
|
"Could not find a writable %s directory. Check the server logs for more information.", tempOrPermanent)); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
|
@param |
203 |
|
@param |
204 |
|
@param |
205 |
|
|
206 |
|
@return@link |
207 |
|
|
208 |
|
|
|
|
| 84.2% |
Uncovered Elements: 3 (19) |
Complexity: 7 |
Complexity Density: 0.64 |
|
209 |
145 |
private File initializeDirectory(final String directoryName, final boolean isTemp, final String tempOrPermanent)... |
210 |
|
{ |
211 |
145 |
final File dir = (isTemp) ? new File(directoryName, TEMP_NAME) : new File(directoryName); |
212 |
|
|
213 |
145 |
if (dir.exists()) { |
214 |
108 |
if (dir.isDirectory() && dir.canWrite()) { |
215 |
107 |
return initDir(dir, isTemp); |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
1 |
this.logger.error("Configured {} directory [{}] is {}.", tempOrPermanent, dir.getAbsolutePath(), |
220 |
1 |
(dir.isDirectory()) ? "not writable" : "not a directory"); |
221 |
|
|
222 |
1 |
return null; |
223 |
|
|
224 |
|
} |
225 |
|
|
226 |
37 |
try { |
227 |
37 |
Files.createDirectories(dir.toPath()); |
228 |
|
|
229 |
37 |
return initDir(dir, isTemp); |
230 |
|
} catch (IOException e) { |
231 |
0 |
this.logger.error("Configured {} directory [{}] could not be created.", tempOrPermanent, |
232 |
|
dir.getAbsolutePath(), e); |
233 |
|
} |
234 |
|
|
235 |
0 |
return null; |
236 |
|
} |
237 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
238 |
144 |
private File initDir(final File directory, final boolean isTemp)... |
239 |
|
{ |
240 |
144 |
if (isTemp) { |
241 |
67 |
try { |
242 |
67 |
FileUtils.cleanDirectory(directory); |
243 |
|
} catch (IOException e) { |
244 |
0 |
throw new RuntimeException(String.format("Failed to empty the temporary directory [%s]. " |
245 |
|
+ "Are there files inside of it which XWiki " + "does not have permission to delete?", |
246 |
|
directory.getAbsolutePath()), e); |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
144 |
return directory; |
251 |
|
} |
252 |
|
} |