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

Return to bug 214816