Example usage for org.apache.hadoop.registry.client.binding RegistryPathUtils lastPathEntry

List of usage examples for org.apache.hadoop.registry.client.binding RegistryPathUtils lastPathEntry

Introduction

In this page you can find the example usage for org.apache.hadoop.registry.client.binding RegistryPathUtils lastPathEntry.

Prototype

public static String lastPathEntry(String path) 

Source Link

Document

Get the last entry in a path; for an empty path returns "".

Usage

From source file:org.apache.slider.client.SliderClient.java

License:Apache License

@Override
public int actionResolve(ActionResolveArgs args) throws YarnException, IOException {
    // as this is an API entry point, validate
    // the arguments
    args.validate();//  www  .j  a  v  a  2s  . c o m
    RegistryOperations operations = getRegistryOperations();
    String path = SliderRegistryUtils.resolvePath(args.path);
    ServiceRecordMarshal serviceRecordMarshal = new ServiceRecordMarshal();
    try {
        if (args.list) {
            File destDir = args.destdir;
            if (destDir != null) {
                destDir.mkdirs();
            }

            Map<String, ServiceRecord> recordMap;
            Map<String, RegistryPathStatus> znodes;
            try {
                znodes = statChildren(registryOperations, path);
                recordMap = extractServiceRecords(registryOperations, path, znodes.values());
            } catch (PathNotFoundException e) {
                // treat the root directory as if if is always there

                if ("/".equals(path)) {
                    znodes = new HashMap<String, RegistryPathStatus>(0);
                    recordMap = new HashMap<String, ServiceRecord>(0);
                } else {
                    throw e;
                }
            }
            // subtract all records from the znodes map to get pure directories
            log.info("Entries: {}", znodes.size());

            for (String name : znodes.keySet()) {
                println("  " + name);
            }
            println("");

            log.info("Service records: {}", recordMap.size());
            for (Entry<String, ServiceRecord> recordEntry : recordMap.entrySet()) {
                String name = recordEntry.getKey();
                ServiceRecord instance = recordEntry.getValue();
                String json = serviceRecordMarshal.toJson(instance);
                if (destDir == null) {
                    println(name);
                    println(json);
                } else {
                    String filename = RegistryPathUtils.lastPathEntry(name) + ".json";
                    File jsonFile = new File(destDir, filename);
                    SliderUtils.write(jsonFile, serviceRecordMarshal.toBytes(instance), true);
                }
            }
        } else {
            // resolve single entry
            ServiceRecord instance = resolve(path);
            File outFile = args.out;
            if (args.destdir != null) {
                outFile = new File(args.destdir, RegistryPathUtils.lastPathEntry(path));
            }
            if (outFile != null) {
                SliderUtils.write(outFile, serviceRecordMarshal.toBytes(instance), true);
            } else {
                println(serviceRecordMarshal.toJson(instance));
            }
        }
        //      TODO JDK7
    } catch (PathNotFoundException e) {
        // no record at this path
        throw new NotFoundException(e, path);
    } catch (NoRecordException e) {
        throw new NotFoundException(e, path);
    }
    return EXIT_SUCCESS;
}