Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RegistryUtil |
|
| 2.5;2.5 |
1 | /* | |
2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package org.seasar.uruma.util.win32; | |
17 | ||
18 | import org.eclipse.swt.internal.win32.OS; | |
19 | import org.eclipse.swt.internal.win32.TCHAR; | |
20 | import org.seasar.uruma.core.UrumaConstants; | |
21 | import org.seasar.uruma.exception.Win32ApiException; | |
22 | import org.seasar.uruma.util.AssertionUtil; | |
23 | ||
24 | /** | |
25 | * Windows レジストリアクセスに関するユーティリティクラスです。<br /> | |
26 | * | |
27 | * @author y-komori | |
28 | */ | |
29 | public class RegistryUtil { | |
30 | 0 | private RegistryUtil() { |
31 | ||
32 | 0 | } |
33 | ||
34 | /** | |
35 | * <code>HKEY_LOCAL_MACHINE</code> を表すハンドルです。<br /> | |
36 | */ | |
37 | 0 | public static int HKEY_LOCAL_MACHINE = OS.HKEY_LOCAL_MACHINE; |
38 | ||
39 | /** | |
40 | * <code>HKEY_CLASSES_ROOT</code> を表すハンドルです。<br /> | |
41 | */ | |
42 | 0 | public static int HKEY_CLASSES_ROOT = OS.HKEY_CLASSES_ROOT; |
43 | ||
44 | /** | |
45 | * <code>HKEY_CURRENT_USER</code> を表すハンドルです。<br /> | |
46 | */ | |
47 | 0 | public static int HKEY_CURRENT_USER = OS.HKEY_CURRENT_USER; |
48 | ||
49 | 0 | private static int KEY_ALL_ACCESS = 0xF003F; |
50 | ||
51 | /** | |
52 | * レジストリをオープンします。<br /> | |
53 | * | |
54 | * @param hKey | |
55 | * ハンドル({@link #HKEY_LOCAL_MACHINE}, {@link #HKEY_CLASSES_ROOT}, | |
56 | * {@link #HKEY_CURRENT_USER} のいずれかを指定) | |
57 | * @param entry | |
58 | * レジストリエントリ | |
59 | * @return レジストリハンドル | |
60 | */ | |
61 | public static RegistryHandle regOpenKey(final int hKey, final String entry) { | |
62 | 0 | AssertionUtil.assertNotNull("entry", entry); |
63 | 0 | RegistryHandle handle = new RegistryHandle(); |
64 | 0 | int retCode = OS.RegOpenKeyEx(hKey, new TCHAR(OS.CP_INSTALLED, entry, |
65 | true), 0, OS.KEY_READ, handle.getPhkResult()); | |
66 | 0 | if (retCode == 0) { |
67 | 0 | return handle; |
68 | } else { | |
69 | 0 | throw new Win32ApiException("RegOpenKeyEX", retCode); |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * レジストリをクローズします。<br /> | |
75 | * | |
76 | * @param handle | |
77 | * オープンされたレジストリハンドル | |
78 | */ | |
79 | public static void regCloseKey(final RegistryHandle handle) { | |
80 | 0 | AssertionUtil.assertNotNull("handle", handle); |
81 | 0 | if (handle.getPointer() != 0) { |
82 | 0 | OS.RegCloseKey(handle.getPointer()); |
83 | } | |
84 | 0 | } |
85 | ||
86 | /** | |
87 | * レジストリから値を読み込みます。<br /> | |
88 | * | |
89 | * @param handle | |
90 | * オープンされたレジストリハンドル | |
91 | * @param valueName | |
92 | * 読み込む値の名称 | |
93 | * @return 読み込んだ結果 | |
94 | */ | |
95 | public static String regQueryValue(final RegistryHandle handle, | |
96 | final String valueName) { | |
97 | 0 | AssertionUtil.assertNotNull("handle", handle); |
98 | 0 | AssertionUtil.assertNotNull("valueName", valueName); |
99 | ||
100 | 0 | TCHAR buf = new TCHAR(OS.CP_INSTALLED, 256); |
101 | 0 | final int[] len = new int[] { 256 }; |
102 | 0 | int retCode = OS.RegQueryValueEx(handle.getPointer(), new TCHAR( |
103 | OS.CP_INSTALLED, valueName, true), 0, null, buf, len); | |
104 | 0 | if (retCode == 0) { |
105 | 0 | return buf.toString(0, buf.strlen()); |
106 | } else { | |
107 | 0 | regCloseKey(handle); |
108 | 0 | throw new Win32ApiException("RegQueryValueEx", retCode); |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * レジストリから値を読み込んで返します。<br /> | |
114 | * | |
115 | * @param hKey | |
116 | * ハンドル({@link #HKEY_LOCAL_MACHINE}, {@link #HKEY_CLASSES_ROOT}, | |
117 | * {@link #HKEY_CURRENT_USER} のいずれかを指定) | |
118 | * @param entry | |
119 | * レジストリエントリ | |
120 | * @param key | |
121 | * キー | |
122 | * @return 読み込んだ値。エントリやキーが見つからなかった場合は <code>null</code> | |
123 | */ | |
124 | public static String getRegistryValue(final int hKey, final String entry, | |
125 | final String key) { | |
126 | try { | |
127 | 0 | RegistryHandle handle = RegistryUtil.regOpenKey(hKey, entry); |
128 | 0 | String value = RegistryUtil.regQueryValue(handle, key); |
129 | 0 | RegistryUtil.regCloseKey(handle); |
130 | 0 | return value; |
131 | 0 | } catch (Win32ApiException ex) { |
132 | 0 | return null; |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
137 | * レジストリから規定値を読み込んで返します。<br /> | |
138 | * | |
139 | * @param hKey | |
140 | * ハンドル({@link #HKEY_LOCAL_MACHINE}, {@link #HKEY_CLASSES_ROOT}, | |
141 | * {@link #HKEY_CURRENT_USER} のいずれかを指定) | |
142 | * @param entry | |
143 | * レジストリエントリ | |
144 | * @return 読み込んだ値。エントリやキーが見つからなかった場合は <code>null</code> | |
145 | */ | |
146 | public static String getRegistryValue(final int hKey, final String entry) { | |
147 | 0 | return getRegistryValue(hKey, entry, UrumaConstants.NULL_STRING); |
148 | } | |
149 | } |