protected BulletinMirroringInformation getNextItemToRetrieve() { try { while (itemsToRetrieve.size() == 0) { String nextAccountId = getNextAccountToRetrieve(); if (nextAccountId == null) return null; int totalIdsReturned = 0; String mirroringCallUsed = "listAvailableIdsForMirroring"; NetworkResponse response = gateway.listAvailableIdsForMirroring(getSecurity(), nextAccountId); if (networkResponseOk(response)) { Vector listwithBulletinMirroringInfo = response.getResultVector(); totalIdsReturned = listwithBulletinMirroringInfo.size(); itemsToRetrieve = listOnlyPacketsThatWeWantUsingBulletinMirroringInformation( nextAccountId, listwithBulletinMirroringInfo); } else { mirroringCallUsed = "OLD MIRRORING CALL(listBulletinsForMirroring)"; response = gateway.listBulletinsForMirroring(getSecurity(), nextAccountId); if (networkResponseOk(response)) { Vector listWithLocalIds = response.getResultVector(); totalIdsReturned = listWithLocalIds.size(); itemsToRetrieve = listOnlyPacketsThatWeWantUsingLocalIds(nextAccountId, listWithLocalIds); } } if (networkResponseOk(response)) { String publicCode = MartusCrypto.getFormattedPublicCode(nextAccountId); if (totalIdsReturned > 0 || itemsToRetrieve.size() > 0) logInfo( mirroringCallUsed + ": " + publicCode + " -> " + totalIdsReturned + " -> " + itemsToRetrieve.size()); } else { logWarning( "MirroringRetriever.getNextItemToRetrieve: Returned NetworkResponse: " + response.getResultCode()); } } if (itemsToRetrieve.size() == 0) return null; return (BulletinMirroringInformation) itemsToRetrieve.remove(0); } catch (Exception e) { logError("MirroringRetriever.getNextUidToRetrieve: ", e); MartusLogger.logException(e); return null; } }
public void processNextBulletin() { if (isSleeping()) return; BulletinMirroringInformation item = getNextItemToRetrieve(); if (item == null) { scheduleSleep(); return; } // TODO handle delete requests when we are propagating deletes. try { UniversalId uid = item.getUid(); String publicCode = MartusCrypto.getFormattedPublicCode(uid.getAccountId()); logNotice("Getting bulletin: " + publicCode + "->" + uid.getLocalId()); String bur = retrieveBurFromMirror(uid); File zip = File.createTempFile("$$$MirroringRetriever", null); try { zip.deleteOnExit(); retrieveOneBulletin(zip, uid); long zipSize = zip.length(); long mTime = item.getmTime(); BulletinHeaderPacket bhp = store.saveZipFileToDatabase(zip, uid.getAccountId(), mTime); store.writeBur(bhp, bur); store.deleteDel(bhp.getUniversalId()); logNotice( "Stored bulletin: " + publicCode + "->" + uid.getLocalId() + " Size: " + zipSize); } finally { zip.delete(); } } catch (ServerErrorException e) { logError("Supplier server:", e); } catch (ServerNotAvailableException e) { // TODO: Notify once per hour that something is wrong } catch (Exception e) { logError(e); } }