Sample BBj Program


rem testbridge.bbj
seterr release_on_error

rem in order to open a plugin, the plugin ALIAS must be defined in config file
rem following OPEN line assumes a line in config file of the form
rem ALIAS J0 com.basis.bbj.bridge.BBjBridgeOpenPlugin
open (1)"J0"

seterr respond_on_error

rem retrieve the test number and execute the test
testNum = num(argv(1))
rem print testNum

switch testnum
  case 0
   gosub getArgCargV
   break

  case 1
   gosub getStrings
   break

  case 2
   gosub getRecordsUsingKnownKeys
   break

  case 3
  case 4
   gosub getRecordsOrStrings
   break

  case 5
   gosub getAllKeysAndRecords
   break

  case 6
   rem generate an intentional error
   print(22)"hello"
   break
swend

release_on_error:
release testNum

respond_on_error:
seterr release_on_error
rem check fid to determine if response is recordOriented
dim finInfo$:tmpl(1,ind=0)
finInfo$ = fin(1,ind=0)
recordOriented = finInfo.ResponseRecordOriented

rem create an error message to send back to Java program
message$ = "error: " + errmes(err) + " on line " + str( tcb(5))

rem write the message
if(recordOriented)
 write(1,key="errorInfo")message$
else
  write(1)message$
endif
rem and release
release

getArgCargV:
rem retrieve argc, argv and write as records to channel 1
  writerecord(1,key="argc: ")str(argc)
  for i=0 to argc-1
   key$ = "argv[" + str(i) + "] = "
   writerecord(1,key=key$)argv(i)
  next i
return

getStrings:
rem assume that channel 1 is string-oriented. read strings and echo to channel 0
label_1:
  read(1,end=eof_1)A$
  write(1)"received: " + A$
  goto label_1
eof_1:
return

getRecordsUsingKnownKeys:
rem assume channel 1 contains key/value pairs with known keys
rem echo key/value pairs to channel 0
  readrecord(1,key="name")name$
  readrecord(1,key="month")month$
  response$ = "the name is " + name$
  writerecord(1,key="name: ")response$
  response$ = "the month is " + month$
  writerecord(1,key="month: ")response$
return

getAllKeysAndRecords:
rem assume channel 1 is record oriented. retrieve all key/value pairs
rem and echo them to channel 1
label_2:
  key$ = key(1,end=eof_2)
  readrecord(1,key=key$,end=eof_2)value$
  writerecord(1,key=value$)key$
  goto label_2
eof_2:
return

getRecordsOrStrings:
rem use fin to discover whethe channlel 1 is record-oriented. then
rem either read records or read strings. In either case echo to channel 1

rem retrieve fid to determine whether request is record-oriented
dim finInfo$:tmpl(1,ind=0)
finInfo$ = fin(1,ind=0)
recordOriented = finInfo.RequestRecordOriented

if recordOriented then
  readrecord(1,key="name")name$
  readrecord(1,key="month")month$
  response$ = "the name is " + name$
  writerecord(1,key="name: ")response$
  response$ = "the month is " + month$
  writerecord(1,key="month: ")response$
else
  while 1
   read(1,end=*break)A$
   write(1)"received: " + A$
  wend
endif

return

end