Wednesday, August 15, 2007

Figuring out the Server Name

A bit about our Off-site Server Project
The aim of this project was always to have an off-site server that was capable of providing us with alternative facilities in the event of a disaster. My employers were on Domino before I started working here (7 years ago) and many of our oldest databases are in relatively "untouched" form. One of the biggest challenges of our off-site server project was always going to be the detection and removal of server-name hard-coding.

Discovering all those server names was a tedious business which I won't go into here. This blog entry will provide you with one bit of code we used to replace a single-line server name. We used several different methods but this method is interesting in that it provides an alternative when the server is local.

Getting the Server Name itself
Generally, getting the server name in a late-domino server is quite easy. There's an @function (@ServerName) for it from R6 onwards, and prior to that it was accessible via @DBName. There are also script methods which I'll detail here. One problem can arise when you don't want the server to be local.

What you need for the Code Sample
The code sample will identify the current server name using LotusScript. If the current server name is local (ie: if we're running from a local replica) it will then use a keyworded value instead. This enables you to have a local replica for speed reasons but access a server for things that really need to. The keyword value is a document in a view called KRetrieval with the first column containing the keyword ServerName and the second column containing the name of the domino server to be used if the database is local. We use the amazingly handy evaluate function to retrieve that value.

A bit of code
Admittedly, this isn't the cleanest of code, but it does the job.

Dim session As New NotesSession
Dim db As NotesDatabase
Dim docs As NotesDocumentCollection
Dim doc As NotesDocument

Set db = session.CurrentDatabase
Set docs = Source.Documents
Set doc = docs.GetFirstDocument()

Dim ServerName As New NotesName(db.Server)
Dim KeywordServerName As String
Dim EvaluateArray As Variant
Dim ServerToUse As String

EvaluateArray = Evaluate({@If(@IsError(@DbLookup("":"NoCache"; ""; "KRetrieval" ; "ServerName"; 2 )) ; "" ; @DbLookup("":"NoCache"; ""; "KRetrieval" ; "ServerName"; 2 ))})

KeywordServerName = EvaluateArray(0)

If Ucase$(Trim$(ServerNameText)) = "LOCAL" Then
Print "You are running this DB from a LOCAL Replica - will use Keyworded Server Name"
ServerNameText = ""
End If

If Trim$(ServerNameText) = "" Then
Print "Setting Server Name to Keyworded Server Name :" + KeywordServerName ServerNameText = KeywordServerName
End If

No comments: