Jump to: navigation, search

COSMOS SDD Tooling Examples

This page contains examples of how to use various functions of the SDD tooling. Please use these examples as references as you create your own applications. Prior knowledge of the SDD is assumed.

SDD Programmatic Interface

The SPI is used to read and write SDDs with a Java interface. These examples will take the reader through reading, writing, and creating SDDs.

Creating SDDs

The SPI is controlled through one instance of a session. In order to use any methods in the SPI, a session must first be created.

SPISession session = SPISession.DEFAULT_INSTANCE;

The SPI objects for the most part correspond to the SDD types. These objects are created from factory objects, so let's create these first.

BaseFactory baseFactory = session.createBaseFactory();
ContentUnitFactory cuFactory = session.createContentUnitFactory();
ArtifactFactory artifactFactory = session.createArtifactFactory();

Each package of the SPI APIs have a factory class. To create an SPI object, all required elements and attributes of the SPI object must be passed into the factory method. This means we'll be creating our deployment descriptor close to the end. Let's create our install artifact first.

Artifact install = artifactFactory.createInstallArtifact();
install.setContentReference("installArtifact");

This sets up our install artifact and sets the content reference to "installArtifact" which will map to one of the contents in the package descriptor. Next we'll add some additional content:

Collection<AdditionalContent> additionalContents = new ArrayList<AdditionalContent>();
additionalContents.add(artifactFactory.createAdditionalContent("bom"));
AdditionalContent additionalContent = artifactFactory.createAdditionalContent("readme");
additionalContent.setType("readme");
additionalContents.add(additionalContent);
install.setAdditionalContents(additionalContents);

A Collection of AdditionalContent types was created and two AdditionalContent objects were created through the artifact factory. The createAdditionalContent method requires the content reference to be defined, which also must match an entry in the package descriptor. All required elements and attributes of the SDD types will be required for the factory methods as well in order to create that type. The final step was to call the appropriate setter method on the install artifact for the additional content.

Now that we have an artifact, we can create our installable unit. Remember that installable units require artifacts.

InstallableUnit iu = cuFactory.createInstallableUnit("IU_ID", "IU_TARGET_REF", install);

We pass in our newly created install artifact to the factory method to create our install artifact.

One of the last objects we need to create are the resources in the topology.

Collection<Resource> resources = new ArrayList<Resource>();
Resource resource = baseFactory.createResource("OS", "OperatingSystem");
resource.setName("Windows Vista");
resource.setDescription(new TranslatableString("The operationg system of the computer.", "osDesc"));
resource.setShortDescription(new TranslatableString("Computer's OS", "osShortDesc"));
resources.add(resource);

Here we have added one resource to our SDD. The factory method takes the reference ID and then the resource type as parameters. In the two description elements, we've introduced the ability for the SDD to handle translatable strings. The first parameter is the default string. The second parameter is the translation key in the properties file that will accompany the SDD.

Now we'll create our final object:

byte b[] = {127, 43, 65, 119, 19, 90, 87, 2, 45, 73, 25, 41, 76, 14, 54, 93};
DeploymentDescriptor dd = baseFactory.createDeploymentDescriptor("1.0", b, 
     Calendar.getInstance().getTime(), iu, resources);

We have everything we need and we finally create the deployment descriptor object. We pass in the schema version (usually 1.0), the descriptor UUID, last modified time, our installable unit, and our resources. Similar techniques can be used to create the package descriptor and add additional elements to the SDD.

Reading SDDs

Using only DOM and the SPI

For reading an SDD, we first need a DOM document. There's a few things I like to setup before reading the actual file into memory.

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setValidating(false);
docBuilderFactory.setNamespaceAware(true);

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.setErrorHandler(new ErrorHandler()
{
    public void warning(SAXParseException e) throws SAXException {
        printErrorMessage(e);
    }

    public void error(SAXParseException e) throws SAXException {
        printErrorMessage(e);
    }

    public void fatalError(SAXParseException e) throws SAXException {
        printErrorMessage(e);
    }
            
    private void printErrorMessage(SAXParseException e)
    {
        System.out.println("Line: " + e.getLineNumber() + ": " + e.getMessage());
    }
});

With that all set, we'll create the Document object.

Document doc = docBuilder.parse(new InputSource("mySDD.xml"));

And now our deployment descriptor.

SPISession session = SPISession.DEFAULT_INSTANCE;
DeploymentDescriptor dd = (DeploymentDescriptor) session.buildDescriptorFromDocument(doc);

At this point, you can run through all the getter methods to get at the data you need to read.

Using the Validator

The validator has methods to read files from the disk to provide you with a Document object.

SDD_DAS sddDas = new SDD_DASImpl();
Document doc = sddDas.loadDocument(new FileInputStream("mySDD.xml"));
SPISession session = SPISession.DEFAULT_INSTANCE;
DeploymentDescriptor dd = (DeploymentDescriptor) session.buildDescriptorFromDocument(doc);

Writing SDDs

Using only DOM and the SPI

Once you've either created an SDD or read one in to change some values, you probably want to write it back to the system. Let's setup some DOM objects first.

DOMImplementationRegistry domRegistry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImpl = (DOMImplementationLS) domRegistry.getDOMImplementation("LS");
LSSerializer serializer = domImpl.createLSSerializer();
serializer.getDomConfig().setParameter("format-pretty-print", true);

This will format our xml like we're used to seeing. Otherwise you may see everything on one line. Now we write it to a file.

Document doc = dd.getOwnerDocument();
FileWriter fw = new FileWriter("mySDD.xml");
BufferedWriter bw = new BufferedWriter(fw);

bw.write(lsSerializer.writeToString(doc));
bw.close();

The dd object here is our deployment descriptor.

Using the Validator

The SDD Validator has methods to write documents to the disk. This will write the SDD to the file mySDD.xml. The null parameter corresponds to the encoding, which defaults to UTF-8. The false parameter tells the validator to save the SDD without validating.

SDD_DAS deploymentDescriptorDAS = new SDD_DASImpl();
deploymentDescriptorDAS.saveDocument(dd.getOwnerDocument(), new FileOutputStream("mySDD.xml"), null, false);

Validator

The validator is used to verify the SDD follows the constraints of the schema. It also allows you to specify your own rules to constrain the SDD. First, we'll look at the easiest way to validate the SDD against the schema.

SDD_DAS deploymentDescriptorDAS = new SDD_DASImpl();
Collection<XMLValidationError> validationErrors = deploymentDescriptorDAS.validate(document);
for (XMLValidationError error : validationErrors) {
	System.err.println(error.getErrorMessage());
}

Using custom rules

If a custom rule is desired, we'll need to create a rule that the validator can understand. To do that, we create a class that implements ValidationRule. ValidationRule has one method to implement.

public Collection<XMLValidationError> validate(InputStream xmlStream)

It receives an InputStream and returns a collection of XMLValidationError objects. Let's take a look at a sample rule.

public class SchemaVersionValidationRule implements ValidationRule 
{
  String schemaVersion;

  public SchemaVersionValidationRule(String schemaVersion) 
  {
    this.schemaVersion = schemaVersion;
  }

  public Collection<XMLValidationError> validate(InputStream xmlStream) 
  {
    Collection<XMLValidationError> validationErrors = new Vector<XMLValidationError>();

    SDD_DAS sddDas = new SDD_DASImpl();
    Document ddDocument = null;
    try
    {
      ddDocument = sddDas.loadDocument(xmlStream);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.exit(1);
    }

    Element dd = ddDocument.getDocumentElement();
    String ddSchema = dd.getAttribute("schemaVersion");

    if (!ddSchema.equals(schemaVersion))
    {
      String errorMsg = "The schema version is not " + schemaVersion;
      validationErrors.add(new XMLValidationError(errorMsg, -1, -1, this, XMLValidationError.ERROR));
    }

    return validationErrors;
  }		
}

This custom rule looks at the schema version in the SDD and returns an error if the wrong version is specified. The constructor of the rule is where we specify the version of the schema we want to see in the SDD. Next is the validate method. We setup the Collection of XMLValidationError types first. Then we get the Document object of our SDD. After that, we get the document element and from that we get the schemaVersion element. We compare this against the version passed into the constructor. If they are different, we create a new XMLValidationError object and add it to the collection. Then we return the collection of errors.

To call this custom rule as a part of our normal validation, we do the following.

SDD_DAS ddDAS = new SDD_DASImpl();
ValidationRule customValidationRule = new SchemaVersionValidationRule("1.0");
ddDAS.addValidation(customValidationRule);

Collection<XMLValidationError> validationErrors = ddDAS.validate(ddDocument);
for (XMLValidationError error : validationErrors) 
{
  System.err.println(error.getErrorMessage());
}

Build Time Generator (BTG)

The BTG will be used to read various sources into SDD fragments and aggregate their content into a single SDD. The Aggregator is still under development, these examples will serve as a guide to use the readers and also how to kick off the merging process from within the Aggregator.

Requisite Packages

In order to use the BTG you will need to pull down the following packages at a minimum:

org.eclipse.cosmos.me.deployment.sdd.common.spi
org.eclipse.cosmos.me.deployment.sdd.common.validation
org.eclipse.cosmos.me.deployment.sdd.tooling.btg

Also you will need to pull down these packages if you wish to use them:

org.eclipse.cosmos.me.deployment.sdd.tooling.btg.aggregator
org.eclipse.cosmos.me.deployment.sdd.tooling.btg.plugin.sddreader
org.eclipse.cosmos.me.deployment.sdd.tooling.btg.plugin.rpmreader

Properties File

The BTG requires a properties file in order to operate. The format is as follows:

[org.eclipse.cosmos.me.deployment.sdd.tooling.btg]
OutputFileNameBase = ESR

followed by a list of the BTG plugins (ie: the aggregator or any readers) you wish to invoke.

Example:

[org.eclipse.cosmos.me.deployment.sdd.tooling.btg.aggregator]
Enabled = true

[org.eclipse.cosmos.me.deployment.sdd.tooling.btg.plugin.sddreader]
Enabled = true
# Name of xml file to read from
FileName = C:\sdd1.xml

[org.eclipse.cosmos.me.deployment.sdd.tooling.btg.plugin.rpmreader]
Enabled = true
# Name of rpm file to read from
FileName = C:\sdd2.rpm

Command Line Arguments

The BTG requires that you specify where the properties file is within a command line argument. The format is as follows:

-propertiesFile "C:\BTG.ini"

Run Configuration

In order to run the BTG, you will need to create a run configuration for it within eclipse. To do this, Click on the down arrow next to the run button in eclipse and select run configurations. Then right click on Eclipse Application on the left, and select "new". Call the new configuration whatever you'd like, I chose "btg". On the right under the main tab for the new configuration, make sure run an application is selected, with the application specified being:

org.eclipse.cosmos.me.deployment.sdd.tooling.btg.application

Then under the arguments tab, clear out the program arguments section completely, and then add in your command line argument from above.

Using the Aggregator

The readers that are specified in the properties file will be invoked and their SDD's will be passed into the Aggregator's aggregateSDDData function within the
Collection<SDDFragment> fragments
variable. From there you can pass the fragments along to the
DeploymentDescriptorType.merge(DeploymentDescriptor dd1, DeploymentDescriptor dd2)
function. This will kick off the merging process.

RPM Reader Example

The RPM Reader will read in an RPM and convert it to an SDD which can then be merged with other SDD's created by other readers.

You can specify its use in the properties file as such:

[org.eclipse.cosmos.me.deployment.sdd.tooling.btg.plugin.rpmreader]
Enabled = true
# Name of rpm file to read from
FileName = C:\sdd2.rpm

As an example, the JRE RPM located here [1] will produce the following SDD:

<?xml version="1.0" encoding="UTF-8"?>
<sdd-dd:DeploymentDescriptor
    xmlns:sdd-dd="http://docs.oasis-open.org/sdd/ns/deploymentDescriptor"
    descriptorID="0EF97934A40A1A2C7119AD4DBD23522D"
    lastModified="2009-03-10T02:41:05-04:00" schemaVersion="1.0" xmlns:sdd-common="http://docs.oasis-open.org/sdd/ns/common">
    <sdd-dd:Topology>
        <sdd-dd:Resource id="os" type="OperatingSystem">
            <sdd-dd:Name>linux</sdd-dd:Name>
            <sdd-dd:Property>
                <sdd-dd:PropertyName>rpmRequirenameIndex</sdd-dd:PropertyName>
                <sdd-dd:Value>50</sdd-dd:Value>
            </sdd-dd:Property>
            <sdd-dd:Property>
                <sdd-dd:PropertyName>arch</sdd-dd:PropertyName>
                <sdd-dd:Value>i586</sdd-dd:Value>
            </sdd-dd:Property>
            <sdd-dd:HostedResource id="glibc_1" type="RequiredSoftware">
                <sdd-dd:Name>glibc</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="java-jre-tools_2" type="RequiredSoftware">
                <sdd-dd:Name>java-jre-tools</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="java-sun-jre_3" type="RequiredSoftware">
                <sdd-dd:Name>java-sun-jre</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="jpackage-utils_4" type="RequiredSoftware">
                <sdd-dd:Name>jpackage-utils</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libX11.so.6_5" type="RequiredSoftware">
                <sdd-dd:Name>libX11.so.6</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libXext.so.6_6" type="RequiredSoftware">
                <sdd-dd:Name>libXext.so.6</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libawt.so_7" type="RequiredSoftware">
                <sdd-dd:Name>libawt.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libc.so.6_8" type="RequiredSoftware">
                <sdd-dd:Name>libc.so.6</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libdl.so.2_9" type="RequiredSoftware">
                <sdd-dd:Name>libdl.so.2</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libgcc_10" type="RequiredSoftware">
                <sdd-dd:Name>libgcc</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libgcc_s.so.1_11" type="RequiredSoftware">
                <sdd-dd:Name>libgcc_s.so.1</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libjava.so_12" type="RequiredSoftware">
                <sdd-dd:Name>libjava.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libjli.so_13" type="RequiredSoftware">
                <sdd-dd:Name>libjli.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libjvm.so_14" type="RequiredSoftware">
                <sdd-dd:Name>libjvm.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libm.so.6_15" type="RequiredSoftware">
                <sdd-dd:Name>libm.so.6</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libmawt.so_16" type="RequiredSoftware">
                <sdd-dd:Name>libmawt.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libmlib_image.so_17" type="RequiredSoftware">
                <sdd-dd:Name>libmlib_image.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libnet.so_18" type="RequiredSoftware">
                <sdd-dd:Name>libnet.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libnsl.so.1_19" type="RequiredSoftware">
                <sdd-dd:Name>libnsl.so.1</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libpthread.so.0_20" type="RequiredSoftware">
                <sdd-dd:Name>libpthread.so.0</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libstdc++_21" type="RequiredSoftware">
                <sdd-dd:Name>libstdc++</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libstdc++.so.5_22" type="RequiredSoftware">
                <sdd-dd:Name>libstdc++.so.5</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libthread_db.so.1_23" type="RequiredSoftware">
                <sdd-dd:Name>libthread_db.so.1</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="libverify.so_24" type="RequiredSoftware">
                <sdd-dd:Name>libverify.so</sdd-dd:Name>
            </sdd-dd:HostedResource>
            <sdd-dd:HostedResource id="rpmlib(PayloadIsBzip2)_25" type="RequiredSoftware">
                <sdd-dd:Name>rpmlib(PayloadIsBzip2)</sdd-dd:Name>
            </sdd-dd:HostedResource>
        </sdd-dd:Resource>
        <sdd-dd:Resource id="rpm_pm" type="PackageManager">
            <sdd-dd:Name>RPM Package Manager</sdd-dd:Name>
        </sdd-dd:Resource>
        <sdd-dd:Resource id="java-sun-jre" type="InstallableResource">
            <sdd-dd:Name>java-sun-jre</sdd-dd:Name>
        </sdd-dd:Resource>
    </sdd-dd:Topology>
    <sdd-dd:InstallableUnit id="java-sun-jre_IU" targetResourceRef="rpm_pm">
        <sdd-dd:Requirements>
            <sdd-dd:Requirement id="PreReqs" operation="install">
                <sdd-dd:ResourceConstraint id="constraint_1" resourceRef="glibc_1"/>
                <sdd-dd:ResourceConstraint id="constraint_2" resourceRef="java-jre-tools_2"/>
                <sdd-dd:ResourceConstraint id="constraint_3" resourceRef="java-sun-jre_3"/>
                <sdd-dd:ResourceConstraint id="constraint_4" resourceRef="jpackage-utils_4"/>
                <sdd-dd:ResourceConstraint id="constraint_5" resourceRef="libX11.so.6_5"/>
                <sdd-dd:ResourceConstraint id="constraint_6" resourceRef="libXext.so.6_6"/>
                <sdd-dd:ResourceConstraint id="constraint_7" resourceRef="libawt.so_7"/>
                <sdd-dd:ResourceConstraint id="constraint_8" resourceRef="libc.so.6_8"/>
                <sdd-dd:ResourceConstraint id="constraint_9" resourceRef="libdl.so.2_9"/>
                <sdd-dd:ResourceConstraint id="constraint_10" resourceRef="libgcc_10"/>
                <sdd-dd:ResourceConstraint id="constraint_11" resourceRef="libgcc_s.so.1_11"/>
                <sdd-dd:ResourceConstraint id="constraint_12" resourceRef="libjava.so_12"/>
                <sdd-dd:ResourceConstraint id="constraint_13" resourceRef="libjli.so_13"/>
                <sdd-dd:ResourceConstraint id="constraint_14" resourceRef="libjvm.so_14"/>
                <sdd-dd:ResourceConstraint id="constraint_15" resourceRef="libm.so.6_15"/>
                <sdd-dd:ResourceConstraint id="constraint_16" resourceRef="libmawt.so_16"/>
                <sdd-dd:ResourceConstraint id="constraint_17" resourceRef="libmlib_image.so_17"/>
                <sdd-dd:ResourceConstraint id="constraint_18" resourceRef="libnet.so_18"/>
                <sdd-dd:ResourceConstraint id="constraint_19" resourceRef="libnsl.so.1_19"/>
                <sdd-dd:ResourceConstraint id="constraint_20" resourceRef="libpthread.so.0_20"/>
                <sdd-dd:ResourceConstraint id="constraint_21" resourceRef="libstdc++_21"/>
                <sdd-dd:ResourceConstraint id="constraint_22" resourceRef="libstdc++.so.5_22"/>
                <sdd-dd:ResourceConstraint id="constraint_23" resourceRef="libthread_db.so.1_23"/>
                <sdd-dd:ResourceConstraint id="constraint_24" resourceRef="libverify.so_24"/>
                <sdd-dd:ResourceConstraint id="constraint_25" resourceRef="rpmlib(PayloadIsBzip2)_25"/>
            </sdd-dd:Requirement>
            <sdd-dd:Requirement id="PackageManager" operation="install">
                <sdd-dd:ResourceConstraint id="rpmConstraint" resourceRef="rpm_pm"/>
            </sdd-dd:Requirement>
        </sdd-dd:Requirements>
        <sdd-dd:Artifacts>
            <sdd-dd:InstallArtifact contentRef="rpmFile" type="rpm"/>
        </sdd-dd:Artifacts>
    </sdd-dd:InstallableUnit>
</sdd-dd:DeploymentDescriptor>