Class WTContext
- All Implemented Interfaces:
Serializable,Cloneable,Map<Object,Object>
This class is useful when there is a need for semi-global static state partitioned by applet or application. Using Java static fields is insufficient for this purpose since a class loaded from the local class path will share static state among applets from all codebases, and even classes loaded from a remote codebase may share static state between multiple applets from that codebase.
During applet initialization or application startup, a call to WTContext.init
associates the calling thread group to a WTContext object and stores associated
application arguments or the applet instance as part of the context. Later, static access is
provided by looking up the applicable WTContext object according to the current
thread's thread group.
This class implements the AppletStub and AppletContext interfaces to provide uniform access to these methods in the context of both applications and applets. It also enables applets to be treated as reusable components by allowing a parent applet or application context to be used as the stub for a new child applet instance.
This class includes applet start, stop, and destroy
methods which can be used to invoke the corresponding methods on all registered listeners.
applets should use these methods so that any child applets or frames that are registered
as listeners can respond to these methods accordingly.
Since untrusted applets cannot set a default locale, this class also has convenience methods
for setting and retrieving a Locale object associated with a WTContext
object. Applets that wish to use a non-default locale should call setLocale with
the desired Locale object, and any code that wishes to use this non-default
locale can call getLocale to retrieve the locale that applies to their context.
Likewise, similar methods are available to associate a timezone with a context.
This class also has convenience methods for loading class files and opening class resource files
using the applicable applet class loader and URL connections. These methods should be used
when classes which may have been loaded from the local classpath need to dynamically load
additional classes or open resource files which may not reside in the local class path. For
example, if core classes are deployed in the local class path, but an applet class is loaded
from a Web server, any calls to Class.forName in the locally loaded classes will
not find classes or files associated with the applet that do not also exist in the local
class path.
Some Windchill classes perform static class initialization based on local properties obtained
from the WTProperties class. Therefore, the WTContext.init method will implicitly
call WTProperties.getLocalProperties to guarantee that the local properties will be
available. If not, an ExceptionInInitializerError is thrown to abort application
or applet startup. This is a safety measure since it is unlikely that static class
initialization errors will be handled gracefully later on.
Example application main:
public static void main (String args)
{
// Initialize WTContext with command line arguments
WTContext.init(args);
// Do application stuff
...
}
Example applet init:
public void init ()
{
// Initialize WTContext for this applet
WTContext.init(this);
// Do applet init stuff
...
}
public void destroy ()
{
// Destroy WTContext for this applet
WTContext.getContext(this).destroy(this);
}
If context sensitive operations are performed from a thread group that does not map to an initialized
context, it may be necessary to temporarily force mapping of the current thread to the desired
context's thread group. The setContext and setContextGroup methods exist
for this purpose.
In Microsoft's Internet Explorer browser, Java's AWT event dispatching may take place in a thread
group that is shared by multiple applet contexts. The following example illustrates explicitly
setting context in a actionPerformed method:
public void actionPerformed (ActionEvent e)
{
try
{
// Set context to parent applet while processing* WTContext.setContext(applet);
// Do action stuff
...
}
finally
{
WTContext.setContext((applet)null);
}
}
If not explicitly set, context lookup will fail over to any context corresponding
to a child thread group of the current group. If none is found, a new empty context is implicitly
created. This failover behavior is intended for fault tolerance in single-user applications or web
browsers. It can be explicitly controlled in multi-user applications using the
setLookupTolerance method.
NOTE:
For performance of context lookup, nested context's are not supported. Stand alone
applications usually have a single context, and applets should have one context per applet
thread group created by the browser. If an application wants to manage multiple contexts
for different thread groups, the application initialization methods should be called with a
null argument array rather than calling the application initialization method with command
line arguments. This is because the application initialization method creates a context
that applies to the highest parent thread group reachable when it is called with the command
line arguments. If an application starts out with a single context, but later wishes to transform
into a container of multiple contexts, the initial context should be destroyed to break this high level
thread group association.
Supported API: true
Extendable false
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddAppletListener(AppletListener listener) Add aAppletListenerto the listener list.static voidaddGlobalAppletListener(AppletListener listener) Add a globalAppletListenerto the listener list.voidAdd aPropertyChangeListenerto the listener list.static ThreadGroupGet the current thread group of the calling thread.voiddestroy()Destroy thisWTContextobject.static WTContextFind theWTContextobject currently associated with the current thread group.static WTContextfindContext(ThreadGroup group) Find theWTContextobject currently associated with the given thread group.String[]getArgs()Get the argument array for this context.Returns the Class loader of this context's top-most applet.Gets the base URL.static WTContextGet theWTContextobject associated with the current thread group.static WTContextgetContext(Component component) Get theWTContextobject currently associated with the given component's top-most parent applet.static WTContextgetContext(ThreadGroup group) Get theWTContextobject currently associated with the given thread group.static ThreadGroupReturnWTContextthread group associated with the current thread.Get an image from a resource file.Returns anImageobject that can then be painted on the screen.Get locale for this context.getParameter(String name) Returns the value of the named parameter in the HTML tag.getResource(String name) Find a resource with a given a name.getResource(URL codebase, String name) Find a resource with a given a name.getResourceAsStream(Class cls, String name) Get anInputStreamfor a given resource name after applying a naming convention based on the supplied class name: if the resource name starts with "/", it is used as is.getResourceAsStream(String name) Get anInputStreamfor a given resource name.getResourceAsStream(String name, URL[] url) Get anInputStreamfor a given resource name.getResourceAsStream(URL codebase, String name) Get anInputStreamfor a given resource name by opening a URL connection relative to the given codebase.getServerResource(String name) Find a server resource with a given name.Get anInputStreamfor a given server resource name by opening a URL connection relative to this context's codebase.Get the system clipboard.Get anInputStreamfor a given system resource name.Get the thread group corresponding to this context.Get time zone for this context.static voidInitializeWTContextfor an application.static voidInitializeWTContextfor a server application.voidInterrupt all active threads in this context's thread group and its subgroups.Returns the Class object associated with the class with the given string name.OverrideHashtable.putto add property change support.OverrideHashtable.removeto add property change support.voidRemove aPropertyChangeListenerfrom the listener list.static voidsetContextGroup(ThreadGroup group) Temporarily set theWTContextobject associated with the current thread to the one associated with the given thread group.static voidsetDefaultTimeZone(TimeZone timezone) Set default time zone.static voidsetLenientLookup(boolean lenient) Set whether context lookup should be lenient with respect to context initialization.voidSet locale for this context.voidsetParentFrame(Frame parent_frame) Set the parent frame for this context.voidsetTimeZone(TimeZone timezone) Set time zone for this context.static voidsetVerbose(boolean verbose) Set verbose mode for debugging.voidshowDocument(URL url) Replaces the Web page currently being viewed with the given URL.voidshowDocument(URL url, String target) Requests that the browser or applet viewer show the Web page indicated by theurlargument.voidstop()Report aApplet.stopcall to any registered listeners.booleanReturn whether local resources should be used as server resources for this context.Methods inherited from class java.util.Hashtable
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, merge, putAll, putIfAbsent, rehash, remove, replace, replace, replaceAll, size, toString, values
-
Method Details
-
init
InitializeWTContextfor an application. Associates a context having the given argument array with the top-most parent thread group of the current thread group.
Supported API: true- Parameters:
args- the args array passed to the application'smainmethod.
-
init
InitializeWTContextfor a server application. Associates a context having the given argument array with the top-most parent thread group of the current thread group. Iflocal_resources_onlyis true, this is a server application for which server resources are available as local (class path) resources. Server resources for a context are otherwise accessed using URLs relative to the server codebase as determined by thewt.util.WTPropertiesclass.
Supported API: true- Parameters:
args- the args array passed to the applicaion'smainmethod.local_resources_only- use local resources as server resources- See Also:
-
destroy
public void destroy()Destroy thisWTContextobject. This should be called to guarantee timely garbage collection of objects referenced by theWTContextobject. If not called, the objects will remain referenced until a new WTContext object is created, at which time theWTContextclass will automatically destroy contexts associated with destroyed thread groups.Any registered
AppletListenerobjects will be informed about of thisdestroycall.Applets should call the version of this method that accepts an
Appletargument to guarantee that this context is only destroyed if theAppletobject is the parentAppletfor this context.
Supported API: true- See Also:
-
getContext
Get theWTContextobject associated with the current thread group. If no association has been created by an explicit call toinitorsetContext, then an empty context is created.
Supported API: true- Returns:
- the current
WTContextobject - See Also:
-
getContext
Get theWTContextobject currently associated with the given thread group. If no context exists for the given thread group, an empty context is created.
Supported API: true- Returns:
- the
WTContextobject - See Also:
-
findContext
Find theWTContextobject currently associated with the current thread group.
Supported API: true- Returns:
- the current
WTContextobject or null if none - See Also:
-
findContext
Find theWTContextobject currently associated with the given thread group.
Supported API: true- Parameters:
group- the thread group- Returns:
- the current
WTContextobject or null if none - See Also:
-
getContext
Get theWTContextobject currently associated with the given component's top-most parent applet. If the given component does not have a parent applet or if no context is currently associated with the applet, the current thread group's context is returned.
Supported API: true- Parameters:
component- theComponentobject- Returns:
- the
WTContextobject - See Also:
-
setContextGroup
Temporarily set theWTContextobject associated with the current thread to the one associated with the given thread group.This method can be used by an application which explicitly manages thread groups with different contexts. It should be used in a try/finally block to reset the association to the previous value or null when processing is complete.
Supported API: true- Parameters:
group- theThreadGroupobject- See Also:
-
getContextGroup
ReturnWTContextthread group associated with the current thread. Returns null if no explicit association has been set usingsetContextGroup.
Supported API: true- Returns:
- the current mapping or null if none
- See Also:
-
getArgs
Get the argument array for this context. Returns the argument array that was specified wheninitwas called for this context. If no argument array was specified, or it was null, an empty array is returned. This allows the return to be treated just like the argument array passed to an application'smainmethod, which is never null.
Supported API: true- Returns:
- the argument array
- See Also:
-
setParentFrame
Set the parent frame for this context.
Supported API: true- See Also:
-
getThreadGroup
Get the thread group corresponding to this context. Returns the ThreadGroup associated with this context when it was initialized.
Supported API: true- Returns:
- the ThreadGroup object
- See Also:
-
currentThreadGroup
Get the current thread group of the calling thread. First checks to see if the currentThread has been mapped to a specific thread group and if so returns the mapped thread group. This mapping occurs through calls tosetContextorsetContextGroup. Otherwise, if a security manager is present, it is called to get the thread group into which any new thread being constructed in this thread would be assigned. By default, it returns the thread group of the current thread, but this could be overriden by specific security manager to return the appropriate thread group if called from within a system thread. If no security manager is present, the current thread's thread group is returned.
Supported API: true- Returns:
- the ThreadGroup object
-
setVerbose
public static void setVerbose(boolean verbose) Set verbose mode for debugging. When true, this class will display trace messages when contexts are created or destroyed and when resources are being loaded via theWTContextclass.
Supported API: true- Parameters:
verbose- the verbose setting
-
interrupt
public void interrupt()Interrupt all active threads in this context's thread group and its subgroups. The calling thread, if included, is not interrupted.Note: a bug in JDK 1.1 prevents applications from terminating properly if their main thread is interrupted, even after the
runmethod has returned. Therefore, all threads named "main" are also skipped.
Supported API: true -
useLocalResourcesOnly
public boolean useLocalResourcesOnly()Return whether local resources should be used as server resources for this context.
Supported API: true- Returns:
- the local_resources_only flag used on the
initmethod.
-
setLenientLookup
public static void setLenientLookup(boolean lenient) Set whether context lookup should be lenient with respect to context initialization. When true (default), lookup for a thread group that does not correspond to an initialized context will fail over to any context initialized for a child thread group. Applications managing multiple contexts for different users may wish to disable this fault tolerance since it may lead to unexpected context sharing. Implicit creation of contexts is still allowed, but only if there is no context already initialized for a child thread group (which would result in nested contexts, which are not allowed).
Supported API: true- Parameters:
lenient-
-
addPropertyChangeListener
Add aPropertyChangeListenerto the listener list. Property change events will be fired to all registered listeners whenever a value in this hashtable is added, changed, or removed.
Supported API: true- Parameters:
listener- thePropertyChangeListenerto be added
-
removePropertyChangeListener
Remove aPropertyChangeListenerfrom the listener list.
Supported API: true- Parameters:
listener- thePropertyChangeListenerto be removed
-
put
OverrideHashtable.putto add property change support.
Supported API: true -
remove
OverrideHashtable.removeto add property change support.
Supported API: true -
addAppletListener
Add aAppletListenerto the listener list.
Supported API: true- Parameters:
listener- theAppletListenerto be added
-
addGlobalAppletListener
Add a globalAppletListenerto the listener list. Global listeners will receiveAppletListenercalls for all contexts. Application code may issue aWTContext.stop()to signal a panic shutdown. This method allows adding a single listener within applications that may have multiple multi contexts active simultaneously.
Supported API: true- Parameters:
listener- theAppletListenerto be added
-
stop
public void stop()Report aApplet.stopcall to any registered listeners.
Supported API: true -
getCodeBase
Gets the base URL.
Supported API: true- Returns:
- the
URLof the applet.
-
getParameter
Returns the value of the named parameter in the HTML tag. For example, if an applet is specified as<applet code="Clock" width=50 height=50>
<param name=Color value="blue">
</applet>then a call to
getParameter("Color")returns the value"blue".
Supported API: true- Parameters:
name- a parameter name.- Returns:
- the value of the named parameter.
-
getImage
Returns anImageobject that can then be painted on the screen. Theurlargumentthat is passed as an argument must specify an absolute URL.This method always returns immediately, whether or not the image exists. When the applet attempts to draw the image on the screen, the data will be loaded. The graphics primitives that draw the image will incrementally paint on the screen.
Supported API: true- Parameters:
url- an absolute URL giving the location of the image.- Returns:
- the image at the specified URL.
- See Also:
-
getImage
Get an image from a resource file. For resources with corresponding HTTP or FILE URLs, the normalgetImagemethod is called. For system resources, the resource is loaded into a byte array and converted to an image from there. This is due to problems Netscape and Microsoft VMs have dealing with system resource URLs.
Supported API: true- Parameters:
name- the name of the resource (full path, may include leading "/")- Returns:
- the image
- See Also:
-
showDocument
Replaces the Web page currently being viewed with the given URL. If this context has an applet, it is passed to the applet context'sshowDocumentmethod. If this context is an application and can execute commands, an attempt is made to launch a browser to view the page. On Windows platforms this is done using rundll32.exe to open the URL in the user's default browser. On unix, this is done by trying to execute netscape. To allow the commands to be configured, a property named wt.context.showDocument.<os_name> can be set where os_name is the value of the os.name Java system property with any space characters replaced by underscore. The value is a MessageFormat pattern where "{0}" will be replaced by the URL. If the value contains more than one command separated by newline, each line is executed until one finishes without writing anything to stderr.
Supported API: true- Parameters:
url- an absolute URL giving the location of the document.
-
showDocument
Requests that the browser or applet viewer show the Web page indicated by theurlargument. Thetargetargument indicates where to display the frame. The target argument is interpreted as follows:"_self"show in the current frame "_parent"show in the parent frame "_top"show in the topmost frame "_blank"show in a new unnamed top-level window name show in a new top-level window named name An applet viewer or browser is free to ignore
showDocument.
Supported API: true- Parameters:
url- an absolute URL giving the location of the document.target- aStringindicating where to display the page.
-
setLocale
Set locale for this context. Setting to null returns the locale to the default returned byLocale.getDefault. If this context corresponds to an applet, the applet's locale is set so it can affect thegetLocalemethod of components contained in the applet.
Supported API: true- Parameters:
locale- theLocaleobject
-
getLocale
Get locale for this context. If locale has not been explicitly set usingsetLocale, the locale defined by a parameter namedwt.context.localeis returned. If this parameter is not set, then the default locale returned by the context's applet or byLocale.getDefaultis returned.
Supported API: true- Returns:
- the
Localeobject
-
setTimeZone
Set time zone for this context. Setting to null returns the time zone to the default.
Supported API: true- Parameters:
timezone- theTimeZoneobject
-
setDefaultTimeZone
Set default time zone. Setting to null returns the time zone to the default returned byTimeZone.getDefault.
Supported API: true- Parameters:
timezone- theTimeZoneobject
-
getTimeZone
Get time zone for this context. If time zone has not been explicitly set usingsetTimeZone, the default time zone is returned.
Supported API: true- Returns:
- the
TimeZoneobject
-
getClassLoader
Returns the Class loader of this context's top-most applet.
Supported API: true -
loadClass
Returns the Class object associated with the class with the given string name. Given the fully-qualified name for a class or interface, this method attempts to locate, load and link the class. This method delegates to the class loader of this context's top-most applet.
Supported API: true- Throws:
ClassNotFoundException- if context's class loader can't find the class file
-
getSystemResourceAsStream
Get anInputStreamfor a given system resource name. This is similar toClassLoader.getSystemResourceAsStreambut attempts to work around Netscape security exceptions. Returns null if no resource by this name is found. The returnedInputStreamis buffered if necessary for reading efficiency.
Supported API: true- Parameters:
name- the name of the resource (full path, may include leading "/")url- a single element URL array to receive the URL corresponding to the stream- Returns:
- an
InputStreamfor the resource, or null if not found
-
getResourceAsStream
Get anInputStreamfor a given resource name. If theWTContextclass was loaded from the local class path, this method first attempts to open the resource as a local system resource. If not found, or unable to access local resources due to security restrictions, this method delegates to the class loader of this context's top-most applet. If that class loader is unable to open the resource, a URL connection is attempted relative to this context's codebase. Null is returned if no resource by this name is found. The returnedInputStreamis buffered if necessary for reading efficiency.
Supported API: true- Parameters:
name- the name of the resource (full path, may include leading "/")- Returns:
- an
InputStreamfor the resource, or null if not found
-
getResourceAsStream
Get anInputStreamfor a given resource name. If theWTContextclass was loaded from the local class path, this method first attempts to open the resource as a local system resource. If not found, or unable to access local resources due to security restrictions, this method delegates to the class loader of this context's top-most applet. If that class loader is unable to open the resource, a URL connection is attempted relative to this context's codebase. Null is returned if no resource by this name is found. The returnedInputStreamis buffered if necessary for reading efficiency.
Supported API: true- Parameters:
name- the name of the resource (full path, may include leading "/")url- a single element URL array to receive the URL corresponding to the stream- Returns:
- an
InputStreamfor the resource, or null if not found
-
getResourceAsStream
Get anInputStreamfor a given resource name after applying a naming convention based on the supplied class name: if the resource name starts with "/", it is used as is. Otherwise, the name of the class's package is prepended, after converting "." to "/". This method first attempts to open the resource as a local system resource. If not found, or unable to access local resources due to security restrictions, this method delegates to the class loader of this context's top-most applet. If that class loader is unable to open the resource, a URL connection is attempted relative to this context's codebase. Null is returned if no resource by this name is found. The returnedInputStreamis buffered if necessary for reading efficiency.
Supported API: true- Parameters:
cls- the class to fully qualify relative resource namesname- the name of the resource (full path, may include leading "/")- Returns:
- an
InputStreamfor the resource, or null if not found
-
getResourceAsStream
Get anInputStreamfor a given resource name by opening a URL connection relative to the given codebase. The returnedInputStreamis buffered if necessary for reading efficiency.Note that if useLocalResourcesOnly() returns true and the codebase result is identical to getCodebase(), then
getResourceAsStream(java.lang.String)is used instead where possible. Note that callers that know they are doing this should simply callgetResourceAsStream(java.lang.String)instead -- the presence of this logic in this method is really a workaround for errant callers.
Supported API: true- Parameters:
codebase- the codebase URLname- the name of the resource (full path, may include leading "/")- Returns:
- an
InputStreamfor the resource, or null if not found - Throws:
IOException
-
getResource
Find a resource with a given a name. The return is a URL to the resource. Doing a getContent() on the URL may return an Image, an AudioClip, or an InputStream. The method delegates to the class loader of this context's top-most applet. If that class loader is unable to open the resource, a URL is built relative to this context's codebase.
Supported API: true- Parameters:
name- the name of the resource (full path, may include leading "/")- Returns:
- a
URLfor the resource
-
getResource
Find a resource with a given a name. The return is a URL to the resource. This method builds a URL relative to a given codebase URL.
Supported API: true- Parameters:
codebase- the codebase URLname- the name of the resource (full path, may include leading "/")- Returns:
- a
URLfor the resource
-
getServerResourceAsStream
Get anInputStreamfor a given server resource name by opening a URL connection relative to this context's codebase. This is similar togetResourceAsStreambut won't find resources in the local class path. The returnedInputStreamis buffered if necessary for reading efficiency.
Supported API: true- Parameters:
name- the name of the resource (full path, may include leading "/")- Returns:
- an
InputStreamfor the resource, or null if not found
-
getServerResource
Find a server resource with a given name. The return is a URL to the resource. This method builds a URL relative to this context's server codebase. This is similar togetResourcebut doesn't return URLs to local class path resources.
Supported API: true- Parameters:
name- the name of the resource (full path, may include leading "/")- Returns:
- a
URLfor the resource
-
getSystemClipboard
Get the system clipboard.
Supported API: true- Returns:
- the system clipboard.
-