jdk/test/com/sun/servicetag/SvcTagClient.java
changeset 16574 51c8dbc33017
parent 16573 5e63bda2ec36
parent 16544 e6a8f1753168
child 16575 d7ad0dfaa411
equal deleted inserted replaced
16573:5e63bda2ec36 16574:51c8dbc33017
     1 /*
       
     2  * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * @bug     6622366
       
    28  */
       
    29 
       
    30 import com.sun.servicetag.*;
       
    31 import java.io.*;
       
    32 import java.util.*;
       
    33 
       
    34 /**
       
    35  * A utility class simulating stclient for testing purpose.
       
    36  */
       
    37 public class SvcTagClient {
       
    38     private static File xmlFile;
       
    39     private static RegistrationData registration;
       
    40     private static String instanceURN;
       
    41     private static String productName;
       
    42     private static String productVersion;
       
    43     private static String productURN;
       
    44     private static String productParent;
       
    45     private static String productParentURN = ""; // optional
       
    46     private static String productDefinedInstanceID = ""; //optional
       
    47     private static String productVendor;
       
    48     private static String platformArch;
       
    49     private static String container;
       
    50     private static String source;
       
    51 
       
    52     // stclient exit value
       
    53     private static final int ST_ERR_REC_NOT_FOUND = 225;
       
    54 
       
    55     public static void main(String[] args) throws Exception {
       
    56         String path = System.getProperty("stclient.registry.path");
       
    57         if (path == null) {
       
    58             System.err.println("stclient registry path missing");
       
    59             System.exit(-1);
       
    60         }
       
    61         xmlFile = new File(path);
       
    62         if (xmlFile.exists()) {
       
    63             BufferedInputStream in = new BufferedInputStream(new FileInputStream(xmlFile));
       
    64             registration = RegistrationData.loadFromXML(in);
       
    65         } else {
       
    66             registration = new RegistrationData();
       
    67         }
       
    68         boolean add = false;
       
    69         boolean delete = false;
       
    70         boolean update = false;
       
    71         boolean get = false;
       
    72         boolean find = false;
       
    73 
       
    74         int count = 0;
       
    75         while (count < args.length) {
       
    76             String arg = args[count];
       
    77             if (!arg.startsWith("-")) {
       
    78                 System.err.println("Invalid option:" + arg);
       
    79                 System.exit(-1);
       
    80             }
       
    81             if (arg.equals("-a")) {
       
    82                 add = true;
       
    83             } else if (arg.equals("-d")) {
       
    84                 delete = true;
       
    85             } else if (arg.equals("-u")) {
       
    86                 update = true;
       
    87             } else if (arg.equals("-g")) {
       
    88                 get = true;
       
    89             } else if (arg.equals("-f")) {
       
    90                 find = true;
       
    91                 productURN = "";
       
    92             } else if (arg.equals("-t")) {
       
    93                 productURN = args[++count];
       
    94             } else if (arg.equals("-i")) {
       
    95                 instanceURN = args[++count];
       
    96             } else if (arg.equals("-p")) {
       
    97                 productName = args[++count];
       
    98             } else if (arg.equals("-e")) {
       
    99                 productVersion = args[++count];
       
   100             } else if (arg.equals("-t")) {
       
   101                 productURN = args[++count];
       
   102             } else if (arg.equals("-F")) {
       
   103                 productParentURN = args[++count];
       
   104             } else if (arg.equals("-P")) {
       
   105                 productParent = args[++count];
       
   106             } else if (arg.equals("-I")) {
       
   107                 productDefinedInstanceID = args[++count];
       
   108             } else if (arg.equals("-m")) {
       
   109                 productVendor = args[++count];
       
   110             } else if (arg.equals("-A")) {
       
   111                 platformArch = args[++count];
       
   112             } else if (arg.equals("-z")) {
       
   113                 container = args[++count];
       
   114             } else if (arg.equals("-S")) {
       
   115                 source = args[++count];
       
   116             } else {
       
   117                 System.err.println("Invalid option:" + arg);
       
   118                 System.exit(-1);
       
   119             }
       
   120             count++;
       
   121         }
       
   122 
       
   123         if (add) {
       
   124             addServiceTag();
       
   125         } else if (delete) {
       
   126             deleteServiceTag();
       
   127         } else if (update) {
       
   128             updateServiceTag();
       
   129         } else if (get) {
       
   130             getServiceTag();
       
   131         } else if (find) {
       
   132             findServiceTags();
       
   133         } else {
       
   134             System.err.println("Error");
       
   135             System.exit(-1);
       
   136         }
       
   137         updateXmlFile();
       
   138     }
       
   139     private static String OUTPUT = "Product instance URN=";
       
   140 
       
   141     private static void addServiceTag() {
       
   142         if (instanceURN == null) {
       
   143             instanceURN = ServiceTag.generateInstanceURN();
       
   144         }
       
   145         ServiceTag st = ServiceTag.newInstance(instanceURN,
       
   146                                                productName,
       
   147                                                productVersion,
       
   148                                                productURN,
       
   149                                                productParent,
       
   150                                                productParentURN,
       
   151                                                productDefinedInstanceID,
       
   152                                                productVendor,
       
   153                                                platformArch,
       
   154                                                container,
       
   155                                                source);
       
   156         registration.addServiceTag(st);
       
   157         System.out.println(OUTPUT + st.getInstanceURN());
       
   158     }
       
   159 
       
   160     private static void deleteServiceTag() {
       
   161         registration.removeServiceTag(instanceURN);
       
   162         System.out.println("instance_urn=" + instanceURN + " deleted");
       
   163     }
       
   164 
       
   165     private static void updateServiceTag() {
       
   166         registration.updateServiceTag(instanceURN, productDefinedInstanceID);
       
   167         System.out.println("instance_urn=" + instanceURN + " updated");
       
   168     }
       
   169 
       
   170     private static void getServiceTag() {
       
   171         ServiceTag st = registration.getServiceTag(instanceURN);
       
   172         if (st == null) {
       
   173             System.err.println("instance_urn=" + instanceURN + " not found");
       
   174             System.exit(ST_ERR_REC_NOT_FOUND);
       
   175         } else {
       
   176             System.out.println(st);
       
   177         }
       
   178     }
       
   179 
       
   180     private static void findServiceTags() {
       
   181         Set<ServiceTag> set = registration.getServiceTags();
       
   182         for (ServiceTag st : set) {
       
   183             if (st.getProductURN().equals(productURN)) {
       
   184                 System.out.println(st.getInstanceURN());
       
   185             }
       
   186         }
       
   187         if (set.size() == 0) {
       
   188             System.out.println("No records found");
       
   189             System.exit(ST_ERR_REC_NOT_FOUND);
       
   190         }
       
   191     }
       
   192     private static void updateXmlFile() throws IOException {
       
   193         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(xmlFile));
       
   194         try {
       
   195             registration.storeToXML(out);
       
   196         } finally {
       
   197             out.close();
       
   198         }
       
   199     }
       
   200 }