Binding an access variable dynamically

You can use dynamic binding to either choose a service at run time or change the service to which a variable is bound.

To bind a variable dynamically, use functions in the ServiceLib EGL library instead of the @BindService property. This option is not available for local access of a service from a Rich UI handler or related library.

Suppose that you created entries in the deployment descriptor file for two slightly different service implementations:
<webBinding interface="interfaces.SpeechTranslator" 
    name="TranslateSpanish" port="SpanishPort"/>

<webBinding interface="interfaces.SpeechTranslator" 
    name="TranslateGerman" port="GermanPort"/>
You might create and bind two variables, one for each of these entries. Alternatively, you can create one variable that is based on the Interface part that the entries share, and then use the ServiceLib.bindService() system function to bind the variable to the service that you want to use:
  1. Create a variable that is based on the Interface or Service part in the deployment descriptor entry:
    myTranslator SpeechTranslator;
  2. Use the bindService() function to bind the variable to the service implementation:
    myTranslator = ServiceLib.bindService("TranslateSpanish");
    In this case, the myTranslator variable is now bound to the entry named TranslateSpanish.
  3. Use the variable to access the service:
    mySpanishString string = myTranslator.translate
        ("This sentence is in Spanish");
  4. You can use bindService() again to bind the service to a different implementation:
    myTranslator = ServiceLib.bindService("TranslateGerman");
  5. At this point, you can use the variable to access the alternate service:
    myGermanString string = myTranslator.translate
        ("This sentence is in German");

Feedback