Skip to content

Commit

Permalink
Issue eclipse-tycho#797 - add a custom transport based on dumb URL#op…
Browse files Browse the repository at this point in the history
…enStream()

Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Mar 23, 2022
1 parent 1815355 commit b8eaf09
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ private static IProvisioningAgent createConfiguredProvisioningAgent(MavenContext
boolean disableP2Mirrors, MavenRepositorySettings mavenRepositorySettings) throws ProvisionException {
// TODO set a temporary folder as persistence location
AgentBuilder agent = new AgentBuilder(Activator.newProvisioningAgent());

if ("tycho".equals(System.getProperty("tycho.p2.transport"))) {
agent.getAgent().registerService(Transport.SERVICE_NAME, new TychoRepositoryTransport(mavenContext));
}
// suppress p2.index access
final Transport transport;
if (mavenContext.isOffline()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*******************************************************************************
* Copyright (c) 2022 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2.remote;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.commons.io.IOUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.repository.AuthenticationFailedException;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory;
import org.eclipse.tycho.core.shared.MavenContext;

@SuppressWarnings("restriction")
public class TychoRepositoryTransport extends org.eclipse.equinox.internal.p2.repository.Transport
implements IAgentServiceFactory {

private MavenContext mavenContext;

public TychoRepositoryTransport(MavenContext mavenContext) {
this.mavenContext = mavenContext;
File cacheLocation = new File(mavenContext.getLocalRepositoryRoot(), ".cache/tycho/httpcache");
cacheLocation.mkdirs();
mavenContext.getLogger().info(
"Using TychoRepositoryTransport for remote P2 access with cache location " + cacheLocation + "...");
}

@Override
public IStatus download(URI toDownload, OutputStream target, long startPos, IProgressMonitor monitor) {
if (startPos > 0) {
return new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"range downloads are not implemented");
}
return download(toDownload, target, monitor);
}

@Override
public IStatus download(URI toDownload, OutputStream target, IProgressMonitor monitor) {
try {
IOUtils.copy(stream(toDownload, monitor), target);
return Status.OK_STATUS;
} catch (AuthenticationFailedException e) {
return new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"authentication failed for " + toDownload, e);
} catch (IOException e) {
return new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"download from " + toDownload + " failed", e);
} catch (CoreException e) {
return e.getStatus();
}
}

@Override
public InputStream stream(URI toDownload, IProgressMonitor monitor)
throws FileNotFoundException, CoreException, AuthenticationFailedException {
mavenContext.getLogger().info("Requested download for " + toDownload + "...");
try {
return toDownload.toURL().openStream();
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, TychoRepositoryTransport.class.getName(),
"download from " + toDownload + " failed", e));
}
}

@Override
public long getLastModified(URI toDownload, IProgressMonitor monitor)
throws CoreException, FileNotFoundException, AuthenticationFailedException {

return System.currentTimeMillis();
}

@Override
public Object createService(IProvisioningAgent agent) {
return this;
}

}

0 comments on commit b8eaf09

Please sign in to comment.