1
|
package eu.dnetlib.enabling.is.sn;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileOutputStream;
|
5
|
import java.io.FileWriter;
|
6
|
import java.io.IOException;
|
7
|
import java.text.SimpleDateFormat;
|
8
|
import java.util.Collection;
|
9
|
import java.util.Date;
|
10
|
import java.util.HashMap;
|
11
|
import java.util.Map;
|
12
|
import java.util.zip.ZipEntry;
|
13
|
import java.util.zip.ZipOutputStream;
|
14
|
|
15
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
16
|
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.springframework.beans.factory.annotation.Required;
|
20
|
|
21
|
import com.google.gson.Gson;
|
22
|
import com.google.gson.reflect.TypeToken;
|
23
|
|
24
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscription;
|
25
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscriptionRegistry;
|
26
|
import eu.dnetlib.enabling.is.sn.rmi.ISSNException;
|
27
|
import eu.dnetlib.enabling.is.sn.rmi.SubscriptionRequestRejectedException;
|
28
|
import eu.dnetlib.enabling.tools.UniqueIdentifierGenerator;
|
29
|
import eu.dnetlib.enabling.tools.UniqueIdentifierGeneratorImpl;
|
30
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
31
|
|
32
|
/**
|
33
|
* Core business logic of ISSN service, decouple from compatibility RMI interface.
|
34
|
*
|
35
|
* @author marko
|
36
|
*
|
37
|
*/
|
38
|
public class ISSNServiceCore {
|
39
|
|
40
|
/**
|
41
|
* logger.
|
42
|
*/
|
43
|
private static final Log log = LogFactory.getLog(ISSNServiceCore.class); // NOPMD by marko on 11/24/08 5:02 PM
|
44
|
|
45
|
/**
|
46
|
* subscription registries to lookup.
|
47
|
*/
|
48
|
private SubscriptionRegistry registry;
|
49
|
|
50
|
/**
|
51
|
* generates subscription identifiers.
|
52
|
*/
|
53
|
private UniqueIdentifierGenerator uuidGenerator = new UniqueIdentifierGeneratorImpl("sn");
|
54
|
|
55
|
/**
|
56
|
* Directory in which backups are saved.
|
57
|
*/
|
58
|
private String backupDir;
|
59
|
|
60
|
/**
|
61
|
* subscribe a consumer to topics matching the topic expression.
|
62
|
*
|
63
|
* @param consumerReference
|
64
|
* EPR to the service to be notified
|
65
|
* @param topicExpression
|
66
|
* topic expression
|
67
|
* @param ttl
|
68
|
* time to live in seconds
|
69
|
* @return subscription identifier
|
70
|
*/
|
71
|
public String subscribe(final W3CEndpointReference consumerReference, final String topicExpression, final int ttl) throws SubscriptionRequestRejectedException {
|
72
|
log.debug("subscribing");
|
73
|
final SubscriptionRequest srq = new SubscriptionRequest(uuidGenerator.generateIdentifier(), consumerReference, topicExpression, ttl); // NOPMD
|
74
|
|
75
|
final String subId = getRegistry().registerSubscription(srq);
|
76
|
if (subId != null) {
|
77
|
return subId;
|
78
|
}
|
79
|
return null;
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Unsubscribe a subscription, by id.
|
84
|
*
|
85
|
* @param subscrId subscription id
|
86
|
* @return true if unsubscribed
|
87
|
*/
|
88
|
public boolean unsubscribe(final String subscrId) {
|
89
|
return getRegistry().unsubscribe(subscrId);
|
90
|
}
|
91
|
|
92
|
public String backup() throws ISSNException {
|
93
|
log.info("Starting backup...");
|
94
|
try {
|
95
|
|
96
|
verifyBackupDir();
|
97
|
|
98
|
String seq = (new SimpleDateFormat("yyyyMMdd-HHmm")).format(new Date());
|
99
|
|
100
|
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(backupDir + "/data-" + seq + ".zip"));
|
101
|
|
102
|
FileWriter logFile = new FileWriter(backupDir + "/report-" + seq + ".log");
|
103
|
logFile.write("Backup started at: " + DateUtils.now_ISO8601() + "\n\n");
|
104
|
|
105
|
backup(zip, logFile);
|
106
|
|
107
|
logFile.write("\nBackup finished at: " + DateUtils.now_ISO8601() + "\n");
|
108
|
|
109
|
logFile.flush();
|
110
|
logFile.close();
|
111
|
|
112
|
zip.flush();
|
113
|
zip.close();
|
114
|
|
115
|
log.info("Backup finished");
|
116
|
return backupDir;
|
117
|
} catch (final Exception e) {
|
118
|
log.error("Backup failed", e);
|
119
|
throw new ISSNException("cannot backup", e);
|
120
|
}
|
121
|
}
|
122
|
|
123
|
private void backup(ZipOutputStream zip, FileWriter logFile) throws IOException {
|
124
|
|
125
|
for (ResourceStateSubscription sub : getRegistry().listSubscriptions()) {
|
126
|
|
127
|
logFile.write("SUBSCRIPTION: " + sub.getSubscriptionId() + "\n");
|
128
|
log.info("Backup of subscription: " + sub.getSubscriptionId());
|
129
|
|
130
|
final Map<String, String> attrs = new HashMap<String, String>(); // NOPMD
|
131
|
attrs.put("prefix", sub.getPrefix());
|
132
|
attrs.put("type", sub.getType());
|
133
|
attrs.put("resourceId", sub.getResourceId());
|
134
|
attrs.put("xpath", sub.getXpath());
|
135
|
attrs.put("id", sub.getSubscriptionId());
|
136
|
attrs.put("subscriber", sub.getSubscriber().toString());
|
137
|
|
138
|
zip.putNextEntry(new ZipEntry(sub.getSubscriptionId()));
|
139
|
zip.write(encodeJSon(attrs).getBytes());
|
140
|
zip.closeEntry();
|
141
|
}
|
142
|
}
|
143
|
|
144
|
private String encodeJSon(final Map<String, String> map) {
|
145
|
return new Gson().toJson(
|
146
|
map,
|
147
|
new TypeToken<Map<String, String>>() {}.getType()
|
148
|
);
|
149
|
}
|
150
|
|
151
|
private void verifyBackupDir() {
|
152
|
File d = new File(backupDir);
|
153
|
if (!d.exists()) d.mkdirs();
|
154
|
}
|
155
|
|
156
|
public UniqueIdentifierGenerator getUuidGenerator() {
|
157
|
return uuidGenerator;
|
158
|
}
|
159
|
|
160
|
public void setUuidGenerator(final UniqueIdentifierGenerator uuidGenerator) {
|
161
|
this.uuidGenerator = uuidGenerator;
|
162
|
}
|
163
|
|
164
|
public String getBackupDir() {
|
165
|
return backupDir;
|
166
|
}
|
167
|
|
168
|
@Required
|
169
|
public void setBackupDir(String backupDir) {
|
170
|
this.backupDir = backupDir;
|
171
|
}
|
172
|
|
173
|
public SubscriptionRegistry getRegistry() {
|
174
|
return registry;
|
175
|
}
|
176
|
|
177
|
public void setRegistry(final SubscriptionRegistry registry) {
|
178
|
this.registry = registry;
|
179
|
}
|
180
|
}
|