List of usage examples for org.apache.commons.io IOUtils closeQuietly
public static void closeQuietly(OutputStream output)
OutputStream
. From source file:com.doculibre.constellio.utils.license.ApplyLicenseUtils.java
/** * @param args/* w ww .j a va 2 s . c o m*/ */ @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { URL licenceHeaderURL = ApplyLicenseUtils.class.getResource("LICENSE_HEADER"); File binDir = ClasspathUtils.getClassesDir(); File projectDir = binDir.getParentFile(); // File dryrunDir = new File(projectDir, "dryrun"); File licenceFile = new File(licenceHeaderURL.toURI()); List<String> licenceLines = readLines(licenceFile); // for (int i = 0; i < licenceLines.size(); i++) { // String licenceLine = licenceLines.get(i); // licenceLines.set(i, " * " + licenceLine); // } // licenceLines.add(0, "/**"); // licenceLines.add(" */"); List<File> javaFiles = (List<File>) org.apache.commons.io.FileUtils.listFiles(projectDir, new String[] { "java" }, true); for (File javaFile : javaFiles) { if (isValidPackage(javaFile)) { List<String> javaFileLines = readLines(javaFile); if (!javaFileLines.isEmpty()) { boolean modified = false; String firstLineTrim = javaFileLines.get(0).trim(); if (firstLineTrim.startsWith("package")) { modified = true; javaFileLines.addAll(0, licenceLines); } else if (firstLineTrim.startsWith("/**")) { int indexOfEndCommentLine = -1; loop2: for (int i = 0; i < javaFileLines.size(); i++) { String javaFileLine = javaFileLines.get(i); if (javaFileLine.indexOf("*/") != -1) { indexOfEndCommentLine = i; break loop2; } } if (indexOfEndCommentLine != -1) { modified = true; int i = 0; loop3: for (Iterator<String> it = javaFileLines.iterator(); it.hasNext();) { it.next(); if (i <= indexOfEndCommentLine) { it.remove(); } else { break loop3; } i++; } javaFileLines.addAll(0, licenceLines); } else { throw new RuntimeException( "Missing end comment for file " + javaFile.getAbsolutePath()); } } if (modified) { // String outputFilePath = javaFile.getPath().substring(projectDir.getPath().length()); // File outputFile = new File(dryrunDir, outputFilePath); // outputFile.getParentFile().mkdirs(); // System.out.println(outputFile.getPath()); // FileOutputStream fos = new FileOutputStream(outputFile); System.out.println(javaFile.getPath()); FileOutputStream fos = new FileOutputStream(javaFile); IOUtils.writeLines(javaFileLines, "\n", fos); IOUtils.closeQuietly(fos); } } } } }
From source file:com.revo.deployr.client.example.data.io.auth.discrete.exec.RepoFileInGraphicsPlotOut.java
public static void main(String args[]) throws Exception { RClient rClient = null;//from www . ja va 2 s .co 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(); /* * 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 ]."); /* * 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 R graphics device plot (result) called * unnamedplot*.png 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<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.athena.peacock.agent.Starter.java
/** * <pre>//from w w w . java 2 s .co m * * </pre> * @param args */ @SuppressWarnings("resource") public static void main(String[] args) { int rand = (int) (Math.random() * 100) % 50; System.setProperty("random.seconds", Integer.toString(rand)); String configFile = null; try { configFile = PropertyUtil.getProperty(PeacockConstant.CONFIG_FILE_KEY); } catch (Exception e) { // nothing to do. } finally { if (StringUtils.isEmpty(configFile)) { configFile = "/peacock/agent/config/agent.conf"; } } /** * ${peacock.agent.config.file.name} ?? load ? ?? ? ? ? . */ String errorMsg = "\n\"" + configFile + "\" file does not exist or cannot read.\n" + "Please check \"" + configFile + "\" file exists and can read."; Assert.isTrue(AgentConfigUtil.exception == null, errorMsg); Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_IP), "ServerIP cannot be empty."); Assert.notNull(AgentConfigUtil.getConfig(PeacockConstant.SERVER_PORT), "ServerPort cannot be empty."); /** * Agent ID ?? ${peacock.agent.agent.file.name} ? ?, * ?? ? Agent ID ? ?? . */ String agentFile = null; String agentId = null; try { agentFile = PropertyUtil.getProperty(PeacockConstant.AGENT_ID_FILE_KEY); } catch (Exception e) { // nothing to do. } finally { if (StringUtils.isEmpty(agentFile)) { agentFile = "/peacock/agent/.agent"; } } File file = new File(agentFile); boolean isNew = false; if (file.exists()) { try { agentId = IOUtils.toString(file.toURI()); // ? ? agent ID agent ID? ? 36? ? ?. if (StringUtils.isEmpty(agentId) || agentId.length() != 36) { throw new IOException(); } } catch (IOException e) { logger.error(agentFile + " file cannot read or saved invalid agent ID.", e); agentId = PeacockAgentIDGenerator.generateId(); isNew = true; } } else { agentId = PeacockAgentIDGenerator.generateId(); isNew = true; } if (isNew) { logger.info("New Agent-ID({}) be generated.", agentId); try { file.setWritable(true); OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(file)); output.write(agentId); file.setReadOnly(); IOUtils.closeQuietly(output); } catch (UnsupportedEncodingException e) { logger.error("UnsupportedEncodingException has occurred : ", e); } catch (FileNotFoundException e) { logger.error("FileNotFoundException has occurred : ", e); } catch (IOException e) { logger.error("IOException has occurred : ", e); } } // Spring Application Context Loading logger.debug("Starting application context..."); AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/context-*.xml"); applicationContext.registerShutdownHook(); }
From source file:com.revo.deployr.client.example.data.io.auth.stateful.preload.RepoFileInGraphicsPlotOut.java
public static void main(String args[]) throws Exception { RClient rClient = null;//from 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 ProjectCreationOptions instance * to specify data inputs that "pre-heat" the R session * workspace or working directory for your project. */ ProjectCreationOptions options = new ProjectCreationOptions(); /* * 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("[ PRELOAD INPUT ] Repository data file input " + "set on project creation, [ ProjectCreationOptions.preloadDirectory ]."); /* * Create a temporary project (R session) passing a * ProjectCreationOptions to "pre-heat" data into the * workspace and/or working directory. */ rProject = rUser.createProject(options); log.info("[ GO STATEFUL ] Created stateful temporary " + "R session [ RProject ]."); /* * 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, null); log.info("[ EXECUTION ] Stateful R script " + "execution completed [ RProjectExecution ]."); /* * Retrieve R graphics device plot (result) called * unnamedplot*.png 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<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) { } } }
From source file:com.revo.deployr.client.example.data.io.auth.discrete.exec.EncodedDataInBinaryFileOut.java
public static void main(String args[]) throws Exception { RClient rClient = null;//from ww w . j a v 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(); /* * 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 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 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 " + 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.ExternalDataInDataFileOut.java
public static void main(String args[]) throws Exception { RClient rClient = null;//from www .j a va2 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(); /* * 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 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 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 " + 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:asm.FindCallers.java
public static void main(String... args) { OutputStream os = null;/* w w w . j a v a2 s .co m*/ try { FindAnnotationWalker w = new FindAnnotationWalker(); FindCallers cl = w.walk(args[0], args[1]); for (Annotated a : cl.annotated) System.out.println(a); System.out.println("Start CSV"); os = new FileOutputStream(args[2]); for (Annotated a : cl.annotated) { if (!a.isPublic) { String line = String.format("%s\t%s\t%s\t%s\t%s\t%s\n", "no", "", "", a.className, a.name, a.getTarget()); os.write(line.getBytes("UTF-8")); } } for (Invocation i : cl.invocations) { if (cl.annotatedMap.containsKey(i.getTarget())) { for (Annotated a : cl.annotatedMap.get(i.getTarget())) { String ok = ""; if (i.sourceClassName.endsWith("Cmd") || i.sourceClassName.endsWith("Test") || i.sourceClassName.contains("/test/")) { ok = "ok"; } if (i.sourceClassName.equals(a.className)) { ok = "no"; } if (ok.equals("") && i.sourceBaseName.equals(a.baseName) && !i.sourceClassName.equals(a.className)) { /* Source and dest are different and they are both have the * same parent, so they obviously one isn't a child of the other */ ok = "ok"; } String line = String.format("%s\t%s\t%s\t%s\t%s\t%s\n", ok, i.sourceClassName, i.sourceMethodName, a.className, a.name, a.getTarget()); os.write(line.getBytes("UTF-8")); } } } System.out.println("Done CSV"); } catch (Throwable e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(os); } }
From source file:com.revo.deployr.client.example.data.io.auth.stateful.preload.EncodedDataInBinaryFileOut.java
public static void main(String args[]) throws Exception { RClient rClient = null;// w w w. ja v a 2 s . 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 ProjectCreationOptions instance * to specify data inputs that "pre-heat" the R session * workspace or working directory for your project. */ ProjectCreationOptions options = new ProjectCreationOptions(); /* * Simulate application generated data. This data * is first encoded using the RDataFactory before * being passed as an input on project initialization. * * This encoded R input is automatically converted * into a workspace object at project initialization. */ RData generatedData = simulateGeneratedData(); if (generatedData != null) { List<RData> rinputs = Arrays.asList(generatedData); options.rinputs = rinputs; } log.info("[ PRELOAD INPUT ] DeployR-encoded R input set on " + "project creation, [ ProjectCreationOptions.rinputs ]."); /* * Create a temporary project (R session) passing a * ProjectCreationOptions to "pre-heat" data into the * workspace and/or working directory. */ rProject = rUser.createProject(options); log.info("[ GO STATEFUL ] Created stateful temporary " + "R session [ RProject ]."); /* * 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, null); 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.revo.deployr.client.example.data.io.auth.stateful.preload.ExternalDataInDataFileOut.java
public static void main(String args[]) throws Exception { RClient rClient = null;/*from ww w .ja va 2s . 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 options = new ProjectCreationOptions(); /* * Load an R object literal "hipStarUrl" into the * workspace on project initialization. * * The R script execution that follows will check 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("[ PRELOAD INPUT ] External data source input " + "set on project creation, [ ProjectCreationOptions.rinputs ]."); /* * Create a temporary project (R session) passing a * ProjectCreationOptions to "pre-heat" data into the * workspace and/or working directory. */ rProject = rUser.createProject(options); log.info("[ GO STATEFUL ] Created stateful temporary " + "R session [ RProject ]."); /* * 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, null); 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:com.revo.deployr.client.example.data.io.auth.stateful.exec.RepoFileInGraphicsPlotOut.java
public static void main(String args[]) throws Exception { RClient rClient = null;//from w ww .jav 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(); /* * 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 ]."); /* * 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 R graphics device plot (result) called * unnamedplot*.png 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<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) { } } }