Class Handle
HandleService
and allows
the bean to be referenced in endpoints using its path. The path can also be used
as a storage key to store information about the bean.
Handles are a useful construct for creating hierarchical and durable identifiers for objects but require some work on behalf of the caller to connect the handles into a graph that can then be resolved into paths. This process is normally handled using the HandleAware interface rather than working with handle objects directly. While handles form a graph of objects, all handle references are weak and will not impact garbage collection.
Handles are chained together into a path, with each handle providing a name within the path. When a child Handle is chained to a parent, it can be chained using a prefix which becomes part of the path. For example, consider a Handle for a pump with the name of "water". It is then chained to a parent Handle with a name of "board". This results in a path of:
board.waterHowever, if the board also contains cutoff switch for water, it too may have the name "water", resulting in two objects with the same path. Since the pump and cutoff switch are different types of objects, a prefix is used when chaining the handles. In general, the prefix represents the type of objects whereas the name represents the name of the object. In our example a prefix of "pump" would be used for pumps and "cutoff" would be used for cutoff switches. This would result in two distinct paths:
board.pump:water board.cutoff:waterPath segments are separated by periods, and each path segment is made up of an optional prefix and name, separated by a colon.
Handle paths are considered opaque. While they are designed to be human readable, they should not be parsed or synthentically generated. The fact that they are human readable aids in debugging and logging.
As handle paths tend to be used as persistence keys and other identifiers, care should be taken when defining paths as changing them later may cause migration issues.
- Since:
- 1.0
- Version:
- 2022-08-30
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new handle with the name derived from the bean class.Constructs a new handle node with the specified name and bean.Constructs a new handle node with the specified name and bean, and attaches is to the specified parent with the supplied prefix.Handle
(String name, Object bean, String prefix, HandleAware parent) Constructs a new handle node with the specified name and bean, and attaches is to the specified parent with the supplied prefix. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the specified handle as a child of this handle without a name prefix.void
Adds the specified handle as a child of this handle using the specified name prefix.void
add
(String prefix, HandleAware child) Adds the specifiedHandleAware
as a child of this handle using the specified name prefix.void
Causes the associated bean to be broadcast fromHandleService
with a topic of:/kos/handle/<topic>/<path>
.getBean()
Get the associated bean.getName()
Gets the name of the node.getPath()
Returns the path of this handle.boolean
Return true if the handle already has a parent.void
Sets the name.toString()
Returns the string representation of this object.
-
Constructor Details
-
Handle
Constructs a new handle with the name derived from the bean class.- Parameters:
bean
- the bean associated with the handle
-
Handle
Constructs a new handle node with the specified name and bean.- Parameters:
name
- the name of the node, used to construct the handlebean
- the bean associated with the handle
-
Handle
Constructs a new handle node with the specified name and bean, and attaches is to the specified parent with the supplied prefix.- Parameters:
name
- the name of the node, used to construct the handlebean
- the bean associated with the handleprefix
- prefix for the bean relative to parentparent
- the parent handle
-
Handle
Constructs a new handle node with the specified name and bean, and attaches is to the specified parent with the supplied prefix.- Parameters:
name
- the name of the node, used to construct the handlebean
- the bean associated with the handleprefix
- prefix for the bean relative to parentparent
- the parent handle
-
-
Method Details
-
getBean
Get the associated bean. -
getName
Gets the name of the node. This is just a component of the overall handle.- Returns:
- the name of the node
-
setName
Sets the name. This is only used if the name wasn't really known when the constructor was called and should be set before paths are generated.- Parameters:
name
- the name of the handle
-
getPath
Returns the path of this handle. This is an opaque string that uniquely identifies this handle in a graph of beans.- Returns:
- the path to the handle
-
hasParent
public boolean hasParent()Return true if the handle already has a parent. -
add
Adds the specified handle as a child of this handle without a name prefix.- Parameters:
child
- the child handle
-
add
Adds the specified handle as a child of this handle using the specified name prefix.- Parameters:
prefix
- the name prefix for the resulting pathchild
- the child handle
-
add
Adds the specifiedHandleAware
as a child of this handle using the specified name prefix.- Parameters:
prefix
- the name prefix for the resulting pathchild
- the child handle
-
broadcast
Causes the associated bean to be broadcast fromHandleService
with a topic of:/kos/handle/<topic>/<path>
. This provides a simple way of sending handle related beans without needing access to the message broker or defining new root topics. This is simply a convenience method.- Parameters:
topic
- the topic fragment to add to the final topicview
- the view used to serialize the object to json
-
toString
Returns the string representation of this object.
-