File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\jna\DotNet.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util.jna;
25
26 import org.jinterop.dcom.common.IJIAuthInfo;
27 import org.jinterop.dcom.common.JIException;
28 import org.jinterop.winreg.IJIWinReg;
29 import org.jinterop.winreg.JIPolicyHandle;
30 import org.jinterop.winreg.JIWinRegFactory;
31
32 import java.net.UnknownHostException;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36 /**
37 * .NET related code.
38 *
39 * @author Kohsuke Kawaguchi
40 */
41 public class DotNet {
42 /**
43 * Returns true if the .NET framework of the given version (or greater) is installed.
44 */
45 public static boolean isInstalled(int major, int minor) {
46 try {
47 // see http://support.microsoft.com/?scid=kb;en-us;315291 for the basic algorithm
48 // observation in my registry shows that the actual key name can be things like "v2.0 SP1"
49 // or "v2.0.50727", so the regexp is written to accomodate this.
50 RegistryKey key = RegistryKey.LOCAL_MACHINE.openReadonly("SOFTWARE\\Microsoft\\.NETFramework");
51 try {
52 for( String keyName : key.getSubKeys() ) {
53 if (matches(keyName, major, minor))
54 return true;
55 }
56 return false;
57 } finally {
58 key.dispose();
59 }
60 } catch (JnaException e) {
61 if(e.getErrorCode()==2) // thrown when openReadonly fails because the key doesn't exist.
62 return false;
63 throw e;
64 }
65 }
66
67 /**
68 * Returns true if the .NET framework of the given version (or grater) is installed
69 * on a remote machine.
70 */
71 public static boolean isInstalled(int major, int minor, String targetMachine, IJIAuthInfo session) throws JIException, UnknownHostException {
72 IJIWinReg registry = JIWinRegFactory.getSingleTon().getWinreg(session,targetMachine,true);
73 JIPolicyHandle hklm=null;
74 JIPolicyHandle key=null;
75
76 try {
77 hklm = registry.winreg_OpenHKLM();
78 key = registry.winreg_OpenKey(hklm,"SOFTWARE\\Microsoft\\.NETFramework", IJIWinReg.KEY_READ );
79
80 for( int i=0; ; i++ ) {
81 String keyName = registry.winreg_EnumKey(key,i)[0];
82 if(matches(keyName,major,minor))
83 return true;
84 }
85 } catch (JIException e) {
86 if(e.getErrorCode()==2)
87 return false; // not found
88 throw e;
89 } finally {
90 if(hklm!=null)
91 registry.winreg_CloseKey(hklm);
92 if(key!=null)
93 registry.winreg_CloseKey(key);
94 registry.closeConnection();
95 }
96 }
97
98 private static boolean matches(String keyName, int major, int minor) {
99 Matcher m = VERSION_PATTERN.matcher(keyName);
100 if(m.matches()) {
101 int mj = Integer.parseInt(m.group(1));
102 if(mj>=major) {
103 int mn = Integer.parseInt(m.group(2));
104 if(mn>=minor)
105 return true;
106 }
107 }
108 return false;
109 }
110
111 private static final Pattern VERSION_PATTERN = Pattern.compile("v(\\d+)\\.(\\d+).*");
112 }
113