| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2017 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.internal.p2.engine; |
| 15 |
|
| 16 |
import java.util.HashMap; |
| 17 |
import java.util.Map; |
| 18 |
import org.eclipse.core.runtime.*; |
| 19 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 20 |
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction; |
| 21 |
import org.eclipse.equinox.p2.engine.spi.Touchpoint; |
| 22 |
import org.eclipse.equinox.p2.metadata.ITouchpointType; |
| 23 |
import org.eclipse.equinox.p2.metadata.VersionRange; |
| 24 |
import org.eclipse.osgi.util.NLS; |
| 25 |
|
| 26 |
public class ActionManager implements IRegistryChangeListener { |
| 27 |
|
| 28 |
private static final String PT_ACTIONS = "actions"; //$NON-NLS-1$ |
| 29 |
private static final String ELEMENT_ACTION = "action"; //$NON-NLS-1$ |
| 30 |
private static final String ATTRIBUTE_CLASS = "class"; //$NON-NLS-1$ |
| 31 |
private static final String ATTRIBUTE_NAME = "name"; //$NON-NLS-1$ |
| 32 |
private static final String TOUCHPOINT_TYPE = "touchpointType"; //$NON-NLS-1$ |
| 33 |
private static final String TOUCHPOINT_VERSION = "touchpointVersion"; //$NON-NLS-1$ |
| 34 |
/** |
| 35 |
* Service name constant for the action manager service. This service is used internally |
| 36 |
* by the engine implementation and should not be referenced directly by clients. |
| 37 |
*/ |
| 38 |
public static final String SERVICE_NAME = ActionManager.class.getName(); |
| 39 |
|
| 40 |
private HashMap<String, IConfigurationElement> actionMap; |
| 41 |
private TouchpointManager touchpointManager; |
| 42 |
|
| 43 |
public ActionManager() { |
| 44 |
this.touchpointManager = new TouchpointManager(); |
| 45 |
RegistryFactory.getRegistry().addRegistryChangeListener(this, EngineActivator.ID); |
| 46 |
} |
| 47 |
|
| 48 |
public Touchpoint getTouchpointPoint(ITouchpointType type) { |
| 49 |
if (type == null || type == ITouchpointType.NONE) |
| 50 |
return null; |
| 51 |
return touchpointManager.getTouchpoint(type); |
| 52 |
} |
| 53 |
|
| 54 |
public String getTouchpointQualifiedActionId(String actionId, ITouchpointType type) { |
| 55 |
if (actionId.indexOf('.') == -1) { |
| 56 |
if (type == null || type == ITouchpointType.NONE) |
| 57 |
return actionId; |
| 58 |
|
| 59 |
Touchpoint touchpoint = touchpointManager.getTouchpoint(type); |
| 60 |
if (touchpoint == null) |
| 61 |
throw new IllegalArgumentException(NLS.bind(Messages.ActionManager_Required_Touchpoint_Not_Found, type.toString(), actionId)); |
| 62 |
actionId = touchpoint.qualifyAction(actionId); |
| 63 |
} |
| 64 |
return actionId; |
| 65 |
} |
| 66 |
|
| 67 |
public ProvisioningAction getAction(String actionId, VersionRange versionRange) { |
| 68 |
IConfigurationElement actionElement = getActionMap().get(actionId); |
| 69 |
if (actionElement != null && actionElement.isValid()) { |
| 70 |
try { |
| 71 |
ProvisioningAction action = (ProvisioningAction) actionElement.createExecutableExtension(ATTRIBUTE_CLASS); |
| 72 |
|
| 73 |
String touchpointType = actionElement.getAttribute(TOUCHPOINT_TYPE); |
| 74 |
if (touchpointType != null) { |
| 75 |
String touchpointVersion = actionElement.getAttribute(TOUCHPOINT_VERSION); |
| 76 |
Touchpoint touchpoint = touchpointManager.getTouchpoint(touchpointType, touchpointVersion); |
| 77 |
if (touchpoint == null) |
| 78 |
throw new IllegalArgumentException(NLS.bind(Messages.ActionManager_Required_Touchpoint_Not_Found, touchpointType, actionId)); |
| 79 |
action.setTouchpoint(touchpoint); |
| 80 |
} |
| 81 |
return action; |
| 82 |
} catch (InvalidRegistryObjectException e) { |
| 83 |
// skip |
| 84 |
} catch (CoreException e) { |
| 85 |
throw new IllegalArgumentException(NLS.bind(Messages.ActionManager_Exception_Creating_Action_Extension, actionId)); |
| 86 |
} |
| 87 |
} |
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
private synchronized Map<String, IConfigurationElement> getActionMap() { |
| 92 |
if (actionMap != null) |
| 93 |
return actionMap; |
| 94 |
IExtensionPoint point = RegistryFactory.getRegistry().getExtensionPoint(EngineActivator.ID, PT_ACTIONS); |
| 95 |
IExtension[] extensions = point.getExtensions(); |
| 96 |
actionMap = new HashMap<>(extensions.length); |
| 97 |
for (IExtension extension : extensions) { |
| 98 |
try { |
| 99 |
IConfigurationElement[] elements = extension.getConfigurationElements(); |
| 100 |
for (IConfigurationElement actionElement : elements) { |
| 101 |
if (!actionElement.getName().equals(ELEMENT_ACTION)) |
| 102 |
continue; |
| 103 |
|
| 104 |
String actionId = actionElement.getAttribute(ATTRIBUTE_NAME); |
| 105 |
if (actionId == null) |
| 106 |
continue; |
| 107 |
|
| 108 |
if (actionId.indexOf('.') == -1) |
| 109 |
actionId = actionElement.getNamespaceIdentifier() + "." + actionId; //$NON-NLS-1$ |
| 110 |
|
| 111 |
actionMap.put(actionId, actionElement); |
| 112 |
} |
| 113 |
}catch (InvalidRegistryObjectException e) { |
| 114 |
// skip |
| 115 |
} |
| 116 |
} |
| 117 |
return actionMap; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public synchronized void registryChanged(IRegistryChangeEvent event) { |
| 122 |
actionMap = null; |
| 123 |
} |
| 124 |
|
| 125 |
static void reportError(String errorMsg) { |
| 126 |
Status errorStatus = new Status(IStatus.ERROR, EngineActivator.ID, 1, errorMsg, null); |
| 127 |
LogHelper.log(errorStatus); |
| 128 |
} |
| 129 |
|
| 130 |
} |