Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6e30998139610ec803d2be4a8478b01778962f12 (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
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.prov.console;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Properties;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.equinox.configurator.Configurator;
import org.eclipse.equinox.prov.artifact.repository.IArtifactRepository;
import org.eclipse.equinox.prov.artifact.repository.IArtifactRepositoryManager;
import org.eclipse.equinox.prov.core.ProvisionException;
import org.eclipse.equinox.prov.core.helpers.ServiceHelper;
import org.eclipse.equinox.prov.director.IDirector;
import org.eclipse.equinox.prov.engine.IProfileRegistry;
import org.eclipse.equinox.prov.engine.Profile;
import org.eclipse.equinox.prov.metadata.IInstallableUnit;
import org.eclipse.equinox.prov.metadata.repository.IMetadataRepository;
import org.eclipse.equinox.prov.metadata.repository.IMetadataRepositoryManager;
import org.eclipse.equinox.prov.query.CompoundIterator;
import org.eclipse.equinox.prov.query.Query;
import org.eclipse.osgi.service.environment.EnvironmentInfo;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.osgi.framework.Version;

public class ProvisioningHelper {

	public static IMetadataRepository addMetadataRepository(URL location) {
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) ServiceHelper.getService(Activator.getContext(), IMetadataRepositoryManager.class.getName());
		if (manager == null)
			throw new IllegalStateException("No metadata repository manager found");
		IMetadataRepository repository = manager.loadRepository(location, null);
		if (repository != null)
			return repository;

		// for convenience create and add a repo here
		// TODO need to get rid o fthe factory method.
		String repositoryName = location + " - metadata"; //$NON-NLS-1$
		IMetadataRepository result = manager.createRepository(location, repositoryName, "org.eclipse.equinox.prov.metadata.repository.simpleRepository"); //$NON-NLS-1$
		return result;
	}

	public static IMetadataRepository getMetadataRepository(URL location) {
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) ServiceHelper.getService(Activator.getContext(), IMetadataRepositoryManager.class.getName());
		if (manager == null)
			throw new IllegalStateException("No metadata repository manager found");
		return manager.getRepository(location);
	}

	public static void removeMetadataRepository(URL location) {
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) ServiceHelper.getService(Activator.getContext(), IMetadataRepositoryManager.class.getName());
		if (manager == null)
			throw new IllegalStateException("No metadata repository manager found");
		IMetadataRepository repo = manager.getRepository(location);
		if (repo != null)
			manager.removeRepository(repo);
	}

	public static IArtifactRepository addArtifactRepository(URL location) {
		IArtifactRepositoryManager manager = (IArtifactRepositoryManager) ServiceHelper.getService(Activator.getContext(), IArtifactRepositoryManager.class.getName());
		if (manager == null)
			// TODO log here
			return null;
		IArtifactRepository repository = manager.loadRepository(location, null);
		if (repository != null)
			return repository;

		// could not load a repo at that location so create one as a convenience
		String repositoryName = location + " - artifacts"; //$NON-NLS-1$
		return manager.createRepository(location, repositoryName, "org.eclipse.equinox.prov.artifact.repository.simpleRepository"); //$NON-NLS-1$
	}

	public static void removeArtifactRepository(URL location) {
		IArtifactRepositoryManager manager = (IArtifactRepositoryManager) ServiceHelper.getService(Activator.getContext(), IArtifactRepositoryManager.class.getName());
		if (manager == null)
			// TODO log here
			return;
		IArtifactRepository[] repos = manager.getKnownRepositories();
		for (int i = 0; i < repos.length; i++) {
			IArtifactRepository repo = repos[i];
			if (repo.getLocation().equals(location)) {
				manager.removeRepository(repo);
				return;
			}
		}
	}

	public static Profile addProfile(String profileId, Properties properties) {
		IProfileRegistry profileRegistry = (IProfileRegistry) ServiceHelper.getService(Activator.getContext(), IProfileRegistry.class.getName());
		if (profileRegistry == null)
			return null;
		Profile profile = profileRegistry.getProfile(profileId);
		if (profile != null)
			return profile;

		profile = new Profile(profileId);

		for (Iterator it = properties.keySet().iterator(); it.hasNext();) {
			String key = (String) it.next();
			profile.setValue(key, properties.getProperty(key));
		}

		if (profile.getValue(Profile.PROP_ENVIRONMENTS) == null) {
			EnvironmentInfo info = (EnvironmentInfo) ServiceHelper.getService(Activator.getContext(), EnvironmentInfo.class.getName());
			if (info != null)
				profile.setValue(Profile.PROP_ENVIRONMENTS, "osgi.os=" + info.getOS() + ",osgi.ws=" + info.getWS() + ",osgi.arch=" + info.getOSArch());
			else
				profile.setValue(Profile.PROP_ENVIRONMENTS, "");
		}

		profileRegistry.addProfile(profile);
		return profile;
	}

	public static void removeProfile(String profileId) {
		IProfileRegistry profileRegistry = (IProfileRegistry) ServiceHelper.getService(Activator.getContext(), IProfileRegistry.class.getName());
		if (profileRegistry == null)
			return;
		Profile profile = profileRegistry.getProfile(profileId);
		if (profile != null)
			profileRegistry.removeProfile(profile);
	}

	public static Profile[] getProfiles() {
		IProfileRegistry profileRegistry = (IProfileRegistry) ServiceHelper.getService(Activator.getContext(), IProfileRegistry.class.getName());
		if (profileRegistry == null)
			return new Profile[0];
		return profileRegistry.getProfiles();
	}

	public static Profile getProfile(String id) {
		IProfileRegistry profileRegistry = (IProfileRegistry) ServiceHelper.getService(Activator.getContext(), IProfileRegistry.class.getName());
		if (profileRegistry == null)
			return null;
		return profileRegistry.getProfile(id);
	}

	/**
	 * Returns the installable units with the given id and version
	 * specifications in the given metadata repository.  <code>null</code>
	 * can be used to indicate wildcards for any of the arguments.
	 * 
	 * @param location The location of the metdata repo to search.  <code>null</code> indicates
	 *        search all known repos.
	 * @param id The id of the IUs to find. <code>null</code> indicates
	 *        wildcard.
	 * @param range The version range of the IUs to find. <code>null</code>
	 *        indicates wildcard.
	 * @return The IUs that match the query
	 */
	public static IInstallableUnit[] getInstallableUnits(URL location, String id, VersionRange range, IProgressMonitor progress) {
		IMetadataRepository[] repositories = null;
		if (location == null)
			repositories = getMetadataRepositories();
		else
			repositories = new IMetadataRepository[] {getMetadataRepository(location)};
		Iterator i = Query.getIterator(repositories, id, range, null, false);
		return CompoundIterator.asArray(i, progress);
	}

	/**
	 * Returns the installable units with the given id and version
	 * specifications.
	 * 
	 * @param profileId The profile to search
	 * @param id The id of the IUs to find. <code>null</code> indicates
	 *        wildcard.
	 * @param range The version range of the IUs to find. <code>null</code>
	 *        indicates wildcard.
	 * @return The IUs that match the query
	 */
	public static IInstallableUnit[] getInstallableUnits(String profileId, String id, VersionRange range, IProgressMonitor progress) {
		Profile[] profiles = null;
		if (profileId == null)
			profiles = getProfiles();
		else
			profiles = new Profile[] {getProfile(profileId)};
		Iterator i = Query.getIterator(profiles, id, range, null, false);
		return CompoundIterator.asArray(i, progress);
	}

	public static IMetadataRepository[] getMetadataRepositories() {
		IMetadataRepositoryManager manager = (IMetadataRepositoryManager) ServiceHelper.getService(Activator.getContext(), IMetadataRepositoryManager.class.getName());
		if (manager == null)
			// TODO log here
			return null;
		IMetadataRepository[] repos = manager.getKnownRepositories();
		if (repos.length > 0)
			return repos;
		return null;
	}

	/**
	 * Install the described IU
	 */
	public static IStatus install(String unitId, String version, Profile profile, IProgressMonitor progress) throws ProvisionException {
		IMetadataRepository[] repos = getMetadataRepositories();
		if (repos == null || profile == null)
			return null;
		// search for a matching IU in the known repositories
		IInstallableUnit toInstall = null;
		Version unitVersion = new Version(version);
		outer: for (int i = 0; i < repos.length; i++) {
			IInstallableUnit[] ius = repos[i].getInstallableUnits(progress);
			for (int j = 0; j < ius.length; j++) {
				if (unitId.equals(ius[j].getId()) && unitVersion.equals(ius[j].getVersion())) {
					toInstall = ius[j];
					break outer;
				}
			}
		}
		if (toInstall == null) {
			StringBuffer error = new StringBuffer();
			error.append("Installable unit not found: " + unitId + ' ' + unitVersion + '\n');
			error.append("Repositories searched:\n");
			for (int i = 0; i < repos.length; i++)
				error.append(repos[i].getLocation() + "\n");
			throw new ProvisionException(error.toString());
		}

		IDirector director = (IDirector) ServiceHelper.getService(Activator.getContext(), IDirector.class.getName());
		if (director == null)
			throw new ProvisionException("No director service found.");

		return director.install(new IInstallableUnit[] {toInstall}, profile, null, progress);
	}

	/**
	 * Uninstall the described IU
	 */
	public static IStatus uninstall(String unitId, String version, Profile profile, IProgressMonitor progress) throws ProvisionException {
		IDirector director = (IDirector) ServiceHelper.getService(Activator.getContext(), IDirector.class.getName());
		if (director == null)
			throw new ProvisionException("No director service found.");

		// return director.uninstall(new InstallableUnit[] {toInstall}, profile,
		// null);
		return null;
	}

	public static void kick(String profileId) {
		Configurator configurator = (Configurator) ServiceHelper.getService(Activator.getContext(), Configurator.class.getName());
		if (configurator == null)
			return;
		if (profileId == null)
			try {
				configurator.applyConfiguration();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else {
			// TODO do some work here to figure out how to kick some random profile			
			//					configurator.applyConfiguration(configURL);
		}
	}

	public static IArtifactRepository[] getArtifactRepositories() {
		IArtifactRepositoryManager manager = (IArtifactRepositoryManager) ServiceHelper.getService(Activator.getContext(), IArtifactRepositoryManager.class.getName());
		if (manager == null)
			// TODO log here
			return null;
		IArtifactRepository[] repos = manager.getKnownRepositories();
		if (repos.length > 0)
			return repos;
		return null;
	}

	public static IArtifactRepository getArtifactRepository(URL repoURL) {
		IArtifactRepository[] repositories = getArtifactRepositories();
		if (repositories == null)
			return null;
		for (int i = 0; i < repositories.length; i++)
			if (repoURL.equals(repositories[i].getLocation()))
				return repositories[i];
		return null;
	}
}

Back to the top