Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 50ddd518c706cee9cd5054c45a1a197f8dce010f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*******************************************************************************
 * Copyright (c) 2008 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Doug Schaefer - initial API and implementation
 *******************************************************************************/ 

package org.eclipse.cdt.p2.internal.repo.artifact;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.p2.Activator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.core.helpers.URLUtil;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.ArtifactDescriptor;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepositoryManager;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRequest;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.equinox.internal.provisional.p2.core.location.AgentLocation;
import org.eclipse.equinox.internal.provisional.p2.core.repository.IRepository;
import org.eclipse.equinox.internal.provisional.p2.engine.IProfile;
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
import org.eclipse.equinox.internal.provisional.spi.p2.artifact.repository.AbstractArtifactRepository;

/**
 * @author DSchaefe
 *
 */
public class InstallArtifactRepository extends AbstractArtifactRepository {

	// Install directory property
	public static final String INSTALL_DIR = "installDir"; //$NON-NLS-1$
	public static final String SUB_DIR = "subdir"; //$NON-NLS-1$
	public static final String FILENAME = "installArtifact.xml"; //$NON-NLS-1$
	
	public static final String COMPRESSION = "compression";
	public static final String GZIP_COMPRESSON = "tar.gz";
	public static final String BZIP2_COMPRESSION = "tar.bz2";
	public static final String ZIP_COMPRESSION = "zip";
	
	private static final String VERSION = "2.0.0"; //$NON-NLS-1$
	private static final String DESCRIPTION = "Wind River Metadata Repository"; //$NON-NLS-1$
	private static final String PROVIDER = "Wind River"; //$NON-NLS-1$
	
	// Map from artifact id to artifact descriptor. We only allow one version of each artifact
	// to be installed at a time.
	private Map<String, IArtifactDescriptor> artifacts = new HashMap<String, IArtifactDescriptor>();
	
	public InstallArtifactRepository(URL aLocation, String aName, Map aProperties) {
		super(aName, InstallArtifactRepository.class.getName(), VERSION, aLocation, DESCRIPTION, PROVIDER, aProperties);
		save();
	}
	
	/**
	 * Constructor for reading in from file.
	 * 
	 * @param _name
	 * @param _type
	 * @param _version
	 * @param _description
	 * @param _provider
	 * @param _artifacts
	 * @param mappingRules
	 * @param _properties
	 */
	InstallArtifactRepository(String _name, String _type, String _version, URL _location, String _description, String _provider, Set<ArtifactDescriptor> _artifacts, Map _properties) {
		super(_name, _type, _version, _location, _description, _provider, _properties);
		for (IArtifactDescriptor descriptor : _artifacts)
			artifacts.put(descriptor.getArtifactKey().getId(), descriptor);
	}

	public static URL getActualLocation(URL base) {
		final String name = FILENAME;
		String spec = base.toExternalForm();
		if (spec.endsWith(name))
			return base;
		if (spec.endsWith("/")) //$NON-NLS-1$
			spec += name;
		else
			spec += "/" + name; //$NON-NLS-1$
		try {
			return new URL(spec);
		} catch (MalformedURLException e) {
			return null;
		}
	}
	
	public static IArtifactRepository getRepository(IProfile profile) throws ProvisionException {
		AgentLocation location = Activator.getDefault().getService(AgentLocation.class);
		String profileId = profile.getProfileId();
		profileId = profileId.replaceAll("[:/\\\\]", "_"); //$NON-NLS-1$ //$NON-NLS-2$
		try {
			URL url = location.getDataArea(Activator.PLUGIN_ID);
			url = new URL(url.toExternalForm() + "installDirRepo/" + profileId + "/" + FILENAME); //$NON-NLS-1$ //$NON-NLS-2$
			IArtifactRepositoryManager repoMgr = Activator.getDefault().getService(IArtifactRepositoryManager.class);
			try {
				return repoMgr.loadRepository(url, null);
			} catch (ProvisionException e) {
				Map<String, String> properties = new HashMap<String, String>();
				properties.put(INSTALL_DIR, profile.getLocalProperty(IProfile.PROP_INSTALL_FOLDER));
				return repoMgr.createRepository(url, profile.getProfileId(), InstallArtifactRepository.class.getName(), properties);
			}
		} catch (MalformedURLException e) {
			Activator.getDefault().log(IStatus.ERROR, "Creating install repo URI", e); //$NON-NLS-1$
			return null;
		}
	}
	
	@Override
	public boolean isModifiable() {
		// We're always modifiable
		// TODO - unless we're a shared install...
		return true;
	}
	
	@Override
	public boolean contains(IArtifactDescriptor descriptor) {
		return contains(descriptor.getArtifactKey());
	}

	@Override
	public synchronized boolean contains(IArtifactKey key) {
		IArtifactDescriptor desc = artifacts.get(key.getId());
		if (desc == null)
			return false;
		return desc.getArtifactKey().equals(key);
	}

	@Override
	public IStatus getArtifact(IArtifactDescriptor descriptor, OutputStream destination, IProgressMonitor monitor) {
		// copying from this repository is not supported, yet...
		throw new UnsupportedOperationException();
	}

	@Override
	public synchronized IArtifactDescriptor[] getArtifactDescriptors(IArtifactKey key) {
		// we only have one artifact descriptor per key
		IArtifactDescriptor desc = artifacts.get(key);
		if (desc != null)
			return new IArtifactDescriptor[] { desc };
		return new IArtifactDescriptor[0];
	}

	@Override
	public synchronized IArtifactKey[] getArtifactKeys() {
		Collection<IArtifactDescriptor> descs = artifacts.values();
		IArtifactKey[] keys = new IArtifactKey[descs.size()];
		int i = 0;
		for (IArtifactDescriptor desc : descs)
			keys[i++] = desc.getArtifactKey();
		return keys;
	}

	@Override
	public IStatus getArtifacts(IArtifactRequest[] requests, IProgressMonitor monitor) {
		// TODO Auto-generated method stub
		return Status.OK_STATUS;
	}

	@Override
	public synchronized void addDescriptor(IArtifactDescriptor descriptor) {
		super.addDescriptor(descriptor);
		artifacts.put(descriptor.getArtifactKey().getId(), descriptor);
		save();
	}

	@Override
	public synchronized void addDescriptors(IArtifactDescriptor[] descriptors) {
		super.addDescriptors(descriptors);
		for (IArtifactDescriptor descriptor : descriptors)
			artifacts.put(descriptor.getArtifactKey().getId(), descriptor);
		save();
	}

	synchronized Collection<IArtifactDescriptor> getDescriptors() {
		return artifacts.values();
	}
	
	private File getFileListFile(String artifact) throws IOException {
		File file;
		try {
			file = new File(URLUtil.toURI(location));
		} catch (URISyntaxException e) {
			throw new IOException(e);
		}
		if (file.getName().equals(FILENAME))
			file = file.getParentFile();
		return new File(file, artifact + ".txt"); //$NON-NLS-1$
	}
	
	@Override
	public OutputStream getOutputStream(IArtifactDescriptor descriptor)	throws ProvisionException {
		// Do the modifiable check in the superclass
		super.getOutputStream(descriptor);
		
		// Add the descriptor to the list and save it
		IArtifactDescriptor oldDesc = artifacts.get(descriptor.getArtifactKey().getId());
		if (oldDesc != null)
			removeDescriptor(oldDesc);
		addDescriptor(descriptor);
		
		// Start the extractor
		try {
			String installDirName = (String)getProperties().get(INSTALL_DIR);
			if (installDirName == null)
				throw new ProvisionException("Install directory not set"); //$NON-NLS-1$
			File installDir = new File(installDirName);
			String subDir = (String)descriptor.getProperties().get(SUB_DIR);
			if (subDir != null)
				installDir = new File(installDir, subDir);
			PipedOutputStream out = new PipedOutputStream();
			PipedInputStream in = new PipedInputStream(out);
			String compression = descriptor.getProperty(COMPRESSION);
			if (ZIP_COMPRESSION.equals(compression)) {
				// TODO a zip extractor
			} else {
				TarExtractor extractor = new TarExtractor(in, installDir, 
						new FileListWriter(getFileListFile(descriptor.getArtifactKey().getId())),
						compression);
				extractor.start();
			}
			return out;
		} catch (IOException e) {
			// TODO How could that happen
			throw new ProvisionException(e.getLocalizedMessage());
		}
	}

	private void deleteFiles(String artifact) {
		File fileListFile = null;
		try {
			fileListFile = getFileListFile(artifact);
			FileListReader reader = new FileListReader(fileListFile);
			InstalledFile file;
			while ((file = reader.getNext()) != null) {
				file.uninstall();
			}
			reader.close();
		} catch (IOException e) {
			Activator.getDefault().log(IStatus.WARNING, "deleting file", e); //$NON-NLS-1$
		} finally {
			if (fileListFile != null)
				fileListFile.delete();
		}
	}
	
	@Override
	public synchronized void removeAll() {
		super.removeAll();
		for (String artifact : artifacts.keySet())
			deleteFiles(artifact);
		artifacts.clear();
		save();
	}

	@Override
	public void removeDescriptor(IArtifactDescriptor descriptor) {
		removeDescriptor(descriptor.getArtifactKey());
	}

	@Override
	public synchronized void removeDescriptor(IArtifactKey key) {
		super.removeDescriptor(key);
		deleteFiles(key.getId());
		artifacts.remove(key);
		save();
	}

	private void save() {
		try {
			OutputStream os = null;
			try {
				URL actualLocation = getActualLocation(location);
				File artifactsFile = new File(actualLocation.getPath());
				artifactsFile.getParentFile().mkdirs();
				os = new FileOutputStream(artifactsFile);
				super.setProperty(IRepository.PROP_TIMESTAMP, Long.toString(System.currentTimeMillis()));
				new InstallArtifactRepositoryIO().write(this, os);
			} catch (IOException e) {
				// TODO proper exception handling
				e.printStackTrace();
			} finally {
				if (os != null)
					os.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

Back to the top