public void run() {
try {
URL host = m_discovery.discover();
ServiceReference[] refs = m_context.getServiceReferences(RepositoryReplication.class.getName(), null);
for (ServiceReference ref : refs) {
RepositoryReplication repository = (RepositoryReplication) m_context.getService(ref);
SortedRangeSet localRange = repository.getRange();
Object customer = ref.getProperty("customer");
Object name = ref.getProperty("name");
String filter = "customer=" + customer + "&name=" + name;
URL query = new URL(host, "/replication/query?" + filter);
HttpURLConnection connection = (HttpURLConnection) query.openConnection();
if (connection.getResponseCode() == HttpServletResponse.SC_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
try {
String line = reader.readLine();
int i = line.lastIndexOf(',');
if (i > 0) {
SortedRangeSet remoteRange = new SortedRangeSet(line.substring(i + 1));
SortedRangeSet delta = localRange.diffDest(remoteRange);
RangeIterator iterator = delta.iterator();
while (iterator.hasNext()) {
long version = iterator.next();
URL get = new URL(host, "/replication/get?" + filter + "&version=" + version);
HttpURLConnection connection2 = (HttpURLConnection) get.openConnection();
repository.put(connection2.getInputStream(), version);
}
}
}
catch (Exception e) {
m_log.log(LogService.LOG_WARNING, "Error parsing remote range", e);