Example usage for org.apache.commons.io IOUtils closeQuietly

List of usage examples for org.apache.commons.io IOUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils closeQuietly.

Prototype

public static void closeQuietly(OutputStream output) 

Source Link

Document

Unconditionally close an OutputStream.

Usage

From source file:com.revo.deployr.client.example.data.io.auth.discrete.exec.RepoFileInRepoFileOut.java

public static void main(String args[]) throws Exception {

    RClient rClient = null;//from w  ww .ja  va  2 s . c  om

    try {

        /*
         * Determine DeployR server endpoint.
         */
        String endpoint = System.getProperty("endpoint");
        log.info("[ CONFIGURATION  ] Using endpoint=" + endpoint);

        /*
         * Establish RClient connection to DeployR server.
         *
         * An RClient connection is the mandatory starting
         * point for any application using the client library.
         */
        rClient = RClientFactory.createClient(endpoint);

        log.info("[   CONNECTION   ] Established anonymous " + "connection [ RClient ].");

        /*
         * Build a basic authentication token.
         */
        RAuthentication rAuth = new RBasicAuthentication(System.getProperty("username"),
                System.getProperty("password"));

        /*
         * Establish an authenticated handle with the DeployR
         * server, rUser. Following this call the rClient 
         * connection is operating as an authenticated connection
         * and all calls on rClient inherit the access permissions
         * of the authenticated user, rUser.
         */
        RUser rUser = rClient.login(rAuth);
        log.info("[ AUTHENTICATION ] Upgraded to authenticated " + "connection [ RUser ].");

        /*
         * Create the AnonymousProjectExecutionOptions object
         * to specify data inputs and output to the script.
         *
         * This options object can be used to pass standard
         * execution model parameters on execution calls. All
         * fields are optional.
         *
         * See the Standard Execution Model chapter in the
         * Client Library Tutorial on the DeployR website for
         * further details.
         */
        AnonymousProjectExecutionOptions options = new AnonymousProjectExecutionOptions();

        /* 
         * Preload from the DeployR repository the following
         * data input file:
         * /testuser/example-data-io/hipStar.dat
         */
        ProjectPreloadOptions preloadDirectory = new ProjectPreloadOptions();
        preloadDirectory.filename = "hipStar.dat";
        preloadDirectory.directory = "example-data-io";
        preloadDirectory.author = "testuser";
        options.preloadDirectory = preloadDirectory;

        log.info("[   DATA INPUT   ] Repository data file input "
                + "set on execution, [ ProjectExecutionOptions.preloadDirectory ].");

        /* 
         * Request storage of entire workspace as a
         * binary rData file to the DeployR-repository
         * following the execution.
         *
         * Alternatively, you could use storageOptions.objects
         * to store individual objects from the workspace.
         */
        ProjectStorageOptions storageOptions = new ProjectStorageOptions();
        // Use random file name for this example.
        storageOptions.workspace = Long.toHexString(Double.doubleToLongBits(Math.random()));
        storageOptions.directory = "example-data-io";
        options.storageOptions = storageOptions;

        log.info("[  EXEC OPTION   ] Repository storage request "
                + "set on execution [ ProjectExecutionOptions.storageOptions ].");

        /*
         * Execute an analytics Web service as an authenticated
         * user based on a repository-managed R script:
         * /testuser/example-data-io/dataIO.R
         */
        RScriptExecution exec = rClient.executeScript("dataIO.R", "example-data-io", "testuser", null, options);

        log.info("[   EXECUTION    ] Discrete R script " + "execution completed [ RScriptExecution ].");

        /*
         * Retrieve repository-managed file(s) that were 
         * generated by the execution per ProjectStorageOptions.
         *
         * Outputs generated by an execution can be used in any
         * number of ways by client applications, including:
         *
         * 1. Use output data to perform further calculations.
         * 2. Display output data to an end-user.
         * 3. Write output data to a database.
         * 4. Pass output data along to another Web service.
         * 5. etc.
         */
        List<RRepositoryFile> repoFiles = exec.about().repositoryFiles;

        for (RRepositoryFile repoFile : repoFiles) {
            log.info("[  DATA OUTPUT   ] Retrieved repository " + "file output " + repoFile.about().filename
                    + " [ RRepositoryFile ].");
            InputStream fis = null;
            try {
                fis = repoFile.download();
            } catch (Exception ex) {
                log.warn("Repository-managed file download " + ex);
            } finally {
                IOUtils.closeQuietly(fis);
                try { // Clean-up after example.
                    repoFile.delete();
                } catch (Exception dex) {
                    log.warn("Repository-managed file delete " + dex);
                }
            }
        }

    } catch (Exception ex) {
        log.warn("Unexpected runtime exception=" + ex);
    } finally {
        try {
            if (rClient != null) {
                /*
                 * Release rClient connection before application exits.
                 */
                rClient.release();
            }
        } catch (Exception fex) {
        }
    }

}

From source file:com.p2p.peercds.cli.TorrentMain.java

/**
 * Torrent reader and creator.//w  w w  . ja v  a2 s .co m
 *
 * <p>
 * You can use the {@code main()} function of this class to read or create
 * torrent files. See usage for details.
 * </p>
 *
 */
public static void main(String[] args) {
    BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%-5p: %m%n")));

    CmdLineParser parser = new CmdLineParser();
    CmdLineParser.Option help = parser.addBooleanOption('h', "help");
    CmdLineParser.Option filename = parser.addStringOption('t', "torrent");
    CmdLineParser.Option create = parser.addBooleanOption('c', "create");
    CmdLineParser.Option announce = parser.addStringOption('a', "announce");

    try {
        parser.parse(args);
    } catch (CmdLineParser.OptionException oe) {
        System.err.println(oe.getMessage());
        usage(System.err);
        System.exit(1);
    }

    // Display help and exit if requested
    if (Boolean.TRUE.equals((Boolean) parser.getOptionValue(help))) {
        usage(System.out);
        System.exit(0);
    }

    String filenameValue = (String) parser.getOptionValue(filename);
    if (filenameValue == null) {
        usage(System.err, "Torrent file must be provided!");
        System.exit(1);
    }

    Boolean createFlag = (Boolean) parser.getOptionValue(create);

    //For repeated announce urls
    @SuppressWarnings("unchecked")
    Vector<String> announceURLs = (Vector<String>) parser.getOptionValues(announce);

    String[] otherArgs = parser.getRemainingArgs();

    if (Boolean.TRUE.equals(createFlag) && (otherArgs.length != 1 || announceURLs.isEmpty())) {
        usage(System.err,
                "Announce URL and a file or directory must be " + "provided to create a torrent file!");
        System.exit(1);
    }

    OutputStream fos = null;
    try {
        if (Boolean.TRUE.equals(createFlag)) {
            if (filenameValue != null) {
                fos = new FileOutputStream(filenameValue);
            } else {
                fos = System.out;
            }

            //Process the announce URLs into URIs
            List<URI> announceURIs = new ArrayList<URI>();
            for (String url : announceURLs) {
                announceURIs.add(new URI(url));
            }

            //Create the announce-list as a list of lists of URIs
            //Assume all the URI's are first tier trackers
            List<List<URI>> announceList = new ArrayList<List<URI>>();
            announceList.add(announceURIs);

            File source = new File(otherArgs[0]);
            if (!source.exists() || !source.canRead()) {
                throw new IllegalArgumentException(
                        "Cannot access source file or directory " + source.getName());
            }

            String creator = String.format("%s (ttorrent)", System.getProperty("user.name"));

            Torrent torrent = null;
            if (source.isDirectory()) {
                File[] files = source.listFiles(Constants.hiddenFilesFilter);
                Arrays.sort(files);
                torrent = Torrent.create(source, Arrays.asList(files), announceList, creator);
            } else {
                torrent = Torrent.create(source, announceList, creator);

            }

            torrent.save(fos);
        } else {
            Torrent.load(new File(filenameValue), true);
        }
    } catch (Exception e) {
        logger.error("{}", e.getMessage(), e);
        System.exit(2);
    } finally {
        if (fos != System.out) {
            IOUtils.closeQuietly(fos);
        }
    }
}

From source file:com.revo.deployr.client.example.data.io.auth.stateful.exec.EncodedDataInBinaryFileOut.java

public static void main(String args[]) throws Exception {

    RClient rClient = null;//w  w  w  .  j  av  a2 s . c o  m
    RProject rProject = null;

    try {

        /*
         * Determine DeployR server endpoint.
         */
        String endpoint = System.getProperty("endpoint");
        log.info("[ CONFIGURATION  ] Using endpoint=" + endpoint);

        /*
         * Establish RClient connection to DeployR server.
         *
         * An RClient connection is the mandatory starting
         * point for any application using the client library.
         */
        rClient = RClientFactory.createClient(endpoint);

        log.info("[   CONNECTION   ] Established anonymous " + "connection [ RClient ].");

        /*
         * Build a basic authentication token.
         */
        RAuthentication rAuth = new RBasicAuthentication(System.getProperty("username"),
                System.getProperty("password"));

        /*
         * Establish an authenticated handle with the DeployR
         * server, rUser. Following this call the rClient 
         * connection is operating as an authenticated connection
         * and all calls on rClient inherit the access permissions
         * of the authenticated user, rUser.
         */
        RUser rUser = rClient.login(rAuth);
        log.info("[ AUTHENTICATION ] Upgraded to authenticated " + "connection [ RUser ].");

        /*
         * Create a temporary project (R session).
         *
         * Optionally:
         * ProjectCreationOptions options =
         * new ProjectCreationOptions();
         *
         * Populate options as needed, then:
         *
         * rProject = rUser.createProject(options);
         */
        rProject = rUser.createProject();

        log.info("[  GO STATEFUL   ] Created stateful temporary " + "R session [ RProject ].");

        /*
         * Create a ProjectExecutionOptions instance
         * to specify data inputs and output to the
         * execution of the repository-managed R script.
         *
         * This options object can be used to pass standard
         * execution model parameters on execution calls. All
         * fields are optional.
         *
         * See the Standard Execution Model chapter in the
         * Client Library Tutorial on the DeployR website for
         * further details.
         */
        ProjectExecutionOptions options = new ProjectExecutionOptions();

        /* 
         * Simulate application generated data. This data
         * is first encoded using the RDataFactory before
         * being passed as an input on the execution.
         *
         * This encoded R input is automatically converted
         * into a workspace object before script execution.
         */
        RData generatedData = simulateGeneratedData();
        if (generatedData != null) {
            List<RData> rinputs = Arrays.asList(generatedData);
            options.rinputs = rinputs;
        }

        log.info("[   DATA INPUT   ] DeployR-encoded R input set on execution, "
                + "[ ProjectExecutionOptions.rinputs ].");

        /*
         * Execute a public analytics Web service as an authenticated
         * user based on a repository-managed R script:
         * /testuser/example-data-io/dataIO.R
         */
        RProjectExecution exec = rProject.executeScript("dataIO.R", "example-data-io", "testuser", null,
                options);

        log.info("[   EXECUTION    ] Stateful R script " + "execution completed [ RProjectExecution ].");

        /*
         * Retrieve the working directory file (artifact) called
         * hip.rData that was generated by the execution.
         *
         * Outputs generated by an execution can be used in any
         * number of ways by client applications, including:
         *
         * 1. Use output data to perform further calculations.
         * 2. Display output data to an end-user.
         * 3. Write output data to a database.
         * 4. Pass output data along to another Web service.
         * 5. etc.
         */
        List<RProjectFile> wdFiles = exec.about().artifacts;

        for (RProjectFile wdFile : wdFiles) {
            if (wdFile.about().filename.equals("hip.rData")) {
                log.info("[  DATA OUTPUT   ] Retrieved working directory " + "file output "
                        + wdFile.about().filename + " [ RProjectFile ].");
                InputStream fis = null;
                try {
                    fis = wdFile.download();
                } catch (Exception ex) {
                    log.warn("Working directory binary file download " + ex);
                } finally {
                    IOUtils.closeQuietly(fis);
                }
            }
        }

    } catch (Exception ex) {
        log.warn("Unexpected runtime exception=" + ex);
    } finally {
        try {
            if (rProject != null) {
                /*
                 * Close rProject before application exits.
                 */
                rProject.close();
            }
        } catch (Exception fex) {
        }
        try {
            if (rClient != null) {
                /*
                 * Release rClient connection before application exits.
                 */
                rClient.release();
            }
        } catch (Exception fex) {
        }
    }

}

From source file:com.lightboxtechnologies.spectrum.SequenceFileExport.java

public static void main(String[] args) throws Exception {
    final Configuration conf = new Configuration();

    final String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

    String imageID;//from  w  ww.j a v a 2  s  .  c  o m
    String outpath;
    String friendlyname;
    final Set<String> exts = new HashSet<String>();

    if ("-f".equals(otherArgs[0])) {
        if (otherArgs.length != 4) {
            die();
        }

        // load extensions from file
        final Path extpath = new Path(otherArgs[1]);

        InputStream in = null;
        try {
            in = extpath.getFileSystem(conf).open(extpath);

            Reader r = null;
            try {
                r = new InputStreamReader(in);

                BufferedReader br = null;
                try {
                    br = new BufferedReader(r);

                    String line;
                    while ((line = br.readLine()) != null) {
                        exts.add(line.trim().toLowerCase());
                    }

                    br.close();
                } finally {
                    IOUtils.closeQuietly(br);
                }

                r.close();
            } finally {
                IOUtils.closeQuietly(r);
            }

            in.close();
        } finally {
            IOUtils.closeQuietly(in);
        }

        imageID = otherArgs[2];
        friendlyname = otherArgs[3];
        outpath = otherArgs[4];
    } else {
        if (otherArgs.length < 3) {
            die();
        }

        // read extensions from trailing args
        imageID = otherArgs[0];
        friendlyname = otherArgs[1];
        outpath = otherArgs[2];

        // lowercase all file extensions
        for (int i = 2; i < otherArgs.length; ++i) {
            exts.add(otherArgs[i].toLowerCase());
        }
    }

    conf.setStrings("extensions", exts.toArray(new String[exts.size()]));

    final Job job = SKJobFactory.createJobFromConf(imageID, friendlyname, "SequenceFileExport", conf);
    job.setJarByClass(SequenceFileExport.class);
    job.setMapperClass(SequenceFileExportMapper.class);
    job.setNumReduceTasks(0);

    job.setOutputKeyClass(BytesWritable.class);
    job.setOutputValueClass(MapWritable.class);

    job.setInputFormatClass(FsEntryHBaseInputFormat.class);
    FsEntryHBaseInputFormat.setupJob(job, imageID);

    job.setOutputFormatClass(SequenceFileOutputFormat.class);
    SequenceFileOutputFormat.setOutputCompressionType(job, SequenceFile.CompressionType.BLOCK);

    FileOutputFormat.setOutputPath(job, new Path(outpath));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
}

From source file:com.revo.deployr.client.example.data.io.auth.stateful.exec.ExternalDataInDataFileOut.java

public static void main(String args[]) throws Exception {

    RClient rClient = null;//  w  w  w  .j a v a  2s .co  m
    RProject rProject = null;

    try {

        /*
         * Determine DeployR server endpoint.
         */
        String endpoint = System.getProperty("endpoint");
        log.info("[ CONFIGURATION  ] Using endpoint=" + endpoint);

        /*
         * Establish RClient connection to DeployR server.
         *
         * An RClient connection is the mandatory starting
         * point for any application using the client library.
         */
        rClient = RClientFactory.createClient(endpoint);

        log.info("[   CONNECTION   ] Established anonymous " + "connection [ RClient ].");

        /*
         * Build a basic authentication token.
         */
        RAuthentication rAuth = new RBasicAuthentication(System.getProperty("username"),
                System.getProperty("password"));

        /*
         * Establish an authenticated handle with the DeployR
         * server, rUser. Following this call the rClient 
         * connection is operating as an authenticated connection
         * and all calls on rClient inherit the access permissions
         * of the authenticated user, rUser.
         */
        RUser rUser = rClient.login(rAuth);
        log.info("[ AUTHENTICATION ] Upgraded to authenticated " + "connection [ RUser ].");

        /*
         * Create a temporary project (R session).
         *
         * Optionally:
         * ProjectCreationOptions options =
         * new ProjectCreationOptions();
         *
         * Populate options as needed, then:
         *
         * rProject = rUser.createProject(options);
         */
        rProject = rUser.createProject();

        log.info("[  GO STATEFUL   ] Created stateful temporary " + "R session [ RProject ].");

        /*
         * Create a ProjectExecutionOptions instance
         * to specify data inputs and output to the
         * execution of the repository-managed R script.
         *
         * This options object can be used to pass standard
         * execution model parameters on execution calls. All
         * fields are optional.
         *
         * See the Standard Execution Model chapter in the
         * Client Library Tutorial on the DeployR website for
         * further details.
         */
        ProjectExecutionOptions options = new ProjectExecutionOptions();

        /* 
         * Load an R object literal "hipStarUrl" into the
         * workspace prior to script execution.
         *
         * The R script checks for the existence of "hipStarUrl"
         * in the workspace and if present uses the URL path
         * to load the Hipparcos star dataset from the DAT file
         * at that location.
         */
        RData hipStarUrl = RDataFactory.createString("hipStarUrl", HIP_DAT_URL);
        List<RData> rinputs = Arrays.asList(hipStarUrl);
        options.rinputs = rinputs;

        log.info("[   DATA INPUT   ] External data source input "
                + "set on execution, [ ProjectExecutionOptions.rinputs ].");

        /*
         * Execute a public analytics Web service as an authenticated
         * user based on a repository-managed R script:
         * /testuser/example-data-io/dataIO.R
         */
        RProjectExecution exec = rProject.executeScript("dataIO.R", "example-data-io", "testuser", null,
                options);

        log.info("[   EXECUTION    ] Stateful R script " + "execution completed [ RProjectExecution ].");

        /*
         * Retrieve the working directory file (artifact) called
         * hip.csv that was generated by the execution.
         *
         * Outputs generated by an execution can be used in any
         * number of ways by client applications, including:
         *
         * 1. Use output data to perform further calculations.
         * 2. Display output data to an end-user.
         * 3. Write output data to a database.
         * 4. Pass output data along to another Web service.
         * 5. etc.
         */
        List<RProjectFile> wdFiles = exec.about().artifacts;

        for (RProjectFile wdFile : wdFiles) {
            if (wdFile.about().filename.equals("hip.csv")) {
                log.info("[  DATA OUTPUT   ] Retrieved working directory " + "file output "
                        + wdFile.about().filename + " [ RProjectFile ].");
                InputStream fis = null;
                try {
                    fis = wdFile.download();
                } catch (Exception ex) {
                    log.warn("Working directory data file download " + ex);
                } finally {
                    IOUtils.closeQuietly(fis);
                }
            }
        }

    } catch (Exception ex) {
        log.warn("Unexpected runtime exception=" + ex);
    } finally {
        try {
            if (rProject != null) {
                /*
                 * Close rProject before application exits.
                 */
                rProject.close();
            }
        } catch (Exception fex) {
        }
        try {
            if (rClient != null) {
                /*
                 * Release rClient connection before application exits.
                 */
                rClient.release();
            }
        } catch (Exception fex) {
        }
    }

}

From source file:net.padaf.xmpbox.parser.XMLPropertiesDescriptionManager.java

/**
 * Sample of using to write/read information
 * /* w  w w .ja v a  2  s. com*/
 * @param args
 *            Not used
 * @throws BuildPDFAExtensionSchemaDescriptionException
 *             When errors during building/reading xml file
 */
public static void main(String[] args) throws BuildPDFAExtensionSchemaDescriptionException {
    XMLPropertiesDescriptionManager ptMaker = new XMLPropertiesDescriptionManager();

    // add Descriptions
    for (int i = 0; i < 3; i++) {
        ptMaker.addPropertyDescription("name" + i, "description" + i);

    }

    // Display XML conversion
    System.out.println("Display XML Result:");
    ptMaker.toXML(System.out);

    // Sample to show how to build object from XML file
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ptMaker.toXML(bos);
    IOUtils.closeQuietly(bos);

    // emulate a new reading
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    ptMaker = new XMLPropertiesDescriptionManager();
    ptMaker.loadListFromXML(is);
    List<PropertyDescription> result = ptMaker.getPropertiesDescriptionList();
    System.out.println();
    System.out.println();
    System.out.println("Result of XML Loading :");
    for (PropertyDescription propertyDescription : result) {
        System.out.println(propertyDescription.getPropertyName() + " :" + propertyDescription.getDescription());
    }
}

From source file:com.hs.mail.deliver.Deliver.java

public static void main(String[] args) {
    CommandLine cli = null;//from w w w . java 2 s  . c  o m
    try {
        cli = new PosixParser().parse(OPTS, args);
    } catch (ParseException e) {
        usage();
        System.exit(EX_USAGE);
    }

    // Configuration file path
    String config = cli.getOptionValue("c", DEFAULT_CONFIG_LOCATION);
    // Message file path
    File file = new File(cli.getOptionValue("p"));
    // Envelope sender address
    String from = cli.getOptionValue("f");
    // Destination mailboxes
    String[] rcpts = cli.getOptionValues("r");

    if (!file.exists()) {
        // Message file must exist
        logger.error("File not exist: " + file.getAbsolutePath());
        System.exit(EX_TEMPFAIL);
    }

    if (from == null || rcpts == null) {
        // If sender or recipients address was not specified, get the
        // addresses from the message header.
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            MessageHeader header = new MessageHeader(is);
            if (from == null && header.getFrom() != null) {
                from = header.getFrom().getAddress();
            }
            if (rcpts == null) {
                rcpts = getRecipients(header);
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
            System.exit(EX_TEMPFAIL);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    if (from == null || ArrayUtils.isEmpty(rcpts)) {
        usage();
        System.exit(EX_USAGE);
    }

    Deliver deliver = new Deliver();

    deliver.init(config);
    // Spool the incoming message
    deliver.deliver(from, rcpts, file);

    System.exit(EX_OK);
}

From source file:com.xiaoxiaomo.flink.batch.distcp.DistCp.java

public static void main(String[] args) throws Exception {

    // set up the execution environment
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

    ParameterTool params = ParameterTool.fromArgs(args);
    if (!params.has("input") || !params.has("output")) {
        System.err.println("Usage: --input <path> --output <path> [--parallelism <n>]");
        return;/*from   w w w  .j a v  a  2s  . c o  m*/
    }

    final Path sourcePath = new Path(params.get("input"));
    final Path targetPath = new Path(params.get("output"));
    if (!isLocal(env) && !(isOnDistributedFS(sourcePath) && isOnDistributedFS(targetPath))) {
        System.out.println("In a distributed mode only HDFS input/output paths are supported");
        return;
    }

    final int parallelism = params.getInt("parallelism", 10);
    if (parallelism <= 0) {
        System.err.println("Parallelism should be greater than 0");
        return;
    }

    // make parameters available in the web interface
    env.getConfig().setGlobalJobParameters(params);

    env.setParallelism(parallelism);

    long startTime = System.currentTimeMillis();
    LOGGER.info("Initializing copy tasks");
    List<FileCopyTask> tasks = getCopyTasks(sourcePath);
    LOGGER.info("Copy task initialization took " + (System.currentTimeMillis() - startTime) + "ms");

    DataSet<FileCopyTask> inputTasks = new DataSource<FileCopyTask>(env, new FileCopyTaskInputFormat(tasks),
            new GenericTypeInfo<FileCopyTask>(FileCopyTask.class), "fileCopyTasks");

    FlatMapOperator<FileCopyTask, Object> res = inputTasks
            .flatMap(new RichFlatMapFunction<FileCopyTask, Object>() {

                private static final long serialVersionUID = 1109254230243989929L;
                private LongCounter fileCounter;
                private LongCounter bytesCounter;

                @Override
                public void open(Configuration parameters) throws Exception {
                    bytesCounter = getRuntimeContext().getLongCounter(BYTES_COPIED_CNT_NAME);
                    fileCounter = getRuntimeContext().getLongCounter(FILES_COPIED_CNT_NAME);
                }

                @Override
                public void flatMap(FileCopyTask task, Collector<Object> out) throws Exception {
                    LOGGER.info("Processing task: " + task);
                    Path outPath = new Path(targetPath, task.getRelativePath());

                    FileSystem targetFs = targetPath.getFileSystem();
                    // creating parent folders in case of a local FS
                    if (!targetFs.isDistributedFS()) {
                        //dealing with cases like file:///tmp or just /tmp
                        File outFile = outPath.toUri().isAbsolute() ? new File(outPath.toUri())
                                : new File(outPath.toString());
                        File parentFile = outFile.getParentFile();
                        if (!parentFile.mkdirs() && !parentFile.exists()) {
                            throw new RuntimeException(
                                    "Cannot create local file system directories: " + parentFile);
                        }
                    }
                    FSDataOutputStream outputStream = null;
                    FSDataInputStream inputStream = null;
                    try {
                        outputStream = targetFs.create(outPath, true);
                        inputStream = task.getPath().getFileSystem().open(task.getPath());
                        int bytes = IOUtils.copy(inputStream, outputStream);
                        bytesCounter.add(bytes);
                    } finally {
                        IOUtils.closeQuietly(inputStream);
                        IOUtils.closeQuietly(outputStream);
                    }
                    fileCounter.add(1L);
                }
            });

    // no data sinks are needed, therefore just printing an empty result
    res.print();

    Map<String, Object> accumulators = env.getLastJobExecutionResult().getAllAccumulatorResults();
    LOGGER.info("== COUNTERS ==");
    for (Map.Entry<String, Object> e : accumulators.entrySet()) {
        LOGGER.info(e.getKey() + ": " + e.getValue());
    }
}

From source file:com.turn.ttorrent.cli.TorrentMain.java

/**
 * Torrent reader and creator.//w  w w.j  a v  a 2 s.  c  om
 *
 * <p>
 * You can use the {@code main()} function of this class to read or create
 * torrent files. See usage for details.
 * </p>
 *
 */
public static void main(String[] args) {
    BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%-5p: %m%n")));

    CmdLineParser parser = new CmdLineParser();
    CmdLineParser.Option help = parser.addBooleanOption('h', "help");
    CmdLineParser.Option filename = parser.addStringOption('t', "torrent");
    CmdLineParser.Option create = parser.addBooleanOption('c', "create");
    CmdLineParser.Option pieceLength = parser.addIntegerOption('l', "length");
    CmdLineParser.Option announce = parser.addStringOption('a', "announce");

    try {
        parser.parse(args);
    } catch (CmdLineParser.OptionException oe) {
        System.err.println(oe.getMessage());
        usage(System.err);
        System.exit(1);
    }

    // Display help and exit if requested
    if (Boolean.TRUE.equals((Boolean) parser.getOptionValue(help))) {
        usage(System.out);
        System.exit(0);
    }

    String filenameValue = (String) parser.getOptionValue(filename);
    if (filenameValue == null) {
        usage(System.err, "Torrent file must be provided!");
        System.exit(1);
    }

    Integer pieceLengthVal = (Integer) parser.getOptionValue(pieceLength);
    if (pieceLengthVal == null) {
        pieceLengthVal = Torrent.DEFAULT_PIECE_LENGTH;
    } else {
        pieceLengthVal = pieceLengthVal * 1024;
    }
    logger.info("Using piece length of {} bytes.", pieceLengthVal);

    Boolean createFlag = (Boolean) parser.getOptionValue(create);

    //For repeated announce urls
    @SuppressWarnings("unchecked")
    Vector<String> announceURLs = (Vector<String>) parser.getOptionValues(announce);

    String[] otherArgs = parser.getRemainingArgs();

    if (Boolean.TRUE.equals(createFlag) && (otherArgs.length != 1 || announceURLs.isEmpty())) {
        usage(System.err,
                "Announce URL and a file or directory must be " + "provided to create a torrent file!");
        System.exit(1);
    }

    OutputStream fos = null;
    try {
        if (Boolean.TRUE.equals(createFlag)) {
            if (filenameValue != null) {
                fos = new FileOutputStream(filenameValue);
            } else {
                fos = System.out;
            }

            //Process the announce URLs into URIs
            List<URI> announceURIs = new ArrayList<URI>();
            for (String url : announceURLs) {
                announceURIs.add(new URI(url));
            }

            //Create the announce-list as a list of lists of URIs
            //Assume all the URI's are first tier trackers
            List<List<URI>> announceList = new ArrayList<List<URI>>();
            announceList.add(announceURIs);

            File source = new File(otherArgs[0]);
            if (!source.exists() || !source.canRead()) {
                throw new IllegalArgumentException(
                        "Cannot access source file or directory " + source.getName());
            }

            String creator = String.format("%s (ttorrent)", System.getProperty("user.name"));

            Torrent torrent = null;
            if (source.isDirectory()) {
                List<File> files = new ArrayList<File>(
                        FileUtils.listFiles(source, TrueFileFilter.TRUE, TrueFileFilter.TRUE));
                Collections.sort(files);
                torrent = Torrent.create(source, files, pieceLengthVal, announceList, creator);
            } else {
                torrent = Torrent.create(source, pieceLengthVal, announceList, creator);
            }

            torrent.save(fos);
        } else {
            Torrent.load(new File(filenameValue), true);
        }
    } catch (Exception e) {
        logger.error("{}", e.getMessage(), e);
        System.exit(2);
    } finally {
        if (fos != System.out) {
            IOUtils.closeQuietly(fos);
        }
    }
}

From source file:com.revo.deployr.client.example.data.io.auth.stateful.exec.RepoFileInRepoFileOut.java

public static void main(String args[]) throws Exception {

    RClient rClient = null;/*from   ww w  .j  av  a2 s  . c om*/
    RProject rProject = null;

    try {

        /*
         * Determine DeployR server endpoint.
         */
        String endpoint = System.getProperty("endpoint");
        log.info("[ CONFIGURATION  ] Using endpoint=" + endpoint);

        /*
         * Establish RClient connection to DeployR server.
         *
         * An RClient connection is the mandatory starting
         * point for any application using the client library.
         */
        rClient = RClientFactory.createClient(endpoint);

        log.info("[   CONNECTION   ] Established anonymous " + "connection [ RClient ].");

        /*
         * Build a basic authentication token.
         */
        RAuthentication rAuth = new RBasicAuthentication(System.getProperty("username"),
                System.getProperty("password"));

        /*
         * Establish an authenticated handle with the DeployR
         * server, rUser. Following this call the rClient 
         * connection is operating as an authenticated connection
         * and all calls on rClient inherit the access permissions
         * of the authenticated user, rUser.
         */
        RUser rUser = rClient.login(rAuth);
        log.info("[ AUTHENTICATION ] Upgraded to authenticated " + "connection [ RUser ].");

        /*
         * Create a temporary project (R session).
         *
         * Optionally:
         * ProjectCreationOptions options =
         * new ProjectCreationOptions();
         *
         * Populate options as needed, then:
         *
         * rProject = rUser.createProject(options);
         */
        rProject = rUser.createProject();

        log.info("[  GO STATEFUL   ] Created stateful temporary " + "R session [ RProject ].");

        /*
         * Create a ProjectExecutionOptions instance
         * to specify data inputs and output to the
         * execution of the repository-managed R script.
         *
         * This options object can be used to pass standard
         * execution model parameters on execution calls. All
         * fields are optional.
         *
         * See the Standard Execution Model chapter in the
         * Client Library Tutorial on the DeployR website for
         * further details.
         */
        ProjectExecutionOptions options = new ProjectExecutionOptions();

        /* 
         * Preload from the DeployR repository the following
         * binary R object input file:
         * /testuser/example-data-io/hipStar.rData
         */
        ProjectPreloadOptions preloadWorkspace = new ProjectPreloadOptions();
        preloadWorkspace.filename = "hipStar.rData";
        preloadWorkspace.directory = "example-data-io";
        preloadWorkspace.author = "testuser";
        options.preloadWorkspace = preloadWorkspace;

        log.info("[   DATA INPUT   ] Repository binary file input "
                + "set on execution, [ ProjectExecutionOptions.preloadWorkspace ].");

        /* 
         * Request storage of entire workspace as a
         * binary rData file to the DeployR-repository
         * following the execution.
         *
         * Alternatively, you could use storageOptions.objects
         * to store individual objects from the workspace.
         */
        ProjectStorageOptions storageOptions = new ProjectStorageOptions();
        // Use random file name for this example.
        storageOptions.workspace = Long.toHexString(Double.doubleToLongBits(Math.random()));
        storageOptions.directory = "example-data-io";
        options.storageOptions = storageOptions;

        log.info("[  EXEC OPTION   ] Repository storage request "
                + "set on execution [ ProjectExecutionOptions.storageOptions ].");

        /*
         * Execute a public analytics Web service as an authenticated
         * user based on a repository-managed R script:
         * /testuser/example-data-io/dataIO.R
         */
        RProjectExecution exec = rProject.executeScript("dataIO.R", "example-data-io", "testuser", null,
                options);

        log.info("[   EXECUTION    ] Stateful R script " + "execution completed [ RProjectExecution ].");

        /*
         * Retrieve repository-managed file(s) that were 
         * generated by the execution per ProjectStorageOptions.
         *
         * Outputs generated by an execution can be used in any
         * number of ways by client applications, including:
         *
         * 1. Use output data to perform further calculations.
         * 2. Display output data to an end-user.
         * 3. Write output data to a database.
         * 4. Pass output data along to another Web service.
         * 5. etc.
         */
        List<RRepositoryFile> repoFiles = exec.about().repositoryFiles;

        for (RRepositoryFile repoFile : repoFiles) {
            log.info("[  DATA OUTPUT   ] Retrieved repository " + "file output " + repoFile.about().filename
                    + " [ RRepositoryFile ].");
            InputStream fis = null;
            try {
                fis = repoFile.download();
            } catch (Exception ex) {
                log.warn("Repository-managed file download " + ex);
            } finally {
                IOUtils.closeQuietly(fis);
                try { // Clean-up after example.
                    repoFile.delete();
                } catch (Exception dex) {
                    log.warn("Repository-managed file delete " + dex);
                }
            }
        }

    } catch (Exception ex) {
        log.warn("Unexpected runtime exception=" + ex);
    } finally {
        try {
            if (rProject != null) {
                /*
                 * Close rProject before application exits.
                 */
                rProject.close();
            }
        } catch (Exception fex) {
        }
        try {
            if (rClient != null) {
                /*
                 * Release rClient connection before application exits.
                 */
                rClient.release();
            }
        } catch (Exception fex) {
        }
    }

}