Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error reporting in case of failing Application Startup #2297

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -63,6 +64,7 @@
import org.eclipse.tycho.core.utils.TychoProjectUtils;
import org.eclipse.tycho.helper.PluginRealmHelper;
import org.eclipse.tycho.model.project.EclipseProject;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
Expand Down Expand Up @@ -155,7 +157,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
setupLogging(systemBundleContext);
try {
workspace.install(systemBundleContext);
} catch (IOException e) {
} catch (IOException | BundleException e) {
throw new MojoFailureException("Install API workspace failed!", e);
}
FrameworkWiring wiring = framework.adapt(FrameworkWiring.class);
Expand All @@ -166,34 +168,56 @@ public void execute() throws MojoExecutionException, MojoFailureException {
} catch (BundleException e) {
throw new MojoExecutionException("Start framework failed!", e);
}
getLog().debug("Framework started (took " + time(startFw) + ").");
EclipseAppLauncher appLauncher = new EclipseAppLauncher(systemBundleContext, false, true, null,
configuration);
systemBundleContext.registerService(ApplicationLauncher.class, appLauncher, null);
try {
getLog().debug("Framework started (took " + time(startFw) + ").");
EclipseAppLauncher appLauncher = new EclipseAppLauncher(systemBundleContext, false, true, null,
configuration);
systemBundleContext.registerService(ApplicationLauncher.class, appLauncher, null);
Object returnValue = appLauncher.start(null);
if (returnValue instanceof Integer retCode) {
if (retCode != 0) {
throw new MojoFailureException("API Tools failure!");
}
} else {
throw new MojoFailureException("Execute the Api application failed!");
throw applicationStartupError(systemBundleContext, null);
}
} catch (Exception e) {
throw new MojoFailureException("Execute the Api application failed!", e);
}
try {
framework.stop();
framework.waitForStop(0);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (BundleException e) {
// not interesting...
throw applicationStartupError(systemBundleContext, e);
} finally {
try {
framework.stop();
framework.waitForStop(0);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (BundleException e) {
// not interesting...
}
}
getLog().info("API Analysis finished in " + time(start) + ".");
}
}

private MojoExecutionException applicationStartupError(BundleContext systemBundleContext, Exception e) {
String bundleState = Arrays.stream(systemBundleContext.getBundles())
.map(b -> toBundleState(b.getState()) + " | " + b.getSymbolicName())
.collect(Collectors.joining(System.lineSeparator()));
getLog().error(String.format(
"Internal error execute the Api Application for project %s, the current framework state is:\r\n%s",
project.getId(), bundleState), e);
return new MojoExecutionException("Execute the Api application failed!", e);
}

private static String toBundleState(int state) {
return switch (state) {
case Bundle.ACTIVE -> "ACTIVE ";
case Bundle.INSTALLED -> "INSTALLED";
case Bundle.RESOLVED -> "RESOLVED ";
case Bundle.STARTING -> "STARTING ";
case Bundle.STOPPING -> "STOPPING ";
default -> String.valueOf(state);
};
}

private MavenRepositoryLocation getRepository() {
if (apiToolsRepository == null) {
return new MavenRepositoryLocation(null, URI.create(REPO_DEFAULT));
Expand Down Expand Up @@ -308,9 +332,8 @@ private Path createTargetFile() throws MojoExecutionException, MojoFailureExcept
baselineBundles = resolver.getApiBaselineBundles(baselines.stream()
.map(repo -> new MavenRepositoryLocation(repo.getId(), URI.create(repo.getUrl()))).toList(),
artifactKey.get());
getLog().debug(
"API baseline contains " + baselineBundles.size() + " bundles (resolve takes " + time(start)
+ ").");
getLog().debug("API baseline contains " + baselineBundles.size() + " bundles (resolve takes " + time(start)
+ ").");
} catch (IllegalArtifactReferenceException e) {
throw new MojoFailureException("Project specify an invalid artifact key", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,18 @@ public Path getWorkDir() {
return workDir;
}

public void install(BundleContext bundleContext) throws IOException {
public void install(BundleContext bundleContext) throws IOException, BundleException {
if (init.add(workDir)) {
for (Path bundleFile : applicationResolver.getApiApplicationBundles(apiToolsRepo)) {
Bundle bundle;
try (InputStream stream = Files.newInputStream(bundleFile)) {
Bundle bundle = bundleContext.installBundle(bundleFile.toUri().toString(), stream);
if (START_BUNDLES.contains(bundle.getSymbolicName())) {
bundle.start();
}
bundle = bundleContext.installBundle(bundleFile.toUri().toString(), stream);
} catch (BundleException e) {
logger.warn("Can't install " + bundleFile + ": " + e);
continue;
}
if (START_BUNDLES.contains(bundle.getSymbolicName())) {
bundle.start();
}
}
}
Expand Down