File: n_svc_mgr.sru
Size: 13263
Date: Mon, 07 Apr 2008 21:31:17 +0200
$PBExportHeader$n_svc_mgr.sru
$PBExportComments$Service Manager
forward
global type n_svc_mgr from n_svc_base
end type
end forward

shared variables
//loaded services
str_svc sstr[]
end variables

global type n_svc_mgr from n_svc_base autoinstantiate
end type

type variables
Protected:
//local services
str_svc istr[]
end variables

forward prototypes
public function integer of_loadsvc (ref n_svc_base anv_ref, readonly string as_classname)
protected subroutine of_catchloadsvc (ref n_svc_base anv_ref, readonly string as_cls) throws n_x_svc
protected function long of_findsvclocal (readonly string as_cls)
protected function long of_findsvcshared (readonly string as_cls)
public function long of_getsvcusage (readonly string as_cls)
public subroutine of_unloadsvc (readonly string as_cls, readonly boolean ab_destroy)
public subroutine of_unloadsvc (readonly boolean ab_destroy)
public subroutine of_unloadsvc (readonly string as_classname)
public subroutine of_unloadsvc (readonly string as_classnames[])
protected function long of_getinstanceavailableindex ()
protected function long of_getsharedavailableindex ()
end prototypes

public function integer of_loadsvc (ref n_svc_base anv_ref, readonly string as_classname);//===========================================================================
// Function: of_Loadsvc (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  reference n_svc_base anv_ref
//  readonly String as_classname
//---------------------------------------------------------------------------
// Returns: Integer
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

TRY
   of_catchloadsvc(anv_ref, as_classname)
   
   RETURN CRet.SUCCESS
CATCH (n_x_svc ex)
   RETURN CRet.FAILURE
CATCH (runtimeerror err)
   RETURN CRet.FAILURE
END TRY
end function

protected subroutine of_catchloadsvc (ref n_svc_base anv_ref, readonly string as_cls) throws n_x_svc;//===========================================================================
// Function: of_Catchloadsvc (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  reference n_svc_base anv_ref
//  readonly String as_cls
//---------------------------------------------------------------------------
// Returns: (none)
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long n, c

//check if variable is instantiated
IF ISVALID(anv_ref) THEN RETURN
//search if class locally loaded
n = of_findsvclocal(as_cls)
IF n > 0 THEN 
   //return reference
   anv_ref = istr[n].nvo
   RETURN
END IF
//search if class is already shared
n = of_findsvcshared(as_cls)
TRY   
   IF n > 0 THEN 
      //add to local array
      c = UPPERBOUND(istr) + 1
      istr[c].classname = sstr[n].classname
      istr[c].nvo = sstr[n].nvo
      //increment usage counter
      sstr[n].counter = sstr[n].counter + 1
   ELSE
      //instantiate and share if does not exists
      n = of_getsharedavailableindex()
      //create shared
      sstr[n].classname = as_cls
      sstr[n].nvo = CREATE USING as_cls
      sstr[n].counter = 1
      //add to local array
      c = of_getinstanceavailableindex()
      istr[c].classname = sstr[n].classname
      istr[c].nvo = sstr[n].nvo
   END IF
   //return reference
   anv_ref = istr[c].nvo
CATCH (runtimeerror err)
// //if unable to create classname
// lnv_x = CREATE n_x_svc
// lnv_x.of_copyruntimeerror(err)
// Error.of_addexception(lnv_x, TRUE)
// Throw lnv_x
END TRY
end subroutine

protected function long of_findsvclocal (readonly string as_cls);//===========================================================================
// Function: of_Findsvclocal (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  readonly String as_cls
//---------------------------------------------------------------------------
// Returns: Long
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long n, ll_count

ll_count = UPPERBOUND(istr)

FOR n = 1 to ll_count
   IF lower(istr[n].classname) = lower(as_cls) AND ISVALID(istr[n].nvo) THEN RETURN n
NEXT

RETURN CRet.FAILURE
end function

protected function long of_findsvcshared (readonly string as_cls);//===========================================================================
// Function: of_Findsvcshared (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  readonly String as_cls
//---------------------------------------------------------------------------
// Returns: Long
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long n, ll_count

ll_count = UPPERBOUND(sstr)

FOR n = 1 to ll_count
   IF NOT ISVALID(sstr[n].nvo) THEN CONTINUE
   IF lower(sstr[n].classname) = lower(as_cls) THEN RETURN n
NEXT

RETURN CRet.FAILURE
end function

public function long of_getsvcusage (readonly string as_cls);//===========================================================================
// Function: of_Getsvcusage (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  readonly String as_cls
//---------------------------------------------------------------------------
// Returns: Long
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long n

n = of_findsvcshared(as_cls)

IF n = CRet.FAILURE THEN RETURN CRet.FAILURE

RETURN sstr[n].counter
end function

public subroutine of_unloadsvc (readonly string as_cls, readonly boolean ab_destroy);//===========================================================================
// Function: of_Unloadsvc (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  readonly String as_cls
//  readonly Boolean ab_destroy
//---------------------------------------------------------------------------
// Returns: (none)
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================
long n

//search for class
n = of_findsvcshared(as_cls)

IF n = CRet.FAILURE THEN RETURN

//decrement counter
sstr[n].counter --

//When service is #pooled, class is shared globally.
//Always keep one instance active
IF NOT ab_destroy THEN
   IF sstr[n].counter = 0 AND sstr[n].nvo.of_ispooled() THEN RETURN
END IF

//DESTROY object if no more usage
IF sstr[n].counter = 0 THEN
   IF ISVALID(sstr[n].nvo) THEN DESTROY sstr[n].nvo
   //reset index values
   sstr[n].counter = 0
   SetNull(sstr[n].classname)
END IF
end subroutine

public subroutine of_unloadsvc (readonly boolean ab_destroy);//===========================================================================
// Function: of_Unloadsvc (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  readonly Boolean ab_destroy
//---------------------------------------------------------------------------
// Returns: (none)
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long ll_count, n

ll_count = UPPERBOUND(istr)

FOR n = 1 to ll_count
   of_unloadsvc(istr[n].classname, ab_destroy)
NEXT
end subroutine

public subroutine of_unloadsvc (readonly string as_classname);//===========================================================================
// Function: of_Unloadsvc (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  readonly String as_classname
//---------------------------------------------------------------------------
// Returns: (none)
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

of_unloadsvc(as_classname, TRUE)
end subroutine

public subroutine of_unloadsvc (readonly string as_classnames[]);//===========================================================================
// Function: of_Unloadsvc (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Parameters:
//  readonly String as_classnames[]
//---------------------------------------------------------------------------
// Returns: (none)
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long ll_count, n

ll_count = UPPERBOUND(as_classnames)

FOR n = 1 to ll_count
   of_unloadsvc(as_classnames[n], TRUE)
NEXT
end subroutine

protected function long of_getinstanceavailableindex ();//===========================================================================
// Function: of_Getinstanceavailableindex (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Returns: Long
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long ll_index
long ll_count, n

ll_count = UPPERBOUND(istr)

IF ll_count > 0 THEN
   FOR n = 1 to ll_count
      IF istr[n].counter = 0 THEN
         IF ISVALID(istr[n].nvo) THEN
            CONTINUE
         ELSE
            RETURN n
         END IF
      END IF
   NEXT
   RETURN ll_count + 1
ELSE
   //return 1st index
   RETURN 1
END IF
end function

protected function long of_getsharedavailableindex ();//===========================================================================
// Function: of_Getsharedavailableindex (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Returns: Long
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================

long ll_index
long ll_count, n

ll_count = UPPERBOUND(sstr)

IF ll_count > 0 THEN
   FOR n = 1 to ll_count
      IF sstr[n].counter = 0 THEN
         IF ISVALID(sstr[n].nvo) THEN
            CONTINUE
         ELSE
            RETURN n
         END IF
      END IF
   NEXT
   RETURN ll_count + 1
ELSE
   //return 1st index
   RETURN 1
END IF
end function

on n_svc_mgr.create
call super::create
end on

on n_svc_mgr.destroy
call super::destroy
end on

event destructor;call super::destructor;//===========================================================================
// Event   : Destructor (public )
// Object: n_svc_mgr
//---------------------------------------------------------------------------
// Description:
// Note:
//---------------------------------------------------------------------------
// Returns: Long
//---------------------------------------------------------------------------
// Author: (Yeyi) Gabriel B. Abulencia
//===========================================================================
of_unloadsvc(FALSE)
end event