Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 66b03c6a172ab609ae6a83b0bdb0f63442f67174 (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 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.engine;

import java.util.*;
import java.util.Map.Entry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.equinox.internal.provisional.p2.engine.ProvisioningAction;

public class ParameterizedProvisioningAction extends ProvisioningAction {

	private ProvisioningAction action;
	private Map actionParameters;

	public ParameterizedProvisioningAction(ProvisioningAction action, Map actionParameters) {
		if (action == null || actionParameters == null)
			throw new IllegalArgumentException("Both action and action pararameters must not be null."); //$NON-NLS-1$
		this.action = action;
		this.actionParameters = actionParameters;
	}

	public IStatus execute(Map parameters) {
		parameters = processActionParameters(parameters);
		return action.execute(parameters);
	}

	public IStatus undo(Map parameters) {
		parameters = processActionParameters(parameters);
		return action.undo(parameters);
	}

	private Map processActionParameters(Map parameters) {
		Map result = new HashMap(parameters);
		for (Iterator it = actionParameters.entrySet().iterator(); it.hasNext();) {
			Entry entry = (Entry) it.next();
			String name = (String) entry.getKey();
			String value = processVariables((String) entry.getValue(), parameters);
			result.put(name, value);
		}
		return Collections.unmodifiableMap(result);
	}

	private String processVariables(String parameterValue, Map parameters) {

		int variableBeginIndex = parameterValue.indexOf("${"); //$NON-NLS-1$
		if (variableBeginIndex == -1)
			return parameterValue;

		int variableEndIndex = parameterValue.indexOf('}', variableBeginIndex + 2);
		if (variableEndIndex == -1)
			return parameterValue;

		String preVariable = parameterValue.substring(0, variableBeginIndex);
		String variableName = parameterValue.substring(variableBeginIndex + 2, variableEndIndex);
		String variableValue = parameters.get(variableName).toString();
		String postVariable = processVariables(parameterValue.substring(variableEndIndex + 1), parameters);
		return preVariable + variableValue + postVariable;
	}
}

Back to the top