2424import java .io .InputStreamReader ;
2525import java .util .Date ;
2626import 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 */
3335class 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 );
0 commit comments