/
FilePersistenceProvider.java
executable file
·340 lines (307 loc) · 13.3 KB
/
FilePersistenceProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*******************************************************************************
* Copyright (c) 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.ui.internal.persistence;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;
import com.ibm.websphere.jsonsupport.JSON;
import com.ibm.websphere.jsonsupport.JSONFactory;
import com.ibm.websphere.jsonsupport.JSONMarshallException;
import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;
import com.ibm.websphere.ras.annotation.Trivial;
import com.ibm.ws.ffdc.annotation.FFDCIgnore;
import com.ibm.ws.ui.persistence.IPersistenceDebugger;
import com.ibm.ws.ui.persistence.IPersistenceProvider;
import com.ibm.wsspi.kernel.service.location.WsLocationAdmin;
import com.ibm.wsspi.kernel.service.location.WsResource;
import com.ibm.wsspi.kernel.service.utils.AtomicServiceReference;
import com.ibm.wsspi.kernel.service.utils.FileUtils;
/**
* Set the service.ranking=0 so that we are considered the "default" provider.
*/
@Component(service = { IPersistenceProvider.class },
configurationPolicy = ConfigurationPolicy.IGNORE,
property = { "service.vendor=IBM", "service.ranking:Integer=0", "com.ibm.ws.ui.persistence.provider=FILE" })
public class FilePersistenceProvider implements IPersistenceProvider {
private static final TraceComponent tc = Tr.register(FilePersistenceProvider.class);
static final String DEFAULT_PERSIST_LOCATION = "${server.output.dir}/resources/adminCenter-1.0/";
static final String KEY_LOCATION_SERVICE = "locationServiceRef";
private final AtomicServiceReference<WsLocationAdmin> locationServiceRef = new AtomicServiceReference<WsLocationAdmin>(KEY_LOCATION_SERVICE);
protected static final String KEY_JSON_SERVICE = "jsonService";
private final Map<String, File> fileLockObjs = new HashMap<String, File>();
// For unittest
private JSON json;
private final IPersistenceDebugger fDebug;
private ComponentContext cc = null;
/**
* Zero-argument constructor for use by DS.
*/
public FilePersistenceProvider() {
fDebug = new FilePersistenceDebugger();
}
/**
* Unit test constructor. Allows for override of the JSON API object and
* IFileDebugger.
*
* @param json
* @param fDebug
*/
FilePersistenceProvider(final JSON json, final IPersistenceDebugger fDebug) {
this.json = json;
this.fDebug = fDebug;
}
@Reference(service = WsLocationAdmin.class, name = KEY_LOCATION_SERVICE,
policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
protected void setLocationService(final ServiceReference<WsLocationAdmin> reference) {
locationServiceRef.setReference(reference);
}
protected void unsetLocationService(final ServiceReference<WsLocationAdmin> reference) {
locationServiceRef.unsetReference(reference);
}
protected JSON getJSONService() throws JSONMarshallException {
// This is an internal JSON object for unittest only
// not used in real product situations
if (json != null) {
return json;
}
JSON jsonService = JSONFactory.newInstance();
return jsonService;
}
@Activate
protected void activate(final ComponentContext context) {
cc = context;
locationServiceRef.activate(cc);
}
@Deactivate
protected void deactivate() {
cc = null;
locationServiceRef.deactivate(cc);
}
/**
* This method should not be called directly by any method except getPersistenceFileLockObj().
* <p>
* This method will create the File object.
*
* @param name The persistence name associated with the File
* @return The File object which represents the underlying persistent file.
* @throws IOException If the parent directories could not be created (if necessary)
* @see {@link #getPersistenceFileLockObj(String)}
*/
@Trivial
private File getPersistenceFile(final String name) {
final WsResource persistenceFile = locationServiceRef.getServiceWithException().resolveResource(DEFAULT_PERSIST_LOCATION + name + ".json");
final File file = persistenceFile.asFile();
if (tc.isEventEnabled()) {
Tr.event(tc, "Persistence for [" + name + "] file: " + file.getAbsolutePath());
}
return file;
}
/**
* Gets the persistence file for the given resource. This File object
* is also the 'lock object' used to synchronize reads and writes to
* a particular storage name.
* <p>
* Access to this method is synchronized. Callers should use the returned
* object for local synchronization.
*
* @param name The persistence name associated with the File 'lock object'
* @return The associated File 'lock object'
* @throws IOException If the File 'lock object' could not be properly created because the parent directories could not be created.
*/
@Trivial
private synchronized File getPersistenceFileLockObj(final String name) {
File lockObj = fileLockObjs.get(name);
if (lockObj == null) {
lockObj = getPersistenceFile(name);
fileLockObjs.put(name, lockObj);
}
return lockObj;
}
/**
* Attempts to create the parent files, if needed. If the parent
* directories do not exist they will be created. If they can not be
* created, an exception will be thrown to the caller. Parent directories
* are NOT created by the Rest handler JSON API so we must ensure they are created here.
*
* @param file The File whose parents should be created, if needed.
* @throws IOException If the parent directories could not be created.
*/
@Trivial
private void createParentIfNeeded(final File file) throws IOException {
final File parentDir = file.getParentFile();
if (!FileUtils.fileExists(parentDir)) {
if (tc.isEventEnabled()) {
Tr.event(tc, "Parent directories do not exist. Attempting creation.");
}
if (!FileUtils.fileMkDirs(parentDir)) {
throw new IOException("Unable to create required parent directories for " + file.getAbsolutePath());
}
}
}
/** {@inheritDoc} */
@Override
public void store(final String name, final Object pojo) throws JSONMarshallException, IOException {
final File file = getPersistenceFileLockObj(name);
try {
synchronized (file) {
createParentIfNeeded(file);
JSON jsonService = getJSONService();
jsonService.serializeToFile(file, pojo);
}
} catch (JSONMarshallException e) {
// This should NEVER, ever happen
if (tc.isEventEnabled()) {
Tr.event(tc, "Unexpected JSONMarshallException caught while storing data to the file. THIS SHOULD NEVER HAPPEN.", e);
}
String msg = e.getMessage();
if (msg != null && msg.equals("I/O exception of some sort has occurred")) {
Tr.error(tc, "FILE_PERSISTENCE_STORE_IO_ERROR", file.getAbsolutePath(), e.getMessage());
}
throw e;
} catch (IOException e) {
Tr.error(tc, "FILE_PERSISTENCE_STORE_IO_ERROR", file.getAbsolutePath(), e.getMessage());
throw e;
}
}
/** {@inheritDoc} */
@Override
public void storePlainText(final String name, final String content) throws JSONMarshallException, IOException {
final File file = getPersistenceFileLockObj(name);
try {
synchronized (file) {
createParentIfNeeded(file);
org.apache.commons.io.FileUtils.writeStringToFile(file, content, "UTF-8");
}
} catch (IOException e) {
Tr.error(tc, "FILE_PERSISTENCE_STORE_IO_ERROR", file.getAbsolutePath(), e.getMessage());
throw e;
}
}
/**
* {@inheritDoc} <p>
* FileNotFound exception is expected. Do not do anything in that flow.
* Otherwise, catch each other type of problem and log a meaningful error
* message so the customer could possibly try to resolve it. This will also
* cause an FFDC to get generated, which is what we want as these situations
* should <b>never</b> happen in normal flows.
*/
@FFDCIgnore(FileNotFoundException.class)
@Override
public <T> T load(final String name, final Class<T> clazz) throws JSONMarshallException, IOException {
final File file = getPersistenceFileLockObj(name);
try {
synchronized (file) {
JSON jsonService = getJSONService();
return jsonService.parse(file, clazz);
}
} catch (JSONMarshallException e) {
String msg = e.getMessage();
if (msg != null && msg.equals("Unable to parse non-well-formed content")) {
Tr.error(tc, "FILE_PERSISTENCE_BAD_JSON", file.getAbsolutePath(), fDebug.getFileContents(file));
} else if (msg != null && msg.equals("Fatal problems occurred while mapping content")) {
Tr.error(tc, "FILE_PERSISTENCE_WRONG_CLASS", file.getAbsolutePath(), clazz.getCanonicalName(), fDebug.getFileContents(file));
}
throw e;
} catch (FileNotFoundException e) {
// This is an expected error flow, do not FFDC or log anything
throw e;
} catch (IOException e) {
Tr.error(tc, "FILE_PERSISTENCE_IO_ERROR", file.getAbsolutePath(), e.getMessage());
throw e;
}
}
/**
* {@inheritDoc} <p>
* FileNotFound exception is expected. Do not do anything in that flow.
* Otherwise, catch each other type of problem and log a meaningful error
* message so the customer could possibly try to resolve it. This will also
* cause an FFDC to get generated, which is what we want as these situations
* should <b>never</b> happen in normal flows.
*/
@FFDCIgnore(FileNotFoundException.class)
@Override
public String loadPlainText(final String name) throws FileNotFoundException, IOException {
final File file = getPersistenceFileLockObj(name);
try {
synchronized (file) {
return org.apache.commons.io.FileUtils.readFileToString(file, "UTF-8");
}
} catch (FileNotFoundException e) {
// This is an expected error flow, do not FFDC or log anything
throw e;
} catch (NoSuchFileException e) {
// This is an expected error flow, do not FFDC or log anything
throw e;
} catch (IOException e) {
Tr.error(tc, "FILE_PERSISTENCE_IO_ERROR", file.getAbsolutePath(), e.getMessage());
throw e;
}
}
/** {@inheritDoc} */
@Override
public long getLastModified(final String name) {
final File file = getPersistenceFileLockObj(name);
synchronized (file) {
return FileUtils.fileLastModified(file);
}
}
/** {@inheritDoc} */
@Override
public boolean delete(String name) throws IOException {
final File file = getPersistenceFileLockObj(name);
synchronized (file) {
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
boolean deleted = file.delete();
File parent = file.getParentFile();
String[] sa = parent.list();
if (sa.length == 0) {
if (parent.delete() == false) {
Tr.warning(tc, "TOOLDATA_PARENT_DIR_DELETE_RESULT_FALSE", parent.getAbsolutePath());
}
}
return deleted;
}
});
}
}
/** {@inheritDoc} */
@Override
public boolean exists(String name) {
final File file = getPersistenceFileLockObj(name);
synchronized (file) {
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return file.exists();
}
});
}
}
}