
OpenNTF Code Bin
About This Code
Brief Description:
OSHelper class for some common OS operations . . .
Contributor:
Dallas Gimpel
Notes Version:
R6.x, R7.x
Last Modified:
20 Jul 2007
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
This is just a helper class (as the name suggests) that I thought might be useful for others. The class performs a handful of common operating system operations using LS2J.
Public methods:
- IsPathValid - boolean, true if the specified path exists.
- CreatePath - boolean, true if the specified path can be created or already exists.
- GetRootDirectories - boolean, returns true if root directories can be retrieved. Results are written to the String array parameter.
- GetLocalIPAddress - boolean, returns true if the local machine's ip address can be retrieved. Results are written to the String parameter.
- GetLocalMachineName - boolean, returns true if the local machine's name can be retrieved. Results are written to the String parameter.
- GetTimeInMillis - boolean, returns true if the local machine's time can be retrieved in milliseconds. Results are written to the Double parameter.
Sample usage:
Dim osHelper As OSHelper
Dim dblTime As Double
Dim strValu As String
Dim strDriveArray() As String
Set osHelper = New OSHelper()
If Not(osHelper.IsPathValid("c:\my stuff")) Then
MsgBox {The path "c:\My Stuff" is not valid.}, , "Results . . ."
End If
If osHelper.CreatePath("c:\My Music\Pink Floyd") Then
MsgBox {The path "c:\My Music\Pink Floyd" has been created.}, , "Results . . ."
End If
If osHelper.GetRootDirectories(strDriveArray) Then
MsgBox {Drive letters are: } & Chr(13) & Join(strDriveArray, Chr(13)), , "Results . . ."
End If
If osHelper.GetLocalIPAddress(strValu$) Then
MsgBox {IP Address is: } & strValu$, , "Results . . ."
End If
If osHelper.GetLocalMachineName(strValu$) Then
MsgBox {Machine name is "} & strValu$ & {" . . .}, , "Results . . ."
End If
If osHelper.GetTimeInMillis(dblTime#) Then
MsgBox {Local machine's time in millis: } & dblTime#, , "Results . . ."
End If
Option Declare
Uselsx "*javacon"
Public Class OSHelper
Private jSess As JavaSession
'+++ CLASS CONSTRUCTOR +++
Sub New()
Set Me.jSess = New JavaSession()
End Sub
'+++ CLASS DESTRUCTOR +++
Sub Delete()
If Not(Me.jSess Is Nothing) Then
Set Me.jSess = Nothing
End If
End Sub
'+++ PUBLIC METHODS +++
Public Function IsPathValid(pstrPath As String) As Boolean
On Error Goto errorHandler
Const JCLASS_NAME = "java/io/File"
Const JNI_CLASS_SIGNATURE = "(Ljava/lang/String;)V"
Dim jClss As JavaClass
Dim jObjFile As JavaObject
IsPathValid = False
If Len(Trim(pstrPath$)) = 0 Then
Goto thisExit
End If
Set jClss = Me.jSess.GetClass(JCLASS_NAME)
Set jObjFile = jClss.CreateObject(JNI_CLASS_SIGNATURE, pstrPath$)
IsPathValid = jObjFile.exists()
thisExit:
'// Dereference Java objects in reverse order of creation.
Set jObjFile = Nothing
If Not(jClss Is Nothing) Then
Delete jClss
End If
Exit Function
errorHandler:
Call Me.processError()
Resume thisExit
End Function
Public Function CreatePath(pstrPath As String) As Boolean
On Error Goto errorHandler
Const JCLASS_NAME = "java/io/File"
Const JNI_CLASS_SIGNATURE = "(Ljava/lang/String;)V"
Dim jClss As JavaClass
Dim jObjFile As JavaObject
CreatePath = False
If Len(Trim(pstrPath$)) = 0 Then
Goto thisExit
End If
Set jClss = Me.jSess.GetClass(JCLASS_NAME)
Set jObjFile = jClss.CreateObject(JNI_CLASS_SIGNATURE, pstrPath$)
If jObjFile.exists() Then
CreatePath = True
Else
CreatePath = jObjFile.mkdirs()
End If
thisExit:
'// Dereference Java objects in reverse order of creation.
Set jObjFile = Nothing
If Not(jClss Is Nothing) Then
Delete jClss
End If
Exit Function
errorHandler:
Call Me.processError()
Resume thisExit
End Function
Public Function GetRootDirectories(pstrRootDirsOut() As String) As Boolean
On Error Goto errorHandler
Const JCLASS_NAME = "java/io/File"
Const JNI_CLASS_SIGNATURE = "(Ljava/lang/String;)V"
Const JMETHOD_NAME = "listRoots"
Const JNI_METHOD_SIGNATURE = "()[Ljava/io/File;"
Dim jClssFile As JavaClass
Dim jmethListRoots As JavaMethod
Dim intCount As Integer
Dim strDir As String
Dim varRootDirs As Variant
GetRootDirectories = False
Set jClssFile = Me.jSess.GetClass(JCLASS_NAME)
Set jmethListRoots = jClssFile.GetMethod(JMETHOD_NAME, JNI_METHOD_SIGNATURE)
varRootDirs = jmethListRoots.Invoke()
If Isarray(varRootDirs) Then
intCount% = -1
Redim pstrRootDirsOut(0 To Ubound(varRootDirs)) As String
Forall jObjFile In varRootDirs
strDir$ = ""
strDir$ = jObjFile.toString()
If Len(strDir$) > 0 Then
intCount% = intCount% + 1
pstrRootDirsOut(intCount%) = strDir$
End If
Set jObjFile = Nothing '// clean up each object as we go to avoid crashes
End Forall
Erase varRootDirs
Redim Preserve pstrRootDirsOut(0 To intCount%) As String
GetRootDirectories = True
End If
thisExit:
'// Dereference Java objects in reverse order of creation.
varRootDirs = Null
If Not(jClssFile Is Nothing) Then
Delete jClssFile
End If
Exit Function
errorHandler:
Call Me.processError()
Resume thisExit
End Function
Public Function GetLocalIPAddress(pstrIPAddrOut As String) As Boolean
On Error Goto errorHandler
GetLocalIPAddress = Me.getLocalMachineDetail(pstrIPAddrOut$)
thisExit:
Exit Function
errorHandler:
Call Me.processError()
Resume thisExit
End Function
Public Function GetLocalMachineName(pstrNameOut As String) As Boolean
On Error Goto errorHandler
GetLocalMachineName = Me.getLocalMachineDetail(pstrNameOut$)
thisExit:
Exit Function
errorHandler:
Call Me.processError()
Resume thisExit
End Function
Public Function GetTimeInMillis(pdblTimeOut As Double) As Boolean
On Error Goto errorHandler
Const JCLASS_NAME = "java/lang/System"
Const JMETHOD_NAME = "currentTimeMillis"
Const JNI_METHOD_SIGNATURE = "()J"
Dim jClss As JavaClass
Dim jMethod As JavaMethod
getTimeInMillis = False
Set jClss = Me.jSess.GetClass(JCLASS_NAME)
Set jMethod = jClss.GetMethod(JMETHOD_NAME, "()J")
pdblTimeOut# = jMethod.Invoke()
getTimeInMillis = True
thisExit:
If Not(jMethod Is Nothing) Then
Delete jMethod
End If
If Not(jClss Is Nothing) Then
Delete jClss
End If
Exit Function
errorHandler:
Call Me.processError()
Resume thisExit
End Function
'+++ PRIVATE METHODS +++
Private Function getLocalMachineDetail(pstrDetailOut As String) As Boolean
On Error Goto errorHandler
Const JCLASS_NAME = "java/net/InetAddress"
Const JMETHOD_NAME = "getLocalHost"
Const JNI_METHOD_SIGNATURE = "()Ljava/net/InetAddress;"
Dim jClss As JavaClass
Dim jMethod As JavaMethod
Dim jObjAddr As JavaObject
getLocalMachineDetail = False
Set jClss = Me.jSess.GetClass(JCLASS_NAME)
Set jMethod = jClss.GetMethod(JMETHOD_NAME, JNI_METHOD_SIGNATURE)
Set jObjAddr = jMethod.Invoke()
Select Case True
Case Strcompare(Getthreadinfo(10), "GetLocalIPAddress", 5) = 0
pstrDetailOut$ = jObjAddr.getHostAddress()
Case Strcompare(Getthreadinfo(10), "GetLocalMachineName", 5) = 0
pstrDetailOut$ = jObjAddr.getHostName()
Case Else
Error 7042, {Method "} & Getthreadinfo(10) & {" is undefined in getLocalMachineDetail method.}
End Select
getLocalMachineDetail = True
thisExit:
'// Dereference Java objects in reverse order of creation.
If Not(jObjAddr Is Nothing) Then
Delete jObjAddr
End If
If Not(jMethod Is Nothing) Then
Delete jMethod
End If
If Not(jClss Is Nothing) Then
Delete jClss
End If
Exit Function
errorHandler:
Call Me.processError()
Resume thisExit
End Function
Private Sub processError()
Const NL = {
}
Dim jErrObj As JavaError
Dim strErrMsg As String
'// Check the state of the JavaSession object passed and capture the error details.
If Not(Me.jSess Is Nothing) Then
Set jErrObj = Me.jSess.GetLastJavaError()
If Not(jErrObj Is Nothing) Then
strErrMsg$ = jErrObj.ErrorMsg
If Len(strErrMsg$) > 0 Then
strErrMsg$ = "JavaError encountered: " & strErrMsg$ & NL & "Stacktrace:" & NL & jErrObj.StackTrace
End If
Call Me.jSess.ClearJavaError()
Set jErrObj = Nothing
End If
End If
'// If an LS error has been raised, capture the details.
If Not(Err = 0) Then
If Len(strErrMsg$) > 0 Then
strErrMsg$ = "Error " & Err & ": " & Error & " encountered at line " & Erl & " of " & Typename(Me) & "." & Getthreadinfo(10) & "." & NL & strErrMsg$
Else
strErrMsg$ = "Error " & Err & ": " & Error & " encountered at line " & Erl & " of " & Typename(Me) & "." & Getthreadinfo(10) & "."
End If
End If
'// If an error has been recorded, display its details.
If Len(strErrMsg$) > 0 Then
Msgbox strErrMsg$, , "Error encountered . . ."
Print "Error " & Err & ": " & Error & " encountered at line " & Erl & " of " & Getthreadinfo(10) & " . . ."
End If
End Sub
End Class
Usage / Example
Comments
No documents found