remove mylyn core net dependency and removed corresponding code

This commit is contained in:
Martin Lippert
2022-04-25 11:18:28 +02:00
parent 155fa29f9f
commit 49873660e6
15 changed files with 20 additions and 1239 deletions

View File

@@ -127,14 +127,14 @@
<feature id="org.eclipse.tm4e.feature"/>
<bundle id="javassist"/>
<bundle id="javassist.source"/>
<!-- <bundle id="javassist.source"/> -->
<bundle id="javax.activation"/>
<bundle id="javax.activation.source"/>
<bundle id="javax.annotation"/>
<bundle id="javax.annotation.source"/>
<bundle id="javax.ws.rs"/>
<bundle id="javax.xml.bind"/>
<bundle id="javax.xml.bind.source"/>
<!-- <bundle id="javax.xml.bind"/> -->
<!-- <bundle id="javax.xml.bind.source"/> -->
<bundle id="javax.xml.stream"/>
<bundle id="org.aopalliance"/>
<bundle id="org.apache.log4j"/>

View File

@@ -18,7 +18,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.springframework.ide.eclipse.boot.launch,
org.eclipse.jdt.launching,
org.eclipse.debug.ui,
org.mockito;bundle-version="[1.9.5,1.9.6)",
org.mockito;bundle-version="1.9.5",
org.springframework.ide.eclipse.boot,
org.eclipse.core.expressions,
org.springframework.ide.eclipse.boot.test,
@@ -30,7 +30,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.apache.commons.lang,
javax.ws.rs;bundle-version="2.0.1",
org.eclipse.osgi,
org.hamcrest;bundle-version="[1.1.0,1.2.0)"
org.hamcrest;bundle-version="1.1.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy

View File

@@ -6,7 +6,6 @@ Bundle-Version: 4.15.0.qualifier
Bundle-Activator: org.springsource.ide.eclipse.commons.internal.core.CorePlugin
Bundle-Vendor: VMware, Inc.
Require-Bundle: org.eclipse.core.resources,
org.eclipse.mylyn.commons.net;bundle-version="[3.3.0,4.0.0)",
org.eclipse.equinox.p2.core;resolution:=optional,
org.eclipse.equinox.p2.repository;resolution:=optional,
org.eclipse.jdt.core,
@@ -19,7 +18,8 @@ Require-Bundle: org.eclipse.core.resources,
org.eclipse.text,
org.springsource.ide.eclipse.commons.livexp,
org.eclipse.debug.core,
org.eclipse.jface
org.eclipse.jface,
org.apache.commons.lang3
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.springsource.ide.eclipse.commons.core,
@@ -29,8 +29,7 @@ Export-Package: org.springsource.ide.eclipse.commons.core,
org.springsource.ide.eclipse.commons.core.templates,
org.springsource.ide.eclipse.commons.core.util,
org.springsource.ide.eclipse.commons.internal.core,
org.springsource.ide.eclipse.commons.internal.core.commandhistory,
org.springsource.ide.eclipse.commons.internal.core.net
org.springsource.ide.eclipse.commons.internal.core.commandhistory
Import-Package: org.eclipse.core.runtime,
org.eclipse.core.runtime.jobs,
org.eclipse.core.runtime.preferences,

View File

@@ -1,175 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.core;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.osgi.util.NLS;
import org.springsource.ide.eclipse.commons.core.util.IOUtil;
import org.springsource.ide.eclipse.commons.internal.core.CorePlugin;
import org.springsource.ide.eclipse.commons.internal.core.net.HttpClientTransportService;
import org.springsource.ide.eclipse.commons.internal.core.net.ITransportService;
import org.springsource.ide.eclipse.commons.internal.core.net.P2TransportService;
/**
* Provides helper methods for downloading files.
* @author Steffen Pingel
*/
public class HttpUtil {
private static ITransportService transport;
public static IStatus download(String url, File archiveFile, File targetDirectory, IProgressMonitor monitor) {
return download(url, archiveFile, targetDirectory, null, monitor);
}
public static IStatus download(String url, File archiveFile, File targetDirectory, String prefix,
IProgressMonitor monitor) {
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
SubMonitor progress = SubMonitor.convert(monitor, 100);
targetDirectory.mkdirs();
// download archive file
try {
try {
OutputStream out = new BufferedOutputStream(new FileOutputStream(archiveFile));
try {
HttpUtil.download(new URI(url), out, progress.newChild(70));
}
catch (CoreException e) {
return new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, NLS.bind(
"I/O error while retrieving data: {0}", e.getMessage()), e);
}
catch (URISyntaxException e) {
return new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, NLS.bind("Invalid URL: {0}", url), e);
}
finally {
out.close();
}
}
catch (IOException e) {
return new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, "I/O error while retrieving data", e);
}
// extract archive file
try {
URL fileUrl = archiveFile.toURI().toURL();
ZipFileUtil.unzip(fileUrl, targetDirectory, prefix, progress.newChild(30));
if (targetDirectory.listFiles().length <= 0) {
String message = NLS.bind("Zip file {0} appears to be empty", archiveFile);
return new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, message);
}
}
catch (IOException e) {
return new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, "Error while extracting archive", e);
}
}
finally {
archiveFile.delete();
}
return Status.OK_STATUS;
}
public static void download(URI uri, OutputStream out, IProgressMonitor monitor) throws CoreException {
String protocol = uri.getScheme();
if ("file".equals(protocol)) {
// Yes. it is a bit strange that HttpUtil knows how to read from
// file url. But it is just easier that
// way. Don't need to special case file urls in other places.
// We should consider renaming this class but it has the potential
// of breaking a lot of dependencies.
File f = new File(uri);
FileInputStream contents = null;
try {
contents = new FileInputStream(f);
byte[] buf = new byte[40 * 1024];
int read;
while ((read = contents.read(buf)) >= 0) {
// read = -1 means EOF
// read == 0 probably is impossible but handle it anyway.
if (read > 0) {
out.write(buf, 0, read);
}
}
}
catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, e.getMessage(), e));
}
finally {
try {
if (contents != null) {
contents.close();
}
}
catch (IOException e) {
}
}
}
else {
getTransport().download(uri, out, monitor);
}
}
public static long getLastModified(URI location, IProgressMonitor monitor) throws CoreException {
return getTransport().getLastModified(location, monitor);
}
public static synchronized ITransportService getTransport() {
if (transport == null) {
if (Platform.isRunning()) {
try {
transport = new P2TransportService();
}
catch (ClassNotFoundException e) {
// fall back to HttpClientTransport
}
}
if (transport == null) {
transport = new HttpClientTransportService();
}
}
return transport;
}
public static InputStream stream(URI uri, IProgressMonitor monitor) throws CoreException {
return getTransport().stream(uri, monitor);
}
public static void ping(URI uri) throws MalformedURLException, IOException, CoreException {
URLConnection connection = uri.toURL().openConnection();
connection.setConnectTimeout(500);
InputStream input = connection.getInputStream();
IOUtil.consume(input);
}
}

View File

@@ -1,6 +1,6 @@
// COPIED from spring-ide org.springframework.ide.eclipse.core.SpringCoreUtils
/*******************************************************************************
* Copyright (c) 2012, 2013 Pivotal Software, Inc.
* Copyright (c) 2012, 2022 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -27,7 +27,7 @@ import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* Copyright (c) 2012, 2022 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -12,7 +12,7 @@ package org.springsource.ide.eclipse.commons.internal.core;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;

View File

@@ -1,233 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.internal.core.net;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.mylyn.commons.net.WebLocation;
import org.eclipse.osgi.util.NLS;
import org.springsource.ide.eclipse.commons.internal.core.CorePlugin;
/**
* A utility for accessing web resources.
* @author Steffen Pingel
*/
public class HttpClientTransportService implements ITransportService {
private static final int BUFFER_SIZE = 4 * 1024;
public HttpClientTransportService() {
}
/**
* Download an HTTP-based resource
*
* @param target the target file to which the content is saved
* @param location the web location of the content
* @param monitor the monitor
* @throws IOException if a network or IO problem occurs
*/
public void download(java.net.URI uri, OutputStream out, IProgressMonitor progressMonitor) throws CoreException {
WebLocation location = new WebLocation(uri.toString());
SubMonitor monitor = SubMonitor.convert(progressMonitor);
monitor.subTask(NLS.bind("Fetching {0}", location.getUrl()));
try {
HttpClient client = new HttpClient();
org.eclipse.mylyn.commons.net.WebUtil.configureHttpClient(client, ""); //$NON-NLS-1$
GetMethod method = new GetMethod(location.getUrl());
try {
HostConfiguration hostConfiguration = org.eclipse.mylyn.commons.net.WebUtil.createHostConfiguration(
client, location, monitor);
int result = org.eclipse.mylyn.commons.net.WebUtil.execute(client, hostConfiguration, method, monitor);
if (result == HttpStatus.SC_OK) {
long total = method.getResponseContentLength();
if (total != -1) {
monitor.setWorkRemaining((int) total);
}
InputStream in = org.eclipse.mylyn.commons.net.WebUtil.getResponseBodyAsStream(method, monitor);
try {
in = new BufferedInputStream(in);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
if (total != -1) {
monitor.worked(len);
}
else {
monitor.worked(1);
monitor.setWorkRemaining(10000);
}
if (monitor.isCanceled()) {
// this point is reached if the user requests a
// cancellation
throw new OperationCanceledException();
}
}
}
catch (OperationCanceledException e) {
// this point is reached if there is some problem with
// the download
throw toException(location, result);
}
catch (IOException e) {
// this point is reached if there is some problem with
// the network
throw toException(location, 500);
}
finally {
in.close();
}
}
else {
throw toException(location, result);
}
}
finally {
method.releaseConnection();
}
}
catch (IOException e) {
throw toException(location, e);
}
finally {
monitor.done();
}
}
/**
* Verify availability of resources at the given web locations. Normally
* this would be done using an HTTP HEAD.
*
* @param locations the locations of the resource to verify
* @param one indicate if only one of the resources must exist
* @param progressMonitor the monitor
* @return true if the resource exists
*/
public long getLastModified(java.net.URI uri, IProgressMonitor progressMonitor) throws CoreException {
WebLocation location = new WebLocation(uri.toString());
SubMonitor monitor = SubMonitor.convert(progressMonitor);
monitor.subTask(NLS.bind("Fetching {0}", location.getUrl()));
try {
HttpClient client = new HttpClient();
org.eclipse.mylyn.commons.net.WebUtil.configureHttpClient(client, ""); //$NON-NLS-1$
HeadMethod method = new HeadMethod(location.getUrl());
try {
HostConfiguration hostConfiguration = org.eclipse.mylyn.commons.net.WebUtil.createHostConfiguration(
client, location, monitor);
int result = org.eclipse.mylyn.commons.net.WebUtil.execute(client, hostConfiguration, method, monitor);
if (result == HttpStatus.SC_OK) {
Header lastModified = method.getResponseHeader("Last-Modified"); //$NON-NLS-1$
if (lastModified != null) {
try {
return DateUtil.parseDate(lastModified.getValue()).getTime();
}
catch (DateParseException e) {
// fall through
}
}
return 0;
}
else {
throw toException(location, result);
}
}
catch (IOException e) {
throw toException(location, e);
}
finally {
method.releaseConnection();
}
}
finally {
monitor.done();
}
}
/**
* Read a web-based resource at the specified location using the given
* processor.
*
* @param location the web location of the content
* @param processor the processor that will handle content
* @param progressMonitor the monitor
* @throws IOException if a network or IO problem occurs
*/
public InputStream stream(java.net.URI uri, IProgressMonitor progressMonitor) throws CoreException {
WebLocation location = new WebLocation(uri.toString());
SubMonitor monitor = SubMonitor.convert(progressMonitor);
monitor.subTask(NLS.bind("Fetching {0}", location.getUrl()));
try {
HttpClient client = new HttpClient();
org.eclipse.mylyn.commons.net.WebUtil.configureHttpClient(client, ""); //$NON-NLS-1$
boolean success = false;
GetMethod method = new GetMethod(location.getUrl());
try {
HostConfiguration hostConfiguration = org.eclipse.mylyn.commons.net.WebUtil.createHostConfiguration(
client, location, monitor);
int result = org.eclipse.mylyn.commons.net.WebUtil.execute(client, hostConfiguration, method, monitor);
if (result == HttpStatus.SC_OK) {
InputStream in = org.eclipse.mylyn.commons.net.WebUtil.getResponseBodyAsStream(method, monitor);
success = true;
return in;
}
else {
throw toException(location, result);
}
}
catch (IOException e) {
throw toException(location, e);
}
finally {
if (!success) {
method.releaseConnection();
}
}
}
finally {
monitor.done();
}
}
private CoreException toException(WebLocation location, int result) {
return new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, NLS.bind(
"Download of {0} failed: Unexpected HTTP response {1}", location.getUrl(), result)));
}
private CoreException toException(WebLocation location, IOException e) throws CoreException {
String message = e.getMessage() != null ? e.getMessage() : "Unexpected error";
return new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, NLS.bind(
"Download of {0} failed: {1}", location.getUrl(), message), e));
}
}

View File

@@ -1,31 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.internal.core.net;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
/**
* @author Steffen Pingel
*/
public interface ITransportService {
public abstract void download(URI uri, OutputStream out, IProgressMonitor monitor) throws CoreException;
public abstract long getLastModified(URI location, IProgressMonitor monitor) throws CoreException;
public abstract InputStream stream(URI uri, IProgressMonitor monitor) throws CoreException;
}

View File

@@ -1,120 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.internal.core.net;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.osgi.util.NLS;
import org.springsource.ide.eclipse.commons.internal.core.CorePlugin;
/**
* @author Terry Denney
*/
public class JDKTransportService implements ITransportService {
private static final int BUFFER_SIZE = 4 * 1024;
public void download(URI uri, OutputStream out, IProgressMonitor progressMonitor) throws CoreException {
SubMonitor monitor = SubMonitor.convert(progressMonitor);
try {
URL url = uri.toURL();
monitor.subTask(NLS.bind("Fetching {0}", url));
try {
InputStream in = url.openStream();
InputStream bufferedIn = new BufferedInputStream(in);
try {
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = bufferedIn.read(buffer)) != -1) {
out.write(buffer, 0, len);
monitor.worked(1);
monitor.setWorkRemaining(10000);
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
}
}
finally {
bufferedIn.close();
in.close();
}
}
catch (IOException e) {
throw toException(url, e);
}
}
catch (MalformedURLException e) {
throw toException(uri, e);
}
}
public long getLastModified(URI location, IProgressMonitor monitor) throws CoreException {
try {
URL url = location.toURL();
try {
return url.openConnection().getLastModified();
}
catch (IOException e) {
throw toException(url, e);
}
}
catch (MalformedURLException e) {
throw toException(location, e);
}
}
public InputStream stream(URI uri, IProgressMonitor monitor) throws CoreException {
try {
URL url = uri.toURL();
try {
InputStream in = url.openStream();
return in;
}
catch (IOException e) {
throw toException(url, e);
}
}
catch (MalformedURLException e) {
throw toException(uri, e);
}
}
private CoreException toException(URI uri, IOException e) throws CoreException {
String message = e.getMessage() != null ? e.getMessage() : "Unexpected error";
return new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, NLS.bind(
"Download of {0} failed: {1}", uri.getFragment(), message), e));
}
private CoreException toException(URL url, IOException e) throws CoreException {
String message = e.getMessage() != null ? e.getMessage() : "Unexpected error";
return new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, NLS.bind(
"Download of {0} failed: {1}", url, message), e));
}
}

View File

@@ -1,151 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.internal.core.net;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.springsource.ide.eclipse.commons.internal.core.CorePlugin;
/**
* @author Steffen Pingel
*/
public class P2TransportService implements ITransportService {
private Object transport;
private Method downloadMethod;
private Method streamMethod;
private Method getLastModifiedMethod;
public P2TransportService() throws ClassNotFoundException {
// TODO e3.5 remove reflection
try {
Class<?> clazz;
try {
clazz = Class.forName("org.eclipse.equinox.internal.p2.repository.RepositoryTransport"); //$NON-NLS-1$
Method getInstanceMethod = clazz.getDeclaredMethod("getInstance"); //$NON-NLS-1$
transport = getInstanceMethod.invoke(null);
}
catch (ClassNotFoundException e) {
// the class moved to a different bundle in 3.7
transport = getTransport_e3_7();
clazz = transport.getClass();
}
downloadMethod = clazz.getDeclaredMethod("download", URI.class, OutputStream.class, IProgressMonitor.class); //$NON-NLS-1$
streamMethod = clazz.getDeclaredMethod("stream", URI.class, IProgressMonitor.class); //$NON-NLS-1$
getLastModifiedMethod = clazz.getDeclaredMethod("getLastModified", URI.class, IProgressMonitor.class); //$NON-NLS-1$
}
catch (LinkageError e) {
throw new ClassNotFoundException("Failed to load P2 transport", e); //$NON-NLS-1$
}
catch (Exception e) {
throw new ClassNotFoundException("Failed to load P2 transport", e); //$NON-NLS-1$
}
}
private static Object getTransport_e3_7() throws Exception {
// This line is here merely to make sure that the bundle gets activated
// before trying to use the service (to get rid of a race condition).
Class<?> clazz = Class.forName("org.eclipse.equinox.p2.core.IProvisioningAgent"); //$NON-NLS-1$
BundleContext bundleContext = CorePlugin.getDefault().getBundle().getBundleContext();
ServiceReference serviceReference = bundleContext
.getServiceReference("org.eclipse.equinox.p2.core.IProvisioningAgent");
if (serviceReference != null) {
try {
Object agent = bundleContext.getService(serviceReference);
if (agent != null) {
Method getServiceMethod = agent.getClass().getDeclaredMethod("getService", String.class); //$NON-NLS-1$
return getServiceMethod.invoke(agent, "org.eclipse.equinox.internal.p2.repository.Transport");
}
}
finally {
bundleContext.ungetService(serviceReference);
}
}
throw new RuntimeException("Transport service not available");
}
private void convertException(InvocationTargetException e) throws CoreException {
if (e.getCause() instanceof CoreException) {
throw (CoreException) e.getCause();
}
else {
throw new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, e.getCause().getMessage(),
e.getCause()));
}
}
public void download(URI uri, OutputStream out, IProgressMonitor monitor) throws CoreException {
try {
IStatus result = (IStatus) downloadMethod.invoke(transport, uri, out, monitor);
if (result.getSeverity() == IStatus.CANCEL) {
throw new OperationCanceledException();
}
if (!result.isOK()) {
throw new CoreException(result);
}
}
catch (InvocationTargetException e) {
if (e.getCause() instanceof CoreException) {
throw (CoreException) e.getCause();
}
}
catch (IllegalArgumentException e) {
throw new RuntimeException(e);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public long getLastModified(URI location, IProgressMonitor monitor) throws CoreException {
try {
return (Long) getLastModifiedMethod.invoke(transport, location, monitor);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
convertException(e);
}
// should never happen
throw new IllegalStateException();
}
public InputStream stream(URI uri, IProgressMonitor monitor) throws CoreException {
try {
return (InputStream) streamMethod.invoke(transport, uri, monitor);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
convertException(e);
}
// should never happen
throw new IllegalStateException();
}
}

View File

@@ -6,8 +6,13 @@ Bundle-Version: 4.15.0.qualifier
Bundle-ActivationPolicy: lazy
Bundle-Vendor: VMware, Inc.
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.eclipse.swtbot.go,
org.springsource.ide.eclipse.commons.tests.util,
Require-Bundle: org.springsource.ide.eclipse.commons.tests.util,
org.eclipse.debug.core,
org.hamcrest.library
org.hamcrest.library,
org.eclipse.swtbot.eclipse.finder,
org.eclipse.core.runtime,
org.eclipse.swt,
org.eclipse.ui,
org.apache.log4j,
org.junit
Export-Package: org.springsource.ide.eclipse.commons.frameworks.test.util

View File

@@ -11,7 +11,6 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.swtbot.eclipse.gef.finder,
org.eclipse.ui.ide,
org.eclipse.ui.views.properties.tabbed,
org.eclipse.mylyn.commons.net,
org.eclipse.core.net,
org.springsource.ide.eclipse.commons.core,
org.springsource.ide.eclipse.commons.frameworks.core

View File

@@ -1,171 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.tests.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.core.runtime.CoreException;
import org.springsource.ide.eclipse.commons.core.HttpUtil;
/**
* Manages a cache of downloaded files used by tests.
*
* @author Kris De Volder, Steffen Pingel
*
* @since 2.8
*/
public class DownloadManager {
/**
* An instance of this interface represent an action to execute on a downloaded
* File. The action may indicate failure by throwing an exception or by
* returning false. A failed action may trigger the DownloadManager to
* clear the cache and try again for a limited number of times.
*/
public interface DownloadRequestor {
void exec(File downloadedFile) throws Exception;
}
private final String cacheDirectory;
private static DownloadManager defaultInstance = null;
public static DownloadManager getDefault() {
if (defaultInstance==null) {
defaultInstance = new DownloadManager();
}
return defaultInstance;
}
public DownloadManager() {
this(System.getProperty(
"com.springsource.sts.tests.cache",
System.getProperty("user.home") + File.separatorChar + ".sts-test-cache"));
deleteBuildSnapshots();
}
/**
* Build snapshots from a previous test run shouldn't be used from the cache. So delete them
* when the DownloadManager instance is created.
*/
private void deleteBuildSnapshots() {
//Only do this on the build site, locally it is easy enough to delete buildsnaps manually
// as needed/desired.
if (StsTestUtil.isOnBuildSite()) {
File cache = new File(cacheDirectory);
if (cache.isDirectory()) {
String[] names = cache.list();
for (String name : names) {
if (name.contains("SNAPSHOT")) {
try {
new File(cache, name).delete();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
}
}
public DownloadManager(String cacheDir) {
this.cacheDirectory = cacheDir;
}
/**
* This method is deprecated, please use doWithDownload to provide proper recovery
* for cache corruption.
*/
@Deprecated
public File downloadFile(URI uri) throws URISyntaxException, FileNotFoundException, CoreException, IOException {
String protocol = uri.getScheme();
if ("file".equals(protocol)) {
return new File(uri);
}
String path = uri.getPath();
int i = path.lastIndexOf("/");
if (i >= 0) {
path = path.substring(i + 1);
}
File target = new File(cacheDirectory, path);
if (target.exists()) {
return target;
}
File cache = new File(cacheDirectory);
if (!cache.exists()) {
cache.mkdirs();
}
File targetPart = new File(cache, path + ".part");
FileOutputStream out = new FileOutputStream(targetPart);
try {
System.out.println("Downloading " + uri + " to " + target);
HttpUtil.download(uri, out, null);
}
finally {
out.close();
}
if (!targetPart.renameTo(target)) {
throw new IOException("Error while renaming " + targetPart + " to " + target);
}
return target;
}
/**
* This method tries to download or fetch a File from the cache, then passes the
* downloaded file to the DownloadRequestor.
* <p>
* If the requestor fails to properly execute on the downloaded file, the cache
* will be presumed to be corrupt. The file will be deleted from the cache
* and the download will be tried again. (for a limited number of times)
*/
public void doWithDownload(URI target, DownloadRequestor action) throws Exception {
int tries = 4; // try at most X times
Exception e = null;
File downloadedFile = null;
do {
tries--;
try {
downloadedFile = downloadFile(target);
action.exec(downloadedFile);
return; // action and download succeeded without exceptions
} catch (Exception caught) {
caught.printStackTrace();
//Presume the cache may be corrupt!
System.out.println("Delete corrupt download: "+downloadedFile);
//downloaded file may be null if download failed, rather than its processing:
if (downloadedFile!=null) {
downloadedFile.delete();
}
e = caught;
}
} while (tries>0);
//Can only get here if action or download failed...
//thus, e can not be null.
throw e;
}
public File getCacheDir() {
return new File(cacheDirectory);
}
}

View File

@@ -1,283 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.tests.util;
import java.io.File;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.ConcurrentModificationException;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestFailure;
import junit.framework.TestListener;
import junit.framework.TestResult;
import junit.framework.TestSuite;
import org.eclipse.core.internal.jobs.JobManager;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.commons.net.WebUtil;
import org.eclipse.mylyn.internal.commons.net.CommonsNetPlugin;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.utils.ClassUtils;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
/**
* Prints the name of each test to System.err when it started and dumps a stack
* trace of all thread to System.err if a test takes longer than 10 minutes.
* @author Steffen Pingel
* @author Kris De Volder
*/
public class ManagedTestSuite extends TestSuite {
private class DumpThreadTask extends TimerTask {
private final Test test;
private final Thread testThread;
public DumpThreadTask(Test test, Thread testThread) {
this.test = test;
this.testThread = testThread;
}
private void dumpJobs() {
StringBuffer sb = new StringBuffer();
sb.append(MessageFormat.format("Jobs:\n", test.toString()));
Job[] jobs = Job.getJobManager().find(null);
for (Job job : jobs) {
sb.append(job.getName().toString());
sb.append(" [");
sb.append(JobManager.printState(job.getState()));
sb.append(", ");
sb.append(job.getClass().getName());
sb.append("]");
sb.append("\n");
}
System.err.println(sb.toString());
}
@Override
public void run() {
// dump all thread for diagnosis
StringBuffer sb = StsTestUtil.getStackDumps();
System.err.println(
MessageFormat.format("Test {0} is taking too long:\n", test.toString()) +
sb.toString());
dumpJobs();
// killTest("Test is taking too long");
// capture screenshot for diagnosis
final String fileName = "screenshots/screenshot-" + ClassUtils.simpleClassName(test.getClass()) + "." //$NON-NLS-1$ //$NON-NLS-2$
+ SWTBotPreferences.SCREENSHOT_FORMAT.toLowerCase();
File screenshotFile = new File("screenshots");
System.err.println("Captured screenshot to " + screenshotFile.getAbsolutePath());
screenshotFile.mkdirs();
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
// This deadlocks when run in UI thread!
SWTUtils.captureScreenshot(fileName);
}
});
// attempt to close any modal dialogs
if (test instanceof ShutdownWatchdog) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null) {
Shell shell = window.getShell();
Shell[] shells = window.getShell().getDisplay().getShells();
for (Shell child : shells) {
if (child != shell) {
child.close();
}
}
}
}
});
}
}
@SuppressWarnings("deprecation")
private void killTest(String debugInfo) {
try {
// Yes, the stop method is deprecated, but I don't know another
// way to attempt to stop a runaway test without the cooperation
// of the test/thread itself. This may not work as desired in
// all cases, but is almost certainly better than leaving the
// "stuck" test hanging.
System.err.println("[TIMEOUT] " + test);
testThread.stop();
}
catch (Throwable e) {
e.printStackTrace();
}
}
};
private class Listener implements TestListener {
private DumpThreadTask task;
private Timer timer = new Timer(true);
public void addError(Test test, Throwable t) {
System.err.println("[ERROR]");
}
public void addFailure(Test test, AssertionFailedError t) {
System.err.println("[FAILURE]");
}
private void dumpList(String header, Enumeration<TestFailure> failures) {
System.err.println(header);
while (failures.hasMoreElements()) {
TestFailure failure = failures.nextElement();
System.err.print(" ");
System.err.println(failure.toString());
}
}
public void dumpResults(TestResult result) {
System.err.println();
dumpList("Failures: ", result.failures());
System.err.println();
dumpList("Errors: ", result.errors());
int failedCount = result.errorCount() + result.failureCount();
System.err.println();
System.err.println(MessageFormat.format("{0} out of {1} tests failed", failedCount, result.runCount()));
}
public void endTest(Test test) {
if (task != null) {
task.cancel();
task = null;
}
}
public void startTest(Test test) {
Thread testThread = Thread.currentThread();
System.err.println("Running " + test.toString());
task = new DumpThreadTask(test, testThread);
try {
timer.scheduleAtFixedRate(task, DELAY, DELAY);
} catch (IllegalStateException e) {
//No idea where, who or why, but timer gets 'canceled'.
// We'll need a new one
timer = new Timer(true);
timer.scheduleAtFixedRate(task, DELAY, DELAY);
}
}
}
public class ShutdownWatchdog implements Test {
public int countTestCases() {
return 1;
}
public void run(TestResult result) {
// do nothing
}
@Override
public String toString() {
return "ShutdownWatchdog";
}
}
public long DELAY = 10 * 60 * 1000;
private final Listener listener = new Listener();
public ManagedTestSuite() {
}
public ManagedTestSuite(String name) {
super(name);
}
@Override
public void run(TestResult result) {
result.addListener(listener);
dumpSystemInfo();
super.run(result);
listener.dumpResults(result);
// add dummy test to dump threads in case shutdown hangs
listener.startTest(new ShutdownWatchdog());
}
private void dumpSystemInfo() {
try {
if (Platform.isRunning() && CommonsNetPlugin.getProxyService() != null
&& CommonsNetPlugin.getProxyService().isSystemProxiesEnabled()
&& !CommonsNetPlugin.getProxyService().hasSystemProxies()) {
// XXX e3.5/gtk.x86_64 activate manual proxy configuration which
// defaults to Java system properties if system proxy support is
// not available
System.err.println("Forcing manual proxy configuration");
CommonsNetPlugin.getProxyService().setSystemProxiesEnabled(false);
CommonsNetPlugin.getProxyService().setProxiesEnabled(true);
}
Properties p = System.getProperties();
if (Platform.isRunning()) {
p.put("build.system", Platform.getOS() + "-" + Platform.getOSArch() + "-" + Platform.getWS());
}
else {
p.put("build.system", "standalone");
}
String info = "System: ${os.name} ${os.version} (${os.arch}) / ${build.system} / ${java.vendor} ${java.vm.name} ${java.version}";
for (Entry<Object, Object> entry : p.entrySet()) {
info = info.replaceFirst(Pattern.quote("${" + entry.getKey() + "}"), entry.getValue().toString());
}
System.err.println(info);
System.err.print("Proxy : " + WebUtil.getProxy("google.com", IProxyData.HTTP_PROXY_TYPE) + " (Platform)");
try {
System.err.print(" / " + ProxySelector.getDefault().select(new URI("https://google.com")) + " (Java)");
}
catch (URISyntaxException e) {
// ignore
}
System.err.println();
System.err.println();
}
catch (ConcurrentModificationException e) {
// Not sure why but sometimes thrown by the code that is dumping out
// system properties!
// Catch and print it, but don't abort the test runner simply
// because this info can't be dumped.
e.printStackTrace();
}
}
}

View File

@@ -1,58 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 Pivotal Software, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.springsource.ide.eclipse.commons.tests.util;
import junit.framework.Test;
import junit.framework.TestCase;
/**
* A test to test ManagedTestSuite. We want to try to more gracefully handle
* hanging tests by terminating just the offending test, not the whole build.
* <p>
* This test isn't part of the build and isn't supposed to "pass". It's just
* something I wipped up to play around with ManagedTestSuite.
* <p>
* In Eclipse: run this test as JUnitPluginTest/SWTBotTest and the expected behaviour is to
* see "testThatHangs" fail and the other tests pass (rather than the test run
* hanging).
* @author Kris De Volder
*/
public class TestTests {
public static class HangingTest extends TestCase {
public void testThatHangs() throws Exception {
while (true) {
}
}
public void testGoodOne() throws Exception {
}
}
public static class GoodTest extends TestCase {
public void testGoodA() throws Exception {
}
public void testGoodB() throws Exception {
}
}
public static Test suite() {
ManagedTestSuite suite = new ManagedTestSuite(TestTests.class.getName());
suite.DELAY = 15000; // Make this go a little faster :-)
suite.addTestSuite(HangingTest.class);
suite.addTestSuite(GoodTest.class);
return suite;
}
}