Class LiteralBasedProvider

java.lang.Object
com.lightstreamer.interfaces.metadata.MetadataProviderAdapter
com.lightstreamer.adapters.metadata.LiteralBasedProvider
All Implemented Interfaces:
MetadataProvider

public class LiteralBasedProvider extends MetadataProviderAdapter
Simple full implementation of a Metadata Adapter, made available in Lightstreamer SDK. The Adapter is not meant for production use, but it can be used as a starting point for real Adapters.
The class handles Item List specifications, a special case of Item Group name formed by simply concatenating the names of the Items contained in a List in a space separated way. Similarly, the class handles Field List specifications, a special case of Field Schema name formed by concatenating the names of the contained Fields. The handling of Item List and Field List specifications is required by some optional methods in the client APIs that take advantage of Item Lists and Field Lists.
The resource levels are assigned the same for all Items and Users, according with values that can be supplied together with adapter configuration, inside the "metadata_provider" element that defines the Adapter.
The return of the getAllowedMaxBandwidth method can be supplied in a "max_bandwidth" parameter; the return of the getAllowedMaxItemFrequency method can be supplied in a "max_frequency" parameter; the return of the getAllowedBufferSize method can be supplied in a "buffer_size" parameter; the return of the getDistinctSnapshotLength method can be supplied in a "distinct_snapshot_length" parameter; the return of the getMinSourceFrequency method can be supplied in a "prefilter_frequency" parameter. All resource limits not supplied are granted as unlimited, but for distinct_snapshot_length, which defaults as 10.
The return of the modeMayBeAllowed method (i.e. the association of the proper publishing Mode to each Item) can be configured by supplying a list of rules, which define Item families, where all Items in each family share the same set of allowed Modes (but remember that only one out of the MERGE, DISTINCT and COMMAND Modes is supported by the Server for each Item; only the RAW mode is supported without restrictions). Each family is specified by providing a pattern upon which all names of the Items in the family need to match. The description of each family can be supplied with a pair of parameters, named "item_family_<n>" and "modes_for_item_family_<n>", where <n> is a progressive number, unique for each family. The former parameter specifies the pattern, in java.util.regex.Pattern format, while the latter one specifies the allowed modes, as a list of names, with commas and spaces as allowed separators. In case more than one of the supplied patterns match an Item name, the Item is assigned to the family with the smallest progressive. Items that do not belong to any family are not allowed in any Mode; however, if no families are defined at all, then all Items are allowed in all Modes and the Clients should ensure that the same Item cannot be requested in two conflicting Modes.
There are no access restrictions, but an optional User name check is performed if a comma separated list of User names is supplied in an "allowed_users" parameter.
The following snippet shows an example of configuration of this Adapter in adapters.xml:
 
<!-- Mandatory. Define the Metadata Provider. -->
<metadata_provider>

    <!-- Mandatory. Java class name of the adapter. -->
    <adapter_class>com.lightstreamer.adapters.metadata.LiteralBasedProvider</adapter_class>

    ......

    <!-- Optional. List of initialization parameters specific for LiteralBasedProvider. -->

    <!-- Optional.
         Define values to be returned in getAllowedMaxBandwidth(),
         getAllowedMaxItemFrequency(), getAllowedBufferSize() and
         getDistinctSnapshotLength() methods, for any User and Item
         supplied. -->
    <!--
    <param name="max_bandwidth">40</param>
    <param name="max_frequency">3</param>
    <param name="buffer_size">30</param>
    <param name="distinct_snapshot_length">10</param>
     -->

    <!-- Optional.
         Define comma-separated list of User names to be checked
         for allowance by the notifyUser() method. -->
    <!--
    <param name="allowed_users">user123,user456</param>
     -->

    <!-- Optional.
         Define how the modeMayBeAllowed method should behave, by
         associating to each item the modes in which it can be managed
         by the Server.
         Each pair of parameters of the form "item_family_<n>" and
         "modes_for_item_family_<n>" define respectively the item name
         pattern (in java.util.regex.Pattern format) and the allowed
         modes (in comma separated format) for a family of items.
         Each item is assigned to the first family that matches its name.
         If no families are specified at all, then modeMayBeAllowed
         always returns true, though this is not recommended, because
         the Server does not support more than one mode out of MERGE,
         DISTINCT, and COMMAND for the same item. In such a case, the
         Server would just manage each item in the mode specified by the
         first Client request it receives for the item and would be up to
         the Clients to ensure that the same item cannot be requested in
         two conflicting Modes. -->
    <param name="item_family_1">item.*</param>
    <param name="modes_for_item_family_1">MERGE</param>
    <!--
    <param name="item_family_2">portfolio.*</param>
    <param name="modes_for_item_family_2">COMMAND</param>
     -->

</metadata_provider>
 
 
The source code of the Adapter is provided on GitHub: https://github.com/Lightstreamer/Lightstreamer-example-ReusableMetadata-adapter-java
  • Constructor Details

    • LiteralBasedProvider

      public LiteralBasedProvider()
      Void constructor required by Lightstreamer Kernel.
  • Method Details

    • init

      public void init(Map params, File dir) throws MetadataProviderException
      Reads configuration settings for user and resource constraints. If some setting is missing, the corresponding constraint is not set.
      Specified by:
      init in interface MetadataProvider
      Overrides:
      init in class MetadataProviderAdapter
      Parameters:
      params - Can contain the configuration settings.
      dir - Not used.
      Throws:
      MetadataProviderException - in case of configuration errors.
    • getItems

      public String[] getItems(String user, String sessionID, String itemList) throws ItemsException
      Resolves an Item List specification supplied in a Request. The names of the Items in the List are returned.
      The operation is deferred to a simpler 2-arguments version of the method, where the sessionID argument is discarded. This also ensures backward compatibility with old adapter classes derived from this one.
      Parameters:
      user - A User name.
      sessionID - A Session ID. Not used.
      itemList - An Item List specification.
      Returns:
      An array with the names of the Items in the List.
      Throws:
      ItemsException - never thrown
    • getItems

      public String[] getItems(String user, String itemList) throws ItemsException
      Resolves an Item List specification supplied in a Request. The names of the Items in the List are returned. Item List specifications are expected to be formed by simply concatenating the names of the contained Items, in a space separated way.
      Parameters:
      user - A User name. Not used.
      itemList - An Item List specification.
      Returns:
      An array with the names of the Items in the List.
      Throws:
      ItemsException - never thrown.
    • getSchema

      public String[] getSchema(String user, String sessionID, String itemList, String fieldList) throws SchemaException
      Resolves a Field List specification supplied in a Request. The names of the Fields in the List are returned.
      The operation is deferred to a simpler 3-arguments version of the method, where the sessionID argument is discarded. This also ensures backward compatibility with old adapter classes derived from this one.
      Parameters:
      user - A User name.
      sessionID - A Session ID. Not used.
      itemList - The specification of the Item List whose Items the Field List is to be applied to.
      fieldList - A Field List specification.
      Returns:
      An array with the names of the Fields in the List.
      Throws:
      SchemaException - never thrown
    • getSchema

      public String[] getSchema(String user, String itemList, String fieldList) throws SchemaException
      Resolves a Field List specification supplied in a Request. The names of the Fields in the List are returned. Field List specifications are expected to be formed by simply concatenating the names of the contained Fields, in a space separated way.
      Parameters:
      user - A User name. Not used.
      itemList - The specification of the Item List whose Items the Field List is to be applied to. Not used.
      fieldList - A Field List specification.
      Returns:
      An array with the names of the Fields in the List.
      Throws:
      SchemaException - never thrown.
    • notifyUser

      public void notifyUser(String user, String password, Map httpHeaders) throws AccessException, CreditsException
      Checks if a user is enabled to make Requests to the related Data Providers.
      The check is deferred to a simpler 2-arguments version of the method, where the httpHeader argument is discarded. This also ensures backward compatibility with old adapter classes derived from this one.
      Note that, for authentication purposes, only the user and password arguments should be consulted.
      Specified by:
      notifyUser in interface MetadataProvider
      Overrides:
      notifyUser in class MetadataProviderAdapter
      Parameters:
      user - A User name.
      password - An optional password.
      httpHeaders - A Map that contains a name-value pair for each header found in the HTTP request that originated the call. Not used.
      Throws:
      AccessException - if a list of User names has been configured and the supplied name does not belong to the list.
      CreditsException - never thrown.
      See Also:
    • notifyUser

      public void notifyUser(String user, String password) throws AccessException, CreditsException
      Checks if a user is enabled to make Requests to the related Data Providers. If a list of User names has been configured, this list is checked. Otherwise, any User name is allowed. No password check is performed.
      Overrides:
      notifyUser in class MetadataProviderAdapter
      Parameters:
      user - A User name.
      password - An optional password. Not used.
      Throws:
      AccessException - if a list of User names has been configured and the supplied name does not belong to the list.
      CreditsException - never thrown.
    • getAllowedMaxBandwidth

      public double getAllowedMaxBandwidth(String user)
      Returns the bandwidth level to be allowed to a User for a push Session.
      Specified by:
      getAllowedMaxBandwidth in interface MetadataProvider
      Overrides:
      getAllowedMaxBandwidth in class MetadataProviderAdapter
      Parameters:
      user - A User name. Not used.
      Returns:
      The bandwidth, in Kbit/sec, as supplied in the Metadata Adapter configuration.
    • getAllowedMaxItemFrequency

      public double getAllowedMaxItemFrequency(String user, String item)
      Returns the ItemUpdate frequency to be allowed to a User for a specific Item.
      Specified by:
      getAllowedMaxItemFrequency in interface MetadataProvider
      Overrides:
      getAllowedMaxItemFrequency in class MetadataProviderAdapter
      Parameters:
      user - A User name. Not used.
      item - An Item Name. Not used.
      Returns:
      The allowed Update frequency, in Updates/sec, as supplied in the Metadata Adapter configuration.
    • getAllowedBufferSize

      public int getAllowedBufferSize(String user, String item)
      Returns the size of the buffer internally used to enqueue subsequent ItemUpdates for the same Item.
      Specified by:
      getAllowedBufferSize in interface MetadataProvider
      Overrides:
      getAllowedBufferSize in class MetadataProviderAdapter
      Parameters:
      user - A User name. Not used.
      item - An Item Name. Not used.
      Returns:
      The allowed buffer size, as supplied in the Metadata Adapter configuration.
    • getMinSourceFrequency

      public double getMinSourceFrequency(String item)
      Returns the minimum ItemEvent frequency from the supplier Data Adapter at which the events for an Item are guaranteed to be delivered to the Clients without loss of information. In case of an incoming ItemEvent frequency greater than the specified frequency, Lightstreamer Kernel may prefilter the events flow down to this frequency.
      Specified by:
      getMinSourceFrequency in interface MetadataProvider
      Overrides:
      getMinSourceFrequency in class MetadataProviderAdapter
      Parameters:
      item - not used.
      Returns:
      The maximum frequency to be allowed by the prefilter, as supplied in the Metadata Adapter configuration.
    • getDistinctSnapshotLength

      public int getDistinctSnapshotLength(String item)
      Returns the maximum allowed length for a Snapshot of any Item that has been requested with publishing Mode DISTINCT.
      Specified by:
      getDistinctSnapshotLength in interface MetadataProvider
      Overrides:
      getDistinctSnapshotLength in class MetadataProviderAdapter
      Parameters:
      item - An Item Name.
      Returns:
      The maximum allowed length for the Snapshot, as supplied in the Metadata Adapter configuration. In case no value has been supplied, a default value of 10 events is returned, which is thought to be enough to satisfy typical Client requests.
    • modeMayBeAllowed

      public boolean modeMayBeAllowed(String item, Mode mode)
      Called by Lightstreamer Kernel to ask for the allowance of a publishing Mode for an Item.
      Specified by:
      modeMayBeAllowed in interface MetadataProvider
      Overrides:
      modeMayBeAllowed in class MetadataProviderAdapter
      Parameters:
      item - An Item name.
      mode - A publishing Mode.
      Returns:
      true or false, based on a sequence of rules of the general form <pattern,allowed_modes> supplied in the Adapter configuration. If no pattern matches the Item name, then false is returned; otherwise, the first matching pattern is considered and true is returned only if the related allowed_modes contain the specified Mode. However, if no rules are available at all, then true is always returned. In the latter case, as in any case of loose configuration, conflicting Modes may be both allowed for the same Item, so the Clients should ensure that the same Item cannot be requested in two conflicting Modes.