public boolean synchronizeContentProvider(int contentSourceId) throws Exception {
ContentSourceManagerLocal contentSourceManager = LookupUtil.getContentSourceManager();
ContentProvider provider = getIsolatedContentProvider(contentSourceId);
ContentSourceSyncResults results = null;
SubjectManagerLocal subjMgr = LookupUtil.getSubjectManager();
Subject overlord = subjMgr.getOverlord();
// append to this as we go along, building a status report
StringBuilder progress = new StringBuilder();
try {
ContentSource contentSource = contentSourceManager.getContentSource(overlord, contentSourceId);
if (contentSource == null) {
throw new Exception("Cannot sync a non-existing content source [" + contentSourceId + "]");
}
// This should not take very long so it should be OK to block other
// callers.
// We are avoiding the problem that would occur if we try to
// synchronize the same source
// at the same time. We could do it more cleverly by synchronizing
// on a per content source
// basis, but I don't see a need right now to make this more
// complicated.
// We can come back and revisit if we need more fine-grained
// locking.
synchronized (synchronizeContentSourceLock) {
progress.append(new Date()).append(": ");
progress.append("Start synchronization of content source [").append(contentSource.getName()).append(
"]\n");
progress.append(new Date()).append(": ");
progress.append("Getting currently known list of packages...\n");
results = new ContentSourceSyncResults(contentSource);
results.setResults(progress.toString());
results = contentSourceManager.persistContentSourceSyncResults(results);
}
if (results == null) {
// note that it technically is still possible to have concurrent
// syncs - if two
// threads running in two different servers (i.e. different VMs)
// both try to sync the
// same content source and both enter the
// persistContentSourceSyncResults method at
// the same time, you'll get two inprogress rows - this is so
// rare as to not care.
// Even if it does happen, it may still work, or
// one sync will get an error and rollback its tx and no harm
// will be done.
log.info("Content source [" + contentSource.getName()
+ "] is already being synchronized - this sync request will be ignored.");
return false;
}
RepoSourceSynchronizer repoSourceSynchronizer = new RepoSourceSynchronizer(contentSource, provider);
repoSourceSynchronizer.synchronizeCandidateRepos(progress);
results.setStatus(ContentSyncStatus.SUCCESS);
results.setResults(progress.toString());
} catch (Throwable t) {
if (results != null) {
// try to reload the results in case it was updated by the SLSB
// before the
// exception happened
ContentSourceSyncResults reloadedResults = contentSourceManager.getContentSourceSyncResults(results
.getId());
if (reloadedResults != null) {
results = reloadedResults;
if (results.getResults() != null) {
progress = new StringBuilder(results.getResults());