Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: acbca843637ce88b5ce7a4630b917b76faca3f8f (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
/*******************************************************************************
 *  Copyright (c) 2008, 2009 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.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
import org.eclipse.equinox.p2.engine.spi.Touchpoint;
import org.eclipse.equinox.p2.metadata.ITouchpointType;
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.osgi.util.NLS;

public class ActionManager implements IRegistryChangeListener {

	private static final String PT_ACTIONS = "actions"; //$NON-NLS-1$
	private static final String ELEMENT_ACTION = "action"; //$NON-NLS-1$
	private static final String ATTRIBUTE_CLASS = "class"; //$NON-NLS-1$
	private static final String ATTRIBUTE_NAME = "name"; //$NON-NLS-1$
	private static final String TOUCHPOINT_TYPE = "touchpointType"; //$NON-NLS-1$
	private static final String TOUCHPOINT_VERSION = "touchpointVersion"; //$NON-NLS-1$
	/**
	 * Service name constant for the action manager service. This service is used internally
	 * by the engine implementation and should not be referenced directly by clients.
	 */
	public static final String SERVICE_NAME = ActionManager.class.getName();

	private HashMap<String, IConfigurationElement> actionMap;
	private TouchpointManager touchpointManager;

	public ActionManager() {
		this.touchpointManager = new TouchpointManager();
		RegistryFactory.getRegistry().addRegistryChangeListener(this, EngineActivator.ID);
	}

	public Touchpoint getTouchpointPoint(ITouchpointType type) {
		if (type == null || type == ITouchpointType.NONE)
			return null;
		return touchpointManager.getTouchpoint(type);
	}

	public String getTouchpointQualifiedActionId(String actionId, ITouchpointType type) {
		if (actionId.indexOf('.') == -1) {
			if (type == null || type == ITouchpointType.NONE)
				return actionId;

			Touchpoint touchpoint = touchpointManager.getTouchpoint(type);
			if (touchpoint == null)
				throw new IllegalArgumentException(NLS.bind(Messages.ActionManager_Required_Touchpoint_Not_Found, type.toString(), actionId));
			actionId = touchpoint.qualifyAction(actionId);
		}
		return actionId;
	}

	public ProvisioningAction getAction(String actionId, VersionRange versionRange) {
		IConfigurationElement actionElement = getActionMap().get(actionId);
		if (actionElement != null && actionElement.isValid()) {
			try {
				ProvisioningAction action = (ProvisioningAction) actionElement.createExecutableExtension(ATTRIBUTE_CLASS);

				String touchpointType = actionElement.getAttribute(TOUCHPOINT_TYPE);
				if (touchpointType != null) {
					String touchpointVersion = actionElement.getAttribute(TOUCHPOINT_VERSION);
					Touchpoint touchpoint = touchpointManager.getTouchpoint(touchpointType, touchpointVersion);
					if (touchpoint == null)
						throw new IllegalArgumentException(NLS.bind(Messages.ActionManager_Required_Touchpoint_Not_Found, touchpointType, actionId));
					action.setTouchpoint(touchpoint);
				}
				return action;
			} catch (InvalidRegistryObjectException e) {
				// skip
			} catch (CoreException e) {
				throw new IllegalArgumentException(NLS.bind(Messages.ActionManager_Exception_Creating_Action_Extension, actionId));
			}
		}
		return null;
	}

	private synchronized Map<String, IConfigurationElement> getActionMap() {
		if (actionMap != null)
			return actionMap;
		IExtensionPoint point = RegistryFactory.getRegistry().getExtensionPoint(EngineActivator.ID, PT_ACTIONS);
		IExtension[] extensions = point.getExtensions();
		actionMap = new HashMap<String, IConfigurationElement>(extensions.length);
		for (int i = 0; i < extensions.length; i++) {
			try {
				IConfigurationElement[] elements = extensions[i].getConfigurationElements();
				for (int j = 0; j < elements.length; j++) {
					IConfigurationElement actionElement = elements[j];
					if (!actionElement.getName().equals(ELEMENT_ACTION))
						continue;

					String actionId = actionElement.getAttribute(ATTRIBUTE_NAME);
					if (actionId == null)
						continue;

					if (actionId.indexOf('.') == -1)
						actionId = actionElement.getNamespaceIdentifier() + "." + actionId; //$NON-NLS-1$

					actionMap.put(actionId, actionElement);
				}
			} catch (InvalidRegistryObjectException e) {
				// skip
			}
		}
		return actionMap;
	}

	public synchronized void registryChanged(IRegistryChangeEvent event) {
		actionMap = null;
	}

	static void reportError(String errorMsg) {
		Status errorStatus = new Status(IStatus.ERROR, EngineActivator.ID, 1, errorMsg, null);
		LogHelper.log(errorStatus);
	}

}

Back to the top