Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b8db9093260dee424c8b46515cf4ab72672f44d (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
/*******************************************************************************
 * Copyright (c) 2008, 2010 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.garbagecollector;

import java.util.Map;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.equinox.p2.core.*;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.engine.IProfileRegistry;

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

	private static final String DEFAULT_PROFILE_ID = "SDKProfile"; //$NON-NLS-1$
	private String profileId;
	private IProvisioningAgent agent;

	/*
	 * Return the profile with the given id, or null if not found.
	 */
	private IProfile getProfile(String id) {
		IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
		return profileRegistry == null ? null : profileRegistry.getProfile(id);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
	 */
	public Object start(IApplicationContext context) throws Exception {
		Map<?, ?> allArgs = context.getArguments();
		String[] args = (String[]) allArgs.get(IApplicationContext.APPLICATION_ARGS);
		processArguments(args);
		// if the user didn't give us a profile id, then use the default SDK one
		if (profileId == null)
			profileId = DEFAULT_PROFILE_ID;

		initializeServices();
		IProfile profile = getProfile(profileId);
		if (profile == null)
			throw new IllegalArgumentException("\"" + profileId + "\" is not a valid profile identifier."); //$NON-NLS-1$//$NON-NLS-2$
		GarbageCollector gc = (GarbageCollector) agent.getService(GarbageCollector.SERVICE_NAME);
		gc.runGC(profile);
		agent.stop();
		return null;
	}

	private void initializeServices() throws ProvisionException {
		IProvisioningAgentProvider provider = GCActivator.getService(IProvisioningAgentProvider.class);
		agent = provider.createAgent(null);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop() {
		// nothing to do
	}

	/*
	 * Iterate over the command-line arguments and pull out the ones which are important to us.
	 */
	public void processArguments(String[] args) throws Exception {
		if (args == null)
			return;
		for (int i = 0; i < args.length; i++) {
			String opt = args[i];
			// 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 (opt.equalsIgnoreCase("-profile")) //$NON-NLS-1$
				profileId = arg;
		}
	}
}

Back to the top