blob: cdf1be9a063993c82e30786f4d3330d29885c43c [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2016, 2020 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.internal.jcommons.rmi.eruntime;
import static org.eclipse.statet.internal.jcommons.rmi.CommonsRmiInternals.BUNDLE_ID;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.osgi.framework.Bundle;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.RegistryFactory;
import org.eclipse.core.runtime.Status;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.runtime.UriUtils;
@NonNullByDefault
public class ERuntimeContributor {
private static final String EXTENSIONPOINT_ID= "org.eclipse.statet.jcommons.rmi.ERegistry"; //$NON-NLS-1$
private static void log(final IStatus status) {
Platform.getLog(Platform.getBundle(BUNDLE_ID)).log(status);
}
public ERuntimeContributor() {
}
public List<URI> addCodebaseEntries(final List<URI> entries) {
final List<IStatus> statusList= new ArrayList<>();
final List<String> pluginIds= new ArrayList<>();
collectPluginIds(EXTENSIONPOINT_ID, "codebaseEntry", pluginIds); //$NON-NLS-1$
for (final String pluginId : pluginIds) {
final Bundle pluginBundle= Platform.getBundle(pluginId);
if (pluginBundle != null) {
addCodebasePath(pluginBundle, entries, statusList);
final Bundle[] fragments= Platform.getFragments(pluginBundle);
if (fragments != null) {
for (final Bundle fragmentBundle : fragments) {
addCodebasePath(fragmentBundle, entries, statusList);
}
}
}
}
if (!statusList.isEmpty()) {
log(new MultiStatus(BUNDLE_ID, 0,
statusList.toArray(new IStatus[statusList.size()]),
"An error occurred when looking up RJ libraries. This may cause problems starting/running RJ.",
null ));
}
return entries;
}
private void collectPluginIds(final String extensionPointId, final String entryId,
final List<String> pluginIds) {
final IConfigurationElement[] elements= RegistryFactory.getRegistry()
.getConfigurationElementsFor(extensionPointId);
for (final IConfigurationElement element : elements) {
if (element.getName().equals(entryId)) {
final String pluginId= element.getAttribute("pluginId"); //$NON-NLS-1$
if (pluginId != null && pluginId.length() > 0
&& !pluginIds.contains(pluginId)) {
pluginIds.add(pluginId);
}
}
}
}
private void addCodebasePath(final Bundle bundle, final List<URI> entries,
final List<IStatus> statusList) {
// see also ERJContext
try {
URI url= UriUtils.toUri(FileLocator.resolve(bundle.getEntry("/"))); //$NON-NLS-1$
if (UriUtils.isJarUrl(url) && UriUtils.getJarEntryPath(url).isEmpty()) {
url= UriUtils.getJarFileUrl(url);
}
if (Platform.inDevelopmentMode() && url.getPath().endsWith("/")) { //$NON-NLS-1$
final Path path= Paths.get(url);
final Path binPath;
if (Files.isDirectory(binPath= path.resolve("target/classes"))) {
// final URI binUrl= binPath.toUri();
final URI binUrl= new URI(url.toString() + "target/classes/"); //$NON-NLS-1$
if (!entries.contains(binUrl)) {
entries.add(binUrl);
}
}
}
if (!entries.contains(url)) {
entries.add(url);
}
}
catch (final Exception e) {
statusList.add(new Status(IStatus.WARNING, BUNDLE_ID,
String.format("Failed to check location for plug-in: '%1$s'.", //$NON-NLS-1$
bundle.getSymbolicName() ),
e ));
}
}
}