Accessing configuration objects

Configuration objects are accessed like any other Managed Object. They can be located using the extent or a query using unique or non-unique keys.

If a unique key is defined on a configuration object it must include the version identifier. This is required because different versions of a configuration object may be loaded at the same time. If the unique key did not include the version identifier a duplicate key exception would be reported when the second version of the configuration data was loaded. Example 9.1, “Configuration object” has an example of a unique key that contains the version identifier.

Example 9.7, “Locating configuration objects” loads two versions of the same configuration data and then displays the configuration data using an extent query.

Example 9.5. User configuration version 1.0

//     $Revision: 1.1.2.1 $
configuration "user" version "1.0" type "com.kabira.snippets.configuring"
{
        configure com.kabira.snippets.configuring
        {
                User
                {
                        firstName = "John";
                        lastName = "Doe";
                        age = 8;
                        gender = Gender.MALE;
						joined = "2011-01-25 12:00:00";
                };
                User
                {
                        firstName = "Big";
                        lastName = "Steve";
                        age = 19;
                        gender = Gender.MALE;
						joined = "2011-01-25 01:09:12";
				};
        };
};

Example 9.6. User configuration version 2.0

//     $Revision: 1.1.2.1 $
configuration "user" version "2.0" type "com.kabira.snippets.configuring"
{
        configure com.kabira.snippets.configuring
        {
                User
                {
                        firstName = "John";
                        lastName = "Doe";
                        age = 9;
                        gender = Gender.MALE;
 						joined = "2011-01-25 08:04:00";
               };
                User
                {
                        firstName = "Big";
                        lastName = "Steve";
                        age = 87;
                        gender = Gender.MALE;
                        phoneNumber = "415-555-1212";
						joined = "2011-01-25 01:09:12";
				};
        };
};

Example 9.7. Locating configuration objects

//     $Revision: 1.1.2.1 $
package com.kabira.snippets.configuring;

import com.kabira.test.management.Client;
import com.kabira.test.management.CommandFailed;

import com.kabira.platform.ManagedObject;
import com.kabira.platform.Transaction;
import com.kabira.platform.switchconfig.Version;

import java.net.URL;

/**
 * Snippet to locate configuration objects by version
 *
 * NOTE: This snippet requires the classpath to include the
 * directory where the configuration files are located.  This
 * allows the get resource call to locate the configuration files
 * 
 * <p>
 * <h2> Target Nodes</h2>
 * <ul>
 * <li> <b>domainnode</b> = A
 * </ul>
 */
public class LocateConfiguration
{
    /**
     * Execute snippet
     * @param args                      Not used
     * @throws CommandFailed            Configuration command failed
     * @throws ClassNotFoundException   Could not load a class
     */
    public static void main(String[] args) throws CommandFailed, ClassNotFoundException
    {
        Client client = new Client("guest", "guest");

        URL url1 = client.getClass().getClassLoader().getResource("user1.kcs");
        URL url2 = client.getClass().getClassLoader().getResource("user2.kcs");
        URL url3 = client.getClass().getClassLoader().getResource("team.kcs");

        if ((url1 == null) || (url2 == null) || (url3 == null))
        {
            throw new Error(
                "Configuration files not located - are they installed into "
                + "the current class path?");
        }

        Client.Configuration version1 = client.new Configuration(url1);
        Client.Configuration version2 = client.new Configuration(url2);
        Client.Configuration version3 = client.new Configuration(url3);

        //
        //  Resolve configuration classes
        //
        Class.forName("com.kabira.snippets.configuring.User");
        Class.forName("com.kabira.snippets.configuring.Team");

        //
        //  Load configuration files
        //
        version1.load();
        version2.load();
        version3.load();

        //
        //  Activate version 1
        //
        version1.activate();
        displayData();

        //
        //  Activate version 2
        //
        version2.activate();
        displayData();

        //
        //  Deactive version 2
        //
        version2.deactivate();

        //
        //  Remove configurations
        //
        version1.remove();
        version2.remove();
        version3.remove();
    }

    //
    //  Display configuration data and versions
    //
    private static void displayData()
    {
        //
        //  Display the configuration objects
        //
        new Transaction("Locate Configuration")
        {
            @Override
            protected void run()
            {
                //
                //  Display user configuration objects
                //
                System.out.println("Configuration Data");
                for (User user : ManagedObject.extent(User.class))
                {
                    System.out.println(
                        "\tFirst Name: " + user.firstName
                        + " Last Name: " + user.lastName
                        + " Age: " + user.age
                        + " Joined: " + user.joined
                        + " Configuration Version: " + user.versionId
                        + " Configuration Name: " + user.groupId);
                }
                System.out.println("");
                for (Team team : ManagedObject.extent(Team.class))
                {
                    System.out.println(
                        "\tTeam Name: " + team.name
                        + " Configuration Version: " + team.versionId
                        + " Configuration Name: " + team.groupId);
                    for (Player player : team.players)
                    {
                        System.out.println(
                            "\t\tName: " + player.name
                            + " Position: " + player.position);
                    }
                }
                System.out.println("");

                //
                //  Display version data - skip all versions other
                //  then the ones associated with this configuration type
                //
                System.out.println("Versions");
                for (Version version : ManagedObject.extent(Version.class))
                {
                    if (version.groupKindId.equals(
                        "com.kabira.snippets.configuring") != true)
                    {
                        continue;
                    }
                    System.out.println(
                        "\tType: " + version.groupKindId
                        + " Name: " + version.groupId
                        + " Version: " + version.versionId
                        + " State: " + version.state.name());
                }
                System.out.println("");
            }
        }.execute();
    }
}

When this snippet is run the following output is displayed (annotation added).

#
#     All configuration data is loaded in memory
#
[A] Configuration Data
[A]   First Name: John Last Name: Doe Age: 9 Joined: Tue Jan 25 08:04:00 PST 2011 Configuration Version: 2.0 Configuration Name: user
[A]   First Name: Big Last Name: Steve Age: 87 Joined: Tue Jan 25 01:09:12 PST 2011 Configuration Version: 2.0 Configuration Name: user
[A]   First Name: John Last Name: Doe Age: 8 Joined: Tue Jan 25 12:00:00 PST 2011 Configuration Version: 1.0 Configuration Name: user
[A]   First Name: Big Last Name: Steve Age: 19 Joined: Tue Jan 25 01:09:12 PST 2011 Configuration Version: 1.0 Configuration Name: user
[A] 
[A]   Team Name: giants Configuration Version: 1.0 Configuration Name: team
[A]     Name: Tim Lincecum Position: Pitcher
[A]     Name: Matt Cain Position: Pitcher
[A]     Name: Buster Posey Position: Catcher
[A] 

#
#     Version 1.0 of the user configuration is active.  Version 2.0 of 
#     the user configuration is inactive.  Version 1.0 of the team configuration
#     is inactive
#
[A] Versions
[A]   Type: com.kabira.snippets.configuring Name: user Version: 1.0 State: Active
[A]   Type: com.kabira.snippets.configuring Name: user Version: 2.0 State: Inactive
[A]   Type: com.kabira.snippets.configuring Name: team Version: 1.0 State: Inactive
[A] 

#
#     All configuration data still in memory after activating version 2.0 of
#     user configuration
#
[A] Configuration Data
[A]   First Name: John Last Name: Doe Age: 9 Joined: Tue Jan 25 08:04:00 PST 2011 Configuration Version: 2.0 Configuration Name: user
[A]   First Name: Big Last Name: Steve Age: 87 Joined: Tue Jan 25 01:09:12 PST 2011 Configuration Version: 2.0 Configuration Name: user
[A]   First Name: John Last Name: Doe Age: 8 Joined: Tue Jan 25 12:00:00 PST 2011 Configuration Version: 1.0 Configuration Name: user
[A]   First Name: Big Last Name: Steve Age: 19 Joined: Tue Jan 25 01:09:12 PST 2011 Configuration Version: 1.0 Configuration Name: user
[A] 
[A]   Team Name: giants Configuration Version: 1.0 Configuration Name: team
[A]     Name: Tim Lincecum Position: Pitcher
[A]     Name: Matt Cain Position: Pitcher
[A]     Name: Buster Posey Position: Catcher
[A]

#
#     Version 2.0 of the user configuration is active.  Version 1.0 of 
#     the user configuration is inactive.  Version 1.0 of the team configuration
#     is inactive
#
[A] Versions
[A]   Type: com.kabira.snippets.configuring Name: user Version: 1.0 State: Inactive
[A]   Type: com.kabira.snippets.configuring Name: user Version: 2.0 State: Active
[A]   Type: com.kabira.snippets.configuring Name: team Version: 1.0 State: Inactive