Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 214816 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/internal/p2/metadata/repository/ECFMetadataTransport.java (+62 lines)
Lines 12-18 Link Here
12
12
13
import java.io.IOException;
13
import java.io.IOException;
14
import java.io.OutputStream;
14
import java.io.OutputStream;
15
import java.util.Date;
15
import org.eclipse.core.runtime.*;
16
import org.eclipse.core.runtime.*;
17
import org.eclipse.ecf.core.*;
16
import org.eclipse.ecf.filetransfer.*;
18
import org.eclipse.ecf.filetransfer.*;
17
import org.eclipse.ecf.filetransfer.events.*;
19
import org.eclipse.ecf.filetransfer.events.*;
18
import org.eclipse.ecf.filetransfer.identity.FileCreateException;
20
import org.eclipse.ecf.filetransfer.identity.FileCreateException;
Lines 50-55 Link Here
50
		return transfer(factory.newInstance(), toDownload, target, monitor);
52
		return transfer(factory.newInstance(), toDownload, target, monitor);
51
	}
53
	}
52
54
55
	/**
56
	 * Gets the last modified date for the specified file.
57
	 * @param location - The URL location of the file.
58
	 * @return The <code>Date</code> of the file's last modified date, <code>null</code> if operation was not successful.
59
	 */
60
	public Date getLastModified(String location) {
61
		IContainer container;
62
		try {
63
			container = ContainerFactory.getDefault().createContainer();
64
		} catch (ContainerCreateException e) {
65
			return null;
66
		}
67
		IRemoteFileSystemBrowserContainerAdapter adapter = (IRemoteFileSystemBrowserContainerAdapter) container.getAdapter(IRemoteFileSystemBrowserContainerAdapter.class);
68
		if (adapter == null) {
69
			return null;
70
		}
71
		IRemoteFile remoteFile = checkFile(adapter, location);
72
		if (remoteFile == null) {
73
			return null;
74
		}
75
		return new Date(remoteFile.getInfo().getLastModified());
76
	}
77
78
	private IRemoteFile checkFile(final IRemoteFileSystemBrowserContainerAdapter retrievalContainer, final String location) {
79
		final Object[] result = new Object[1];
80
		IRemoteFileSystemListener listener = new IRemoteFileSystemListener() {
81
			public void handleRemoteFileEvent(IRemoteFileSystemEvent event) {
82
				if (event instanceof IRemoteFileSystemBrowseEvent) {
83
					IRemoteFileSystemBrowseEvent fsbe = (IRemoteFileSystemBrowseEvent) event;
84
					IRemoteFile[] remoteFiles = fsbe.getRemoteFiles();
85
					if (remoteFiles != null && remoteFiles.length > 0) {
86
						synchronized (result) {
87
							result[0] = remoteFiles[0];
88
							result.notify();
89
						}
90
					}
91
				}
92
			}
93
		};
94
		try {
95
			retrievalContainer.sendBrowseRequest(FileIDFactory.getDefault().createFileID(retrievalContainer.getBrowseNamespace(), location), listener);
96
		} catch (RemoteFileSystemException e) {
97
			return null;
98
		} catch (FileCreateException e) {
99
			return null;
100
		}
101
		synchronized (result) {
102
			while (result[0] == null) {
103
				boolean logged = false;
104
				try {
105
					result.wait();
106
				} catch (InterruptedException e) {
107
					if (!logged)
108
						LogHelper.log(new Status(IStatus.WARNING, Activator.ID, "Unexpected interrupt while waiting on ECF browse", e)); //$NON-NLS-1$
109
				}
110
			}
111
		}
112
		return (IRemoteFile) result[0];
113
	}
114
53
	private IStatus transfer(final IRetrieveFileTransferContainerAdapter retrievalContainer, final String toDownload, final OutputStream target, final IProgressMonitor monitor) {
115
	private IStatus transfer(final IRetrieveFileTransferContainerAdapter retrievalContainer, final String toDownload, final OutputStream target, final IProgressMonitor monitor) {
54
		final IStatus[] result = new IStatus[1];
116
		final IStatus[] result = new IStatus[1];
55
		IFileTransferListener listener = new IFileTransferListener() {
117
		IFileTransferListener listener = new IFileTransferListener() {
(-)src/org/eclipse/equinox/internal/p2/metadata/repository/SimpleMetadataRepositoryFactory.java (-51 / +85 lines)
Lines 12-23 Link Here
12
12
13
import java.io.*;
13
import java.io.*;
14
import java.net.URL;
14
import java.net.URL;
15
import java.util.Date;
15
import java.util.jar.JarEntry;
16
import java.util.jar.JarEntry;
16
import java.util.jar.JarInputStream;
17
import java.util.jar.JarInputStream;
17
import org.eclipse.core.runtime.*;
18
import org.eclipse.core.runtime.*;
18
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
19
import org.eclipse.equinox.internal.p2.core.helpers.*;
19
import org.eclipse.equinox.internal.p2.core.helpers.Tracing;
20
import org.eclipse.equinox.p2.core.ProvisionException;
20
import org.eclipse.equinox.p2.core.ProvisionException;
21
import org.eclipse.equinox.p2.core.location.AgentLocation;
21
import org.eclipse.equinox.p2.metadata.repository.IMetadataRepository;
22
import org.eclipse.equinox.p2.metadata.repository.IMetadataRepository;
22
import org.eclipse.equinox.spi.p2.metadata.repository.IMetadataRepositoryFactory;
23
import org.eclipse.equinox.spi.p2.metadata.repository.IMetadataRepositoryFactory;
23
24
Lines 34-108 Link Here
34
	}
35
	}
35
36
36
	public IMetadataRepository load(URL location, IProgressMonitor monitor) throws ProvisionException {
37
	public IMetadataRepository load(URL location, IProgressMonitor monitor) throws ProvisionException {
38
		final String PROTOCOL_FILE = "file"; //$NON-NLS-1$
37
		long time = 0;
39
		long time = 0;
38
		boolean compress = true;
40
		boolean compress = true;
41
		boolean local = false;
39
		final String debugMsg = "Restoring metadata repository "; //$NON-NLS-1$
42
		final String debugMsg = "Restoring metadata repository "; //$NON-NLS-1$
40
		File temp = null;
43
		AgentLocation agentLocation = (AgentLocation) ServiceHelper.getService(Activator.getContext(), AgentLocation.class.getName());
44
		Date timestamp, cacheTimestamp;
45
		URL actualFile, dataArea;
46
		File cacheFile = null;
41
		if (Tracing.DEBUG_METADATA_PARSING) {
47
		if (Tracing.DEBUG_METADATA_PARSING) {
42
			Tracing.debug(debugMsg + location);
48
			Tracing.debug(debugMsg + location);
43
			time = -System.currentTimeMillis();
49
			time = -System.currentTimeMillis();
44
		}
50
		}
45
		try {
51
		try {
46
			temp = File.createTempFile(CONTENT_FILENAME, JAR_EXTENSION);
52
			IStatus status = Status.OK_STATUS;
53
			OutputStream metadata = null;
47
			SubMonitor sub = SubMonitor.convert(monitor, 300);
54
			SubMonitor sub = SubMonitor.convert(monitor, 300);
48
			OutputStream metadata = new BufferedOutputStream(new FileOutputStream(temp));
55
			String fileLocation = URLMetadataRepository.getActualLocation(location, JAR_EXTENSION).toExternalForm();
49
			IStatus status = getTransport().download(URLMetadataRepository.getActualLocation(location, JAR_EXTENSION).toExternalForm(), metadata, sub);
56
			int hashCode = location.hashCode();
50
			URL actualFile = URLMetadataRepository.getActualLocation(location);
57
			timestamp = getTransport().getLastModified(fileLocation);
51
			if (!status.isOK()) {
58
			cacheTimestamp = null;
52
				// retry uncompressed
59
			actualFile = URLMetadataRepository.getActualLocation(location);
53
				metadata.close();
60
			// check if the file is from a local repo
54
				if (!temp.delete()) {
61
			if (PROTOCOL_FILE.equals(actualFile.getProtocol())) {
55
					temp.deleteOnExit();
62
				local = true;
56
				}
63
				cacheFile = new File(URLMetadataRepository.getActualLocation(location, JAR_EXTENSION).getPath());
57
				temp = File.createTempFile(CONTENT_FILENAME, XML_EXTENSION);
64
				if (!cacheFile.exists()) {
58
				metadata = new BufferedOutputStream(new FileOutputStream(temp));
65
					cacheFile = new File(actualFile.getPath());
59
				compress = false;
66
					compress = false;
60
				status = getTransport().download(URLMetadataRepository.getActualLocation(location, XML_EXTENSION).toExternalForm(), metadata, sub);
67
				}
61
				if (!status.isOK())
62
					return null;
63
			}
64
			if (metadata != null) {
65
				metadata.close();
66
			}
68
			}
67
			InputStream inStream = new BufferedInputStream(new FileInputStream(temp));
69
			if (!local) {
68
			if (compress) {
70
				dataArea = agentLocation.getDataArea(Activator.ID + "/cache/"); //$NON-NLS-1$
69
				JarInputStream jInStream = new JarInputStream(inStream);
71
				String cachePath = dataArea.getPath();
70
				JarEntry jarEntry = jInStream.getNextJarEntry();
72
				if (timestamp != null) {
71
				String entryName = new Path(actualFile.getPath()).lastSegment();
73
					cacheFile = new File(cachePath + CONTENT_FILENAME + hashCode + JAR_EXTENSION);
72
				while (jarEntry != null && (!entryName.equals(jarEntry.getName()))) {
74
					cacheTimestamp = new Date(cacheFile.lastModified());
73
					jarEntry = jInStream.getNextJarEntry();
75
				} else {
76
					fileLocation = URLMetadataRepository.getActualLocation(location, XML_EXTENSION).toExternalForm();
77
					timestamp = getTransport().getLastModified(fileLocation);
78
					if (timestamp != null) {
79
						compress = false;
80
						cacheFile = new File(cachePath, CONTENT_FILENAME + hashCode + XML_EXTENSION);
81
						cacheTimestamp = new Date(cacheFile.lastModified());
82
					} else
83
						return null;
74
				}
84
				}
75
				if (jarEntry == null) {
85
				if (timestamp.after(cacheTimestamp)) {
76
					throw new FileNotFoundException("Repository not found in compressed file."); //$NON-NLS-1$
86
					// retrieve the newer index file
87
					File parentFile = cacheFile.getParentFile();
88
					if (!parentFile.exists()) {
89
						parentFile.mkdirs();
90
					}
91
					cacheFile.createNewFile();
92
					metadata = new BufferedOutputStream(new FileOutputStream(cacheFile));
93
					status = getTransport().download(fileLocation, metadata, sub);
77
				}
94
				}
78
				inStream = jInStream;
79
			}
95
			}
80
			InputStream descriptorStream = new BufferedInputStream(inStream);
96
			if (status.isOK()) {
81
			try {
97
				if (metadata != null) {
82
				IMetadataRepository result = new MetadataRepositoryIO().read(temp.toURL(), descriptorStream, sub.newChild(100));
98
					metadata.close();
83
				if (result instanceof LocalMetadataRepository)
99
				}
84
					((LocalMetadataRepository) result).initializeAfterLoad(location);
100
				InputStream inStream = new BufferedInputStream(new FileInputStream(cacheFile));
85
				if (result instanceof URLMetadataRepository)
101
				if (compress) {
86
					((URLMetadataRepository) result).initializeAfterLoad(location);
102
					JarInputStream jInStream = new JarInputStream(inStream);
87
				if (Tracing.DEBUG_METADATA_PARSING) {
103
					JarEntry jarEntry = jInStream.getNextJarEntry();
88
					time += System.currentTimeMillis();
104
					String entryName = new Path(actualFile.getPath()).lastSegment();
89
					Tracing.debug(debugMsg + "time (ms): " + time); //$NON-NLS-1$ 
105
					while (jarEntry != null && (!entryName.equals(jarEntry.getName()))) {
90
				}
106
						jarEntry = jInStream.getNextJarEntry();
91
				return result;
107
					}
92
			} finally {
108
					if (jarEntry == null) {
93
				if (descriptorStream != null)
109
						throw new FileNotFoundException("Repository not found in compressed file."); //$NON-NLS-1$
94
					descriptorStream.close();
110
					}
111
					inStream = jInStream;
112
				}
113
				InputStream descriptorStream = new BufferedInputStream(inStream);
114
				try {
115
					IMetadataRepository result = new MetadataRepositoryIO().read(cacheFile.toURL(), descriptorStream, sub.newChild(100));
116
					if (result instanceof LocalMetadataRepository)
117
						((LocalMetadataRepository) result).initializeAfterLoad(location);
118
					if (result instanceof URLMetadataRepository)
119
						((URLMetadataRepository) result).initializeAfterLoad(location);
120
					if (Tracing.DEBUG_METADATA_PARSING) {
121
						time += System.currentTimeMillis();
122
						Tracing.debug(debugMsg + "time (ms): " + time); //$NON-NLS-1$ 
123
					}
124
					return result;
125
				} finally {
126
					if (descriptorStream != null)
127
						descriptorStream.close();
128
				}
95
			}
129
			}
96
		} catch (FileNotFoundException e) {
130
		} catch (FileNotFoundException e) {
97
			//if the repository doesn't exist, then it's fine to return null
131
			//if the repository doesn't exist, then it's fine to return null
98
		} catch (IOException e) {
132
		} catch (IOException e) {
99
			log(debugMsg, e);
133
			log(debugMsg, e);
100
		} finally {
134
		}
101
			if (temp != null && !temp.delete()) {
135
		if (cacheFile != null) {
102
				temp.deleteOnExit();
136
			cacheFile.deleteOnExit();
103
			}
104
		}
137
		}
105
		return null;
138
		return null;
139
106
	}
140
	}
107
141
108
	private ECFMetadataTransport getTransport() {
142
	private ECFMetadataTransport getTransport() {

Return to bug 214816