1 | /* |
2 | * Win32Verifier.java |
3 | * |
4 | * Created on 08.09.2004. |
5 | * |
6 | * eaio: NativeCall - calling operating system methods from Java |
7 | * Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>) |
8 | * <http://eaio.com> |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a |
11 | * copy of this software and associated documentation files (the "Software"), |
12 | * to deal in the Software without restriction, including without limitation |
13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
14 | * and/or sell copies of the Software, and to permit persons to whom the |
15 | * Software is furnished to do so, subject to the following conditions: |
16 | * |
17 | * The above copyright notice and this permission notice shall be included |
18 | * in all copies or substantial portions of the Software. |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
23 | * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
24 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
25 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
26 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
27 | * |
28 | */ |
29 | package com.eaio.nativecall; |
30 | |
31 | /** |
32 | * A {@link com.eaio.nativecall.Verifier} for the Windows environment. |
33 | * |
34 | * @author <a href="mailto:jb@eaio.com">Johann Burkard</a> |
35 | * @version $Id: Win32Verifier.java,v 1.3 2006/04/19 20:54:58 grnull Exp $ |
36 | */ |
37 | public class Win32Verifier implements Verifier { |
38 | |
39 | /** |
40 | * Constructor for Win32Verifier. Does nothing. |
41 | */ |
42 | public Win32Verifier() {} |
43 | |
44 | /** |
45 | * Verifies that the {@link java.lang.System} property "os.name" starts |
46 | * with "Windows". |
47 | * |
48 | * @see Verifier#supports() |
49 | */ |
50 | public boolean supports() throws SecurityException { |
51 | return System.getProperty("os.name").startsWith("Windows"); |
52 | } |
53 | |
54 | /** |
55 | * Returns the default module name if the module name is <code>null</code> |
56 | * or an empty String. If the module name contains forward slashes (/), they |
57 | * are converted to backward slashes (\). |
58 | * |
59 | * @see <a |
60 | * href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp" |
61 | * target="_top"> |
62 | * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp |
63 | * </a> |
64 | * @see com.eaio.nativecall.Verifier#verifyModuleName(java.lang.String) |
65 | */ |
66 | public String verifyModuleName(String module) { |
67 | if (module == null || module.length() == 0) { |
68 | return getDefaultModule(); |
69 | } |
70 | if (module.indexOf('/') != -1) { |
71 | module = module.replace('/', '\\'); |
72 | } |
73 | return module; |
74 | } |
75 | |
76 | /** |
77 | * Throws a {@link java.lang.NullPointerException} if the function |
78 | * name is <code>null</code> or an empty String. No further processing is |
79 | * done. |
80 | * |
81 | * @see com.eaio.nativecall.Verifier#verifyFunctionName(java.lang.String) |
82 | */ |
83 | public String verifyFunctionName(String function) { |
84 | if (function == null || function.length() == 0) { |
85 | throw new NullPointerException(); |
86 | } |
87 | return function; |
88 | } |
89 | |
90 | /** |
91 | * Returns "kernel32". |
92 | * |
93 | * @return "kernel32" |
94 | * @see com.eaio.nativecall.Verifier#getDefaultModule() |
95 | */ |
96 | public String getDefaultModule() { |
97 | return "kernel32"; |
98 | } |
99 | |
100 | /** |
101 | * If the function name ends on 'W' (Windows' Unicode functions), a |
102 | * <code>char</code> array is returned, otherwise a <code>byte</code> array |
103 | * is returned. |
104 | * <p> |
105 | * The arrays are always <code>null</code>-terminated. |
106 | * |
107 | * @see com.eaio.nativecall.Verifier#handleString(java.lang.String, |
108 | * java.lang.String, |
109 | * java.lang.String) |
110 | */ |
111 | public Object handleString(String val, String module, String function) { |
112 | if (function.charAt(function.length() - 1) == 'W') { |
113 | char[] buf = new char[val.length() + 1]; |
114 | val.getChars(0, val.length(), buf, 0); |
115 | return buf; |
116 | } |
117 | byte[] buf = new byte[val.length() + 1]; |
118 | val.getBytes(0, val.length(), buf, 0); |
119 | return buf; |
120 | } |
121 | |
122 | } |