Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/tools/AddStructureBlob.java
6005 views
/*******************************************************************************1* Copyright (c) 2001, 2019 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9ddr.tools;2223import java.io.File;24import java.io.FileInputStream;25import java.io.IOException;26import java.util.HashMap;27import java.util.Properties;2829import com.ibm.j9ddr.tools.store.J9DDRStructureStore;30import com.ibm.j9ddr.tools.store.StructureKey;31import com.ibm.j9ddr.tools.store.StructureMismatchError;3233public class AddStructureBlob {34private static final String PROP_DDR_ORDER = "ddr.order"; //property to control the order in which ddr processes blobs/supersets3536HashMap<String, String> opts = new HashMap<String, String>();3738public AddStructureBlob() {39super();40initArgs();41}4243private void initArgs() {44opts.put("-d", null); // database directory45opts.put("-f", null); // structure file or directory of structure files to add46opts.put("-k", null); // key47opts.put("-s", null); // optional superset filename48opts.put("-c", ""); //optional value to provide a cache properties file49}5051public static void main(String[] args) {52AddStructureBlob app = new AddStructureBlob();53app.parseArgs(args);54app.run();55}5657//loads the order (if specified) in which blobs/supersets are to be processed58private String[] loadSpecNames() {59String path = opts.get("-c");60if((null == path) || (path.length() == 0)) {61//path for properties file not specified so return an empty array62return new String[0];63}64Properties props = new Properties();65File file = new File(path);66if(file.exists()) {67try {68FileInputStream in = new FileInputStream(file);69props.load(in);70String specs = props.getProperty(PROP_DDR_ORDER);71if(null == specs) {72String msg = String.format("The cache properties file [%s] specified by the -c option does not specify a %s key", file.getAbsolutePath(), PROP_DDR_ORDER);73throw new IllegalArgumentException(msg);74}75return specs.split(",");76} catch (Exception e) {77String msg = String.format("The cache properties file [%s] specified by the -c option could not be read", file.getAbsolutePath());78throw new IllegalArgumentException(msg, e);79}80} else {81String msg = String.format("The cache properties file [%s] specified by the -c option does not exist", file.getAbsolutePath());82throw new IllegalArgumentException(msg);83}84}8586private void run() {87String directoryName = opts.get("-d");88String supersetName = opts.get("-s");8990if( supersetName == null ) {91supersetName = J9DDRStructureStore.DEFAULT_SUPERSET_FILE_NAME;92}9394try {95J9DDRStructureStore store = new J9DDRStructureStore(directoryName, supersetName);96String path = opts.get("-c");97if((null == path) || (path.length() == 0)) {98//path for properties file not specified so default to adding from a single file or directory99addFromFile(store);100} else {101//a property file controls the order in which blobs are processed102addFromPropertiesFile(store);103}104} catch (IOException e) {105e.printStackTrace();106} catch (StructureMismatchError e) {107e.printStackTrace();108}109}110111//add a structure from a specified path112private void addFromFile(J9DDRStructureStore store) throws IOException, StructureMismatchError {113String directoryName = opts.get("-d");114String structureName = opts.get("-f");115String keyString = opts.get("-k");116String supersetName = opts.get("-s");117118if (keyString == null) {119File structurePath = new File(structureName);120File[] structureFiles;121if( structurePath.isDirectory() ) {122structureFiles = structurePath.listFiles();123} else {124structureFiles = new File[] {structurePath};125structurePath = structurePath.getParentFile();126}127for( File file: structureFiles ) {128if(file.exists()) {129store.add(null, file.getPath(), false);130store.updateSuperset();131System.out.println("Added " + file.getName() + " to superset " + supersetName + " in " + directoryName);132} else {133System.out.println("WARNING : The specified structure file " + file.getName() + " does not exist and was ignored");134}135}136} else {137StructureKey key = new StructureKey(keyString);138store.add(key, structureName, true);139store.updateSuperset();140System.out.println(String.format("Added %s to %s/%s as %s", structureName, directoryName, store.getSuperSetFileName(), keyString));141}142}143144//the order in which files are added is controlled by a properties file, it expects the -f parameter to point to a directory containing145//either blobs or supersets146private void addFromPropertiesFile(J9DDRStructureStore store) throws IOException, StructureMismatchError {147String directoryName = opts.get("-d");148String keyString = opts.get("-k");149File structurePath = new File(opts.get("-f"));150String[] specs = loadSpecNames(); //load the spec order (if specified on the command line)151File[] structureFiles = new File[specs.length];152for(int i = 0; i < specs.length; i++) {153structureFiles[i] = new File(structurePath, specs[i]);154}155for( File file: structureFiles ) {156if(file.exists()) {157StructureKey key = new StructureKey(file.getName() + "." + keyString);158store.add(key, file.getPath(), false);159store.updateSuperset();160System.out.println(String.format("Added %s to %s/%s as %s", file.getAbsolutePath(), directoryName, store.getSuperSetFileName(), key));161} else {162System.out.println("WARNING : The specified structure file " + file.getName() + " does not exist and was ignored");163}164}165}166167private void parseArgs(String[] args) {168169if((args.length == 0) || ((args.length % 2) != 0)) {170System.out.println("Not enough options specified as program arguments");171System.out.println(" args [" + args.length + "]");172printHelp();173System.exit(1);174}175176for(int i = 0; i < args.length; i+=2) {177if(opts.containsKey(args[i])) {178opts.put(args[i], args[i+1]);179} else {180System.out.println("Invalid option : " + args[i]);181printHelp();182System.exit(1);183}184}185186for(String key:opts.keySet()) {187String value = opts.get(key);188if(value == null) {189if (!(key.equals("-k") || key.equals("-s"))) {190System.err.println("The option " + key + " has not been set.\n");191printHelp();192System.exit(1);193}194}195}196}197198/**199* Print usage help to stdout200*/201private static void printHelp() {202System.out.println("Usage :\n\njava AddStructureBlob -d <directory name> -f <structure file name> [-s <superset file name>] [-k <structure key>]\n");203}204}205206207