Developers, Developers, Developers! Maksim Sorokin IT Blog

27Aug/103

Working with Windows Registry in Java 2

Ok, so the way to read registry data, which I described in previous post may be not a good idea. It is safer to rely on Windows Reg command. I wrote a simple utility to query registry using that utility.

  • isExists finds whereas registry path exists
  • get gets the value for specific key on certain registry path

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class WindowsRegistry {

  public static boolean isExists(WindowsRegistryRootKey rootKey, String registryPath) throws Exception {
    return (getOutput(rootKey, registryPath).size() > 0);
  }

  public static String get(WindowsRegistryRootKey rootKey, String registryPath, String key) throws Exception {
    List output = getOutput(rootKey, registryPath, key);

    return (getValueFromOutput(output));
  }

  static List getOutput(WindowsRegistryRootKey rootKey, String registryPath) throws Exception {
    if ((rootKey == null) || (registryPath == null)) {
      return new ArrayList();
    }

    String command = String.format("reg query \"%s\\%s\"", rootKey.getRootKey(), registryPath);

    return execRegQuery(command);
  }

  public static List getOutput(WindowsRegistryRootKey rootKey, String registryPath, String key) throws Exception {
    if ((rootKey == null) || (registryPath == null) || (key == null)) {
      return new ArrayList();
    }

    String command = String.format("reg query \"%s\\%s\" /v %s", rootKey.getRootKey(), registryPath, key);

    return execRegQuery(command);
  }

  private static List execRegQuery(String command) throws Exception {
    List result = new ArrayList();

    Process process = Runtime.getRuntime().exec(command);

    InputStreamReader isr = new InputStreamReader(process.getInputStream());
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
      result.add(line);
    }

    return result;
  }

  static String getValueFromOutput(List output) {
    for (String line : output) {
      String[] chunks = line.split("    ");
      if (isChunksValid(chunks)) {
        return chunks[3];
      }
    }

    return null;
  }

  private static boolean isChunksValid(String[] chunks) {
    if ((chunks.length != 4) || (chunks[0].trim().length() != 0)) {
      return false;
    }

    for (int i = 1; i < chunks.length; i++) {
      String chunk = chunks[i];
      if (chunk.trim().length() == 0) {
        return false;
      }
    }

    return true;
  }
}
public enum WindowsRegistryRootKey {

  HKEY_LOCAL_MACHINE("HKLM");

  private String rootKey;

  WindowsRegistryRootKey(String rootKey) {
    this.rootKey = rootKey;
  }

  String getRootKey() {
    return rootKey;
  }
}

And tests!

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

public class WindowsRegistryTest {

  @Test
  public void testIsExists_doesntExist() throws Exception {
    Assert.assertFalse(WindowsRegistry.isExists(WindowsRegistryRootKey.HKEY_LOCAL_MACHINE, "a"));
  }

  @Test
  public void testIsExists() throws Exception {
    Assert.assertTrue(WindowsRegistry.isExists(WindowsRegistryRootKey.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion"));
  }

  @Test
  public void testGetOutput_passNull1() throws Exception {
    Assert.assertEquals(0, WindowsRegistry.getOutput(null, "a").size());
  }

  @Test
  public void testGetOutput_passNull2() throws Exception {
    Assert.assertEquals(0, WindowsRegistry.getOutput(WindowsRegistryRootKey.HKEY_LOCAL_MACHINE, null).size());
  }

  @Test
  public void testGetOutput_passNullBoth() throws Exception {
    Assert.assertEquals(0, WindowsRegistry.getOutput(null, null).size());
  }

  @Test
  public void testGetOutput2_passNull1() throws Exception {
    Assert.assertEquals(0, WindowsRegistry.getOutput(null, "a", "b").size());
  }

  @Test
  public void testGetOutput2_passNull2() throws Exception {
    Assert.assertEquals(0, WindowsRegistry.getOutput(WindowsRegistryRootKey.HKEY_LOCAL_MACHINE, "a", null).size());
  }

  @Test
  public void testGetOutput2_passNull3() throws Exception {
    Assert.assertEquals(0, WindowsRegistry.getOutput(WindowsRegistryRootKey.HKEY_LOCAL_MACHINE, null, "b").size());
  }

  @Test
  public void testGetOutput2_passNullAll() throws Exception {
    Assert.assertEquals(0, WindowsRegistry.getOutput(null, null, null).size());
  }

  @Test
  public void testGetValueFromOutput_noValid() throws Exception {
    List output = new ArrayList();
    output.add("HKEY");

    Assert.assertNull(WindowsRegistry.getValueFromOutput(output));
  }

  @Test
  public void testGetValueFromOutput() throws Exception {
    List output = new ArrayList();
    output.add("                              ");
    output.add("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
    output.add("    ProductName    REG_SZ    Windows 7 Professional");

    Assert.assertEquals("Windows 7 Professional", WindowsRegistry.getValueFromOutput(output));
  }
}
Comments (3) Trackbacks (0)
  1. I’m sure the best for you aabags.com online aabags.com for more

  2. Visit us at times to obtain more facts and facts regarding Visit us at times to buy more knowledge and facts at all events Ulotki


Leave a comment


Trackbacks are disabled.