blob: b8210fbebc7d68901dcbd932f40e9d2803901eb3 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2009, 2019 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.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.osgi.framework.Bundle;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.statet.jcommons.collections.ImCollections;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.runtime.UriUtils;
import org.eclipse.statet.rj.RjInvalidConfigurationException;
import org.eclipse.statet.rj.server.util.PathEntry;
import org.eclipse.statet.rj.server.util.RJContext;
import org.eclipse.statet.rj.server.util.ServerUtils;
/**
* Server utilities when using the Eclipse Platform.
*/
@NonNullByDefault
public class ERJContext extends RJContext {
private static final String BUNDLE_ID= ServerUtils.RJ_SERVER_ID;
private static void log(final IStatus status) {
Platform.getLog(Platform.getBundle(BUNDLE_ID)).log(status);
}
public ERJContext() {
}
/**
* Searches the specified RJ libraries in the Eclipse Platform installations
* <p>
* Uses always the os/arch currently running.</p>
*
* @param libIds the bundle ids of the libraries
* @return the file paths of the found libraries
*/
@Override
public List<PathEntry> searchRJLibs(final List<String> libIds) {
final List<IStatus> statusList= new ArrayList<>();
final Set<PathEntry> resolved= new LinkedHashSet<>();
for (final String libId : libIds) {
final Bundle pluginBundle= Platform.getBundle(libId);
if (pluginBundle != null) {
addPath(pluginBundle, resolved, statusList);
final Bundle[] fragments= Platform.getFragments(pluginBundle);
if (fragments != null) {
for (final Bundle fragmentBundle : fragments) {
addPath(fragmentBundle, resolved, 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 ImCollections.toList(resolved);
}
private void addPath(final Bundle bundle, final Set<PathEntry> classpath,
final List<IStatus> statusList) {
// 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);
Path binPath;
if (Platform.inDevelopmentMode() && Files.isDirectory(binPath= path.resolve("bin"))) {
classpath.add(new PathEntry(libId, binPath));
}
classpath.add(new PathEntry(libId, path));
return;
}
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 ));
}
}
@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); //$NON-NLS-1$
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);
}
}
}