Skip to content

Commit

Permalink
bndpomrepo: Fix initialization problems and move poll work
Browse files Browse the repository at this point in the history
We fix the initialization issues in BndPomRepository. A memoizer is
used to make sure there is only one call to the preparation code
and the memoized value is the promise of the eventual completion
of preparation. So both prepare and init will ensure that preparation
has commenced and init will then wait for it to complete.

We also move the poll method call to the normal executor.

Signed-off-by: BJ Hargrave <[email protected]>
  • Loading branch information
bjhargrave committed Feb 22, 2022
1 parent 6d0034b commit 2d151b9
Showing 1 changed file with 51 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import aQute.bnd.build.Workspace;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.http.HttpClient;
import aQute.bnd.memoize.Memoize;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.repository.BaseRepository;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class BndPomRepository extends BaseRepository
private static final String MAVEN_REPO_LOCAL = System.getProperty("maven.repo.local", "~/.m2/repository");
private static final int DEFAULT_POLL_TIME = 300;

private Promise<Boolean> initialized;
private final Memoize<Promise<Boolean>> prepared;

private PomConfiguration configuration;
private Registry registry;
Expand All @@ -80,33 +81,45 @@ public class BndPomRepository extends BaseRepository

private String status;

@SuppressWarnings("deprecation")
public BndPomRepository() {
prepared = Memoize.supplier(this::preparePromise);
}

private boolean init() {
try {
if (initialized == null) {
prepare();
}
return initialized.getValue();
return prepared.get() // start preparation
.getValue(); // wait for completion
} catch (Exception e) {
throw Exceptions.duck(e);
}
}

@SuppressWarnings("deprecation")
@Override
public void prepare() throws Exception {
public void prepare() {
prepared.get(); // start preparation
}

private Promise<Boolean> preparePromise() {
if (configuration.name() == null) {
status("Must get a name");
}

this.name = configuration.name();

if (configuration.snapshotUrl() != null && configuration.snapshotUrls() != null) {
status("snapshotUrl and snapshotUrls property is set. Please only use snapshotUrl.");
if (configuration.snapshotUrl() != null) {
@SuppressWarnings("deprecation")
String snapshotUrls = configuration.snapshotUrls();
if (snapshotUrls != null) {
status("snapshotUrl and snapshotUrls property is set. Please only use snapshotUrl.");
}
}

if (configuration.releaseUrl() != null && configuration.releaseUrls() != null) {
status("releaseUrl and releaseUrls property is set. Please only use releaseUrl.");
if (configuration.releaseUrl() != null) {
@SuppressWarnings("deprecation")
String releaseUrls = configuration.releaseUrls();
if (releaseUrls != null) {
status("releaseUrl and releaseUrls property is set. Please only use releaseUrl.");
}
}

if (configuration.pom() != null) {
Expand Down Expand Up @@ -136,12 +149,11 @@ public void prepare() throws Exception {
status("Neither pom, archive nor query property are set");
}

initialized = Processor.getPromiseFactory()
.submit(this::internalInitialize);
return Processor.getPromiseFactory()
.submit(this::prepareAsync);
}

@SuppressWarnings("deprecation")
private boolean internalInitialize() {
private boolean prepareAsync() {
if (!isOk()) {
return false;
}
Expand All @@ -151,10 +163,18 @@ private boolean internalInitialize() {
localRepo = IO.getFile(configuration.local(MAVEN_REPO_LOCAL));
File location = workspace.getFile(getLocation());

String releaseUrl = configuration.releaseUrl() != null ? configuration.releaseUrl()
: configuration.releaseUrls();
String snapshotUrl = configuration.snapshotUrl() != null ? configuration.snapshotUrl()
: configuration.snapshotUrls();
String releaseUrl = configuration.releaseUrl();
if (releaseUrl == null) {
@SuppressWarnings("deprecation")
String releaseUrls = configuration.releaseUrls();
releaseUrl = releaseUrls;
}
String snapshotUrl = configuration.snapshotUrl();
if (snapshotUrl == null) {
@SuppressWarnings("deprecation")
String snapshotUrls = configuration.snapshotUrls();
snapshotUrl = snapshotUrls;
}

List<MavenBackingRepository> release = MavenBackingRepository.create(releaseUrl, reporter, localRepo,
client);
Expand Down Expand Up @@ -206,15 +226,19 @@ private void startPoll() {
AtomicBoolean inPoll = new AtomicBoolean();
pomPoller = Processor.getScheduledExecutor()
.scheduleAtFixedRate(() -> {
if (inPoll.getAndSet(true))
if (inPoll.getAndSet(true)) {
return;
try {
poll();
} catch (Exception e) {
reporter.exception(e, "Error when polling for %s for change", this);
} finally {
inPoll.set(false);
}
Processor.getExecutor()
.execute(() -> {
try {
poll();
} catch (Exception e) {
reporter.exception(e, "Error when polling for %s for change", this);
} finally {
inPoll.set(false);
}
});
}, polltime, polltime, TimeUnit.SECONDS);
}
}
Expand Down

0 comments on commit 2d151b9

Please sign in to comment.