KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > desktop > internal > impl > WinAPIWrapper


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.desktop.internal.impl;
22
23
24 /**
25  * Bottom layer java wrapper for Windows registry relevant APIs
26  *
27  * @version 0.9
28  */

29 public class WinAPIWrapper {
30     static {
31         System.loadLibrary("jdic");
32
33         Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
34             public void run() {shutDown();}
35         });
36     }
37  
38     /**
39      * Windows handles to hives.
40      */

41     public final static int HKEY_CLASSES_ROOT = 0x80000000;
42     public final static int HKEY_CURRENT_USER = 0x80000001;
43     public final static int HKEY_LOCAL_MACHINE = 0x80000002;
44   
45     /* Windows error or status codes. */
46     public static final int ERROR_SUCCESS = 0;
47   
48     /* Constants for Windows registry element size limits */
49     public static final int MAX_KEY_LENGTH = 255;
50
51     /* Constants used to interpret returns of native functions */
52     private static final int OPENED_KEY_HANDLE = 0;
53     private static final int ERROR_CODE = 1;
54     private static final int SUBKEYS_NUMBER = 0;
55     
56     /* Windows security masks */
57     public static final int KEY_READ = 0x20019;
58   
59     /**
60      * Java wrapper for Windows registry API RegOpenKey()
61      * @param hKey Windows registry folder
62      * @param subKey key name
63      * @return ERROR_SUCCESS if succeed, or error code if fail
64      */

65     private static native int[] RegOpenKey(int hKey, byte[] subKey,
66             int securityMask);
67                                                  
68     /**
69      * Java wrapper for Windows registry API RegCloseKey()
70      */

71     private static native int RegCloseKey(int hKey);
72   
73     /**
74      * Java wrapper for Windows registry API RegQueryValueEx()
75      */

76     private static native byte[] RegQueryValueEx(int hKey, byte[] valueName);
77
78     /**
79      * Java wrapper for Windows AssocQueryString.
80      */

81     private static native byte[] AssocQueryString(byte[] fileExt, byte[] verb);
82   
83     /*
84      * Java wrapper for Windows API ExpandEnvironmentStrings()
85      */

86     private static native byte[] ExpandEnvironmentStrings(byte[] envBytes);
87     
88     /**
89      * Resolves the target file from a link file.
90      *
91      * @param filePath The path name of the given file.
92      * @return The target file path name.
93      */

94     private static native String JavaDoc resolveLinkFile(byte[] filePath);
95     
96     /**
97      * Launch the application for the given file.
98      * @param filePath Path name of the given file.
99      * @param verb Specify the verb to be executed.
100      * @return error code
101      */

102     private static native int shellExecute(byte[] filePath, byte[] verb);
103     
104     /**
105      * Opens the system default mailer with relevant information filled in.
106      *
107      * @param toArray the email address array of the "To" field.
108      * @param ccArray the email address array of the "Cc" field.
109      * @param bccArray the email address array of the "Bcc" field.
110      * @param subject the string of the "Subject" field.
111      * @param body the string of the "Body" field.
112      * @param attachArray the array of the abosolute paths of the attached files.
113      */

114     private static synchronized native void openMapiMailer(String JavaDoc[] toArray, String JavaDoc[] ccArray,
115         String JavaDoc[] bccArray, String JavaDoc subject, String JavaDoc body, String JavaDoc[] attachArray);
116
117     /**
118      * Calls this method to uninitialize COM library.
119      */

120     protected static native void shutDown();
121
122     /**
123      * Native method to browser the given url in the given target window using IE.
124      *
125      * @param urlStr the given url.
126      * @param target the given name of the target browser windows.
127      * @return true if the operation succeeds.
128      */

129     private static native boolean nativeBrowseURLInIE(String JavaDoc urlStr, String JavaDoc target);
130
131
132     /**
133      * Returns this java string as a null-terminated byte array
134      */

135     private static byte[] stringToByteArray(String JavaDoc str) {
136         if (str == null) {
137             return null;
138         }
139     
140         byte[] srcByte = str.getBytes();
141         int srcLength = srcByte.length;
142         byte[] result = new byte[srcLength + 1];
143
144         System.arraycopy(srcByte, 0, result, 0, srcLength);
145         result[srcLength] = 0;
146   
147         return result;
148     }
149     
150     /**
151      * Converts a null-terminated byte array to java string
152      */

153     private static String JavaDoc byteArrayToString(byte[] array) {
154         if (array != null) {
155             String JavaDoc temString = new String JavaDoc(array);
156
157             if (temString != null) {
158                 return temString.substring(0, temString.length() - 1);
159             }
160         }
161         return null;
162     }
163
164     /**
165      * Suppress default constructor for noninstantiability.
166      */

167     private WinAPIWrapper() {}
168
169     /**
170      * Retrieves the data associated with the default or unnamed value of a specified
171      * registry key. The data must be a null-terminated string.
172      * @param hKey specified windows registry folder constant
173      * @param subKey given sub key (not null)
174      * @param valueName given value name (not null)
175      * @return content of the value, or null if fail or not exist
176      */

177     public static String JavaDoc WinRegQueryValueEx(int hKey, String JavaDoc subKey, String JavaDoc valueName) {
178         byte[] lpSubKey = stringToByteArray(subKey);
179         int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);
180     
181         if (openResult == null) {
182             return null;
183         }
184
185         if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
186             return null;
187         } else {
188             byte[] valueBytes;
189             byte[] lpValueName = stringToByteArray(valueName);
190
191             valueBytes =
192                 RegQueryValueEx(openResult[OPENED_KEY_HANDLE], lpValueName);
193             RegCloseKey(openResult[OPENED_KEY_HANDLE]);
194       
195             if (valueBytes != null) {
196                 if ((valueBytes.length == 1) && (valueBytes[0] == 0) && (valueName.equals("")) ){
197                     return null;
198                 } else {
199                     return byteArrayToString(valueBytes);
200                 }
201             } else {
202                 return null;
203             }
204         }
205     }
206
207     /**
208      * Searches for and retrieves a file association-related string from the registry.
209      *
210      * @param fileOrProtocal The given file extension or url protocal name
211      * @param verb The given verb
212      * @return The file association-related string for the given file extension and verb, or null
213      */

214     public static String JavaDoc WinAssocQueryString(String JavaDoc fileOrProtocal, String JavaDoc verb) {
215         byte[] fileOrProtocalBytes = stringToByteArray(fileOrProtocal);
216         byte[] verbBytes = stringToByteArray(verb);
217         
218         byte[] queryResult = AssocQueryString(fileOrProtocalBytes, verbBytes);
219         if (queryResult != null) {
220             if ((queryResult.length == 1) && (queryResult[0] == 0) && (queryResult.equals("")) ){
221                 return null;
222             } else {
223                 return byteArrayToString(queryResult);
224             }
225         } else {
226             return null;
227         }
228     }
229
230     /**
231      * Resolves the target file from a link file.
232      *
233      * @param filePath The path name of the given file.
234      * @return The target file path name.
235      */

236     public static String JavaDoc WinResolveLinkFile(String JavaDoc filePath) {
237         byte[] filePathBytes = stringToByteArray(filePath);
238         return resolveLinkFile(filePathBytes);
239     }
240     
241     /**
242      * Launch the application for the given file.
243      * @param filePath Path name of the given file.
244      * @param verb Specify the verb to be executed.
245      * @return true if succeed.
246      */

247     public static boolean WinShellExecute(String JavaDoc filePath, String JavaDoc verb) {
248         byte[] filePathBytes = stringToByteArray(filePath);
249         byte[] verbBytes = stringToByteArray(verb);
250         int exeResult = shellExecute(filePathBytes, verbBytes);
251         if (exeResult > 32) {
252             return true;
253         } else {
254             return false;
255         }
256     }
257     
258     public static boolean WinBrowseURLInIE(String JavaDoc urlStr, String JavaDoc target) {
259         return nativeBrowseURLInIE(urlStr, target);
260     }
261
262     /**
263      * Opens the system default mailer with relevant information filled in.
264      *
265      * @param toArray the email address array of the "To" field.
266      * @param ccArray the email address array of the "Cc" field.
267      * @param bccArray the email address array of the "Bcc" field.
268      * @param subject the string of the "Subject" field.
269      * @param body the string of the "Body" field.
270      * @param attachArray the array of the abosolute paths of the attached files.
271      */

272     public static synchronized void WinOpenMapiMailer(String JavaDoc[] toArray, String JavaDoc[] ccArray,
273         String JavaDoc[] bccArray, String JavaDoc subject, String JavaDoc body, String JavaDoc[] attachArray) {
274         openMapiMailer(toArray, ccArray, bccArray, subject, body, attachArray);
275     }
276 }
277
278
279
Popular Tags