jdk/src/share/classes/com/sun/servicetag/RegistrationDocument.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1327
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     4
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1327
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1327
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    10
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    15
 * accompanied this code).
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    16
 *
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1327
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1327
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1327
diff changeset
    23
 * questions.
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    24
 */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    25
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    26
package com.sun.servicetag;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    27
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    28
import java.io.*;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    29
import java.net.URL;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    30
import java.util.Collection;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    31
import java.util.Map;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    32
import java.util.Set;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    33
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    34
import org.w3c.dom.Document;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    35
import org.w3c.dom.Element;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    36
import org.w3c.dom.Node;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    37
import org.w3c.dom.NodeList;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    38
import org.xml.sax.SAXException;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    39
import org.xml.sax.InputSource;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    40
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    41
import javax.xml.XMLConstants;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    42
import javax.xml.parsers.DocumentBuilder;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    43
import javax.xml.parsers.DocumentBuilderFactory;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    44
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    45
import javax.xml.parsers.ParserConfigurationException;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    46
import javax.xml.validation.Schema;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    47
import javax.xml.validation.SchemaFactory;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    48
import javax.xml.validation.Validator;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    49
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    50
// For write operation
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    51
import javax.xml.transform.OutputKeys;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    52
import javax.xml.transform.Transformer;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    53
import javax.xml.transform.TransformerException;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    54
import javax.xml.transform.TransformerFactory;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    55
import javax.xml.transform.TransformerConfigurationException;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    56
import javax.xml.transform.dom.DOMSource;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    57
import javax.xml.transform.stream.StreamResult;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    58
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    59
/**
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    60
 * XML Support Class for Product Registration.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    61
 */
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    62
class RegistrationDocument {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    63
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    64
    private static final String REGISTRATION_DATA_SCHEMA =
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    65
            "/com/sun/servicetag/resources/product_registration.xsd";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    66
    private static final String REGISTRATION_DATA_VERSION = "1.0";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    67
    private static final String SERVICE_TAG_VERSION = "1.0";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    68
    final static String ST_NODE_REGISTRATION_DATA = "registration_data";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    69
    final static String ST_ATTR_REGISTRATION_VERSION = "version";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    70
    final static String ST_NODE_ENVIRONMENT = "environment";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    71
    final static String ST_NODE_HOSTNAME = "hostname";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    72
    final static String ST_NODE_HOST_ID = "hostId";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    73
    final static String ST_NODE_OS_NAME = "osName";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    74
    final static String ST_NODE_OS_VERSION = "osVersion";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    75
    final static String ST_NODE_OS_ARCH = "osArchitecture";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    76
    final static String ST_NODE_SYSTEM_MODEL = "systemModel";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    77
    final static String ST_NODE_SYSTEM_MANUFACTURER = "systemManufacturer";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    78
    final static String ST_NODE_CPU_MANUFACTURER = "cpuManufacturer";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    79
    final static String ST_NODE_SERIAL_NUMBER = "serialNumber";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    80
    final static String ST_NODE_REGISTRY = "registry";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    81
    final static String ST_ATTR_REGISTRY_URN = "urn";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    82
    final static String ST_ATTR_REGISTRY_VERSION = "version";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    83
    final static String ST_NODE_SERVICE_TAG = "service_tag";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    84
    final static String ST_NODE_INSTANCE_URN = "instance_urn";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    85
    final static String ST_NODE_PRODUCT_NAME = "product_name";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    86
    final static String ST_NODE_PRODUCT_VERSION = "product_version";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    87
    final static String ST_NODE_PRODUCT_URN = "product_urn";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    88
    final static String ST_NODE_PRODUCT_PARENT_URN = "product_parent_urn";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    89
    final static String ST_NODE_PRODUCT_PARENT = "product_parent";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    90
    final static String ST_NODE_PRODUCT_DEFINED_INST_ID = "product_defined_inst_id";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    91
    final static String ST_NODE_PRODUCT_VENDOR = "product_vendor";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    92
    final static String ST_NODE_PLATFORM_ARCH = "platform_arch";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    93
    final static String ST_NODE_TIMESTAMP = "timestamp";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    94
    final static String ST_NODE_CONTAINER = "container";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    95
    final static String ST_NODE_SOURCE = "source";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    96
    final static String ST_NODE_INSTALLER_UID = "installer_uid";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    97
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    98
    static RegistrationData load(InputStream in) throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
    99
        Document document = initializeDocument(in);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   100
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   101
        // Gets the registration URN
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   102
        Element root = getRegistrationDataRoot(document);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   103
        Element registryRoot =
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   104
                getSingletonElementFromRoot(root, ST_NODE_REGISTRY);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   105
        String urn = registryRoot.getAttribute(ST_ATTR_REGISTRY_URN);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   106
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   107
        // Construct a new RegistrationData object from the DOM tree
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   108
        // Initialize the environment map and service tags
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   109
        RegistrationData regData = new RegistrationData(urn);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   110
        addServiceTags(registryRoot, regData);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   111
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   112
        Element envRoot = getSingletonElementFromRoot(root, ST_NODE_ENVIRONMENT);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   113
        buildEnvironmentMap(envRoot, regData);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   114
        return regData;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   115
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   116
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   117
    static void store(OutputStream os, RegistrationData registration)
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   118
            throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   119
        // create a new document with the root node
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   120
        Document document = initializeDocument();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   121
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   122
        // create the nodes for the environment map and the service tags
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   123
        // in the registration data
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   124
        addEnvironmentNodes(document,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   125
                            registration.getEnvironmentMap());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   126
        addServiceTagRegistry(document,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   127
                              registration.getRegistrationURN(),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   128
                              registration.getServiceTags());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   129
        transform(document, os);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   130
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   131
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   132
    // initialize a document from an input stream
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   133
    private static Document initializeDocument(InputStream in) throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   134
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   135
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   136
            // XML schema for validation
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   137
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   138
            URL xsdUrl = RegistrationDocument.class.getResource(REGISTRATION_DATA_SCHEMA);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   139
            Schema schema = sf.newSchema(xsdUrl);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   140
            Validator validator = schema.newValidator();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   141
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   142
            DocumentBuilder builder = factory.newDocumentBuilder();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   143
            Document doc = builder.parse(new InputSource(in));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   144
            validator.validate(new DOMSource(doc));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   145
            return doc;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   146
        } catch (SAXException sxe) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   147
            IllegalArgumentException e = new IllegalArgumentException("Error generated in parsing");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   148
            e.initCause(sxe);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   149
            throw e;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   150
        } catch (ParserConfigurationException pce) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   151
            // Parser with specific options can't be built
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   152
            // should not reach here
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   153
            throw new InternalError("Error in creating the new document", pce);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   154
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   155
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   156
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   157
    // initialize a new document for the registration data
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   158
    private static Document initializeDocument() throws IOException {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   159
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   160
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   161
            DocumentBuilder builder = factory.newDocumentBuilder();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   162
            Document doc = builder.newDocument();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   163
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   164
            // initialize the document with the registration_data root
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   165
            Element root = doc.createElement(ST_NODE_REGISTRATION_DATA);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   166
            doc.appendChild(root);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   167
            root.setAttribute(ST_ATTR_REGISTRATION_VERSION, REGISTRATION_DATA_VERSION);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   168
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   169
            return doc;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   170
        } catch (ParserConfigurationException pce) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   171
            // Parser with specified options can't be built
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   172
            // should not reach here
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   173
            throw new InternalError("Error in creating the new document", pce);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   174
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   175
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   176
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   177
    // Transform the current DOM tree with the given output stream.
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   178
    private static void transform(Document document, OutputStream os) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   179
        try {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   180
            // Use a Transformer for output
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   181
            TransformerFactory tFactory = TransformerFactory.newInstance();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   182
            tFactory.setAttribute("indent-number", new Integer(3));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   183
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   184
            Transformer transformer = tFactory.newTransformer();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   185
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   186
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   187
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   188
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   189
            transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   190
            transformer.transform(new DOMSource(document),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   191
                new StreamResult(new BufferedWriter(new OutputStreamWriter(os, "UTF-8"))));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   192
        } catch (UnsupportedEncodingException ue) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   193
            // Should not reach here
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   194
            throw new InternalError("Error generated during transformation", ue);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   195
        } catch (TransformerConfigurationException tce) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   196
            // Error generated by the parser
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   197
            // Should not reach here
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   198
            throw new InternalError("Error in creating the new document", tce);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   199
        } catch (TransformerException te) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   200
            // Error generated by the transformer
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   201
            throw new InternalError("Error generated during transformation", te);
1327
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   202
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   203
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   204
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   205
    private static void addServiceTagRegistry(Document document,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   206
                                              String registryURN,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   207
                                              Set<ServiceTag> svcTags) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   208
        // add service tag registry node and its attributes
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   209
        Element reg = document.createElement(ST_NODE_REGISTRY);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   210
        reg.setAttribute(ST_ATTR_REGISTRY_URN, registryURN);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   211
        reg.setAttribute(ST_ATTR_REGISTRY_VERSION, SERVICE_TAG_VERSION);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   212
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   213
        Element root = getRegistrationDataRoot(document);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   214
        root.appendChild(reg);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   215
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   216
        // adds the elements for the service tags
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   217
        for (ServiceTag st : svcTags) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   218
            addServiceTagElement(document, reg, st);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   219
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   220
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   221
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   222
    private static void addServiceTagElement(Document document,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   223
                                             Element registryRoot,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   224
                                             ServiceTag st) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   225
        Element svcTag = document.createElement(ST_NODE_SERVICE_TAG);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   226
        registryRoot.appendChild(svcTag);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   227
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   228
                        ST_NODE_INSTANCE_URN, st.getInstanceURN());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   229
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   230
                        ST_NODE_PRODUCT_NAME, st.getProductName());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   231
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   232
                        ST_NODE_PRODUCT_VERSION, st.getProductVersion());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   233
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   234
                        ST_NODE_PRODUCT_URN, st.getProductURN());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   235
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   236
                        ST_NODE_PRODUCT_PARENT_URN, st.getProductParentURN());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   237
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   238
                        ST_NODE_PRODUCT_PARENT, st.getProductParent());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   239
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   240
                        ST_NODE_PRODUCT_DEFINED_INST_ID,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   241
                        st.getProductDefinedInstanceID());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   242
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   243
                        ST_NODE_PRODUCT_VENDOR, st.getProductVendor());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   244
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   245
                        ST_NODE_PLATFORM_ARCH, st.getPlatformArch());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   246
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   247
                        ST_NODE_TIMESTAMP, Util.formatTimestamp(st.getTimestamp()));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   248
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   249
                        ST_NODE_CONTAINER, st.getContainer());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   250
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   251
                        ST_NODE_SOURCE, st.getSource());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   252
        addChildElement(document, svcTag,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   253
                        ST_NODE_INSTALLER_UID,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   254
                        String.valueOf(st.getInstallerUID()));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   255
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   256
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   257
    private static void addChildElement(Document document, Element root,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   258
                                        String element, String text) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   259
        Element node = document.createElement(element);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   260
        node.appendChild(document.createTextNode(text));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   261
        root.appendChild(node);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   262
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   263
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   264
    // Constructs service tags from the document
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   265
    private static void addServiceTags(Element registryRoot,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   266
                                       RegistrationData registration) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   267
        NodeList children = registryRoot.getElementsByTagName(ST_NODE_SERVICE_TAG);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   268
        int length = (children == null ? 0 : children.getLength());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   269
        for (int i = 0; i < length; i++) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   270
            Element svcTagElement = (Element) children.item(i);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   271
            ServiceTag st = getServiceTag(svcTagElement);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   272
            registration.addServiceTag(st);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   273
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   274
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   275
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   276
    // build environment map from the document
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   277
    private static void buildEnvironmentMap(Element envRoot,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   278
                                         RegistrationData registration) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   279
        registration.setEnvironment(ST_NODE_HOSTNAME, getTextValue(envRoot, ST_NODE_HOSTNAME));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   280
        registration.setEnvironment(ST_NODE_HOST_ID, getTextValue(envRoot, ST_NODE_HOST_ID));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   281
        registration.setEnvironment(ST_NODE_OS_NAME, getTextValue(envRoot, ST_NODE_OS_NAME));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   282
        registration.setEnvironment(ST_NODE_OS_VERSION, getTextValue(envRoot, ST_NODE_OS_VERSION));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   283
        registration.setEnvironment(ST_NODE_OS_ARCH, getTextValue(envRoot, ST_NODE_OS_ARCH));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   284
        registration.setEnvironment(ST_NODE_SYSTEM_MODEL, getTextValue(envRoot, ST_NODE_SYSTEM_MODEL));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   285
        registration.setEnvironment(ST_NODE_SYSTEM_MANUFACTURER, getTextValue(envRoot, ST_NODE_SYSTEM_MANUFACTURER));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   286
        registration.setEnvironment(ST_NODE_CPU_MANUFACTURER, getTextValue(envRoot, ST_NODE_CPU_MANUFACTURER));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   287
        registration.setEnvironment(ST_NODE_SERIAL_NUMBER, getTextValue(envRoot, ST_NODE_SERIAL_NUMBER));
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   288
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   289
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   290
    // add the nodes representing the environment map in the document
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   291
    private static void addEnvironmentNodes(Document document,
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   292
                                            Map<String, String> envMap) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   293
        Element root = getRegistrationDataRoot(document);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   294
        Element env = document.createElement(ST_NODE_ENVIRONMENT);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   295
        root.appendChild(env);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   296
        Set<Map.Entry<String, String>> keys = envMap.entrySet();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   297
        for (Map.Entry<String, String> entry : keys) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   298
            addChildElement(document, env, entry.getKey(), entry.getValue());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   299
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   300
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   301
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   302
    private static Element getRegistrationDataRoot(Document doc) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   303
        Element root = doc.getDocumentElement();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   304
        if (!root.getNodeName().equals(ST_NODE_REGISTRATION_DATA)) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   305
            throw new IllegalArgumentException("Not a " +
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   306
                    ST_NODE_REGISTRATION_DATA +
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   307
                    " node \"" + root.getNodeName() + "\"");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   308
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   309
        return root;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   310
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   311
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   312
    private static Element getSingletonElementFromRoot(Element root, String name) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   313
        NodeList children = root.getElementsByTagName(name);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   314
        int length = (children == null ? 0 : children.getLength());
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   315
        if (length != 1) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   316
            throw new IllegalArgumentException("Invalid number of " + name +
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   317
                    " nodes = " + length);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   318
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   319
        Element e = (Element) children.item(0);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   320
        if (!e.getNodeName().equals(name)) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   321
            throw new IllegalArgumentException("Not a  " + name +
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   322
                    " node \"" + e.getNodeName() + "\"");
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   323
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   324
        return e;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   325
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   326
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   327
    // Constructs one ServiceTag instance from a service tag element root
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   328
    private static ServiceTag getServiceTag(Element svcTagElement) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   329
        return new ServiceTag(
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   330
            getTextValue(svcTagElement, ST_NODE_INSTANCE_URN),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   331
            getTextValue(svcTagElement, ST_NODE_PRODUCT_NAME),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   332
            getTextValue(svcTagElement, ST_NODE_PRODUCT_VERSION),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   333
            getTextValue(svcTagElement, ST_NODE_PRODUCT_URN),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   334
            getTextValue(svcTagElement, ST_NODE_PRODUCT_PARENT),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   335
            getTextValue(svcTagElement, ST_NODE_PRODUCT_PARENT_URN),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   336
            getTextValue(svcTagElement, ST_NODE_PRODUCT_DEFINED_INST_ID),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   337
            getTextValue(svcTagElement, ST_NODE_PRODUCT_VENDOR),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   338
            getTextValue(svcTagElement, ST_NODE_PLATFORM_ARCH),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   339
            getTextValue(svcTagElement, ST_NODE_CONTAINER),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   340
            getTextValue(svcTagElement, ST_NODE_SOURCE),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   341
            Util.getIntValue(getTextValue(svcTagElement, ST_NODE_INSTALLER_UID)),
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   342
            Util.parseTimestamp(getTextValue(svcTagElement, ST_NODE_TIMESTAMP))
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   343
        );
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   344
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   345
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   346
    private static String getTextValue(Element e, String tagName) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   347
        String value = "";
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   348
        NodeList nl = e.getElementsByTagName(tagName);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   349
        if (nl != null && nl.getLength() > 0) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   350
            Element el = (Element) nl.item(0);
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   351
            Node node = el.getFirstChild();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   352
            if (node != null) {
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   353
                value = node.getNodeValue();
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   354
            }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   355
        }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   356
        return value;
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   357
    }
912486b03832 6581243: Service Tag and Product Registration Support in JDK 7
mchung
parents:
diff changeset
   358
}