Skip to content

Commit b91db6f

Browse files
committed
Merge remote-tracking branch 'origin/master'
Conflicts: pom.xml
2 parents 10021b7 + 48ffe3e commit b91db6f

7 files changed

Lines changed: 102 additions & 67 deletions

File tree

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: java
2+
jdk:
3+
- oraclejdk8

pom.xml

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,70 @@
4343
</repository>
4444
</distributionManagement>
4545

46-
<build>
47-
<plugins>
48-
<plugin>
49-
<groupId>org.sonatype.plugins</groupId>
50-
<artifactId>nexus-staging-maven-plugin</artifactId>
51-
<version>1.6.3</version>
52-
<extensions>true</extensions>
53-
<configuration>
54-
<serverId>ossrh</serverId>
55-
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
56-
<autoReleaseAfterClose>true</autoReleaseAfterClose>
57-
</configuration>
58-
</plugin>
59-
60-
<plugin>
61-
<groupId>org.apache.maven.plugins</groupId>
62-
<artifactId>maven-gpg-plugin</artifactId>
63-
<version>1.5</version>
64-
<executions>
65-
<execution>
66-
<id>sign-artifacts</id>
67-
<phase>verify</phase>
68-
<goals>
69-
<goal>sign</goal>
70-
</goals>
71-
</execution>
72-
</executions>
73-
</plugin>
74-
<plugin>
75-
<groupId>org.apache.maven.plugins</groupId>
76-
<artifactId>maven-compiler-plugin</artifactId>
77-
<configuration>
78-
<source>1.6</source>
79-
<target>1.6</target>
80-
</configuration>
81-
</plugin>
82-
</plugins>
83-
</build>
46+
<profiles>
47+
<profile>
48+
<id>release-profile</id>
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.sonatype.plugins</groupId>
53+
<artifactId>nexus-staging-maven-plugin</artifactId>
54+
<version>1.6.3</version>
55+
<extensions>true</extensions>
56+
<configuration>
57+
<serverId>ossrh</serverId>
58+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
59+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
60+
</configuration>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-gpg-plugin</artifactId>
65+
<version>1.5</version>
66+
<executions>
67+
<execution>
68+
<id>sign-artifacts</id>
69+
<phase>verify</phase>
70+
<goals>
71+
<goal>sign</goal>
72+
</goals>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</profile>
79+
<profile>
80+
<id>coverture</id>
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.jacoco</groupId>
85+
<artifactId>jacoco-maven-plugin</artifactId>
86+
<version>0.7.6.201602180812</version>
87+
<configuration>
88+
<destFile>${sonar.jacoco.reportPath}</destFile>
89+
</configuration>
90+
<executions>
91+
<execution>
92+
<id>default-prepare-agent</id>
93+
<goals>
94+
<goal>prepare-agent</goal>
95+
</goals>
96+
</execution>
97+
<execution>
98+
<id>default-report</id>
99+
<phase>prepare-package</phase>
100+
<goals>
101+
<goal>report</goal>
102+
</goals>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
</profile>
109+
</profiles>
84110

85111
<dependencies>
86112
<dependency>
@@ -92,7 +118,7 @@
92118
<dependency>
93119
<groupId>com.profesorfalken</groupId>
94120
<artifactId>jPowerShell</artifactId>
95-
<version>1.3</version>
121+
<version>1.4.1</version>
96122
</dependency>
97123
<dependency>
98124
<groupId>com.google.guava</groupId>
@@ -104,5 +130,6 @@
104130
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
105131
<maven.compiler.source>1.5</maven.compiler.source>
106132
<maven.compiler.target>1.5</maven.compiler.target>
133+
<sonar.java.source>1.5</sonar.java.source>
107134
</properties>
108135
</project>

src/main/java/com/profesorfalken/wmi4java/WMI4Java.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
public class WMI4Java {
5151
private static final String NEWLINE_REGEX = "\\r?\\n";
5252
private static final String SPACE_REGEX = "\\s+";
53+
54+
private static final String GENERIC_ERROR_MSG = "Error calling WMI4Java";
5355

5456
private String namespace = "*";
5557
private String computerName = ".";
@@ -151,7 +153,7 @@ public List<String> listClasses() {
151153
wmiClasses.addAll(hs);
152154

153155
} catch (Exception ex) {
154-
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, "Error calling WMI4Java", ex);
156+
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, GENERIC_ERROR_MSG, ex);
155157
wmiClasses = Collections.emptyList();
156158
}
157159

@@ -182,7 +184,7 @@ public List<String> listProperties(String wmiClass) {
182184
foundPropertiesList.removeAll(notAllowed);
183185

184186
} catch (Exception ex) {
185-
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, "Error calling WMI4Java", ex);
187+
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, GENERIC_ERROR_MSG, ex);
186188
foundPropertiesList = Collections.emptyList();
187189
}
188190
return foundPropertiesList;
@@ -220,7 +222,7 @@ public Map<String, String> getWMIObject(String wmiClass) {
220222
}
221223
}
222224
} catch (WMIException ex) {
223-
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, "Error calling WMI4Java", ex);
225+
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, GENERIC_ERROR_MSG, ex);
224226
foundWMIClassProperties = Collections.emptyMap();
225227
}
226228
return foundWMIClassProperties;
@@ -247,7 +249,7 @@ public String getRawWMIObjectOutput(String wmiClass) {
247249
try {
248250
rawData = getWMIStub().listObject(wmiClass, this.namespace, this.computerName);
249251
} catch (WMIException ex) {
250-
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, "Error calling WMI4Java", ex);
252+
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, GENERIC_ERROR_MSG, ex);
251253
rawData = "";
252254
}
253255
return rawData;

src/main/java/com/profesorfalken/wmi4java/WMIException.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
*/
2323
public class WMIException extends Exception{
2424

25-
public WMIException() {
26-
}
27-
2825
public WMIException(String message) {
2926
super(message);
3027
}

src/main/java/com/profesorfalken/wmi4java/WMIPowerShell.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
* @author Javier Garcia Alonso
3030
*/
3131
class WMIPowerShell implements WMIStub {
32+
private static final String NAMESPACE_PARAM = "-Namespace ";
33+
private static final String GETWMIOBJECT_COMMAND = "Get-WMIObject ";
3234

33-
private String executeCommand(String command) throws WMIException {
35+
private static String executeCommand(String command) throws WMIException {
3436
String commandResponse = null;
3537
PowerShell powerShell = null;
3638
try {
@@ -60,19 +62,19 @@ private String executeCommand(String command) throws WMIException {
6062
public String listClasses(String namespace, String computerName) throws WMIException {
6163
String namespaceString = "";
6264
if (!"*".equals(namespace)) {
63-
namespaceString += "-Namespace " + namespace;
65+
namespaceString += NAMESPACE_PARAM + namespace;
6466
}
6567

66-
return executeCommand("Get-WMIObject "
68+
return executeCommand(GETWMIOBJECT_COMMAND
6769
+ namespaceString + " -List | Sort Name");
6870
}
6971

7072
@Override
7173
public String listProperties(String wmiClass, String namespace, String computerName) throws WMIException {
72-
String command = "Get-WMIObject " + wmiClass + " ";
74+
String command = GETWMIOBJECT_COMMAND + wmiClass + " ";
7375

7476
if (!"*".equals(namespace)) {
75-
command += "-Namespace " + namespace;
77+
command += NAMESPACE_PARAM + namespace;
7678
}
7779

7880
command += " | ";
@@ -86,10 +88,10 @@ public String listProperties(String wmiClass, String namespace, String computerN
8688

8789
@Override
8890
public String listObject(String wmiClass, String namespace, String computerName) throws WMIException {
89-
String command = "Get-WMIObject " + wmiClass + " ";
91+
String command = GETWMIOBJECT_COMMAND + wmiClass + " ";
9092

9193
if (!"*".equals(namespace)) {
92-
command += "-Namespace " + namespace;
94+
command += NAMESPACE_PARAM + namespace;
9395
}
9496

9597
command += " | ";

src/main/java/com/profesorfalken/wmi4java/WMIVBScript.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,26 @@
2424
import java.io.InputStreamReader;
2525
import java.util.Date;
2626
import java.util.List;
27+
import java.util.logging.Level;
28+
import java.util.logging.Logger;
2729

2830
/**
2931
* WMI Stub implementation based in VBS
3032
*
3133
* @author Javier Garcia Alonso
3234
*/
3335
class WMIVBScript implements WMIStub {
36+
37+
private static final String ROOT_CIMV2 = "root/cimv2";
38+
private static final String IMPERSONATION_VARIABLE = "Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\";
3439

3540
private static final String CRLF = "\r\n";
3641

37-
private String executeScript(String scriptCode) throws WMIException {
42+
private static String executeScript(String scriptCode) throws WMIException {
3843
String scriptResponse = "";
3944
File tmpFile = null;
4045
FileWriter writer = null;
46+
BufferedReader errorOutput = null;
4147

4248
try {
4349
tmpFile = File.createTempFile("wmi4java" + new Date().getTime(), ".vbs");
@@ -58,7 +64,7 @@ private String executeScript(String scriptCode) throws WMIException {
5864
}
5965

6066
if (scriptResponse.isEmpty()) {
61-
BufferedReader errorOutput
67+
errorOutput
6268
= new BufferedReader(new InputStreamReader(process.getInputStream()));
6369
String errorResponse = "";
6470
while ((line = errorOutput.readLine()) != null) {
@@ -82,8 +88,11 @@ private String executeScript(String scriptCode) throws WMIException {
8288
if (tmpFile != null) {
8389
tmpFile.delete();
8490
}
91+
if (errorOutput != null) {
92+
errorOutput.close();
93+
}
8594
} catch (IOException ioe) {
86-
throw new WMIException(ioe.getMessage(), ioe);
95+
Logger.getLogger(WMI4Java.class.getName()).log(Level.SEVERE, "Exception closing in finally", ioe);
8796
}
8897
}
8998
return scriptResponse.trim();
@@ -95,12 +104,12 @@ public String listClasses(String namespace, String computerName) throws WMIExcep
95104
try {
96105
StringBuilder scriptCode = new StringBuilder(200);
97106

98-
String namespaceCommand = "root/cimv2";
107+
String namespaceCommand = ROOT_CIMV2;
99108
if (!"*".equals(namespace)) {
100109
namespaceCommand = namespace;
101110
}
102111

103-
scriptCode.append("Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\")
112+
scriptCode.append(IMPERSONATION_VARIABLE)
104113
.append(computerName).append("/").append(namespaceCommand).append("\")").append(CRLF);
105114

106115
scriptCode.append("Set colClasses = objWMIService.SubclassesOf()").append(CRLF);
@@ -122,12 +131,12 @@ public String listProperties(String wmiClass, String namespace, String computerN
122131
try {
123132
StringBuilder scriptCode = new StringBuilder(200);
124133

125-
String namespaceCommand = "root/cimv2";
134+
String namespaceCommand = ROOT_CIMV2;
126135
if (!"*".equals(namespace)) {
127136
namespaceCommand = namespace;
128137
}
129138

130-
scriptCode.append("Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\")
139+
scriptCode.append(IMPERSONATION_VARIABLE)
131140
.append(computerName).append("/").append(namespaceCommand).append(":")
132141
.append(wmiClass).append("\")").append(CRLF);
133142

@@ -154,12 +163,12 @@ public String queryObject(String wmiClass, List<String> wmiProperties, List<Stri
154163
try {
155164
StringBuilder scriptCode = new StringBuilder(200);
156165

157-
String namespaceCommand = "root/cimv2";
166+
String namespaceCommand = ROOT_CIMV2;
158167
if (!"*".equals(namespace)) {
159168
namespaceCommand = namespace;
160169
}
161170

162-
scriptCode.append("Set objWMIService=GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\")
171+
scriptCode.append(IMPERSONATION_VARIABLE)
163172
.append(computerName).append("/").append(namespaceCommand).append("\")").append(CRLF);
164173

165174
scriptCode.append("Set colClasses = objWMIService.SubclassesOf()").append(CRLF);

src/main/java/com/profesorfalken/wmi4java/MainGetInfo.java renamed to src/test/java/com/profesorfalken/wmi4java/MainGetInfo.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package com.profesorfalken.wmi4java;
72

83
import java.util.Arrays;

0 commit comments

Comments
 (0)