From 7bf7f3d284a3fbeb91ab3bf0e12437b8f5d92ab5 Mon Sep 17 00:00:00 2001 From: alena Date: Mon, 10 Jan 2011 12:17:23 -0800 Subject: [PATCH] Api xml doc: generate xml per command in addition to xml containing all api commands --- client/tomcatconf/commands.properties.in | 1 - .../com/cloud/api/doc/ApiXmlDocWriter.java | 81 +++++++++++++++++-- 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in index a0f2bb7b083..eb37d83d507 100755 --- a/client/tomcatconf/commands.properties.in +++ b/client/tomcatconf/commands.properties.in @@ -168,7 +168,6 @@ listCapacity=com.cloud.api.commands.ListCapacityCmd;1 addHost=com.cloud.api.commands.AddHostCmd;1 addCluster=com.cloud.api.commands.AddClusterCmd;1 deleteCluster=com.cloud.api.commands.DeleteClusterCmd;1 -add=com.cloud.api.commands.AddHostCmd;1 reconnectHost=com.cloud.api.commands.ReconnectHostCmd;1 updateHost=com.cloud.api.commands.UpdateHostCmd;1 deleteHost=com.cloud.api.commands.DeleteHostCmd;1 diff --git a/server/src/com/cloud/api/doc/ApiXmlDocWriter.java b/server/src/com/cloud/api/doc/ApiXmlDocWriter.java index 9f10077e7c2..1e68f8bd219 100644 --- a/server/src/com/cloud/api/doc/ApiXmlDocWriter.java +++ b/server/src/com/cloud/api/doc/ApiXmlDocWriter.java @@ -18,8 +18,10 @@ package com.cloud.api.doc; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.ObjectOutputStream; @@ -30,6 +32,8 @@ import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Properties; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.apache.log4j.Logger; @@ -57,7 +61,7 @@ public class ApiXmlDocWriter { public static void main (String[] args) { Properties preProcessedCommands = new Properties(); - Enumeration command = null; + Enumeration command = null; String[] fileNames = null; List argsList = Arrays.asList(args); @@ -114,14 +118,23 @@ public class ApiXmlDocWriter { XStream xs = new XStream(); xs.alias("command", Command.class); xs.alias("arg", Argument.class); + String xmlDocDir = dirName + "/xmldoc"; + String rootAdminDirName = xmlDocDir + "/root_admin"; + String domainAdminDirName = xmlDocDir + "/domain_admin"; + String regularUserDirName = xmlDocDir + "/regular_user"; + (new File(rootAdminDirName)).mkdirs(); + (new File(domainAdminDirName)).mkdirs(); + (new File(regularUserDirName)).mkdirs(); ObjectOutputStream out = xs.createObjectOutputStream(new FileWriter(dirName + "/commands.xml"), "commands"); - ObjectOutputStream outDomainAdmin = xs.createObjectOutputStream(new FileWriter(dirName + "/commandsDomainAdmin.xml"), "commands"); - ObjectOutputStream regularUser = xs.createObjectOutputStream(new FileWriter(dirName + "/commandsRegularUser.xml"), "commands"); + ObjectOutputStream rootAdmin = xs.createObjectOutputStream(new FileWriter(rootAdminDirName + "/" + "rootAdminSummary.xml"), "commands"); + ObjectOutputStream outDomainAdmin = xs.createObjectOutputStream(new FileWriter(domainAdminDirName + "/" + "domainAdminSummary.xml"), "commands"); + ObjectOutputStream regularUser = xs.createObjectOutputStream(new FileWriter(regularUserDirName + "/regularUserSummary.xml"), "commands"); - while (command.hasMoreElements()) { + while (command.hasMoreElements()) { + ObjectOutputStream singleCommandOs = null; String key = (String) command.nextElement(); - Class clas = Class.forName(all_api_commands.getProperty(key)); + Class clas = Class.forName(all_api_commands.getProperty(key)); ArrayList request = new ArrayList(); ArrayList response = new ArrayList(); @@ -170,7 +183,7 @@ public class ApiXmlDocWriter { } } - Class responseClas = impl.responseObject(); + Class responseClas = impl.responseObject(); //Get response parameters Field[] responseFields = responseClas.getDeclaredFields(); @@ -188,22 +201,76 @@ public class ApiXmlDocWriter { //Write command to xml file out.writeObject(apiCommand); + rootAdmin.writeObject(apiCommand); + + //Write single command to xml file + singleCommandOs = xs.createObjectOutputStream(new FileWriter(rootAdminDirName + "/" + key + ".xml"), "command"); if (domain_admin_api_commands.containsKey(key)){ outDomainAdmin.writeObject(apiCommand); + singleCommandOs = xs.createObjectOutputStream(new FileWriter(domainAdminDirName + "/" + key + ".xml"), "command"); } if (regular_user_api_commands.containsKey(key)){ + singleCommandOs = xs.createObjectOutputStream(new FileWriter(regularUserDirName + "/" + key + ".xml"), "command"); regularUser.writeObject(apiCommand); } + singleCommandOs.writeObject(apiCommand); + singleCommandOs.close(); } out.close(); + rootAdmin.close(); outDomainAdmin.close(); - regularUser.close(); + regularUser.close(); + + //gzip directory with xml doc + zipDir(dirName + "xmldoc.zip", xmlDocDir); + + //Delete directory + deleteDir(new File(xmlDocDir)); + } catch (Exception ex) { ex.printStackTrace(); System.exit(2); } } + + private static void zipDir(String zipFileName, String dir) throws Exception { + File dirObj = new File(dir); + ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); + addDir(dirObj, out); + out.close(); + } + + static void addDir(File dirObj, ZipOutputStream out) throws IOException { + File[] files = dirObj.listFiles(); + byte[] tmpBuf = new byte[1024]; + String pathToDir = dirName; + + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) { + addDir(files[i], out); + continue; + } + FileInputStream in = new FileInputStream(files[i].getPath()); + out.putNextEntry(new ZipEntry(files[i].getPath().substring(pathToDir.length()))); + int len; + while ((len = in.read(tmpBuf)) > 0) { + out.write(tmpBuf, 0, len); + } + out.closeEntry(); + in.close(); + } + } + + private static void deleteDir(File dir) { + if (dir.isDirectory()) { + String[] children = dir.list(); + for (int i=0; i