public abstract class HubManager extends Object implements PlasticListener
register()
. If you want to stop listening,
call unregister()
. Either of these may be called more than
once harmlessly. It is a good idea to call register
immediately before sending a plastic message to ensure that the
hub is up to date (it may be a different hub server than last time
you looked). This class implements PlasticListener
and deals with generic messages as well as unregistering from the
hub on JVM shutdown etc. Concrete subclasses must implement
doPerform(java.net.URI, java.net.URI, java.util.List)
to provide any application-specific services.
The generic messages which are handled by this object directly are currently:
Actions are also provided for registering/unregistering and starting a hub, ready for use in a GUI.
CURRENT_VERSION
Constructor and Description |
---|
HubManager(String appName,
URI[] supportedMessages)
Constructs a new manager.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
checkArgs(List args,
Class[] types)
Utility method to check the types of a list of arguments.
|
static URI |
createURI(String uri)
Convenience method to turn a String into a URI without throwing
any pesky checked exceptions.
|
abstract Object |
doPerform(URI sender,
URI message,
List args)
Does the work for processing an application-type hub message.
|
void |
finalize()
Finalizer makes an attempt to unregister from the hub, but won't
wait around indefinitely for this to happen.
|
ListModel |
getApplicationListModel()
Returns a ListModel which keeps track of the currently registered
applications.
|
PlasticHubListener |
getHub()
Returns the hub which this listener is registered with.
|
Action |
getHubStartAction(boolean internal)
Returns an action which will attempt to start up a PLASTIC hub.
|
Action |
getHubWatchAction()
Returns an action that can be used to post a window showing the
state of the hub with which this object has a connection.
|
Action |
getRegisterAction(boolean reg)
Returns an action which will register/unregister with the PLASTIC hub.
|
URI |
getRegisteredId()
Returns the ID with which this listener is registered with the
prevailing PLASTIC hub.
|
JToggleButton.ToggleButtonModel |
getRegisterToggle()
Returns a toggle button model which keeps track (as far as possible)
of whether this listener is currently registered with a hub.
|
Object |
perform(URI sender,
URI message,
List args)
Implements the PlasticListener interface.
|
void |
register()
Attempts to ensure that this listener is registered with the
prevailing PLASTIC hub.
|
void |
setAutoRegister(int interval)
Controls whether auto registration is used, and how often to attempt
a connection if unregistered.
|
void |
unregister()
Makes an attempt to unregister from the hub if it's not already been
done.
|
public HubManager(String appName, URI[] supportedMessages)
supportedMessages
argument lists the additional
messages for which support is provided. Some standard ones are
supported by the HubManager itself. If this array is empty, then
all messages will be delivered.appName
- the name of the application whose connections this
object will managesupportedMessages
- URIs of PLASTIC messages supported directly
by this implementation's doPerform(java.net.URI, java.net.URI, java.util.List)
methodpublic URI getRegisteredId()
null
will be returned. If you want to do your best
to get a non-null ID, call register()
first.public PlasticHubListener getHub()
null
will be returned. If you want to do your best
to get a non-null hub, call register()
first.public void register() throws IOException
IOException
- if no registration can be achievedpublic void unregister()
public void setAutoRegister(int interval)
interval>0
, then a separate thread will
remain active ensuring that this object is always registered
with a hub, if a hub is present. It will check every
interval
milliseconds that there is a connection to
the hub, and attempt to make one if there is not.
If interval<=0
(the default), then no autoregistration
is performed.interval
- autoregistration attempt interval in milliseconds,
or <=0 to disable registrationpublic Action getRegisterAction(boolean reg)
reg
- true for register action, false for unregister actionpublic Action getHubStartAction(boolean internal)
internal
flag, the action may either
start a hub in this JVM (in which case it will expire when the JVM does)
or in a separate process.
An attempt is made to keep the enabledness of this action up to date
with current knowledge about whether the hub is running or not.internal
- true for an in-JVM hub, false for one in a new JVMpublic Action getHubWatchAction()
public JToggleButton.ToggleButtonModel getRegisterToggle()
setSelected
method.public ListModel getApplicationListModel()
ApplicationItem
.public Object perform(URI sender, URI message, List args)
perform
in interface PlasticListener
sender
- the ID of the originating application.message
- the URI representing the action.args
- any arguments to pass.public abstract Object doPerform(URI sender, URI message, List args) throws IOException
sender
argument may be any of the URIs which
this object presented as the supported messages argument of
the HubManager
constructor.sender
- sender IDmessage
- message ID (determines the action required)args
- message argument listIOException
public void finalize() throws Throwable
public static URI createURI(String uri)
uri
- URI textIllegalArgumentException
- if uri doesn't look like a URIpublic static boolean checkArgs(List args, Class[] types) throws IOException
types
, or a
subclass of that.args
- actual argument listtypes
- classes you want to see represented in the initial
elements of list
IOException
Copyright © 2024 Central Laboratory of the Research Councils. All Rights Reserved.