The source code below demonstrates the use of the virus scan provider.
import javax.naming.*;
import com.sap.security.core.server.vsi.api.*;
import com.sap.security.core.server.vsi.api.exception.*;
/* Virus Scan Interface example */
public class VsiTestScan ... {
...
try {
/* Lookup the VSI service. */
Context ctx = new InitialContext();
VSIService vsiService = (VSIService)ctx.lookup(VSIService.JNDI_NAME);
if (vsiService != null) {
/* get scan instance */
Instance myInstance = null;
try {
myInstance = vsiService.getInstance();
if (myInstance != null) {
/* perform virus scan */
if (myInstance.scanBytes(Virus.EICAR) == true) {
/*
* true means no infection and no scan error:
* Scanning the EICAR test pattern virus
* must either return false or throw an Exception,
* otherwise the underlying scan engine has
* not recognized the EICAR pattern.
*/
/* not expected error */
}
}
else {
/* The returned instance was null:
* This means, the virus scan profile is not active
* => do here nothing to allow the scan switch on/off
*/
}
}
catch (VirusInfectionException vse) {
Infection[] myInfections = vse.getInfections();
String errorText =
vse.getLocalizedMessage();
/* print out only the locale error text */
if (myInfections.length == 1) {
/* the scan engine has found the infection */
/* ...
*/
}
else {
/* not expected error */
/* ...
*/
}
}
catch (Exception e) {
/* catch all other Exceptions,
* including VirusScanException and
* VSIServiceException here as not
* expected error
*/
String errorText = e.getLocalizedMessage();
/* print
out only the locale error text */
/* ...
*/
}
finally {
/* release the scan instance */
vsiService.releaseInstance(myInstance);
}
}
else {
/* Virus Scan Provider service is not started */
/* ...
*/
}
/* ...
*/
}