Global context

The global context, as the name implies, provides a central construct for applications to store and retrieve environment information.

Scripts in IBM® WebSphere® sMash run in the context of an HTTP request, the request zone therefore provides access to the HTTP request data. All the information the application has been configured with (including port numbers, directory locations and dependencies) is provided through the config zone. APIs for the global context are available for Java, Groovy and PHP.

The Zones

WebSphere sMash provides a default set of zone handlers for applications. Each zone handler provides different lifetime and scope behavior. The global context can be extended with additional zone handlers as described in the Extending the global context.

Certain zones persists data and make it available across JVM restarts. Data is serialized and persisted to the file system or an external store. For other zones persistence does not apply and are held only in memory. Following is a tour of the standard zones. Zones are identified by a zone prefix.

Non-Persistent Zones

Config zone
Data in the /config zone is loaded from configuration files. This data is globally visible and is available for the lifetime of the application. Config zone may be modified, however the changes will be lost on JVM restart when the contents are reinitialized from configuration files.
Request zone
Data in the /request zone is visible to the thread that is processing the HTTP request. Request zone is available from the time a request enters the system until the response is sent out. Request zone provides a union of functionality offered by HttpServletRequest and HttpServletResponse. It includes access to incoming data: request parameters, headers, cookies, POST body, input stream and outgoing data: outgoing headers, outgoing cookies and output steam. This zone is non-persistent.
Event zone
Data in the /event zone is visible to the thread that is processing the event for the duration of that event. WebSphere sMash provides an event processing framework to enable loosely coupled components that publish and subscribe to these events. If an event is delivered to multiple event handlers, all the event handlers have access to the original event data from the event zone. This zone is non-persistent.
Tmp zone
Data in the /tmp zone is visible globally to all threads of an application. It provides a scratch pad area that the application may use to store any objects. This zone is non-persistent.

Persistent Zones

User zone
Data in the /user zone is visible to all threads of an HTTP session. HTTP session is identified by the value of the zsessionid cookie found on the request. User zone is preserved across server recycles. The contents of this zone are serialized using java serialization, therefore only serializable objects should be placed in it. An HTTP session times out after a period of inactivity. The idle timeout is configured by setting /config/userZoneIdleTimeout in zero.config. Session also times out and is invalidated after the zsessionid cookie expires.
App zone
Data in the /app zone is visible globally to all threads of an application. It provides a scratch pad area that the application may use to store serializable objects. This zone is persistent across server recycles.
Storage zone
Data in the /storage zone is visible globally to all threads of an application. It provides a scratch pad area that the application may use to store json serializable objects. They include Lists, Maps, String, Double, Long and Boolean objects. If an Integer is placed in the zone, it will be converted to a Long after recycle/restart. Similarly a Float is converted to Double, all Maps are converted to Hashmap and all Lists are converted to ArrayList. This zone is persistent across server recycles and restarts.

Accessing the Global Context

Global Context is available from all parts of an application. The method of access depends on the language being used. Java API to the GlobalContext defines the access characteristics. Bindings are provided to this API through Groovy and PHP. The global context looks like a map with access through key-value pairs. One can get, put, list and delete different keys from the global context.
Details on accessing the global context >>

Global Context Commands

Global context commands are special objects placed in the Global context. These command expose a similar interface to that of the global context itself. An access on a key where a command is stored is delegated to the command itself.
Details on Global Context Commands >>

Concurrent Access to the Global Context

A locking mechanism is provided by the Global Context for zones that require concurrent access. Config, Tmp, User, App and Storage zones allow concurrent access. The locking mechanism is provided by a Command stored at key="" in these zones. lock, unlock, invalidate and save are operations defined on the Command.
Following is an examples in Groovy that accesses the command.
// lock user zone
zpost ("/user#lock", true)
def cnt = zget ("/usr/cnt")
// assume cnt is not null
cnt++
zput ("/usr/cnt", cnt)
// unlock zone
zpost ("/user#unlock", true)

// if cnt > 10 zone will be invalidated
if (cnt>10) {
	zpost ("/user#invalidate", true)
}
The above pattern can be applied to the listed zones if multiple threads are expected to access a zone concurrently.

Extending the global context

Global context can be extended by creating new zones and providing new zone handlers for those zones, or providing new zone handlers for existing zones.
Details on extending the global context >>

Version 1.1.0.0.21442