org.eclipse.equinox.p2.transport.ecf_1.0.100.v20120913-155635

16:44:31.261 INFO  jd.cli.Main - Decompiling org.eclipse.equinox.p2.transport.ecf_1.0.100.v20120913-155635.jar
package org.eclipse.equinox.internal.p2.transport.ecf;

import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransferFactory;
import org.eclipse.ecf.provider.filetransfer.IFileTransferProtocolToFactoryMapper;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

public class Activator
  implements BundleActivator
{
  public static final String ID = "org.eclipse.equinox.p2.transport.ecf";
  private static final String HTTP = "http";
  private static final String HTTPS = "https";
  private static BundleContext context;
  private ServiceTracker<IRetrieveFileTransferFactory, IRetrieveFileTransferFactory> retrievalFactoryTracker;
  private ServiceTracker<IFileTransferProtocolToFactoryMapper, IFileTransferProtocolToFactoryMapper> protocolToFactoryMapperTracker = null;
  private static Activator plugin;
  
  public void start(BundleContext aContext)
    throws Exception
  {
    context = aContext;
    plugin = this;
  }
  
  public void stop(BundleContext aContext)
    throws Exception
  {
    context = null;
    plugin = null;
    if (retrievalFactoryTracker != null)
    {
      retrievalFactoryTracker.close();
      retrievalFactoryTracker = null;
    }
    if (protocolToFactoryMapperTracker != null)
    {
      protocolToFactoryMapperTracker.close();
      protocolToFactoryMapperTracker = null;
    }
  }
  
  public static BundleContext getContext()
  {
    return context;
  }
  
  public static Activator getDefault()
  {
    return plugin;
  }
  
  public IRetrieveFileTransferFactory getRetrieveFileTransferFactory()
  {
    return (IRetrieveFileTransferFactory)getFileTransferServiceTracker().getService();
  }
  
  public synchronized void useJREHttpClient()
  {
    IFileTransferProtocolToFactoryMapper mapper = getProtocolToFactoryMapper();
    if (mapper != null)
    {
      String providerId = mapper.getBrowseFileTransferFactoryId("http");
      if (providerId != null) {
        mapper.removeBrowseFileTransferFactory(providerId);
      }
      providerId = mapper.getRetrieveFileTransferFactoryId("http");
      if (providerId != null) {
        mapper.removeRetrieveFileTransferFactory(providerId);
      }
      providerId = mapper.getSendFileTransferFactoryId("http");
      if (providerId != null) {
        mapper.removeSendFileTransferFactory(providerId);
      }
      providerId = mapper.getBrowseFileTransferFactoryId("https");
      if (providerId != null) {
        mapper.removeBrowseFileTransferFactory(providerId);
      }
      providerId = mapper.getRetrieveFileTransferFactoryId("https");
      if (providerId != null) {
        mapper.removeRetrieveFileTransferFactory(providerId);
      }
      providerId = mapper.getSendFileTransferFactoryId("https");
      if (providerId != null) {
        mapper.removeSendFileTransferFactory(providerId);
      }
    }
  }
  
  private synchronized ServiceTracker<IRetrieveFileTransferFactory, IRetrieveFileTransferFactory> getFileTransferServiceTracker()
  {
    if (retrievalFactoryTracker == null)
    {
      retrievalFactoryTracker = new ServiceTracker(getContext(), IRetrieveFileTransferFactory.class, null);
      retrievalFactoryTracker.open();
      startBundle("org.eclipse.ecf");
      startBundle("org.eclipse.ecf.provider.filetransfer");
    }
    return retrievalFactoryTracker;
  }
  
  private IFileTransferProtocolToFactoryMapper getProtocolToFactoryMapper()
  {
    if (protocolToFactoryMapperTracker == null)
    {
      protocolToFactoryMapperTracker = new ServiceTracker(context, IFileTransferProtocolToFactoryMapper.class, null);
      protocolToFactoryMapperTracker.open();
    }
    return (IFileTransferProtocolToFactoryMapper)protocolToFactoryMapperTracker.getService();
  }
  
  private boolean startBundle(String bundleId)
  {
    PackageAdmin packageAdmin = (PackageAdmin)ServiceHelper.getService(getContext(), PackageAdmin.class.getName());
    if (packageAdmin == null) {
      return false;
    }
    Bundle[] bundles = packageAdmin.getBundles(bundleId, null);
    if ((bundles != null) && (bundles.length > 0)) {
      for (int i = 0; i < bundles.length; i++) {
        try
        {
          if ((bundles[i].getState() & 0x2) == 0)
          {
            bundles[i].start(2);
            bundles[i].start(1);
            return true;
          }
        }
        catch (BundleException localBundleException) {}
      }
    }
    return false;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.Activator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory;

public class ECFTransportComponent
  implements IAgentServiceFactory
{
  public Object createService(IProvisioningAgent agent)
  {
    return new RepositoryTransport(agent);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.ECFTransportComponent
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import java.io.FileNotFoundException;
import java.io.IOException;
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.eclipse.core.runtime.jobs.Job;
import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.ContainerFactory;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.IContainerFactory;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.filetransfer.IRemoteFile;
import org.eclipse.ecf.filetransfer.IRemoteFileInfo;
import org.eclipse.ecf.filetransfer.IRemoteFileSystemBrowserContainerAdapter;
import org.eclipse.ecf.filetransfer.IRemoteFileSystemListener;
import org.eclipse.ecf.filetransfer.IRemoteFileSystemRequest;
import org.eclipse.ecf.filetransfer.RemoteFileSystemException;
import org.eclipse.ecf.filetransfer.events.IRemoteFileSystemBrowseEvent;
import org.eclipse.ecf.filetransfer.events.IRemoteFileSystemEvent;
import org.eclipse.ecf.filetransfer.identity.FileCreateException;
import org.eclipse.ecf.filetransfer.identity.FileIDFactory;
import org.eclipse.ecf.filetransfer.identity.IFileID;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.internal.p2.repository.AuthenticationFailedException;
import org.eclipse.equinox.internal.p2.repository.JREHttpClientRequiredException;
import org.eclipse.equinox.internal.p2.repository.Messages;
import org.eclipse.equinox.internal.p2.repository.RepositoryPreferences;
import org.eclipse.osgi.util.NLS;

public class FileInfoReader
  extends Job
  implements IRemoteFileSystemListener
{
  private Exception exception;
  private IProgressMonitor theMonitor;
  private final int connectionRetryCount;
  private final long connectionRetryDelay;
  private final IConnectContext connectContext;
  final Boolean[] barrier = new Boolean[1];
  private IRemoteFile[] remoteFiles;
  private IRemoteFileSystemRequest browseRequest;
  
  protected IStatus run(IProgressMonitor monitor)
  {
    synchronized (barrier)
    {
      while (barrier[0] == null) {
        try
        {
          barrier.wait(1000L);
          if ((theMonitor.isCanceled()) && (browseRequest != null)) {
            browseRequest.cancel();
          }
        }
        catch (InterruptedException localInterruptedException) {}
      }
    }
    return Status.OK_STATUS;
  }
  
  private void waitOnSelf()
  {
    schedule();
    while (barrier[0] == null)
    {
      boolean logged = false;
      try
      {
        join();
      }
      catch (InterruptedException e)
      {
        if (!logged) {
          LogHelper.log(new Status(2, "org.eclipse.equinox.p2.repository", "Unexpected interrupt while waiting on ECF browse request", e));
        }
      }
    }
  }
  
  public FileInfoReader(IConnectContext aConnectContext)
  {
    super(Messages.repo_loading);
    barrier[0] = null;
    
    setSystem(true);
    setUser(false);
    connectionRetryCount = RepositoryPreferences.getConnectionRetryCount();
    connectionRetryDelay = RepositoryPreferences.getConnectionMsRetryDelay();
    connectContext = aConnectContext;
  }
  
  public IRemoteFile[] getRemoteFiles(URI location, IProgressMonitor monitor)
    throws AuthenticationFailedException, FileNotFoundException, CoreException, JREHttpClientRequiredException
  {
    if (monitor != null) {
      monitor.beginTask(location.toString(), 1);
    }
    try
    {
      sendBrowseRequest(location, monitor);
      waitOnSelf();
      
      checkException(location, connectionRetryCount);
      
      return remoteFiles;
    }
    finally
    {
      if (monitor != null) {
        monitor.done();
      }
    }
  }
  
  public IRemoteFile getRemoteFile(URI location, IProgressMonitor monitor)
    throws AuthenticationFailedException, FileNotFoundException, CoreException, JREHttpClientRequiredException
  {
    getRemoteFiles(location, monitor);
    return (remoteFiles != null) && (remoteFiles.length > 0) ? remoteFiles[0] : null;
  }
  
  public long getLastModified(URI location, IProgressMonitor monitor)
    throws AuthenticationFailedException, FileNotFoundException, CoreException, JREHttpClientRequiredException
  {
    IRemoteFile file = getRemoteFile(location, monitor);
    if (file == null) {
      throw new FileNotFoundException(location.toString());
    }
    return file.getInfo().getLastModified();
  }
  
  public void handleRemoteFileEvent(IRemoteFileSystemEvent event)
  {
    exception = event.getException();
    if (exception != null) {
      synchronized (barrier)
      {
        barrier[0] = Boolean.TRUE;
        barrier.notify();
      }
    }
    if ((event instanceof IRemoteFileSystemBrowseEvent))
    {
      IRemoteFileSystemBrowseEvent fsbe = (IRemoteFileSystemBrowseEvent)event;
      remoteFiles = fsbe.getRemoteFiles();
      if (theMonitor != null) {
        theMonitor.worked(1);
      }
      synchronized (barrier)
      {
        barrier[0] = Boolean.TRUE;
        barrier.notify();
      }
    }
    synchronized (barrier)
    {
      barrier[0] = Boolean.FALSE;
      barrier.notify();
    }
  }
  
  protected void sendBrowseRequest(URI uri, IProgressMonitor monitor)
    throws CoreException, FileNotFoundException, AuthenticationFailedException, JREHttpClientRequiredException
  {
    try
    {
      container = ContainerFactory.getDefault().createContainer();
    }
    catch (ContainerCreateException localContainerCreateException)
    {
      IContainer container;
      throw RepositoryStatusHelper.fromMessage(Messages.ecf_configuration_error);
    }
    IContainer container;
    IRemoteFileSystemBrowserContainerAdapter adapter = (IRemoteFileSystemBrowserContainerAdapter)container.getAdapter(IRemoteFileSystemBrowserContainerAdapter.class);
    if (adapter == null) {
      throw RepositoryStatusHelper.fromMessage(Messages.ecf_configuration_error);
    }
    adapter.setConnectContextForAuthentication(connectContext);
    
    exception = null;
    theMonitor = monitor;
    for (int retryCount = 0;; retryCount++)
    {
      if ((monitor != null) && (monitor.isCanceled())) {
        throw new OperationCanceledException();
      }
      try
      {
        IFileID fileID = FileIDFactory.getDefault().createFileID(adapter.getBrowseNamespace(), uri.toString());
        browseRequest = adapter.sendBrowseRequest(fileID, this);
      }
      catch (RemoteFileSystemException e)
      {
        exception = e;
      }
      catch (FileCreateException e)
      {
        exception = e;
      }
      if (checkException(uri, retryCount)) {
        break;
      }
    }
  }
  
  protected Exception getException()
  {
    return exception;
  }
  
  private boolean checkException(URI uri, int attemptCounter)
    throws CoreException, FileNotFoundException, AuthenticationFailedException, JREHttpClientRequiredException
  {
    if (exception != null)
    {
      RepositoryStatusHelper.checkJREHttpClientRequired(exception);
      
      RepositoryStatusHelper.checkPermissionDenied(exception);
      
      RepositoryStatusHelper.checkFileNotFound(exception, uri);
      
      Throwable t = RepositoryStatusHelper.unwind(exception);
      if ((t instanceof CoreException)) {
        throw RepositoryStatusHelper.unwindCoreException((CoreException)t);
      }
      if (((t instanceof IOException)) && (attemptCounter < connectionRetryCount))
      {
        exception = null;
        try
        {
          LogHelper.log(new Status(2, "org.eclipse.equinox.p2.repository", NLS.bind(Messages.connection_to_0_failed_on_1_retry_attempt_2, new String[] { uri.toString(), t.getMessage(), String.valueOf(attemptCounter) }), t));
          
          Thread.sleep(connectionRetryDelay);
          return false;
        }
        catch (InterruptedException localInterruptedException) {}
      }
      throw RepositoryStatusHelper.wrap(exception);
    }
    return true;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.FileInfoReader
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ecf.filetransfer.UserCancelledException;

class FileReader$1
  extends InputStream
{
  FileReader$1(FileReader paramFileReader, PipedInputStream paramPipedInputStream) {}
  
  public int available()
    throws IOException
  {
    checkException();
    return val$input.available();
  }
  
  public void close()
    throws IOException
  {
    FileReader.hardClose(val$input);
    checkException();
  }
  
  public void mark(int readlimit)
  {
    val$input.mark(readlimit);
  }
  
  public boolean markSupported()
  {
    return val$input.markSupported();
  }
  
  public int read()
    throws IOException
  {
    checkException();
    return val$input.read();
  }
  
  public int read(byte[] b)
    throws IOException
  {
    checkException();
    return val$input.read(b);
  }
  
  public int read(byte[] b, int off, int len)
    throws IOException
  {
    checkException();
    return val$input.read(b, off, len);
  }
  
  public void reset()
    throws IOException
  {
    checkException();
    val$input.reset();
  }
  
  public long skip(long n)
    throws IOException
  {
    checkException();
    return val$input.skip(n);
  }
  
  private void checkException()
    throws IOException
  {
    if (this$0.getException() == null) {
      return;
    }
    Throwable t = RepositoryStatusHelper.unwind(this$0.getException());
    IOException e;
    IOException e;
    if ((t instanceof IOException))
    {
      e = (IOException)t;
    }
    else
    {
      if ((t instanceof UserCancelledException))
      {
        Throwable cause = t;
        t = new OperationCanceledException(t.getMessage());
        t.initCause(cause);
      }
      e = new IOException(t.getMessage());
      e.initCause(t);
    }
    throw e;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.FileReader.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent;
import org.eclipse.equinox.internal.p2.repository.Messages;

public class FileReader$CancelHandler
  extends Job
{
  private boolean done = false;
  
  protected FileReader$CancelHandler(FileReader paramFileReader)
  {
    super(Messages.FileTransport_cancelCheck);
    setSystem(true);
  }
  
  public IStatus run(IProgressMonitor jobMonitor)
  {
    while ((!done) && (!jobMonitor.isCanceled()))
    {
      try
      {
        Thread.sleep(1000L);
      }
      catch (InterruptedException localInterruptedException)
      {
        return Status.CANCEL_STATUS;
      }
      if ((this$0.theMonitor != null) && (this$0.theMonitor.isCanceled()) && 
        (this$0.connectEvent != null)) {
        this$0.connectEvent.cancel();
      }
    }
    return Status.OK_STATUS;
  }
  
  protected void canceling()
  {
    Thread t = getThread();
    if (t != null) {
      t.interrupt();
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.FileReader.CancelHandler
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import org.eclipse.ecf.filetransfer.IFileRangeSpecification;

class FileReader$DownloadRange
  implements IFileRangeSpecification
{
  private long startPosition;
  
  public FileReader$DownloadRange(long startPos)
  {
    startPosition = startPos;
  }
  
  public long getEndPosition()
  {
    return -1L;
  }
  
  public long getStartPosition()
  {
    return startPosition;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.FileReader.DownloadRange
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ecf.filetransfer.IIncomingFileTransfer;

public abstract interface FileReader$IFileReaderProbe
{
  public abstract void onStart(FileReader paramFileReader, IIncomingFileTransfer paramIIncomingFileTransfer, IProgressMonitor paramIProgressMonitor);
  
  public abstract void onData(FileReader paramFileReader, IIncomingFileTransfer paramIIncomingFileTransfer, IProgressMonitor paramIProgressMonitor);
  
  public abstract void onDone(FileReader paramFileReader, IIncomingFileTransfer paramIIncomingFileTransfer, IProgressMonitor paramIProgressMonitor);
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.FileReader.IFileReaderProbe
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;

class FileReader$SuppressBlockedMonitor
  extends SubProgressMonitor
{
  public FileReader$SuppressBlockedMonitor(IProgressMonitor monitor, int ticks)
  {
    super(monitor, ticks);
  }
  
  public void setBlocked(IStatus reason) {}
  
  public void clearBlocked() {}
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.p2.transport.ecf.FileReader.SuppressBlockedMonitor
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.p2.transport.ecf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.util.Date;
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.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.filetransfer.FileTransferJob;
import org.eclipse.ecf.filetransfer.IFileRangeSpecification;
import org.eclipse.ecf.filetransfer.IFileTransferListener;
import org.eclipse.ecf.filetransfer.IFileTransferPausable;
import org.eclipse.ecf.filetransfer.IIncomingFileTransfer;
import org.eclipse.ecf.filetransfer.IRetrieveFileTransferContainerAdapter;
import org.eclipse.ecf.filetransfer.IncomingFileTransferException;
import org.eclipse.ecf.filetransfer.UserCancelledException;
import org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent;
import org.eclipse.ecf.filetransfer.events.IFileTransferEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent;
import org.eclipse.ecf.filetransfer.identity.FileCreateException;
import org.eclipse.ecf.filetransfer.identity.FileIDFactory;
import org.eclipse.ecf.filetransfer.identity.IFileID;
import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransferFactory;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.internal.p2.repository.AuthenticationFailedException;
import org.eclipse.equinox.internal.p2.repository.FileInfo;
import org.eclipse.equinox.internal.p2.repository.JREHttpClientRequiredException;
import org.eclipse.equinox.internal.p2.repository.Messages;
import org.eclipse.equinox.internal.p2.repository.ProgressStatistics;
import org.eclipse.equinox.internal.p2.repository.RepositoryPreferences;
import org.eclipse.equinox.internal.p2.repository.RepositoryTracing;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.osgi.util.NLS;

public final class FileReader
  extends FileTransferJob
  implements IFileTransferListener
{
  private static IFileReaderProbe testProbe;
  
  static class SuppressBlockedMonitor
    extends SubProgressMonitor
  {
    public SuppressBlockedMonitor(IProgressMonitor monitor, int ticks)
    {
      super(ticks);
    }
    
    public void setBlocked(IStatus reason) {}
    
    public void clearBlocked() {}
  }
  
  private boolean closeStreamWhenFinished = false;
  private Exception exception;
  private FileInfo fileInfo;
  private long lastProgressCount;
  private long lastStatsCount;
  protected IProgressMonitor theMonitor;
  private OutputStream theOutputStream;
  private ProgressStatistics statistics;
  private final int connectionRetryCount;
  private final long connectionRetryDelay;
  private final IConnectContext connectContext;
  private URI requestUri;
  protected IFileTransferConnectStartEvent connectEvent;
  private Job cancelJob;
  private boolean monitorStarted;
  private IProvisioningAgent agent;
  private boolean isPause = false;
  private boolean hasPaused = false;
  private IFileTransferPausable pasuable = null;
  
  public FileReader(IProvisioningAgent aAgent, IConnectContext aConnectContext)
  {
    super(Messages.FileTransport_reader);
    
    setSystem(true);
    setUser(false);
    connectionRetryCount = RepositoryPreferences.getConnectionRetryCount();
    connectionRetryDelay = RepositoryPreferences.getConnectionMsRetryDelay();
    connectContext = aConnectContext;
    agent = aAgent;
  }
  
  public FileInfo getLastFileInfo()
  {
    return fileInfo;
  }
  
  protected class CancelHandler
    extends Job
  {
    private boolean done = false;
    
    protected CancelHandler()
    {
      super();
      setSystem(true);
    }
    
    public IStatus run(IProgressMonitor jobMonitor)
    {
      while ((!done) && (!jobMonitor.isCanceled()))
      {
        try
        {
          Thread.sleep(1000L);
        }
        catch (InterruptedException localInterruptedException)
        {
          return Status.CANCEL_STATUS;
        }
        if ((theMonitor != null) && (theMonitor.isCanceled()) && 
          (connectEvent != null)) {
          connectEvent.cancel();
        }
      }
      return Status.OK_STATUS;
    }
    
    protected void canceling()
    {
      Thread t = getThread();
      if (t != null) {
        t.interrupt();
      }
    }
  }
  
  public synchronized void handleTransferEvent(IFileTransferEvent event)
  {
    if ((event instanceof IFileTransferConnectStartEvent))
    {
      connectEvent = ((IFileTransferConnectStartEvent)event);
      cancelJob = new CancelHandler();
      
      cancelJob.schedule(500L);
    }
    else if ((event instanceof IIncomingFileTransferReceiveStartEvent))
    {
      if (cancelJob != null) {
        cancelJob.cancel();
      }
      IIncomingFileTransfer source = ((IIncomingFileTransferEvent)event).getSource();
      try
      {
        FileInfo fi = new FileInfo();
        Date lastModified = source.getRemoteLastModified();
        if (lastModified != null) {
          fi.setLastModified(lastModified.getTime());
        }
        fi.setName(source.getRemoteFileName());
        fi.setSize(source.getFileLength());
        fileInfo = fi;
        
        ((IIncomingFileTransferReceiveStartEvent)event).receive(theOutputStream, this);
      }
      catch (IOException e)
      {
        exception = e;
        return;
      }
      long fileLength = source.getFileLength();
      ProgressStatistics stats = new ProgressStatistics(agent, requestUri, source.getRemoteFileName(), fileLength);
      setStatistics(stats);
      if (theMonitor != null)
      {
        theMonitor.beginTask(null, 1000);
        monitorStarted = true;
        theMonitor.subTask(stats.report());
        lastStatsCount = 0L;
        lastProgressCount = 0L;
      }
      onStart(source);
    }
    else if ((event instanceof IIncomingFileTransferReceiveDataEvent))
    {
      IIncomingFileTransfer source = ((IIncomingFileTransferEvent)event).getSource();
      if (theMonitor != null)
      {
        if (theMonitor.isCanceled())
        {
          source.cancel();
          return;
        }
        br = source.getBytesReceived();
        long count = br - lastStatsCount;
        lastStatsCount = br;
        ProgressStatistics stats = getStatistics();
        if (stats != null)
        {
          stats.increase(count);
          fileInfo.setAverageSpeed(stats.getAverageSpeed());
          if (stats.shouldReport())
          {
            count = br - lastProgressCount;
            lastProgressCount = br;
            theMonitor.subTask(stats.report());
            theMonitor.worked((int)(1000L * count / stats.getTotal()));
          }
        }
      }
      pauseIfPossible(source);
      onData(source);
    }
    else if ((event instanceof IIncomingFileTransferReceiveDoneEvent))
    {
      hasPaused = false;
      if (closeStreamWhenFinished) {
        hardClose(theOutputStream);
      }
      if (exception == null) {
        exception = ((IIncomingFileTransferReceiveDoneEvent)event).getException();
      }
      onDone(((IIncomingFileTransferReceiveDoneEvent)event).getSource());
    }
    else if ((event instanceof IIncomingFileTransferReceivePausedEvent))
    {
      hasPaused = true;
    }
    else if ((event instanceof IIncomingFileTransferReceiveResumedEvent))
    {
      if (cancelJob != null) {
        cancelJob.cancel();
      }
      try
      {
        ((IIncomingFileTransferReceiveResumedEvent)event).receive(theOutputStream, this);
      }
      catch (IOException e)
      {
        exception = e;
      }
      finally
      {
        hasPaused = false;
      }
    }
  }
  
  private synchronized void pauseIfPossible(IIncomingFileTransfer source)
  {
    if ((isPaused()) && (!hasPaused))
    {
      pasuable = 
        ((IFileTransferPausable)source.getAdapter(IFileTransferPausable.class));
      if (pasuable != null) {
        pasuable.pause();
      }
    }
  }
  
  public InputStream read(URI url, IProgressMonitor monitor)
    throws CoreException, FileNotFoundException, AuthenticationFailedException, JREHttpClientRequiredException
  {
    final PipedInputStream input = new PipedInputStream();
    try
    {
      output = new PipedOutputStream(input);
    }
    catch (IOException e)
    {
      PipedOutputStream output;
      throw RepositoryStatusHelper.wrap(e);
    }
    PipedOutputStream output;
    RepositoryTracing.debug("Downloading {0}", url);
    
    sendRetrieveRequest(url, output, null, true, monitor);
    
    new InputStream()
    {
      public int available()
        throws IOException
      {
        checkException();
        return input.available();
      }
      
      public void close()
        throws IOException
      {
        FileReader.hardClose(input);
        checkException();
      }
      
      public void mark(int readlimit)
      {
        input.mark(readlimit);
      }
      
      public boolean markSupported()
      {
        return input.markSupported();
      }
      
      public int read()
        throws IOException
      {
        checkException();
        return input.read();
      }
      
      public int read(byte[] b)
        throws IOException
      {
        checkException();
        return input.read(b);
      }
      
      public int read(byte[] b, int off, int len)
        throws IOException
      {
        checkException();
        return input.read(b, off, len);
      }
      
      public void reset()
        throws IOException
      {
        checkException();
        input.reset();
      }
      
      public long skip(long n)
        throws IOException
      {
        checkException();
        return input.skip(n);
      }
      
      private void checkException()
        throws IOException
      {
        if (getException() == null) {
          return;
        }
        Throwable t = RepositoryStatusHelper.unwind(getException());
        IOException e;
        IOException e;
        if ((t instanceof IOException))
        {
          e = (IOException)t;
        }
        else
        {
          if ((t instanceof UserCancelledException))
          {
            Throwable cause = t;
            t = new OperationCanceledException(t.getMessage());
            t.initCause(cause);
          }
          e = new IOException(t.getMessage());
          e.initCause(t);
        }
        throw e;
      }
    };
  }
  
  public void readInto(URI uri, OutputStream anOutputStream, IProgressMonitor monitor)
    throws CoreException, FileNotFoundException, AuthenticationFailedException, JREHttpClientRequiredException
  {
    readInto(uri, anOutputStream, -1L, monitor);
  }
  
  public boolean belongsTo(Object family)
  {
    return family == this;
  }
  
  /* Error */
  public void readInto(URI uri, OutputStream anOutputStream, long startPos, IProgressMonitor monitor)
    throws CoreException, FileNotFoundException, AuthenticationFailedException, JREHttpClientRequiredException
  {
    // Byte code:
    //   0: aload 5
    //   2: ifnonnull +12 -> 14
    //   5: new 219	org/eclipse/core/runtime/NullProgressMonitor
    //   8: dup
    //   9: invokespecial 475	org/eclipse/core/runtime/NullProgressMonitor:<init>	()V
    //   12: astore 5
    //   14: aload_0
    //   15: aload_1
    //   16: aload_2
    //   17: lload_3
    //   18: ldc2_w 195
    //   21: lcmp
    //   22: ifeq +14 -> 36
    //   25: new 252	org/eclipse/equinox/internal/p2/transport/ecf/FileReader$DownloadRange
    //   28: dup
    //   29: lload_3
    //   30: invokespecial 517	org/eclipse/equinox/internal/p2/transport/ecf/FileReader$DownloadRange:<init>	(J)V
    //   33: goto +4 -> 37
    //   36: aconst_null
    //   37: iconst_0
    //   38: aload 5
    //   40: invokevirtual 514	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:sendRetrieveRequest	(Ljava/net/URI;Ljava/io/OutputStream;Lorg/eclipse/equinox/internal/p2/transport/ecf/FileReader$DownloadRange;ZLorg/eclipse/core/runtime/IProgressMonitor;)V
    //   43: invokestatic 480	org/eclipse/core/runtime/jobs/Job:getJobManager	()Lorg/eclipse/core/runtime/jobs/IJobManager;
    //   46: aload_0
    //   47: new 254	org/eclipse/equinox/internal/p2/transport/ecf/FileReader$SuppressBlockedMonitor
    //   50: dup
    //   51: aload 5
    //   53: iconst_0
    //   54: invokespecial 518	org/eclipse/equinox/internal/p2/transport/ecf/FileReader$SuppressBlockedMonitor:<init>	(Lorg/eclipse/core/runtime/IProgressMonitor;I)V
    //   57: invokeinterface 533 3 0
    //   62: aload_0
    //   63: aload_1
    //   64: aload_2
    //   65: lload_3
    //   66: aload 5
    //   68: invokevirtual 513	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:waitPaused	(Ljava/net/URI;Ljava/io/OutputStream;JLorg/eclipse/core/runtime/IProgressMonitor;)V
    //   71: aload 5
    //   73: invokeinterface 528 1 0
    //   78: ifeq +19 -> 97
    //   81: aload_0
    //   82: getfield 460	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:connectEvent	Lorg/eclipse/ecf/filetransfer/events/IFileTransferConnectStartEvent;
    //   85: ifnull +12 -> 97
    //   88: aload_0
    //   89: getfield 460	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:connectEvent	Lorg/eclipse/ecf/filetransfer/events/IFileTransferConnectStartEvent;
    //   92: invokeinterface 546 1 0
    //   97: aload_0
    //   98: aload_1
    //   99: aload_0
    //   100: getfield 445	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:connectionRetryCount	I
    //   103: invokespecial 505	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:checkException	(Ljava/net/URI;I)Z
    //   106: pop
    //   107: goto +73 -> 180
    //   110: pop
    //   111: aload 5
    //   113: iconst_1
    //   114: invokeinterface 530 2 0
    //   119: new 220	org/eclipse/core/runtime/OperationCanceledException
    //   122: dup
    //   123: invokespecial 476	org/eclipse/core/runtime/OperationCanceledException:<init>	()V
    //   126: athrow
    //   127: astore 6
    //   129: aload_0
    //   130: getfield 457	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:cancelJob	Lorg/eclipse/core/runtime/jobs/Job;
    //   133: ifnull +16 -> 149
    //   136: aload_0
    //   137: getfield 457	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:cancelJob	Lorg/eclipse/core/runtime/jobs/Job;
    //   140: invokevirtual 478	org/eclipse/core/runtime/jobs/Job:cancel	()Z
    //   143: pop
    //   144: aload_0
    //   145: aconst_null
    //   146: putfield 457	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:cancelJob	Lorg/eclipse/core/runtime/jobs/Job;
    //   149: aload_0
    //   150: getfield 452	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:monitorStarted	Z
    //   153: ifne +12 -> 165
    //   156: aload 5
    //   158: aconst_null
    //   159: iconst_1
    //   160: invokeinterface 532 3 0
    //   165: aload_0
    //   166: iconst_0
    //   167: putfield 452	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:monitorStarted	Z
    //   170: aload 5
    //   172: invokeinterface 527 1 0
    //   177: aload 6
    //   179: athrow
    //   180: aload_0
    //   181: getfield 457	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:cancelJob	Lorg/eclipse/core/runtime/jobs/Job;
    //   184: ifnull +16 -> 200
    //   187: aload_0
    //   188: getfield 457	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:cancelJob	Lorg/eclipse/core/runtime/jobs/Job;
    //   191: invokevirtual 478	org/eclipse/core/runtime/jobs/Job:cancel	()Z
    //   194: pop
    //   195: aload_0
    //   196: aconst_null
    //   197: putfield 457	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:cancelJob	Lorg/eclipse/core/runtime/jobs/Job;
    //   200: aload_0
    //   201: getfield 452	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:monitorStarted	Z
    //   204: ifne +12 -> 216
    //   207: aload 5
    //   209: aconst_null
    //   210: iconst_1
    //   211: invokeinterface 532 3 0
    //   216: aload_0
    //   217: iconst_0
    //   218: putfield 452	org/eclipse/equinox/internal/p2/transport/ecf/FileReader:monitorStarted	Z
    //   221: aload 5
    //   223: invokeinterface 527 1 0
    //   228: return
    // Line number table:
    //   Java source line #352	-> byte code offset #0
    //   Java source line #353	-> byte code offset #5
    //   Java source line #355	-> byte code offset #14
    //   Java source line #356	-> byte code offset #43
    //   Java source line #357	-> byte code offset #62
    //   Java source line #358	-> byte code offset #71
    //   Java source line #359	-> byte code offset #88
    //   Java source line #361	-> byte code offset #97
    //   Java source line #362	-> byte code offset #110
    //   Java source line #363	-> byte code offset #111
    //   Java source line #364	-> byte code offset #119
    //   Java source line #365	-> byte code offset #127
    //   Java source line #367	-> byte code offset #129
    //   Java source line #368	-> byte code offset #136
    //   Java source line #369	-> byte code offset #144
    //   Java source line #372	-> byte code offset #149
    //   Java source line #373	-> byte code offset #156
    //   Java source line #374	-> byte code offset #165
    //   Java source line #375	-> byte code offset #170
    //   Java source line #376	-> byte code offset #177
    //   Java source line #367	-> byte code offset #180
    //   Java source line #368	-> byte code offset #187
    //   Java source line #369	-> byte code offset #195
    //   Java source line #372	-> byte code offset #200
    //   Java source line #373	-> byte code offset #207
    //   Java source line #374	-> byte code offset #216
    //   Java source line #375	-> byte code offset #221
    //   Java source line #377	-> byte code offset #228
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	229	0	this	FileReader
    //   0	229	1	uri	URI
    //   0	229	2	anOutputStream	OutputStream
    //   0	229	3	startPos	long
    //   0	229	5	monitor	IProgressMonitor
    //   110	1	6	localInterruptedException	InterruptedException
    //   127	51	6	localObject	Object
    // Exception table:
    //   from	to	target	type
    //   14	107	110	java/lang/InterruptedException
    //   14	127	127	finally
  }
  
  protected void waitPaused(URI uri, OutputStream anOutputStream, long startPos, IProgressMonitor monitor)
    throws AuthenticationFailedException, JREHttpClientRequiredException, FileNotFoundException, CoreException, OperationCanceledException, InterruptedException
  {
    if (hasPaused)
    {
      while (hasPaused)
      {
        Thread.sleep(1000L);
        if (monitor.isCanceled()) {
          throw new OperationCanceledException();
        }
      }
      Job.getJobManager().join(this, new SuppressBlockedMonitor(monitor, 0));
      waitPaused(uri, anOutputStream, startPos, monitor);
    }
  }
  
  protected void sendRetrieveRequest(URI uri, OutputStream outputStream, DownloadRange range, boolean closeStreamOnFinish, IProgressMonitor monitor)
    throws CoreException, FileNotFoundException, AuthenticationFailedException, JREHttpClientRequiredException
  {
    IRetrieveFileTransferFactory factory = Activator.getDefault().getRetrieveFileTransferFactory();
    if (factory == null) {
      throw RepositoryStatusHelper.fromMessage(Messages.ecf_configuration_error);
    }
    IRetrieveFileTransferContainerAdapter adapter = factory.newInstance();
    adapter.setConnectContextForAuthentication(connectContext);
    
    exception = null;
    closeStreamWhenFinished = closeStreamOnFinish;
    fileInfo = null;
    statistics = null;
    lastProgressCount = 0L;
    lastStatsCount = 0L;
    theMonitor = monitor;
    monitorStarted = false;
    theOutputStream = outputStream;
    requestUri = uri;
    for (int retryCount = 0;; retryCount+
1 2 3

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd