Global context commands
The Command type handler delegates all operations to the implementation of Command stored in the accessed GlobalContext location. This allows you to provide your own implementation of Command and implement the actions to be taken on GET, PUT, POST, and DELETE. For convenience, IBM® WebSphere® sMash provides a zero.core.context.types.commands.BaseCommandImpl with implementations of GET, PUT, POST, and DELETE which return Context.NO_ZONE_UPDATE. Returning Context.NO_ZONE_UPDATE causes no operation to take place.
Because the Command type already has a registered type handler, you do not need to add new type handlers when providing your own Command implementations.
Commands can be used to implement your own behavior. For example, you could defer evaluation of an expensive value until a get is actually called as follows:
@Test
public void testGetCommand() {
zput("/config/command", new BaseCommandImpl() {
@Override
public <V> V get(ParsedURI parsedUri) {
return (V) "foo";
}
});
assertEquals("foo", zget("/config/command"));
}
Another usage of Command would be to implement post to carry out your own custom action.
When using the default implementation of Command provided by BaseCommandImpl Command objects can not be replaced or deleted from the global context. If you need this behavior, you can override the corresponding methods in Command to allow it. For example, the following implementation allows the Command object to be deleted:
@Test
public void testDeleteableCommand() {
GlobalContext.zput("/test/deleteablecmd", new BaseCommandImpl() {
Object value = null;
public <T> T get(ParsedURI parsedUri) {
if (value == null) {
value = "original value";
}
return (T)value;
}
@Override
public <T> T delete(ParsedURI parsedUri) {
return (T) Context.NO_VALUE;
}
});
assertEquals(true, GlobalContext.zcontains("/test/deleteablecmd"));
GlobalContext.zdelete("/test/deleteablecmd");
assertEquals(false, GlobalContext.zcontains("/test/deleteablecmd"));
}