Versions

To find an active version for a specific configuration type and name the ByGroupState key is used. This non-unique key allows Version objects to be selected using the configuration type, name, and state. The ByGroupState key is non-unique since there can be multiple Inactive versions for a specific configuration type and name. There is always only zero or one Active version.

[Note]

For historical reasons the configuration name is called Group in the configuration API. The documentation will always use name to describe this concept.

All configuration objects associated with a version (active or not) are found using the Version.getConfigs() method. This is shown in Example 9.8, “Version object”. Configuration objects are returned from the Version.getConfigs() method in the same order in which they were loaded from the configuration file.

Example 9.8. Version object

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

import com.kabira.platform.KeyFieldValueList;
import com.kabira.platform.KeyManager;
import com.kabira.platform.KeyQuery;
import com.kabira.platform.LockMode;
import com.kabira.test.management.Client;
import com.kabira.test.management.CommandFailed;

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

import java.net.URL;

/**
 * Snippet to locate the active 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 ActiveVersion
{
    /**
     * Execute snippet
     * @param args      Not used
     * @throws CommandFailed            Configuration command filed
     * @throws ClassNotFoundException   Configuration class not found
     */
    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");

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

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

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

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

        //
        //  Activate version 1
        //
        System.out.println("Activate version 1.0");
        version1.activate();
        displayActiveVersion();

        //
        //  Activate version 2
        //
        System.out.println("Activate version 2.0");
        version2.activate();
        displayActiveVersion();

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

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

    //
    //  Locate and display the active version
    //
    private static void displayActiveVersion()
    {
        new Transaction("Display Active Version")
        {
            @Override
            protected void run() throws Rollback
            {
                KeyQuery<Version> kq;
                KeyFieldValueList kfvl;
                //
                //  The active version is selected using the ByGroupState key.
                //
                kq = new KeyManager<Version>().
                    createKeyQuery(Version.class, "ByGroupState");
                kfvl = new KeyFieldValueList();
                kfvl.add("groupKindId", User.class.getPackage().getName());
                kfvl.add("groupId", "user");
                kfvl.add("state", Version.State.Active);
                kq.defineQuery(kfvl);

                for (Version version : kq.getResults(LockMode.READLOCK))
                {
                    System.out.println(
                        "Name: " + version.groupId
                        + " Type: " + version.groupKindId
                        + " Version: " + version.versionId
                        + " State: " + version.state.name());

                    Config[] configArray = version.getConfigs();
                    System.out.println("Contains:");
                    for (Config config : configArray)
                    {
                        User    user = (User)config;

                        if (user == null)
                        {
                            continue;
                        }

                        System.out.println(
                            "\tFirst Name: " + user.firstName
                            + " Last Name: " + user.lastName
                            + " Age: " + user.age
                            + " Gender: " + user.gender
                            + " Joined: " + user.joined
                            + " Phone Number: " + user.phoneNumber
                            + " Configuration Version: " + user.versionId
                            + " Configuration Name: " + user.groupId);
                    }
                }
            }
        }.execute();
    }
}


When this snippet is run it displays the following output (annotation added):

#
#    Version 1.0 is active
#
[A] Activate version 1.0
[A] Name: user Type: com.kabira.snippets.configuring Version: 1.0 State: Active
[A] Contains:
[A]  First Name: John Last Name: Doe Age: 8 Gender: MALE Joined: Tue Jan 25 12:00:00 PST 2011 Phone Number: Not provided Configuration Version: 1.0 Configuration Name: user
[A]  First Name: Big Last Name: Steve Age: 19 Gender: MALE Joined: Tue Jan 25 01:09:12 PST 2011 Phone Number: Not provided Configuration Version: 1.0 Configuration Name: user

#
#     Version 2.0 is active
#
[A] Activate version 2.0
[A] Name: user Type: com.kabira.snippets.configuring Version: 2.0 State: Active
[A] Contains:
[A]  First Name: John Last Name: Doe Age: 9 Gender: MALE Joined: Tue Jan 25 08:04:00 PST 2011 Phone Number: Not provided Configuration Version: 2.0 Configuration Name: user
[A]  First Name: Big Last Name: Steve Age: 87 Gender: MALE Joined: Tue Jan 25 01:09:12 PST 2011 Phone Number: 415-555-1212 Configuration Version: 2.0 Configu