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:de.tudarmstadt.ukp.csniper.resbuild.EvaluationItemFixer.java

public static void main(String[] args) {
    connect(HOST, DATABASE, USER, PASSWORD);

    Map<Integer, String> items = new HashMap<Integer, String>();
    Map<Integer, String> failed = new HashMap<Integer, String>();

    // fetch coveredTexts of dubious items and clean it
    PreparedStatement select = null;
    try {/*from  www .  j a v a 2 s  .  c o  m*/
        StringBuilder selectQuery = new StringBuilder();
        selectQuery.append("SELECT * FROM EvaluationItem ");
        selectQuery.append("WHERE LOCATE(coveredText, '  ') > 0 ");
        selectQuery.append("OR LOCATE('" + LRB + "', coveredText) > 0 ");
        selectQuery.append("OR LOCATE('" + RRB + "', coveredText) > 0 ");
        selectQuery.append("OR LEFT(coveredText, 1) = ' ' ");
        selectQuery.append("OR RIGHT(coveredText, 1) = ' ' ");

        select = connection.prepareStatement(selectQuery.toString());
        log.info("Running query [" + selectQuery.toString() + "].");
        ResultSet rs = select.executeQuery();

        while (rs.next()) {
            int id = rs.getInt("id");
            String coveredText = rs.getString("coveredText");

            try {
                // special handling of double whitespace: in this case, re-fetch the text
                if (coveredText.contains("  ")) {
                    coveredText = retrieveCoveredText(rs.getString("collectionId"), rs.getString("documentId"),
                            rs.getInt("beginOffset"), rs.getInt("endOffset"));
                }

                // replace bracket placeholders and trim the text
                coveredText = StringUtils.replace(coveredText, LRB, "(");
                coveredText = StringUtils.replace(coveredText, RRB, ")");
                coveredText = coveredText.trim();

                items.put(id, coveredText);
            } catch (IllegalArgumentException e) {
                failed.put(id, e.getMessage());
            }
        }
    } catch (SQLException e) {
        log.error("Exception while selecting: " + e.getMessage());
    } finally {
        closeQuietly(select);
    }

    // write logs
    BufferedWriter bwf = null;
    BufferedWriter bws = null;
    try {
        bwf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(LOG_FAILED)), "UTF-8"));
        for (Entry<Integer, String> e : failed.entrySet()) {
            bwf.write(e.getKey() + " - " + e.getValue() + "\n");
        }

        bws = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(new File(LOG_SUCCESSFUL)), "UTF-8"));
        for (Entry<Integer, String> e : items.entrySet()) {
            bws.write(e.getKey() + " - " + e.getValue() + "\n");
        }
    } catch (IOException e) {
        log.error("Got an IOException while writing the log files.");
    } finally {
        IOUtils.closeQuietly(bwf);
        IOUtils.closeQuietly(bws);
    }

    log.info("Texts for [" + items.size() + "] items need to be cleaned up.");

    // update the dubious items with the cleaned coveredText
    PreparedStatement update = null;
    try {
        String updateQuery = "UPDATE EvaluationItem SET coveredText = ? WHERE id = ?";

        update = connection.prepareStatement(updateQuery);
        int i = 0;
        for (Entry<Integer, String> e : items.entrySet()) {
            int id = e.getKey();
            String coveredText = e.getValue();

            // update item in database
            update.setString(1, coveredText);
            update.setInt(2, id);
            update.executeUpdate();
            log.debug("Updating " + id + " with [" + coveredText + "]");

            // show percentage of updated items
            i++;
            int part = (int) Math.ceil((double) items.size() / 100);
            if (i % part == 0) {
                log.info(i / part + "% finished (" + i + "/" + items.size() + ").");
            }
        }
    } catch (SQLException e) {
        log.error("Exception while updating: " + e.getMessage());
    } finally {
        closeQuietly(update);
    }

    closeQuietly(connection);
}

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

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

    RClient rClient = null;//  ww w. jav 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 ProjectCreationOptions instance
         * to specify data inputs that "pre-heat" the R session
         * workspace or working directory for your project.
         */
        ProjectCreationOptions creationOpts = new ProjectCreationOptions();

        /* 
         * 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";
        creationOpts.preloadWorkspace = preloadWorkspace;

        log.info("[ PRELOAD INPUT  ] Repository binary file input "
                + "set on project creation, [ ProjectCreationOptions.preloadWorkspace ].");

        /*
         * Create a temporary project (R session) passing a 
         * ProjectCreationOptions to "pre-heat" data into the
         * workspace and/or working directory.
         */
        rProject = rUser.createProject(creationOpts);

        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 execOpts = new ProjectExecutionOptions();

        /* 
         * 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";
        execOpts.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,
                execOpts);

        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) {
        }
    }

}

From source file:com.l2jfree.loginserver.tools.L2GameServerRegistrar.java

/**
 * Launches the interactive game server registration.
 * /*ww w. j  a  va 2s .  c  o m*/
 * @param args ignored
 */
public static void main(String[] args) {
    // LOW rework this crap
    Util.printSection("Game Server Registration");
    _log.info("Please choose:");
    _log.info("list - list registered game servers");
    _log.info("reg - register a game server");
    _log.info("rem - remove a registered game server");
    _log.info("hexid - generate a legacy hexid file");
    _log.info("quit - exit this application");

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    L2GameServerRegistrar reg = new L2GameServerRegistrar();

    String line;
    try {
        RegistrationState next = RegistrationState.INITIAL_CHOICE;
        while ((line = br.readLine()) != null) {
            line = line.trim().toLowerCase();
            switch (reg.getState()) {
            case GAMESERVER_ID:
                try {
                    int id = Integer.parseInt(line);
                    if (id < 1 || id > 127)
                        throw new IllegalArgumentException("ID must be in [1;127].");
                    reg.setId(id);
                    reg.setState(next);
                } catch (RuntimeException e) {
                    _log.info("You must input a number between 1 and 127");
                }

                if (reg.getState() == RegistrationState.ALLOW_BANS) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con
                                .prepareStatement("SELECT allowBans FROM gameserver WHERE id = ?");
                        ps.setInt(1, reg.getId());
                        ResultSet rs = ps.executeQuery();
                        if (rs.next()) {
                            _log.info("A game server is already registered on ID " + reg.getId());
                            reg.setState(RegistrationState.INITIAL_CHOICE);
                        } else
                            _log.info("Allow account bans from this game server? [y/n]:");
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not remove a game server!", e);
                    } finally {
                        L2Database.close(con);
                    }
                } else if (reg.getState() == RegistrationState.REMOVE) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con.prepareStatement("DELETE FROM gameserver WHERE id = ?");
                        ps.setInt(1, reg.getId());
                        int cnt = ps.executeUpdate();
                        if (cnt == 0)
                            _log.info("No game server registered on ID " + reg.getId());
                        else
                            _log.info("Game server removed.");
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not remove a game server!", e);
                    } finally {
                        L2Database.close(con);
                    }
                    reg.setState(RegistrationState.INITIAL_CHOICE);
                } else if (reg.getState() == RegistrationState.GENERATE) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con
                                .prepareStatement("SELECT authData FROM gameserver WHERE id = ?");
                        ps.setInt(1, reg.getId());
                        ResultSet rs = ps.executeQuery();

                        if (rs.next()) {
                            reg.setAuth(rs.getString("authData"));
                            byte[] b = HexUtil.hexStringToBytes(reg.getAuth());

                            Properties pro = new Properties();
                            pro.setProperty("ServerID", String.valueOf(reg.getId()));
                            pro.setProperty("HexID", HexUtil.hexToString(b));

                            BufferedOutputStream os = new BufferedOutputStream(
                                    new FileOutputStream("hexid.txt"));
                            pro.store(os, "the hexID to auth into login");
                            IOUtils.closeQuietly(os);
                            _log.info("hexid.txt has been generated.");
                        } else
                            _log.info("No game server registered on ID " + reg.getId());

                        rs.close();
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not generate hexid.txt!", e);
                    } finally {
                        L2Database.close(con);
                    }
                    reg.setState(RegistrationState.INITIAL_CHOICE);
                }
                break;
            case ALLOW_BANS:
                try {
                    if (line.length() != 1)
                        throw new IllegalArgumentException("One char required.");
                    else if (line.charAt(0) == 'y')
                        reg.setTrusted(true);
                    else if (line.charAt(0) == 'n')
                        reg.setTrusted(false);
                    else
                        throw new IllegalArgumentException("Invalid choice.");

                    byte[] auth = Rnd.nextBytes(new byte[BYTES]);
                    reg.setAuth(HexUtil.bytesToHexString(auth));

                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con.prepareStatement(
                                "INSERT INTO gameserver (id, authData, allowBans) VALUES (?, ?, ?)");
                        ps.setInt(1, reg.getId());
                        ps.setString(2, reg.getAuth());
                        ps.setBoolean(3, reg.isTrusted());
                        ps.executeUpdate();
                        ps.close();

                        _log.info("Registered game server on ID " + reg.getId());
                        _log.info("The authorization string is:");
                        _log.info(reg.getAuth());
                        _log.info("Use it when registering this login server.");
                        _log.info("If you need a legacy hexid file, use the 'hexid' command.");
                    } catch (SQLException e) {
                        _log.error("Could not register gameserver!", e);
                    } finally {
                        L2Database.close(con);
                    }

                    reg.setState(RegistrationState.INITIAL_CHOICE);
                } catch (IllegalArgumentException e) {
                    _log.info("[y/n]?");
                }
                break;
            default:
                if (line.equals("list")) {
                    Connection con = null;
                    try {
                        con = L2Database.getConnection();
                        PreparedStatement ps = con.prepareStatement("SELECT id, allowBans FROM gameserver");
                        ResultSet rs = ps.executeQuery();
                        while (rs.next())
                            _log.info("ID: " + rs.getInt("id") + ", trusted: " + rs.getBoolean("allowBans"));
                        rs.close();
                        ps.close();
                    } catch (SQLException e) {
                        _log.error("Could not register gameserver!", e);
                    } finally {
                        L2Database.close(con);
                    }
                    reg.setState(RegistrationState.INITIAL_CHOICE);
                } else if (line.equals("reg")) {
                    _log.info("Enter the desired ID:");
                    reg.setState(RegistrationState.GAMESERVER_ID);
                    next = RegistrationState.ALLOW_BANS;
                } else if (line.equals("rem")) {
                    _log.info("Enter game server ID:");
                    reg.setState(RegistrationState.GAMESERVER_ID);
                    next = RegistrationState.REMOVE;
                } else if (line.equals("hexid")) {
                    _log.info("Enter game server ID:");
                    reg.setState(RegistrationState.GAMESERVER_ID);
                    next = RegistrationState.GENERATE;
                } else if (line.equals("quit"))
                    Shutdown.exit(TerminationStatus.MANUAL_SHUTDOWN);
                else
                    _log.info("Incorrect command.");
                break;
            }
        }
    } catch (IOException e) {
        _log.fatal("Could not process input!", e);
    } finally {
        IOUtils.closeQuietly(br);
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.dam.util.TempClinicalDataLoader.java

public static void main(String[] args) {
    // first get the db connection properties
    String url = urlSet.get(args[1]);
    String user = args[2];// w ww. j  a  va 2  s.c  o  m
    String word = args[3];

    // make sure we have the Oracle driver somewhere
    try {
        Class.forName("oracle.jdbc.OracleDriver");
        Class.forName("org.postgresql.Driver");
    } catch (Exception x) {
        System.out.println("Unable to load the driver class!");
        System.exit(0);
    }
    // connect to the database
    try {
        dbConnection = DriverManager.getConnection(url, user, word);
        ClinicalBean.setDBConnection(dbConnection);
    } catch (SQLException x) {
        x.printStackTrace();
        System.exit(1);
    }

    final String xmlList = args[0];
    BufferedReader br = null;
    try {
        final Map<String, String> clinicalFiles = new HashMap<String, String>();
        final Map<String, String> biospecimenFiles = new HashMap<String, String>();
        final Map<String, String> fullFiles = new HashMap<String, String>();

        //noinspection IOResourceOpenedButNotSafelyClosed
        br = new BufferedReader(new FileReader(xmlList));

        // read the file list to get all the files to load
        while (br.ready()) {
            final String[] in = br.readLine().split("\\t");
            String xmlfile = in[0];
            String archive = in[1];

            if (xmlfile.contains("_clinical")) {
                clinicalFiles.put(xmlfile, archive);
            } else if (xmlfile.contains("_biospecimen")) {
                biospecimenFiles.put(xmlfile, archive);
            } else {
                fullFiles.put(xmlfile, archive);
            }
        }

        Date dateAdded = Calendar.getInstance().getTime();

        // NOTE!!! This deletes all data before the load starts, assuming we are re-loading everything.
        // a better way would be to figure out what has changed and load that, or to be able to load multiple versions of the data in the schema
        emptyClinicalTables(user);

        // load any "full" files first -- in case some archives aren't split yet
        for (final String file : fullFiles.keySet()) {
            String archive = fullFiles.get(file);
            System.out.println("Full file " + file + " in " + archive);
            // need to re-instantiate the disease-specific beans for each file
            createDiseaseSpecificBeans(xmlList);
            String disease = getDiseaseName(archive);
            processFullXmlFile(file, archive, disease, dateAdded);

            // memory leak or something... have to commit and close all connections and re-get connection
            // after each file to keep from using too much heap space.  this troubles me, but I have never had
            // the time to figure out why it happens
            resetConnections(url, user, word);
        }

        // now process all clinical files, and insert patients and clinical data
        for (final String clinicalFile : clinicalFiles.keySet()) {
            createDiseaseSpecificBeans(xmlList);
            String archive = clinicalFiles.get(clinicalFile);
            System.out.println("Clinical file " + clinicalFile + " in " + archive);
            String disease = getDiseaseName(archive);
            processClinicalXmlFile(clinicalFile, archive, disease, dateAdded);
            resetConnections(url, user, word);
        }

        // now process biospecimen files
        for (final String biospecimenFile : biospecimenFiles.keySet()) {
            createDiseaseSpecificBeans(xmlList);
            String archive = biospecimenFiles.get(biospecimenFile);
            String disease = getDiseaseName(archive);
            System.out.println("Biospecimen file " + biospecimenFile);
            processBiospecimenXmlFile(biospecimenFile, archive, disease, dateAdded);
            resetConnections(url, user, word);
        }

        // this sets relationships between these clinical tables and data browser tables, since we delete
        // and reload every time
        setForeignKeys();
        dbConnection.commit();
        dbConnection.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    } finally {
        IOUtils.closeQuietly(br);
    }
}

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

/**
 * Sample of using to write/read information
 * //from w  w w  . ja  va 2s.c o m
 * @param args
 *            not used
 * @throws BuildPDFAExtensionSchemaDescriptionException
 *             When errors during building/reading xml file
 */
public static void main(String[] args) throws BuildPDFAExtensionSchemaDescriptionException {
    XMLValueTypeDescriptionManager vtMaker = new XMLValueTypeDescriptionManager();

    // add Descriptions
    for (int i = 0; i < 3; i++) {
        vtMaker.addValueTypeDescription("testType" + i, "nsURI" + i, "prefix" + i, "description" + i);

    }
    List<FieldDescription> fieldSample = new ArrayList<FieldDescription>();
    for (int i = 0; i < 2; i++) {
        fieldSample.add(new FieldDescription("fieldName" + i, "valueType" + i, "description" + i));
    }
    vtMaker.addValueTypeDescription("testTypeField", "http://test.withfield.com/vt/", "prefTest",
            " value type description", fieldSample);

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

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

    // emulate a new reading
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    vtMaker = new XMLValueTypeDescriptionManager();
    vtMaker.loadListFromXML(is);
    List<ValueTypeDescription> result = vtMaker.getValueTypesDescriptionList();
    System.out.println();
    System.out.println();
    System.out.println("Result of XML Loading :");
    for (ValueTypeDescription propertyDescription : result) {
        System.out.println(propertyDescription.getType() + " :" + propertyDescription.getDescription());
        if (propertyDescription.getFields() != null) {
            Iterator<FieldDescription> fit = propertyDescription.getFields().iterator();
            FieldDescription field;
            while (fit.hasNext()) {
                field = fit.next();
                System.out.println("Field " + field.getName() + " :" + field.getValueType());
            }
        }
    }

}

From source file:com.doculibre.constellio.lang.html.HtmlLangDetector.java

public static void main(String[] args) throws Exception {
    //java.net.URL url = new java.net.URL("http://www.gouv.qc.ca");
    java.net.URL url = new java.net.URL("http://andrew.triumf.ca/multilingual/samples/french.meta.html");
    java.net.URLConnection connection = url.openConnection();
    java.io.InputStream is = null;
    String content = null;//from ww w .ja  v a2s  . com
    try {
        is = connection.getInputStream();
        content = IOUtils.toString(is);
    } finally {
        IOUtils.closeQuietly(is);
    }

    HtmlLangDetector langDetector = new HtmlLangDetector();
    System.out.println(langDetector.getLang(content));
}

From source file:com.revo.deployr.client.example.data.io.anon.discrete.exec.MultipleDataInMultipleDataOut.java

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

    RClient rClient = null;//from   w  w w.  j a v a 2  s .  com

    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 ].");

        /*
         * 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();

        /*
         * MultipleDataInMultipleDataOut Example Note:
         * 
         * The inputs sent on this example are contrived
         * and superfluous as the hipStar.rData binary R
         * object input and the hipStarUrl input perform
         * the exact same purpose...to load the Hip STAR
         * dataset into the workspace ahead of execution.
         * 
         * The example is provided to simply demonstrate
         * the mechanism of specifying multiple inputs.
         */

        /* 
         * Preload from the DeployR repository the following
         * binary R object input file:
         * /testuser/example-data-io/hipStar.rData
         *
         * As this is an anonymous operation "hipStar.rData"
         * must have it's repository-managed access controls
         * set to "public".
         */
        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 ].");

        /* 
         * 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, [ ProjectPreloadOptions.rinputs ].");

        /*
         * Request the retrieval of the "hip" data.frame and
         * two vector objects from the workspace following the
         * execution. The corresponding R objects are named as
         * follows:
         * 'hip', hipDim', 'hipNames'.
         */
        options.routputs = Arrays.asList("hip", "hipDim", "hipNames");

        log.info("[  EXEC OPTION   ] DeployR-encoded R object request "
                + "set on execution [ ProjectExecutionOptions.routputs ].");

        /*
         * Execute a public analytics Web service as an anonymous
         * 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 multiple outputs following the execution:
         *
         * 1. R console output.
         * 2. R console output.
         * 3. R console output.
         * 4. R console output.
         * 5. R console output.
         */

        String console = exec.about().console;
        log.info("[  DATA OUTPUT   ] Retrieved R console " + "output [ String ].");

        /*
         * Retrieve the requested R object data encodings from
         * the workspace follwing the script execution. 
         *
         * See the R Object Data Decoding chapter in the
         * Client Library Tutorial on the DeployR website for
         * further details.
         */
        List<RData> objects = exec.about().workspaceObjects;

        for (RData rData : objects) {
            if (rData instanceof RDataFrame) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RDataFrame ].");
                List<RData> hipSubsetVal = ((RDataFrame) rData).getValue();
            } else if (rData instanceof RNumericVector) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RNumericVector ].");
                List<Double> hipDimVal = ((RNumericVector) rData).getValue();
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object " + rData.getName()
                        + " value=" + hipDimVal);
            } else if (rData instanceof RStringVector) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RStringVector ].");
                List<String> hipNamesVal = ((RStringVector) rData).getValue();
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object " + rData.getName()
                        + " value=" + hipNamesVal);
            } else {
                log.info("Unexpected DeployR-encoded R object returned, " + "object name=" + rData.getName()
                        + ", encoding=" + rData.getClass());
            }
        }

        /*
         * Retrieve the working directory files (artifact)
         * was generated by the execution.
         */
        List<RProjectFile> wdFiles = exec.about().artifacts;

        for (RProjectFile wdFile : wdFiles) {
            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 " + ex);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }

        /*
         * Retrieve R graphics device plots (results) called
         * unnamedplot*.png that was generated by the execution.
         */
        List<RProjectResult> results = exec.about().results;

        for (RProjectResult result : results) {
            log.info("[  DATA OUTPUT   ] Retrieved graphics device " + "plot output " + result.about().filename
                    + " [ RProjectResult ].");
            InputStream fis = null;
            try {
                fis = result.download();
            } catch (Exception ex) {
                log.warn("Graphics device plot " + ex);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }

    } 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.revo.deployr.client.example.data.io.auth.discrete.exec.MultipleDataInMultipleDataOut.java

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

    RClient rClient = null;//w  ww.j av a  2s  .  c  o  m

    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();

        /*
         * MultipleDataInMultipleDataOut Example Note:
         * 
         * The inputs sent on this example are contrived
         * and superfluous as the hipStar.rData binary R
         * object input and the hipStarUrl input perform
         * the exact same purpose...to load the Hip STAR
         * dataset into the workspace ahead of execution.
         * 
         * The example is provided to simply demonstrate
         * the mechanism of specifying multiple inputs.
         */

        /* 
         * 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 ].");

        /* 
         * 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, [ ProjectPreloadOptions.rinputs ].");

        /*
         * Request the retrieval of the "hip" data.frame and
         * two vector objects from the workspace following the
         * execution. The corresponding R objects are named as
         * follows:
         * 'hip', hipDim', 'hipNames'.
         */
        options.routputs = Arrays.asList("hip", "hipDim", "hipNames");

        log.info("[  EXEC OPTION   ] DeployR-encoded R object request "
                + "set on execution [ ProjectExecutionOptions.routputs ].");

        /*
         * 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 multiple outputs following the execution:
         *
         * 1. R console output.
         * 2. R console output.
         * 3. R console output.
         * 4. R console output.
         * 5. R console output.
         */

        String console = exec.about().console;
        log.info("[  DATA OUTPUT   ] Retrieved R console " + "output [ String ].");

        /*
         * Retrieve the requested R object data encodings from
         * the workspace follwing the script execution. 
         *
         * See the R Object Data Decoding chapter in the
         * Client Library Tutorial on the DeployR website for
         * further details.
         */
        List<RData> objects = exec.about().workspaceObjects;

        for (RData rData : objects) {
            if (rData instanceof RDataFrame) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RDataFrame ].");
                List<RData> hipSubsetVal = ((RDataFrame) rData).getValue();
            } else if (rData instanceof RNumericVector) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RNumericVector ].");
                List<Double> hipDimVal = ((RNumericVector) rData).getValue();
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object " + rData.getName()
                        + " value=" + hipDimVal);
            } else if (rData instanceof RStringVector) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RStringVector ].");
                List<String> hipNamesVal = ((RStringVector) rData).getValue();
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object " + rData.getName()
                        + " value=" + hipNamesVal);
            } else {
                log.info("Unexpected DeployR-encoded R object returned, " + "object name=" + rData.getName()
                        + ", encoding=" + rData.getClass());
            }
        }

        /*
         * Retrieve the working directory files (artifact)
         * was generated by the execution.
         */
        List<RProjectFile> wdFiles = exec.about().artifacts;

        for (RProjectFile wdFile : wdFiles) {
            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 " + ex);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }

        /*
         * Retrieve R graphics device plots (results) called
         * unnamedplot*.png that was generated by the execution.
         */
        List<RProjectResult> results = exec.about().results;

        for (RProjectResult result : results) {
            log.info("[  DATA OUTPUT   ] Retrieved graphics device " + "plot output " + result.about().filename
                    + " [ RProjectResult ].");
            InputStream fis = null;
            try {
                fis = result.download();
            } catch (Exception ex) {
                log.warn("Graphics device plot " + ex);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        }

    } 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:ezbake.deployer.cli.EzDeployerCli.java

public static void main(String[] args) throws TException, IOException {
    int returnVal = 43;
    try {//from w ww.java  2 s  .com
        EzDeployerCli cli = new EzDeployerCli(args);
        try {
            returnVal = cli.run();
        } finally {
            IOUtils.closeQuietly(cli);
        }
    } catch (DeploymentException e) {
        // A deployment exception won't be useful to print out the stack trace.
        System.out.println(Strings.repeat("\n", 3));
        System.err.println("Error: " + e.getMessage());
        System.out.println(Strings.repeat("\n", 3));
        returnVal = 15;
    } catch (Exception e) {
        e.printStackTrace();
        returnVal = 13;
    } finally {
        System.out.println("Finished");
    }
    System.exit(returnVal);
}

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

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

    RClient rClient = null;/*from   w w  w.  j a  v  a 2  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();

        /*
         * MultipleDataInMultipleDataOut Example Note:
         * 
         * The inputs sent on this example are contrived
         * and superfluous as the hipStar.rData binary R
         * object input and the hipStarUrl input perform
         * the exact same purpose...to load the Hip STAR
         * dataset into the workspace ahead of execution.
         * 
         * The example is provided to simply demonstrate
         * the mechanism of specifying multiple inputs.
         */

        /* 
         * 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 ].");

        /* 
         * 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 ].");

        /*
         * Request the retrieval of the "hip" data.frame and
         * two vector objects from the workspace following the
         * execution. The corresponding R objects are named as
         * follows:
         * 'hip', hipDim', 'hipNames'.
         */
        options.routputs = Arrays.asList("hip", "hipDim", "hipNames");

        log.info("[  EXEC OPTION   ] DeployR-encoded R object request "
                + "set on execution [ ProjectExecutionOptions.routputs ].");

        /*
         * 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 multiple outputs following the execution:
         *
         * 1. R console output.
         * 2. R console output.
         * 3. R console output.
         * 4. R console output.
         * 5. R console output.
         */

        String console = exec.about().console;
        log.info("[  DATA OUTPUT   ] Retrieved R console " + "output [ String ].");

        /*
         * Retrieve the requested R object data encodings from
         * the workspace follwing the script execution. 
         *
         * See the R Object Data Decoding chapter in the
         * Client Library Tutorial on the DeployR website for
         * further details.
         */
        List<RData> objects = exec.about().workspaceObjects;

        for (RData rData : objects) {
            if (rData instanceof RDataFrame) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RDataFrame ].");
                List<RData> hipSubsetVal = ((RDataFrame) rData).getValue();
            } else if (rData instanceof RNumericVector) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RNumericVector ].");
                List<Double> hipDimVal = ((RNumericVector) rData).getValue();
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object " + rData.getName()
                        + " value=" + hipDimVal);
            } else if (rData instanceof RStringVector) {
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object output " + rData.getName()
                        + " [ RStringVector ].");
                List<String> hipNamesVal = ((RStringVector) rData).getValue();
                log.info("[  DATA OUTPUT   ] Retrieved DeployR-encoded R " + "object " + rData.getName()
                        + " value=" + hipNamesVal);
            } else {
                log.info("Unexpected DeployR-encoded R object returned, " + "object name=" + rData.getName()
                        + ", encoding=" + rData.getClass());
            }
        }

        /*
         * Retrieve the working directory files (artifact)
         * was generated by the execution.
         */
        List<RProjectFile> wdFiles = exec.about().artifacts;

        for (RProjectFile wdFile : wdFiles) {
            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);
            }
        }

        /*
         * Retrieve R graphics device plots (results) called
         * unnamedplot*.png that was generated by the execution.
         */
        List<RProjectResult> results = exec.about().results;

        for (RProjectResult result : results) {
            log.info("[  DATA OUTPUT   ] Retrieved graphics device " + "plot output " + result.about().filename
                    + " [ RProjectResult ].");
            InputStream fis = null;
            try {
                fis = result.download();
            } catch (Exception ex) {
                log.warn("Graphics device plot 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) {
        }
    }

}