feisty meow concerns codebase 2.140
RegistryEditor.java
Go to the documentation of this file.
1
2package org.feistymeow.system;
3
4import java.io.*;
5
6/*
7 * this class supports reading and setting values in the Windows registry.
8 *
9 * some example code came from http://www.rgagnon.com/javadetails/java-0480.html
10 * @author Chris Koeritz
11 */
12public class RegistryEditor {
13
14 // the values below can be used for the expected type in the registry calls.
15 public static final String STRING_TYPE = "REG_SZ";
16 public static final String DOUBLEWORD_TYPE = "REG_DWORD";
17
18 // looks up the key provided in the registry and returns true if the key seems to exist.
19 // this is just a check for presence; it could have no subkeys or values under it, but it at
20 // least seems to be there.
21 public static boolean checkKey(String keyName) {
22 try {
23 // make a command to run in windows that will query the value.
24 StringBuilder command = new StringBuilder(QUERY_COMMAND);
25 command.append("\"");
26 command.append(keyName);
27 command.append("\"");
28 // start the command running and trap its output.
29 Process process = Runtime.getRuntime().exec(command.toString());
30 StreamReader reader = new StreamReader(process.getInputStream());
31 reader.start();
32 process.waitFor(); // let the system call finish up.
33 reader.join(); // let the stream reader finish also.
34 return process.exitValue() == 0;
35 } catch (Exception e) {
36 return false;
37 }
38 }
39
40 // retrieves the key provided from the registry and selects out the value requested.
41 // it is necessary to know the expected type, such as REG_SZ or REG_DWORD.
42 // the value is always returned as a string and needs to be cast to different types as appropriate.
43 public static String getValue(String keyName, String valueName, String typeExpected) {
44 try {
45 // make a command to run in windows that will query the value.
46 StringBuilder command = new StringBuilder(QUERY_COMMAND);
47 command.append("\"");
48 command.append(keyName);
49 command.append("\"");
50 command.append(VALUE_FLAG);
51 command.append(valueName);
52 // start the command running and trap its output.
53 Process process = Runtime.getRuntime().exec(command.toString());
54 StreamReader reader = new StreamReader(process.getInputStream());
55 reader.start();
56 process.waitFor(); // let the system call finish up.
57 reader.join(); // let the stream reader finish also.
58 // now grab the results from running the command and extract the answer.
59 String result = reader.getResult();
60 int p = result.indexOf(typeExpected);
61 if (p == -1) return null;
62 // return the result with the stuff before the type chopped off.
63 return result.substring(p + typeExpected.length()).trim();
64 } catch (Exception e) {
65 return null;
66 }
67 }
68
69 // makes a change to the specified "keyName" value called "valueName". the old value will be replaced
70 // with the "newValue" provided. the key and the value do not have to exist prior to the call, but if
71 // they already did exist, they'll be updated.
72 public static boolean setValue(String keyName, String valueName, String typeExpected, String newValue) {
73 try {
74 // make a command to run in windows that will set the value.
75 StringBuilder command = new StringBuilder(SET_COMMAND);
76 command.append("\"");
77 command.append(keyName);
78 command.append("\"");
79 command.append(VALUE_FLAG);
80 command.append(valueName);
81 command.append(TYPE_FLAG);
82 command.append(typeExpected);
83 command.append(FORCE_FLAG);
84 command.append(DATA_FLAG);
85 command.append(newValue);
86 //System.out.println("command to run: " + command);
87 // start the command running and trap its output.
88 Process process = Runtime.getRuntime().exec(command.toString());
89 StreamReader reader = new StreamReader(process.getInputStream());
90 reader.start();
91 process.waitFor(); // let the system call finish up.
92 reader.join(); // let the stream reader finish also.
93 return (process.exitValue() == 0); // zero exit is a success.
94 } catch (Exception e) {
95 return false;
96 }
97 }
98
99 // removes the "valueName" value from the key "keyName". true is returned on success.
100 public static boolean deleteValue(String keyName, String valueName) {
101 try {
102 // make a command to run in windows that will set the value.
103 StringBuilder command = new StringBuilder(DELETE_COMMAND);
104 command.append("\"");
105 command.append(keyName);
106 command.append("\"");
107 command.append(VALUE_FLAG);
108 command.append(valueName);
109 command.append(FORCE_FLAG);
110 //System.out.println("command to run: " + command);
111 // start the command running and trap its output.
112 Process process = Runtime.getRuntime().exec(command.toString());
113 StreamReader reader = new StreamReader(process.getInputStream());
114 reader.start();
115 process.waitFor(); // let the system call finish up.
116 reader.join(); // let the stream reader finish also.
117 return (process.exitValue() == 0); // zero exit is a success.
118 } catch (Exception e) {
119 return false;
120 }
121 }
122
123 // removes the entire key "keyName" from the registry. true is returned on success.
124 public static boolean deleteKey(String keyName) {
125 try {
126 // make a command to run in windows that will set the value.
127 StringBuilder command = new StringBuilder(DELETE_COMMAND);
128 command.append("\"");
129 command.append(keyName);
130 command.append("\"");
131 command.append(FORCE_FLAG);
132 //System.out.println("command to run: " + command);
133 // start the command running and trap its output.
134 Process process = Runtime.getRuntime().exec(command.toString());
135 StreamReader reader = new StreamReader(process.getInputStream());
136 reader.start();
137 process.waitFor(); // let the system call finish up.
138 reader.join(); // let the stream reader finish also.
139 return (process.exitValue() == 0); // zero exit is a success.
140 } catch (Exception e) {
141 return false;
142 }
143 }
144
145 // constants used in the registry code for talking to windows' reg application.
146 private static final String QUERY_COMMAND = "reg query ";
147 private static final String VALUE_FLAG = " /v ";
148
149 private static final String SET_COMMAND = "reg add ";
150 private static final String TYPE_FLAG = " /t ";
151 private static final String DATA_FLAG = " /d ";
152 private static final String FORCE_FLAG = " /f ";
153
154 private static final String DELETE_COMMAND = "reg delete ";
155
156 // wrapper class for stream reading came from web example mentioned above.
157 static class StreamReader extends Thread {
158 private InputStream is;
159 private StringWriter sw;
160
161 StreamReader(InputStream is) {
162 this.is = is;
163 sw = new StringWriter();
164 }
165
166 public void run() {
167 int c;
168 try {
169 while ((c = is.read()) != -1) sw.write(c);
170 } catch (IOException e) { /*nothing*/ }
171 }
172
173 String getResult() { return sw.toString(); }
174 }
175}
static boolean deleteKey(String keyName)
static boolean setValue(String keyName, String valueName, String typeExpected, String newValue)
static boolean checkKey(String keyName)
static String getValue(String keyName, String valueName, String typeExpected)
static boolean deleteValue(String keyName, String valueName)