Context information
The PowerBuilder context feature allows applications to access
certain host (non-PowerBuilder) services. This is a PowerBuilder
implementation of functionality similar to the COM QueryInterface.
PowerBuilder provides access to the following host services:
- Context information service
- Context keyword service
- CORBACurrent service
- Error logging service
- Internet service
- Secure Sockets Layer service
- Transaction server service
PowerBuilder creates service objects appropriate for the current
execution context (native PowerBuilder, PowerBuilder window plug-in,
PowerBuilder window ActiveX, transaction server). This allows your
application to take full advantage of the execution environment.
For example, in a PowerBuilder window plug-in, your application
can access parameters specified in the Embed element. Additionally,
when running the PowerBuilder window ActiveX under Internet Explorer,
your application can access the ActiveX automation server, which
allows your program to access and control the Internet browser.
The context feature uses seven PowerBuilder service objects: ContextInformation,
ContextKeyword, CORBACurrent, ErrorLogging, Inet, SSLServiceProvider,
and TransactionServer; it also uses the InternetResult object. (The
context feature is sometimes called the Context object, but it is not a
PowerBuilder system object.)
For more information about these objects,
see Objects and Controls
or the PowerBuilder
Browser.
Enabling a service
Before you use a service, you instantiate it by calling the GetContextService function.
When you call this function, PowerBuilder returns a reference to
the instantiated service. Use this reference in dot notation when
calling the service’s functions.
To enable a service:
-
Establish an instance variable of the appropriate
type:1ContextInformation icxinfo_base1ContextKeyword icxk_base1CORBACurrent corbcurr_base1ErrorLogging erl_base1Inet iinet_base1SSLServiceProvider sslsp_base1TransactionServer ts_base -
Instantiate the instance variable by calling the GetContextService function:
1this.GetContextService("ContextInformation", &<br /> icxinfo_base)1this.GetContextService("Keyword", icxk_base)1this.GetContextService("CORBACurrent", &1corbcurr_base)1this.GetContextService("ErrorLogging", erl_base)1this.GetContextService("Internet", iinet_base)1this.GetContextService("SSLServiceProvider", &1sslsp_base)1this.GetContextService("TransactionServer",ts_base)
Using a CREATE statement
You can instantiate a service object with a PowerScript CREATE statement. However,
this always creates an object for the default context (native PowerBuilder
execution environment), regardless of where the application is running.
Context information service
You use the context information service to obtain information
about an application’s execution context. The service provides
current version information, as well as whether the application
is running in the PowerBuilder execution environment and whether
as a PowerBuilder window plug-in or a PowerBuilder window ActiveX.
Using this information, you can modify display characteristics and
application behavior. For example, you might hide a Close command
button when running as a plug-in or ActiveX. Additionally, when
running in the PowerBuilder window ActiveX under Internet Explorer,
the context information service can return a reference to an ActiveX
automation server object. Your application can use this reference
to call functions that control the Web browser.
Accessing context information
Using the context information service, you can access the
information in Table 21-4.
| Item | Use this function | Comment |
|---|---|---|
| Full context name | GetName | Value returned depends on the context:
|
| Abbreviated context name | GetShortName | Value returned depends on the context:
|
| Company name | GetCompanyName | Returns Sybase, Inc. |
| Version | GetVersionName | Returns the full version number (for example, 9.0.1) |
| Major version | GetMajorVersion | Returns the major version number (for example, 9) |
| Minor version | GetMinorVersion | Returns the minor version number (for example, 0) |
| Fix version | GetFixesVersion | Returns the fix version number (for example, 1) |
Using the ClassName function
You can also use the ClassName function
to determine the context of the object. The return value of the ClassName function
differs by context. For example, for the Window plug-in the return
value is plugincontextinformation, and for the Window
ActiveX it is rtxcontextinformation.
You can use this information for many purposes, including:
- Modifying application appearance based
on execution context For example, you might hide a Close button when running in
the PowerBuilder window plug-in and PowerBuilder window ActiveX.
In a plug-in or ActiveX, closing the window results in a blank space
in the HTML page. - Verifying that the context supports
the current version For example, if your application requires features or fixes
from Version 9.0.1, you can use the context information
service to check the version in the current execution context.
To access context information:
-
Declare an instance or global variable
of type ContextInformation:1ContextInformation icxinfo_base -
Create the context information service by calling
the GetContextService function:1this.GetContextService("ContextInformation", &1icxinfo_base) -
Call context information service functions as
necessary.This example calls the GetShortName function
to determine the current context and the GetVersionName function
to determine the current version:1String ls_name1String ls_version1Constant String ls_currver = "7.0.01"1icxinfo_base.GetShortName(ls_name)1IF ls_name <> "PBRun" THEN1cb_close.visible = FALSE1END IF1icxinfo_base.GetVersionName(ls_version)1IF ls_version <> ls_currver THEN1MessageBox("Error", &1"Must be at Version " + ls_currver)1END IF
Accessing the ActiveX automation server
If you are running the PowerBuilder window ActiveX in Internet
Explorer Version 3.0 or later, your application can call the context
information service’s GetHostObject function
to obtain a reference to an ActiveX automation server object (the
hosting object). Specifically, if you pass an uninstantiated OLEObject
variable to GetHostObject, it returns a reference
to the IWebBrowserApp automation server.
Your application can call and access IWebBrowserApp methods
and properties, which allow you to access and control certain aspects
of browser behavior, including:
- Go back
- Go forward
- Go home
- Refresh
- Navigate to a specified Web page
- Exit the browser
For more
information on the IWebBrowserApp interface, its methods, and its properties,
see the Internet Explorer documentation or the Microsoft Web site.
To access the ActiveX automation server:
-
Declare instance or global variables of
type ContextInformation and OLEObject:1ContextInformation icxinfo_base1OLEObject iole_browser -
Create the context information service by calling
the GetContextService function:1GetContextService("ContextInformation", &<br /> icxinfo_base) -
Establish a reference to the ActiveX automation
server by calling the GetHostObject function:1Integer li_rtrn1li_rtrn = icxinfo_base.GetHostObject(iole_browser)1IF li_rtrn = 1 THEN1sle_host.Text = "GetHostObject succeeded"1ELSE1sle_host.Text = "GetHostObject failed"<br /> END IF -
Call IWebBrowserApp functions as necessary. This
example calls the Navigate function to open the
default Web browser displaying the Sybase home page:1IF IsValid(iole_browser) THEN1iole_browser.Navigate &1("http://www.sybase.com", 0, 0, 0)1END IF
Context keyword service
Use the context keyword service to access environment information
for the current context. In the default environment, this service
returns host workstation environment variables. In the PowerBuilder
window plug-in, this service allows you to access parameters specified
in the plug-in’s Embed element. When running within EAServer, you can use the keyword
service to get component property values.
For information about using the context keyword service in EAServer, see “Accessing component properties”.
Accessing environment variables
When running in the PowerBuilder execution environment (the
default context), you use this service to return environment variables.
To access environment variables:
-
Declare an instance or global variable
of type ContextKeyword. Also declare an unbounded array of type
String to contain returned values:1ContextKeyword icxk_base1String is_values[] -
Create the context information service by calling
the GetContextService function:1this.GetContextService("Keyword", icxk_base) -
Call the GetContextKeywords function
to access the environment variable you want. This example calls
the GetContextKeywords function to determine
the current application Path:1icxk_base.GetContextKeywords("Path", is_values) -
Extract values from the returned array as necessary.
When accessing environment variables, the array should always have
a single element:1MessageBox("Path", "Path is: " + is_values[1])
Accessing Embed element parameters
The Embed element can contain additional, user-specified,
parameters. Additionally, the Embed element allows more than one
value for each parameter.
When running in the PowerBuilder window plug-in context, you
use this service to access parameters specified in the Embed element.
If the specified parameter is not found, the service tries to match
the specified parameter with an environment variable.
To access Embed element parameters:
-
Declare an instance or global variable
of type ContextKeyword. Also declare an unbounded array of type
String to contain returned values:1ContextKeyword icxk_base1String is_values[ ] -
Create the context information service by calling
the GetContextService function:1GetContextService("Keyword", icxk_base) -
Call the GetContextKeywords function.
This example calls the GetContextKeywords function
to access values for the user-specified parameter, VALID:1icxk_base.GetContextKeywords("VALID", is_values) -
Extract values from the returned array as necessary.
This example displays parameters in a list:1Integer li_count1FOR li_count = 1 to UpperBound(is_values)1lb_parms.AddItem(is_values[li_count])1NEXT
CORBACurrent service
Client applications and EAServer components
marked as OTS style can create, control, and obtain information
about EAServer transactions using
functions of the CORBACurrent context service object. The CORBACurrent
object provides most of the methods defined for the CORBA Current
interface.
For more information, see “Client- and component-demarcated
transactions”.
Error logging service
To record errors generated by PowerBuilder objects running
in a transaction server to a log file, create an instance of the
ErrorLogging service object and invoke its log method. For example:
|
1 |
ErrorLogging erlinfo_base |
|
1 |
this.GetContextService("ErrorLogging", & |
|
1 |
erlinfo_base) |
|
1 |
erlinfo_base.log("Write this string to log") |
The errors are recorded in the EAServer log
if the component is running in EAServer,
and in the Windows NT system application log if the component is running
in MTS.
Internet service
Use the Internet service to:
- Display
a Web page in the default browser (HyperLinkToURL function, which
starts the default browser with the specified URL) - Access the HTML for a specified page (GetURL function,
which performs an HTTP Get) - Send data to a CGI, ISAPI, or NSAPI program (PostURL function,
which performs an HTTP Post)
Hyperlinking to a URL
You call the Internet service’s HyperLinkToURL function
to start the default browser with a specified URL.
To hyperlink to a URL:
-
Declare an instance or global variable
of type Inet:1Inet iinet_base -
Create the Internet service by calling the GetContextService function:
1THIS.GetContextService("Inet", iinet_base) -
Call the HyperLinkToURL function,
passing the URL of the page to display when the browser starts:1iinet_base.HyperlinkToURL &1("http://www.sybase.com")
Getting a URL
You call the Internet service’s GetURL function
to perform an HTTP Get, returning raw HTML for
a specified URL. This function returns the raw HTML using the InternetResult
object.
To perform an HTTP Get:
-
Declare an instance or global variable
of type Inet. Also declare an instance or global variable using
the descendent InternetResult object as the datatype (n_ir_msgbox in
this example):1Inet iinet_base1n_ir_msgbox iir_msgbox -
Create the Internet service by calling the GetContextService function:
1THIS.GetContextService("Internet", iinet_base) -
Create an instance of the descendent InternetResult
object:1iir_msgbox = CREATE n_ir_msgbox -
Call the GetURL function, passing
the URL of the page to be returned and a reference to the instance
of the descendent InternetResult object:1iinet_base.GetURL &1("http://www.sybase.com", iir_msgbox)When the GetURL function completes, it
calls the InternetData function defined in the
descendent InternetResult object, passing the HTML for the specified
URL.
Posting to a URL
You call the Internet service’s PostURL function
to perform an HTTP Post, sending data to a CGI,
ISAPI, or NSAPI program. This function returns the raw HTML using
the InternetResult object.
To perform an HTTP Post:
-
Declare an instance or global variable
of type Inet. Also declare an instance or global variable using
the descendent InternetResult object as the datatype (n_ir_msgbox in
this example):1Inet iinet_base1n_ir_msgbox iir_msgbox -
Create the Internet service by calling the GetContextService function:
1THIS.GetContextService("Internet", iinet_base) -
Create an instance of the descendent InternetResult
object:1iir_msgbox = CREATE n_ir_msgbox -
Establish the arguments to the PostURL function:
1Blob lblb_args1String ls_headers1String ls_url1Long ll_length1ls_url = "http://coltrane.sybase.com/"1ls_url += "cgi-bin/pbcgi80.exe/"1ls_url += "myapp/n_cst_html/f_test?"1lblb_args = Blob("")1ll_length = Len(lblb_args)1ls_headers = "Content-Length: " &<br /> + String(ll_length) + "~n~n" -
Call the PostURL function,
passing the URL of the routine to be executed, the arguments, the
header, an optional server port specification, and a reference to
the instance of the descendent InternetResult object:1iinet_base.PostURL &<br /> (ls_url, lblb_args, ls_headers, 8080, iir_msgbox)When the PostURL function completes, it
calls the InternetData function defined in the
descendent InternetResult object, passing the HTML returned by the
specified routine.
Using the InternetResult object
The GetURL and PostURL functions
both receive data in an InternetResult object. This object acts
as a buffer, receiving and caching the asynchronous data as it is
returned by means of the Internet. When all data is received, the InternetResult
object calls its InternetData function, which
you override to process the data as appropriate.
Implement in descendants of InternetResult You implement this feature by creating standard class user
objects of type InternetResult. In each of these descendent user
objects, define an InternetData function to process
the passed HTML as appropriate.
To implement a descendent InternetResult object:
-
Create a standard class user object of
type InternetResult. -
Declare a new user object function as follows:
- Name InternetData
- Access Public
- Returns Integer
- Argument name Data, passed by value
- Argument datatype Blob
-
Add code to the InternetData function
that processes the returned HTML as appropriate. This example simply
displays the HTML in a MessageBox:1MessageBox("Returned HTML", String(data))1Return 1
Secure Sockets Layer service
PowerBuilder allows you to establish Secure Sockets Layer
(SSL) connections to EAServer.
The SSL protocol allows connections to be secured using public-key
encryption and authentication algorithms that are based on digital certificates.
SSL is a “wrapper” protocol: packets for another protocol are secured
by embedding them inside SSL packets. For example, HTTPS is HTTP secured
by embedding each HTTP packet within an SSL packet. Likewise, IIOPS
is IIOP embedded within SSL.
You use an instance of the SSLServiceProvider object to establish
the connection from the client to the server. For more information,
see the PowerScript Reference
and Chapter 25, “Using SSL in PowerBuilder clients”
Transaction server service
Use the transaction server service to access information about
the context of an object running in a transaction server such as EAServer, MTS, or COM+.
You can use the TransactionServer object to influence transaction
behavior programmatically, and to access the methods of another
component on the transaction server.
For more information, see Chapter 23, “Building an EAServer Component” and Chapter 26, “Building a COM, MTS, or
COM+ Component”
Tools and techniques for building distributed applications with
PowerBuilder.
PowerBuilder Enterprise edition only The tools for building multitier applications available with PowerBuilder
are supported in the Enterprise edition only.