| /*=============================================================================# |
| # Copyright (c) 2009, 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.rj.server.eruntime; |
| |
| import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert; |
| |
| import static org.eclipse.statet.rj.server.util.ServerUtils.RJ_SERVER_ID; |
| |
| import java.io.IOException; |
| import java.net.URI; |
| import java.net.URL; |
| import java.nio.file.Files; |
| import java.nio.file.Path; |
| import java.nio.file.Paths; |
| import java.util.Set; |
| |
| import org.osgi.framework.Bundle; |
| |
| import org.eclipse.core.runtime.FileLocator; |
| import org.eclipse.core.runtime.Platform; |
| |
| import org.eclipse.statet.jcommons.collections.ImCollections; |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| import org.eclipse.statet.jcommons.runtime.UriUtils; |
| import org.eclipse.statet.jcommons.runtime.bundle.BundleEntry; |
| import org.eclipse.statet.jcommons.runtime.bundle.BundleResolver; |
| import org.eclipse.statet.jcommons.runtime.bundle.BundleSpec; |
| import org.eclipse.statet.jcommons.status.ErrorStatus; |
| import org.eclipse.statet.jcommons.status.MultiStatus; |
| |
| import org.eclipse.statet.rj.RjInvalidConfigurationException; |
| import org.eclipse.statet.rj.server.util.RJContext; |
| |
| |
| /** |
| * Server utilities when using the Eclipse Platform. |
| */ |
| @NonNullByDefault |
| public class ERJContext extends RJContext { |
| |
| |
| private static class EPlatformBundleResolver implements BundleResolver { |
| |
| |
| public EPlatformBundleResolver() { |
| } |
| |
| |
| @Override |
| public boolean resolveBundle(final Data data, final BundleSpec bundleSpec, |
| final MultiStatus status) { |
| final Bundle pluginBundle= Platform.getBundle(bundleSpec.getId()); |
| if (pluginBundle != null) { |
| addPath(pluginBundle, data.getResolved(), status); |
| final Bundle[] fragments= Platform.getFragments(pluginBundle); |
| if (fragments != null) { |
| for (final Bundle fragmentBundle : fragments) { |
| addPath(fragmentBundle, data.getResolved(), status); |
| } |
| } |
| return true; |
| } |
| return false; |
| } |
| |
| private void addPath(final Bundle bundle, final Set<BundleEntry> resolved, |
| final MultiStatus status) { |
| // String location= bundle.getLocation(); |
| // if (location.startsWith("initial@")) { |
| // location= location.substring(8); |
| // } |
| // if (location.startsWith("reference:file:")) { //$NON-NLS-1$ |
| // location= location.substring(15); |
| // IPath path= new Path(location); |
| // if (!path.isAbsolute()) { |
| // path= new Path(Platform.getInstallLocation().getURL().getFile()).append(path); |
| // } |
| // String checked= path.lastSegment(); |
| // if (checked.contains("motif")) { //$NON-NLS-1$ |
| // checked= checked.replaceAll("motif", "gtk"); //$NON-NLS-1$ //$NON-NLS-2$ |
| // } |
| // if (checked.contains("gtk")) { //$NON-NLS-1$ |
| // if (is64 && !checked.contains("64")) { //$NON-NLS-1$ |
| // checked= checked.replaceAll("x86", "x86_64"); //$NON-NLS-1$ //$NON-NLS-2$ |
| // } |
| // if (!is64 && checked.contains("64")) { //$NON-NLS-1$ |
| // checked= checked.replaceAll("x86_64", "x86"); //$NON-NLS-1$ //$NON-NLS-2$ |
| // } |
| // } |
| // final String s= path.removeLastSegments(1).append(checked).makeAbsolute().toOSString(); |
| // if (location.endsWith("/")) { // //$NON-NLS-1$ |
| // if (Platform.inDevelopmentMode()) { |
| // classpath.add(s+File.separatorChar+"bin"+File.separatorChar); //$NON-NLS-1$ |
| // } |
| // classpath.add(s+File.separatorChar); |
| // } |
| // else { |
| // classpath.add(s); |
| // } |
| // return; |
| // } |
| try { |
| final String libId= nonNullAssert(bundle.getSymbolicName()); |
| URI url= UriUtils.toUri(FileLocator.resolve(bundle.getEntry("/"))); |
| if (UriUtils.isJarUrl(url) && UriUtils.getJarEntryPath(url).isEmpty()) { |
| url= UriUtils.getJarFileUrl(url); |
| } |
| final Path path= Paths.get(url); |
| if (Platform.inDevelopmentMode() && url.getPath().endsWith("/")) { |
| final Path binPath; |
| if (Files.isDirectory(binPath= path.resolve("target/classes"))) { |
| resolved.add(new BundleEntry(libId, binPath)); |
| } |
| } |
| resolved.add(new BundleEntry(libId, path)); |
| return; |
| } |
| catch (final Exception e) { |
| status.add(new ErrorStatus(RJ_SERVER_ID, |
| String.format("Failed to check location for plug-in: '%1$s'.", //$NON-NLS-1$ |
| bundle.getSymbolicName() ), |
| e )); |
| } |
| } |
| |
| } |
| |
| |
| public ERJContext() { |
| super(ImCollections.emptyList()); |
| } |
| |
| |
| @Override |
| protected @Nullable BundleResolver getBundleResolver(final String id) |
| throws RjInvalidConfigurationException { |
| if (id.equals("default")) { |
| return new EPlatformBundleResolver(); |
| } |
| return super.getBundleResolver(id); |
| } |
| |
| |
| @Override |
| public String getServerPolicyFilePath() throws RjInvalidConfigurationException { |
| try { |
| final Bundle bundle= Platform.getBundle(RJ_SERVER_ID); |
| if (bundle == null) { |
| throw new RjInvalidConfigurationException(String.format("RJ bundle '%1$s' is missing.", RJ_SERVER_ID)); |
| } |
| final URL intern= bundle.getEntry('/' + LOCALHOST_POLICY_FILENAME); |
| final URL java= FileLocator.resolve(intern); |
| final String path= java.toExternalForm(); |
| return path; |
| } |
| catch (final IOException e) { |
| throw new RjInvalidConfigurationException("Failed find server policy file.", e); |
| } |
| } |
| |
| } |