| package org.eclipse.epp.installer.internal.win32.core.operations; |
| |
| import java.text.MessageFormat; |
| |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.epp.installer.core.IErrorConstants; |
| import org.eclipse.epp.installer.core.Win32; |
| import org.eclipse.epp.installer.core.model.Context; |
| import org.eclipse.epp.installer.core.model.InstallOperation; |
| import org.eclipse.epp.installer.internal.core.InstallLogEntry; |
| |
| import com.ice.jni.registry.Registry; |
| import com.ice.jni.registry.RegistryException; |
| import com.ice.jni.registry.RegistryKey; |
| |
| /** |
| * CreateRegistryKeyOperation used to create specified Windows registry key.<br/> |
| * |
| * Key are created in HKEY_LOCAL_MACHINE section of registry. This operation is |
| * Windows only specific.<br/> |
| * |
| * To successful perform of this operation, user must have rights to write into |
| * HKEY_LOCAL_MACHINE section of registry.<br/> |
| */ |
| public class CreateRegistryKeyOperation extends InstallOperation { |
| /** |
| * Log entry opcode for this operation. |
| */ |
| public final static int REGISTRY_CREATE_KEY = (int) 'K'; |
| |
| /** |
| * Registry path in HKEY_LOCAL_MACHINE. |
| */ |
| private String path; |
| |
| /** |
| * Registry key name. |
| */ |
| private String name; |
| |
| /** |
| * Construct new instance of CreateRegistryKeyOperation, with path and key |
| * name. |
| * |
| * @param path |
| * registry path, in HKEY_LOCAL_MACHINE section of registry. |
| * @param name |
| * key name. |
| */ |
| public CreateRegistryKeyOperation(String path, String name) { |
| this.path = path; |
| this.name = name; |
| } |
| |
| /** |
| * Run specified operation. |
| * |
| * To successful perform of this operation, user must have rights to write |
| * into HKEY_LOCAL_MACHINE section of registry. |
| * |
| * @return execution status code. If operation coul'd not be performed then |
| * error status with ERROR_REGISTRY_ERROR code are returned. See |
| * {@link org.eclipse.epp.installer.core.IErrorConstants } for |
| * details. |
| */ |
| protected IStatus run(Context installer) { |
| RegistryKey key = Registry.openSubkey(Registry.HKEY_LOCAL_MACHINE, |
| path, RegistryKey.ACCESS_WRITE); |
| if (key == null) |
| return new Status(IStatus.ERROR, Win32.PLUGIN_ID, |
| IErrorConstants.ERROR_REGISTRY_ERROR, |
| "Can't open key for write: " + path, null); |
| try { |
| key.createSubKey(name, null); |
| installer.getInstallLog().addEntry(new InstallLogEntry(REGISTRY_CREATE_KEY, path |
| + ":" + name)); |
| return Status.OK_STATUS; |
| } catch (RegistryException e) { |
| return new Status(IStatus.ERROR, Win32.PLUGIN_ID, |
| IErrorConstants.ERROR_REGISTRY_ERROR, |
| "Can't create subkey: " + name, e); |
| } finally { |
| try { |
| key.closeKey(); |
| } catch (RegistryException e) { |
| return new Status(IStatus.ERROR, Win32.PLUGIN_ID, |
| IErrorConstants.ERROR_REGISTRY_ERROR, "Registry error", |
| e); |
| } |
| } |
| } |
| |
| /** |
| * Return this operation name. |
| */ |
| public String toString() { |
| return MessageFormat.format("Creating Registry key {0}.", |
| new String[] { name }); |
| } |
| } |