bluemixContext (JavaScript)

Used to help in the development of XPages applications running on Bluemix. The bluemixContext global object is based on the Java class com.ibm.xsp.bluemix.util.context.BluemixContext and provides methods to retrieve information from the XPages runtime and connection information from any bound XPages NoSQL Database service.

It does this by parsing the Bluemix VCAP_SERVICES environment variable provided by the bound service. Use the Bluemix VCAP_SERVICES environment variable to retrieve the information and credentials you need to access the XPages NoSQL Database service with various Bluemix runtimes.

Using the bluemixContext object to retrieve information

As mentioned, the bluemixContext object was developed to provide methods to retrieve information from the Bluemix XPages runtime and as well as connection information from any bound XPages NoSQL Database service. You can also retrieve information about hybrid applications (applications where the application data resides outside of Bluemix).

This object is available in both SSJS (Server-side JavaScript) and Java. The global bluemixContext object already exists for obtaining information. It can be used in your code immediately and does not have to be created in your scripts. Available methods for this object include the following:

atHybridDbName(dbPath) : java.util.Vector
For use with hybrid applications. Returns a Vector value that contains the address of your private Domino server as the first element and the path to your database in the second element. This is the equivalent of using @DbName in an on-premises application, or using the atDbName method from the Bluemix Context class for an XPages application on Bluemix bound to the experimental XPages NoSQL DB service.
findHybridDatabaseName(dbPath) : String
For use with hybrid applications. Returns a String value that can be passed to the databaseName property of XPages data sources. This value will be properly formatted to point a data source to a data NSF on the private Domino server of your hybrid environment.
getAPP_HOME_URL() : String
Returns the Home URL for the application.
getAPP_INCLUDE_XPAGES_TOOLBOX() : String
Returns "true" if the Bluemix application includes the XPages Toolbox.
getAPP_JVM_HEAPSIZE() : String
Returns the heapsize of the JVM running this application.
getAPP_PRELOAD_DB() : String
Returns the preload db environment variable.
getAPP_VERBOSE_STAGING() : String
Returns the verbose staging environment variable.
getBuildPackVersion() : String
Returns the XPages buildpack version number.
getDataService() : DataService
Returns a DataService object representing a bound XPages NoSQL Database service.
getDataServiceByName(String) : DataService
Returns a named DataService object representing a bound XPages NoSQL Database service.
isDataServiceByNameExists(String) : boolean
Returns true if this application is bound to a named XPages NoSQL Database Service.
isDataServiceExists() : boolean
Returns true if this application is bound to any XPages NoSQL Database Service.
isHybridApplication() : boolean
For use with hybrid applications. Returns true if this application has been configured as a hybrid Bluemix application.
isRunningOnBluemix() : boolean
Returns true if this XPages application is running in the Bluemix environment.

The following "get" methods are also available to retrieve specific cloud runtime environment variables (in uppercase) available to your application.

  • getHOME() : String
  • getMEMORY_LIMIT() : String
  • getPORT() : String
  • getPWD() : String
  • getTMPDIR() : String
  • getUSER() : String
  • getVCAP_APP_HOST() : String
  • getVCAP_APP_PORT() : String
  • getVCAP_APPLICATION() : String
  • getVCAP_SERVICES() : String

Retrieving information about the DataService object

The bluemixContext object getDataService and getDataServiceByName methods return another object - the DataService object. This object provides methods to retrieve information about the bound XPages NoSQL Database Service from the VCAP_SERVICES environment variables outlined previously. The available methods include the following:

  • findDatabaseName() : String
  • atDbName() : Vector<String>
  • getAppPath() : String
  • getCredentialsValue(String) : String
  • getHost() : String
  • getLabel() : String
  • getPlan() : String
  • getRootDir() : String
  • getServerName() : String
  • getUserName() : String
  • getValue(String) : String

Code example using the bluemixContext global object

To push an existing XPages applications to Bluemix, you need to split a local copy of existing XPages application into data and design applications before you can deploy your application to Bluemix. In one part of this splitting process, you need to replace all data references in the design application.

Use the bluemixContext object when you reference the data application from the design application. You can use the isRunningOnBluemix method to point your design application to a local copy of the data application when you run the application locally. This is done as follows. Then, you can test the application before deployment to Bluemix.

  • Open the design application.
  • Search the XPages in your application for all occurrences of <xp:dominoDocument> and adjust them using the bluemixContext object.
  • Search the XPages in your application for all occurrences of <xp:dominoView> and adjust them using the bluemixContext object. For example:
    <xp:dominoView databaseName="#{javascript:bluemixContext.isRunningOnBluemix()?
    bluemixContext.getDataService().findDatabaseName() : 'myapp_data.nsf'}">
  • Search for occurrences of @DbName in your code and decide whether to reference the design or data application. If you choose to reference the data application, replace the reference with bluemixContext methods. For example, var one = @Unique(@DbColumn(@DbName(),"xspFiltersView",1)); changes to
    var one = @Unique(@DbColumn(bluemixContext.isRunningOnBluemix()? 
    bluemixContext.getDataService().atDbName():@DbName,"xspFiltersView", 
  • Search for all occurrences of database in your code and decide whether to reference the design or data application. If you choose to reference the data application, replace the reference with bluemixContext methods. For example, database.getViews() changes to
    if( null == requestScope.dataServiceDatabase) {
     if (bluemixContext.isRunningOnBluemix()) {
      var otherDatabaseVector = bluemixContext.getDataService().atDbName();
      var otherDatabaseServer = otherDatabaseVector.get(0);
      var otherDatabaseAppPath = otherDatabaseVector.get(1);
      var otherDatabase = session.getDatabase(otherDatabaseServer,otherDatabaseAppPath);
      requestScope.dataServiceDatabase = otherDatabase;
     } else {
      requestScope.dataServiceDatabase = session.getDatabase(null, "myapp_data.nsf");
     }
    } 
    
    requestScope.dataServiceDatabase.getViews();

For the complete application splitting process described in this example, refer to the Getting started with IBM XPages NoSQL Database for Bluemix documentation found on the Bluemix site.