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