A selector is an optional filter applied to the updates pertaining to a table subscribed by a session. The Client can specify a selector string through the
setSelector()function. On the server the
isSelected() callback method is invoked onto the custom Metadata Adapter for each update to decide whether the current event should be delivered to that session based on the specified selectior string.
In COMMAND mode, a selector could be used to receive a subset of a table.
In this example, based on the
Portfolio Demo, only one of the stocks in the portfolio, namely "STOCK 04", should be received.
The last lines of the front-end index.html page may become:
[highlight=javascript]
/////////////////////////////////table handler
var table = new DynaMetapushTable("portfolio", schema, "COMMAND");
table.setSelector("STOCK 04");
// the selector name follows a custom convention,
// that is imposed by the custom Metadata Adapter,
// because the latter is the only responsible
// for understanding the selector name
// and performing the requested selection
table.setSnapshotRequired(true);
table.setPushedHtmlEnabled(false);
table.setClearOnDisconnected(true);
table.onChangingValues = formatValues;
table.setMaxDynaRows("unlimited");
table.setMetapushFields(2, 1);
table.setMetapushSort(3, direction);
lsPage.addTable(table, "info");
On the Server side, the Metadata Adapter has to be extended, so as to support the selector requests. Currently, the Portfolio Demo is not based on a custom Metadata Adapter; instead, it takes advantage of a reusable Metadata Adapter provided by Lightstreamer (the LiteralBasedProvider). So, we need to create and compile a new Metadata Adapter and then to install it into Lightstreamer Server. It can be installed by putting its "jar" file beside the Portfolio Demo's Data Adapter "jar" file, that you found in the package as:
The new Metadata Adapter source code may be:
[highlight=java]
package myPackage;
import com.lightstreamer.adapters.metadata.LiteralBasedProvider;
public class myMetadataAdapter extends LiteralBasedProvider {
public boolean isSelectorAllowed(String user, String item, String selector) {
if (item.equals("portfolio")) {
return true;
}
return false; // the default
}
public boolean isSelected(String user, String item, String selector, ItemEvent event) {
// we expect the selector name
// to be the name of a stock to be allowed;
// no other type of selection is supported;
// Note: the "user" parameter would allow us to perform
// a selection based on the specific user
String stockName = event.getValueAsString("stock");
// (see the Javadocs for details on the ItemEvent type)
boolean match = stockName.equals(selector);
return match;
}
}
In order to use the new Metadata Adapter, the Portfolio Demo's adapter configuration file adapters.xml should also be changed and may become like this:
[highlight=xml]
<?xml version="1.0"?>
<adapters_conf id="PORTFOLIO">
<metadata_provider>
<adapter_class>myPackage.myMetadataAdapter</adapter_class>
<!-- the following parameters are still managed
by superclass (LiteralBasedProvider) code -->
<param name="search_dir">.</param>
<param name="max_bandwidth">16</param>
<param name="max_frequency">5</param>
<param name="buffer_size">30</param>
</metadata_provider>
<data_provider>
<adapter_class>it.soltec.lightstream_adapters.data.portfolio.PortfolioDemoProvider</adapter_class>
<param name="config_file">portfolio.conf.properties</param>
</data_provider>
</adapters_conf>