Jump to: navigation, search

Orion/Documentation/Developer Guide/Configuration services

Overview

Orion provides a number of service APIs related to service configuration.

Managed Services

A service may need configuration information before it can perform its intended functionality. Such services are called Managed Services. A Managed Service implements a method, updated(), which is called by the Orion configuration framework to provide configuration data to the service. As with all service methods, updated() is called asynchronously. The configuration data takes the form of a dictionary of key-value pairs, called properties. If no configuration data exists for the Managed Service, null properties are passed.

A Managed Service needs to receive its configuration information before the service is invoked to perform other work. For example, a configurable validation service would want to receive any custom validation options (or null, if no custom options were configured) before actually performing any validation. For this reason, the framework guarantees that a Managed Service's updated() method will be called prior to any other service methods the service may implement.

Managed Services can be contributed by registering against the orion.cm.managedservice service name. Every Managed Service must provide a service property named "pid" which gives a PID (persistent identifier). The PID uniquely identifies the Managed Service, and serves as a primary key for its configuration information.

The Orion concept of a Managed Service is analogous to the OSGi Managed Service.

Meta Typing

A Metatype describes the shape of configuration data*. In other words, it specifies what property names can appear in the properties dictionary, and what data types (string, boolean, number, etc) their values may have. Metatype information is defined in terms of Object Classes, which can be reused. Metatype information is associated with a Managed Service's PID. Metatype information is optional, so not every Managed Service need have Metatype information associated with it.

Metatype information can be contributed by registering a service with the orion.cm.metatype service name.

The Orion concept of a Metatype is analogous to the OSGi Metatype.

* In this page we discuss Metatype information solely in the context of configuration management. Strictly speaking, Metatypes are generic, and can potentially be used for other purposes.

Configuration management

Configuration data is managed by a ConfigurationAdmin service, which maintains a database of Configuration objects. The ConfigurationAdmin monitors the service registry and provides configuration data to Managed Services that are registered. Orion's implementation of ConfigurationAdmin persists configuration data to a Preferences Service.

In JavaScript code, configuration information is represented as Configuration objects (refer to "orion.cm.Configuration" in the client API reference for details), which are returned by the ConfigurationAdmin's service methods. Because the ConfigurationAdmin service is currently only accessible to code running in the same window as the service registry, Configuration objects cannot be directly interacted with by external services. Managed Services can only receive configuration information via their updated() method.

The Orion ConfigurationAdmin service is analogous to the OSGi ConfigurationAdmin.

Settings

On top of the basic configuration and metatype APIs, Orion also provides a higher-level Settings API. See Plugging into the settings page for details.

orion.cm.configadmin

The orion.cm.configadmin service, also called ConfigurationAdmin, provides management of configuration information. Internally, the ConfigurationAdmin service is used by the Plugin Settings page to manage the values of settings.

The service methods are:

getConfiguration(pid)
Returns the Configuration with the given PID from the database. If no such Configuration exists, a new one is created and then returned.
listConfigurations()
Returns an array of all current Configuration objects from the database.

Refer to orion.cm.ConfigurationAdmin in the client API reference for a full description of this service's API methods.

Here is an example of how to use the ConfigurationAdmin service to print out all existing configurations and their property values:

var configurations = serviceRegistry.getService("orion.cm.configadmin").listConfigurations().then(function(configurations) {
        configurations.forEach(function(configuration) {
            var properties = configuration.getProperties();
            var propertyInfo = Object.keys(properties).map(function(propertyName) {
                if (propertyName !== "pid") {
                    return "\n        " + propertyName + ": " + JSON.stringify(properties[propertyName]) + "\n";
                }
            }).join("");
            console.log("Configuration pid: " + configuration.getPid() + "\n"
                      + "  properties: {" + propertyInfo + "\n"
                      + "  }");
        });
});

The result might look something like this:

Configuration pid: nonnls.config
  properties: {
        enabled: false
  }
Configuration pid: jslint.config
  properties: {
        options: "laxbreak:true, maxerr:50"
  }

orion.cm.managedservice

Contributes a Managed Service. A Managed Service is a service that can receive configuration data.

Service properties

A Managed Service must define the following property:

pid
String Gives the PID for this Managed Service.

Service methods

A Managed Service must implement the following method:

updated(properties)
The ConfigurationAdmin invokes this method to provide configuration to this Managed Service. If no configuration exists for this Managed Service's PID, properties is null. Otherwise, properties is a dictionary containing the service's configuration data.

Examples

This minimal example shows the implementation of a plugin which registers a Managed Service under the PID "example.pid". When its updated() method is called, it simply prints out what it received:

define(["orion/plugin"], function(PluginProvider) {
    var provider = new PluginProvider();
    provider.registerService(["orion.cm.managedservice"],
        {  pid: "example.pid"
        },
        {  updated: function(properties) {
               if (properties === null) {
                 console.log("We have no properties :(");
               } else {
                 console.log("We got properties!");
                 console.dir(properties);
               }
           }
        });
    provider.connect();
});

Here is a larger example, showing how a validation service (a spellchecker that checks for any occurrences of the misspelling "definately") might accept options through its configuration properties. The service implementation in this example is both a validator and a Managed Service.

define(["orion/plugin"], function(PluginProvider) {
    var provider = new PluginProvider();
    var options;
    provider.registerService(["orion.cm.managedservice", "orion.edit.validator"],
        {  pid: "example.validator",
           contentType: ["text/plain"]
        },
        {  updated: function(properties) {
               if (properties === null) {
                   // No configuration, use default
                   options = { enabled: true };
               } else {
                   options = { enabled: !!properties.enabled };
               }
           },
           checkSyntax: function(title, contents) {
               if (!options.enabled) {
                   return { problems: [] };
               }
               var problems = [];
               contents.split(/\r?\n/).forEach(function(line, i) {
                   var index;
                   if ((index = line.indexOf("definately") !== -1) {
                       problems.push({
                           description: "Misspelled word",
                           line: i+1,
                           start: index,
                           end: index+10,
                           severity: "warning"
                       });
                   }
               });
               return { problems: problems };
           }
        });
    provider.connect();
});

The updated() method here checks its configuration dictionary for a boolean enabled property that determines whether the validator is active. In the case of null properties, the service uses a reasonable default. (It's a best practice for configurable services to behave sanely when no configuration has been defined for them.)

Note that, by virtue of the configuration framework's guarantee that updated() is called before all other service methods, our checkSyntax() method can safely assume that the options variable has been set.

orion.cm.metatype

The orion.cm.metatype service contributes Metatype information. Metatype information is based around Object Classes, which are first-class reusable elements. An Object Class contains one or more Property Types. A Property Type defines an individual property that can appear within a particular instance of the containing Object Class.

The orion.cm.metatype service serves two purposes:

  • Define an Object Class.
  • Associate an Object Class with a PID (see Managed Services).

Object Classes are analogous to OSGi Object Class Definitions, and Property Types to OSGi Attribute Definitions. In OO terms, Object Classes are similar to classes, and Property Types are similar to fields or instance variables.

Service properties

There are two top-level properties: classes (defines an Object Class), and designates (associates an Object Class with a PID). Either of these properties, or both of them, may be specified.

Define an Object Class

To define one or more Object Classes, the classes service property is used:

classes
ObjectClass[]. Defines Object Classes. Object Classes defined here can be referenced elsewhere by their ID. Each ObjectClass element has the following shape:
id
String. Uniquely identifies this Object Class.
name
String. Optional. The name of this Object Class.
properties
PropertyType[]. Defines the Property Types that can appear in instances of this Object Class. Each PropertyType element has the following shape:
id
String. The property id. This is unique within the containing Object Class.
name
String. Optional. The name of this property.
type
'string' | 'number' | 'boolean'. Optional, defaults to 'string'. The data type.
defaultValue
Object. Optional, defaults to null. The default value of this property. This is a literal whose type matches the property's type.
options
PropertyOption[]. Optional, defaults to null. If nonnull, gives an enumeration of allowed values that this property can take. Each PropertyOption element has the following shape:
value
Object. The value of this option. This is a literal value whose type matches the property's type.
label
String. The label for this option.

Associate an Object Class with a PID

To create PID-to-Object-Class associations, the designates service property is used:

designates
Designate[]. Each Designate element has the following shape:
pid
String. The PID for which Object Class information will be associated.
classId
String. References an Object Class by ID. The referenced Object Class will be associated with the PID.

Object Classes are publicly visible, so the Object Class referenced by a Designate element may be defined by a different Metatype service. The order in which Metatype services are registered does not matter.

Service methods

None. This service is purely declarative.

Examples

This example shows how to define an Object Class with ID example.customer. The Object Class has two PropertyTypes.

define(['orion/plugin'], function(PluginProvider) {
    var pluginProvider = new PluginProvider();
    pluginProvider.registerService('orion.cm.metatype',
        {},
        {  classes: [
               {  id: 'example.customer',
                  properties: [
                      {  id: 'fullname',
                         name: 'Full Name',
                         type: 'string'
                      },
                      {  id: 'address',
                         name: 'Mailing Address',
                         type: 'string'
                      }
                  ]
               }
           ]
        });
provider.connect();
});

Building on the previous example, here's how we would use a designates to associate the example.customer Object Class with a PID named example.pid. (The bold text shows what was added.)

define(['orion/plugin'], function(PluginProvider) {
    var pluginProvider = new PluginProvider();
    pluginProvider.registerService('orion.cm.metatype',
        {},
        {  classes: [
               {  id: 'example.customer',
                  properties: [
                      {  id: 'fullname',
                         name: 'Full Name',
                         type: 'string'
                      },
                      {  id: 'address',
                         name: 'Mailing Address',
                         type: 'string'
                      }
                  ]
               }
           ]
        });
    pluginProvider.registerService('orion.cm.metatype',
        {},
        {  designates: [
               {  pid: 'example.pid',
                  classId: 'example.customer'
               }
           ]
        });
    provider.connect();
});

Alternatively, we can use a single service registration, with both classes and designates, to achieve the same effect:

define(['orion/plugin'], function(PluginProvider) {
    var pluginProvider = new PluginProvider();
    pluginProvider.registerService('orion.cm.metatype',
        {},
        {  classes: [
               {  id: 'example.customer',
                  properties: [
                      {  id: 'fullname',
                         name: 'Full Name',
                         type: 'string'
                      },
                      {  id: 'address',
                         name: 'Mailing Address',
                         type: 'string'
                      }
                  ]
               }
           ],
           designates: [
               {  pid: 'example.pid',
                  classId: 'example.customer'
               }
           ]
        });
    provider.connect();
});

See also

Plugging into the settings page