This repository has been archived by the owner on Nov 3, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ReadWriteJsonServiceRegistryDao.java
105 lines (89 loc) · 4.01 KB
/
ReadWriteJsonServiceRegistryDao.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
package net.unicon.cas.addons.serviceregistry;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.apache.commons.io.IOUtils;
import org.jasig.cas.services.AbstractRegisteredService;
import org.jasig.cas.services.RegisteredService;
import org.springframework.core.io.Resource;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* An extension of the JsonServiceRegistryDao that is able to support both read/write operations when
* saving or deleting registered services.
* <p/>
* Note: This implementation is NOT transactional nor is it thread-safe. No such implementation can be possible on top
* of an ordinary file system.
*
* @author Misagh Moayyed
* @author Unicon, inc.
* @since 1.6
*/
public final class ReadWriteJsonServiceRegistryDao extends JsonServiceRegistryDao {
public ReadWriteJsonServiceRegistryDao(final Resource servicesConfigFile) {
super(servicesConfigFile);
this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
}
@Override
protected RegisteredService saveInternal(final RegisteredService registeredService) {
logger.debug("Loading service definitions from resource [{}]", this.servicesConfigFile.getFilename());
final List<RegisteredService> resolvedServices = super.loadServices();
final List<RegisteredService> col = new ArrayList<RegisteredService>(resolvedServices);
if (registeredService.getId() < 0) {
if (registeredService instanceof AbstractRegisteredService) {
final Random random = new Random(registeredService.hashCode());
final int serviceId = random.nextInt(Integer.MAX_VALUE);
((AbstractRegisteredService) registeredService).setId(serviceId);
}
}
boolean foundAndRemovedService = false;
final Iterator<RegisteredService> it = col.iterator();
while(!foundAndRemovedService && it.hasNext()) {
if (it.next().getId() == registeredService.getId()) {
it.remove();
foundAndRemovedService = true;
}
}
col.add(registeredService);
saveListOfRegisteredServices(col);
return registeredService;
}
@Override
protected boolean deleteInternal(final RegisteredService registeredService) {
logger.debug("Loading service definitions from resource [{}]", this.servicesConfigFile.getFilename());
final List<RegisteredService> resolvedServices = super.loadServices();
final RegisteredService regServiceToDelete = findServiceById(registeredService.getId());
if (regServiceToDelete != null) {
logger.debug("Found service definition to remove: [{}]", regServiceToDelete);
final List<RegisteredService> col = new ArrayList<RegisteredService>(resolvedServices);
col.remove(regServiceToDelete);
saveListOfRegisteredServices(col);
return true;
}
return false;
}
private void saveListOfRegisteredServices(final List<RegisteredService> col) {
OutputStream out = null;
FileOutputStream fout = null;
try {
fout = new FileOutputStream(this.servicesConfigFile.getFile());
out = new BufferedOutputStream(fout);
final Map<String, Object> map = new LinkedHashMap<String, Object>(col.size());
map.put(SERVICES_KEY, col);
logger.debug("Writing [{}] service definitions to resource [{}]", col.size(), this.servicesConfigFile.getFilename());
this.objectMapper.writerWithDefaultPrettyPrinter().writeValue(out, map);
fout.flush();
out.flush();
} catch (final Exception e) {
logger.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(fout);
IOUtils.closeQuietly(out);
}
}
}