|
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() { |