Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5dfc0e2a3c4dd8141dc36fc5beb51c7bdcaf6115 (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
/*******************************************************************************
 * 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.p2.director.app;

import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.equinox.internal.p2.console.ProvisioningHelper;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
import org.eclipse.equinox.p2.director.*;
import org.eclipse.equinox.p2.engine.*;
import org.eclipse.equinox.p2.engine.phases.Sizing;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.repository.IMetadataRepository;
import org.eclipse.equinox.p2.query.IQueryable;
import org.eclipse.equinox.p2.query.Query;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.osgi.framework.Version;

/**
 * @since 3.3
 */
public class Application implements IApplication {

	private String destination;
	private URL artifactRepositoryLocation;
	private URL metadataRepositoryLocation;
	private String root;
	private String flavor;
	private String profileId;
	private boolean install;
	private String bundlePool = null;
	private String nl;
	private String os;
	private String arch;
	private String ws;
	private boolean roamingProfile = false;
	private Version version = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
	 */
	public Object start(IApplicationContext context) throws Exception {
		return run((String[]) context.getArguments().get("application.args"));
	}

	public Object run(String[] args) throws Exception {
		long time = -System.currentTimeMillis();
		initializeFromArguments(args);

		Properties props = new Properties();
		props.setProperty(Profile.PROP_INSTALL_FOLDER, destination);
		props.setProperty(Profile.PROP_FLAVOR, flavor);
		if (bundlePool != null)
			if (bundlePool.equals("<destination>"))
				props.setProperty("eclipse.p2.cache", destination);
			else
				props.setProperty("eclipse.p2.cache", bundlePool);
		if (roamingProfile)
			props.setProperty("eclipse.p2.roaming", "true");

		String env = getEnvironmentProperty();
		if (env != null)
			props.setProperty(Profile.PROP_ENVIRONMENTS, env);
		Profile profile = ProvisioningHelper.addProfile(profileId, props);
		String currentFlavor = profile.getValue(Profile.PROP_FLAVOR);
		if (currentFlavor != null && !currentFlavor.endsWith(flavor))
			throw new RuntimeException("Install flavor not consistent with profile flavor");

		IDirector director = (IDirector) ServiceHelper.getService(Activator.getContext(), IDirector.class.getName());
		if (director == null)
			throw new RuntimeException("Director could not be loaded");

		IPlanner planner = (IPlanner) ServiceHelper.getService(Activator.getContext(), IPlanner.class.getName());
		if (planner == null)
			throw new RuntimeException("Planner could not be loaded");

		Engine engine = (Engine) ServiceHelper.getService(Activator.getContext(), Engine.class.getName());
		if (engine == null)
			throw new RuntimeException("Engine could not be loaded");

		ProvisioningHelper.addArtifactRepository(artifactRepositoryLocation);
		IMetadataRepository metadataRepository = ProvisioningHelper.addMetadataRepository(metadataRepositoryLocation);
		IInstallableUnit[] roots = Query.query(new IQueryable[] {metadataRepository}, root, version == null ? null : new VersionRange(version, true, version, true), null, false, null);
		ProvisioningPlan result = null;
		IStatus operationStatus = null;
		if (roots.length > 0) {
			if (install) {
				result = planner.getInstallPlan(roots, profile, new NullProgressMonitor());
			} else {
				result = planner.getUninstallPlan(roots, profile, new NullProgressMonitor());
			}
			if (!result.getStatus().isOK())
				operationStatus = result.getStatus();
			else {
				Sizing sizeComputer = new Sizing(100, "Compute sizes"); //$NON-NLS-1$
				PhaseSet set = new PhaseSet(new Phase[] {sizeComputer}) {};
				operationStatus = engine.perform(profile, set, result.getOperands(), new NullProgressMonitor());
				System.out.println("Estimated size on disk " + sizeComputer.getDiskSize());
				System.out.println("Estimated download size " + sizeComputer.getDlSize());
				operationStatus = engine.perform(profile, new DefaultPhaseSet(), result.getOperands(), new NullProgressMonitor());
			}
		} else {
			operationStatus = new Status(IStatus.INFO, "org.eclipse.equinox.p2.director.test", "The installable unit '" + root + "' has not been found");
		}

		time += System.currentTimeMillis();
		if (operationStatus.isOK()) {
			System.out.println((install ? "installation" : "uninstallation") + " complete (" + time + "ms)");
		} else {
			System.out.println((install ? "installation" : "uninstallation") + " failed. " + operationStatus);
			LogHelper.log(operationStatus);
		}
		return null;
	}

	public void stop() {
	}

	public void initializeFromArguments(String[] args) throws Exception {
		if (args == null)
			return;
		for (int i = 0; i < args.length; i++) {

			if (args[i].equals("-roaming")) {
				roamingProfile = true;
			}

			// check for args without parameters (i.e., a flag arg)

			// check for args with parameters. If we are at the last
			// argument or
			// if the next one
			// has a '-' as the first character, then we can't have an arg
			// with
			// a parm so continue.
			if (i == args.length - 1 || args[i + 1].startsWith("-")) //$NON-NLS-1$
				continue;

			String arg = args[++i];

			if (args[i - 1].equalsIgnoreCase("-profile"))
				profileId = arg;

			// we create a path object here to handle ../ entries in the middle of paths
			if (args[i - 1].equalsIgnoreCase("-destination") || args[i - 1].equalsIgnoreCase("-dest"))
				destination = new Path(arg).toOSString();

			// we create a path object here to handle ../ entries in the middle of paths
			if (args[i - 1].equalsIgnoreCase("-bundlepool") || args[i - 1].equalsIgnoreCase("-bp"))
				bundlePool = new Path(arg).toOSString();

			if (args[i - 1].equalsIgnoreCase("-metadataRepository") || args[i - 1].equalsIgnoreCase("-mr"))
				metadataRepositoryLocation = new URL(arg);

			if (args[i - 1].equalsIgnoreCase("-artifactRepository") | args[i - 1].equalsIgnoreCase("-ar"))
				artifactRepositoryLocation = new URL(arg);

			if (args[i - 1].equalsIgnoreCase("-flavor"))
				flavor = arg;

			if (args[i - 1].equalsIgnoreCase("-installIU")) {
				root = arg;
				install = true;
			}

			if (args[i - 1].equalsIgnoreCase("-version")) {
				version = new Version(arg);
			}

			if (args[i - 1].equalsIgnoreCase("-uninstallIU")) {
				root = arg;
				install = false;
			}

			if (args[i - 1].equalsIgnoreCase("-p2.os")) {
				os = arg;
			}
			if (args[i - 1].equalsIgnoreCase("-p2.ws")) {
				ws = arg;
			}
			if (args[i - 1].equalsIgnoreCase("-p2.nl")) {
				nl = arg;
			}
			if (args[i - 1].equalsIgnoreCase("-p2.arch")) {
				arch = arg;
			}
		}
	}

	private String getEnvironmentProperty() {
		Properties values = new Properties();
		if (os != null)
			values.put("osgi.os", os);
		if (nl != null)
			values.put("osgi.nl", nl);
		if (ws != null)
			values.put("osgi.ws", ws);
		if (arch != null)
			values.put("osgi.arch", arch);
		if (values.isEmpty())
			return null;
		return toString(values);
	}

	private String toString(Properties context) {
		StringBuffer result = new StringBuffer();
		for (Enumeration iter = context.keys(); iter.hasMoreElements();) {
			String key = (String) iter.nextElement();
			result.append(key);
			result.append('=');
			result.append(context.get(key));
			if (iter.hasMoreElements())
				result.append(',');
		}
		return result.toString();
	}
}

Back to the top