blob: fd30e3de77be8e25726f36fc03c15387ac5b29d5 [file] [log] [blame]
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.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
import com.ice.jni.registry.RegistryValue;
/**
* RegistrySetValueOperation used to set value of specified Windows registry key.<br/>
*
* Key should be pressent in HKEY_LOCAL_MACHINE section of registry to perform this operation.<br/>
* To create key you can use: {@link org.eclipse.epp.installer.internal.win32.core.operations.CreateRegistryKeyOperation }.<br/>
*
* Only string value are supported at the moment.<br/>
*
* This operation is Windows only specific.<br/>
*/
public class RegistrySetValueOperation extends InstallOperation {
/**
* Log entry opcode for this operation.
*/
public final static int REGISTRY_SET_VALUE = (int)'V';
/**
* Registry path in HKEY_LOCAL_MACHINE.
*/
private String path;
/**
* Registry key name.
*/
private String name;
/**
* Value to set.
*/
private Object value;
/**
* Construct new instance of RegistrySetValueOperation, with path, key name and value.
*
* @param path registry path, in HKEY_LOCAL_MACHINE section of registry.
* @param name key name.
* @param value value to set.
*/
public RegistrySetValueOperation(String path, String name, Object value) {
this.path = path;
this.name = name;
this.value = value;
}
/**
* 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 writing: " + path, null);
try {
RegistryValue v = null;
if(value instanceof String)
v = new RegStringValue(key, name, (String)value);
if(v != null) {
key.setValue(v);
installer.getInstallLog().addEntry(new InstallLogEntry(REGISTRY_SET_VALUE, path + ":" + name));
}
return Status.OK_STATUS;
} catch(RegistryException e) {
return new Status(IStatus.ERROR, Win32.PLUGIN_ID,
IErrorConstants.ERROR_REGISTRY_ERROR, "Registry error",
e);
} finally {
try {
key.closeKey();
} catch(RegistryException e) {
return new Status(IStatus.ERROR, Win32.PLUGIN_ID,
IErrorConstants.ERROR_REGISTRY_ERROR, "Registry error",
e);
}
}
}
/**
* Return operation name.
*/
public String toString() {
return MessageFormat.format("Writing registry value {0}.", new Object[] {name});
}
}