List of usage examples for java.net URL getFile
public String getFile()
From source file:co.cask.hydrator.plugin.RunTest.java
@Test public void testRunWithScriptInput() throws Exception { String inputTable = "run-shell-script-input"; URL testRunnerUrl = this.getClass().getResource("/SampleScript.sh"); FileUtils.copyFile(new File(testRunnerUrl.getFile()), new File(sourceFolder, "/SampleScript.sh")); ETLStage source = new ETLStage("source", MockSource.getPlugin(inputTable, INPUT)); Map<String, String> runProperties = new ImmutableMap.Builder<String, String>() .put("commandToExecute", "sh " + sourceFolder.toPath() + "/SampleScript.sh") .put("fieldsToProcess", "input").put("outputField", "output").put("outputFieldType", "string") .build();/* ww w . j ava2 s . co m*/ ETLStage transform = new ETLStage("transform", new ETLPlugin("Run", Transform.PLUGIN_TYPE, runProperties, null)); String sinkTable = "run-shell-script-output"; ETLStage sink = new ETLStage("sink", MockSink.getPlugin(sinkTable)); ETLBatchConfig etlConfig = ETLBatchConfig.builder("* * * * *").addStage(source).addStage(transform) .addStage(sink).addConnection(source.getName(), transform.getName()) .addConnection(transform.getName(), sink.getName()).build(); AppRequest<ETLBatchConfig> appRequest = new AppRequest<>(ETLBATCH_ARTIFACT, etlConfig); ApplicationId appId = NamespaceId.DEFAULT.app("RunJarTest"); ApplicationManager appManager = deployApplication(appId.toId(), appRequest); DataSetManager<Table> inputManager = getDataset(inputTable); List<StructuredRecord> input = ImmutableList.of( StructuredRecord.builder(INPUT).set("id", 1).set("input", "Brett").build(), StructuredRecord.builder(INPUT).set("id", 2).set("input", "Chang").build(), StructuredRecord.builder(INPUT).set("id", 3).set("input", "Roy").build(), StructuredRecord.builder(INPUT).set("id", 4).set("input", "John").build(), StructuredRecord.builder(INPUT).set("id", 5).set("input", "Matt").build()); MockSource.writeInput(inputManager, input); MapReduceManager mrManager = appManager.getMapReduceManager(ETLMapReduce.NAME); mrManager.start(); mrManager.waitForFinish(5, TimeUnit.MINUTES); DataSetManager<Table> outputManager = getDataset(sinkTable); List<StructuredRecord> outputRecords = MockSink.readOutput(outputManager); Assert.assertEquals("OutputRecords", 5, outputRecords.size()); for (StructuredRecord record : outputRecords) { int value = (record.get("id")); if (value == 1) { Assert.assertEquals("Brett", record.get("input")); Assert.assertEquals("Welcome User, Brett.", record.get("output")); } else if (value == 2) { Assert.assertEquals("Chang", record.get("input")); Assert.assertEquals("Welcome User, Chang.", record.get("output")); } else if (value == 3) { Assert.assertEquals("Roy", record.get("input")); Assert.assertEquals("Welcome User, Roy.", record.get("output")); } else if (value == 4) { Assert.assertEquals("John", record.get("input")); Assert.assertEquals("Welcome User, John.", record.get("output")); } else { Assert.assertEquals("Matt", record.get("input")); Assert.assertEquals("Welcome User, Matt.", record.get("output")); } } }
From source file:co.cask.hydrator.plugin.RunTest.java
@Test public void testRunWithJarInput() throws Exception { String inputTable = "run-jar-input"; URL testRunnerUrl = this.getClass().getResource("/SampleRunner.jar"); FileUtils.copyFile(new File(testRunnerUrl.getFile()), new File(sourceFolder, "/SampleRunner.jar")); ETLStage source = new ETLStage("source", MockSource.getPlugin(inputTable, INPUT)); Map<String, String> runProperties = new ImmutableMap.Builder<String, String>() .put("commandToExecute", "java -jar " + sourceFolder.toPath() + "/SampleRunner.jar") .put("fieldsToProcess", "input").put("fixedInputs", "CASK").put("outputField", "output") .put("outputFieldType", "string").build(); ETLStage transform = new ETLStage("transform", new ETLPlugin("Run", Transform.PLUGIN_TYPE, runProperties, null)); String sinkTable = "run-jar-output"; ETLStage sink = new ETLStage("sink", MockSink.getPlugin(sinkTable)); ETLBatchConfig etlConfig = ETLBatchConfig.builder("* * * * *").addStage(source).addStage(transform) .addStage(sink).addConnection(source.getName(), transform.getName()) .addConnection(transform.getName(), sink.getName()).build(); AppRequest<ETLBatchConfig> appRequest = new AppRequest<>(ETLBATCH_ARTIFACT, etlConfig); ApplicationId appId = NamespaceId.DEFAULT.app("RunJarTest"); ApplicationManager appManager = deployApplication(appId.toId(), appRequest); DataSetManager<Table> inputManager = getDataset(inputTable); List<StructuredRecord> input = ImmutableList.of( StructuredRecord.builder(INPUT).set("id", 1).set("input", "Brett").build(), StructuredRecord.builder(INPUT).set("id", 2).set("input", "Chang").build(), StructuredRecord.builder(INPUT).set("id", 3).set("input", "Roy").build(), StructuredRecord.builder(INPUT).set("id", 4).set("input", "John").build(), StructuredRecord.builder(INPUT).set("id", 5).set("input", "Michael").build()); MockSource.writeInput(inputManager, input); MapReduceManager mrManager = appManager.getMapReduceManager(ETLMapReduce.NAME); mrManager.start();/* w ww .ja va 2 s . c o m*/ mrManager.waitForFinish(5, TimeUnit.MINUTES); DataSetManager<Table> outputManager = getDataset(sinkTable); List<StructuredRecord> outputRecords = MockSink.readOutput(outputManager); Assert.assertEquals("OutputRecords", 5, outputRecords.size()); for (StructuredRecord record : outputRecords) { int value = (record.get("id")); if (value == 1) { Assert.assertEquals("Brett", record.get("input")); Assert.assertEquals("Hello Brett...Welcome to the CASK!!!", record.get("output")); } else if (value == 2) { Assert.assertEquals("Chang", record.get("input")); Assert.assertEquals("Hello Chang...Welcome to the CASK!!!", record.get("output")); } else if (value == 3) { Assert.assertEquals("Roy", record.get("input")); Assert.assertEquals("Hello Roy...Welcome to the CASK!!!", record.get("output")); } else if (value == 4) { Assert.assertEquals("John", record.get("input")); Assert.assertEquals("Hello John...Welcome to the CASK!!!", record.get("output")); } else { Assert.assertEquals("Michael", record.get("input")); Assert.assertEquals("Hello Michael...Welcome to the CASK!!!", record.get("output")); } } }
From source file:de.stefanheintz.log.jmxservice.LoggingConfigImpl.java
public void resetConfiguration() { ClassLoader cl = getClass().getClassLoader(); LogManager.resetConfiguration(); URL log4jprops = cl.getResource("log4j.properties"); if (log4jprops != null) { PropertyConfigurator.configure(log4jprops); sendNotification(NotificationType.RESET_CONFIGURATION, "used file: " + log4jprops.getFile()); }/*from w w w . ja v a2s. c o m*/ }
From source file:dkpro.similarity.algorithms.lexical.ngrams.CharacterNGramMeasure.java
public CharacterNGramMeasure(int n, String idfValuesFile) throws IOException { this.n = n;//from w ww.j a v a2s . co m URL resourceUrl = ResourceUtils.resolveLocation(idfValuesFile, this, null); idf = new HashMap<String, Double>(); for (String line : FileUtils.readLines(new File(resourceUrl.getFile()))) { String[] linesplit = line.split("\t"); idf.put(linesplit[0], Double.parseDouble(linesplit[1])); } }
From source file:de.cismet.cids.custom.utils.vermessungsunterlagen.tasks.VermUntTaskRisse.java
@Override public void performTask() throws VermessungsunterlagenTaskException { final String prefix = (ServerAlkisConf.getInstance().getVermessungHostBilder().equalsIgnoreCase(host) ? "Vermessungsrisse-Bericht" : "Ergnzende-Dokumente-Bericht"); final String suffix = getJobKey().substring(getJobKey().indexOf("_") + 1, getJobKey().length()); final String filename = getPath() + "/" + prefix + "_" + suffix.replace("/", "--") + ".pdf"; final File src = new File(VermessungsunterlagenHelper.getInstance().getProperties().getAbsPathPdfRisse()); final File dst = new File(getPath() + "/" + src.getName()); if (!dst.exists()) { try {/* www. j a va 2 s. co m*/ FileUtils.copyFile(src, dst); } catch (final Exception ex) { final String message = "Beim Kopieren des Risse-Informations-PDFs kam es zu einem unerwarteten Fehler."; throw new VermessungsunterlagenTaskException(getType(), message, ex); } } final Object[] tmp = VermessungsRissReportHelper.getInstance().generateReportData(auftragsnummer, projektnummer, risseBeans, host, MultiPagePictureReader.class); final Collection<CidsBean> reportBeans = (Collection) tmp[0]; final Map parameters = (Map) tmp[1]; final Collection<URL> additionalFilesToDownload = (Collection) tmp[2]; final JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(reportBeans); { OutputStream out = null; try { out = new FileOutputStream(filename); jasperReportDownload( ServerResourcesLoader.getInstance() .loadJasperReport(WundaBlauServerResources.VERMESSUNGSRISSE_JASPER.getValue()), parameters, dataSource, out); } catch (final Exception ex) { final String message = "Beim Erzeugen des Vermessungsrisse-Berichtes kam es zu einem unerwarteten Fehler."; throw new VermessungsunterlagenTaskException(getType(), message, ex); } finally { closeStream(out); } } final ExtendedAccessHandler extendedAccessHandler = new SimpleHttpAccessHandler(); for (final URL additionalFileToDownload : additionalFilesToDownload) { final String additionalFilename = getPath() + "/" + additionalFileToDownload.getFile() .substring(additionalFileToDownload.getFile().lastIndexOf('/') + 1); final String pureAdditionalFilename = additionalFilename.substring(0, additionalFilename.lastIndexOf('.')); InputStream in = null; OutputStream out = null; try { in = extendedAccessHandler.doRequest(additionalFileToDownload); out = new FileOutputStream(additionalFilename); downloadStream(in, out); } catch (Exception ex) { LOG.warn("could not download additional File", ex); VermessungsunterlagenHelper.writeExceptionJson(ex, VermessungsunterlagenHelper.getInstance().getPath(getJobKey().replace("/", "--")) + "/fehlerprotokoll_" + pureAdditionalFilename + ".json"); } finally { closeStream(in); closeStream(out); } } }
From source file:it.wingstech.csslesser.LessEngine.java
public String compile(URL input) throws LessException { try {/*from w w w . ja va 2 s. com*/ long time = System.currentTimeMillis(); logger.debug("Compiling URL: " + input.getProtocol() + ":" + input.getFile()); String result = call(cf, new Object[] { input.getProtocol() + ":" + input.getFile(), getClass().getClassLoader() }); logger.debug("The compilation of '" + input + "' took " + (System.currentTimeMillis() - time) + " ms."); return result; } catch (Exception e) { throw parseLessException(e); } }
From source file:eu.impact_project.iif.t2.client.WorkflowRunnerTest.java
/** * Test of doPost method, of class WorkflowRunner. *//*from w w w. j a v a2s . co m*/ @Test public void testDoPost() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); ServletConfig config = mock(ServletConfig.class); ServletContext context = mock(ServletContext.class); RequestDispatcher dispatcher = mock(RequestDispatcher.class); ServletOutputStream stream = mock(ServletOutputStream.class); HttpSession session = mock(HttpSession.class); when(request.getSession(true)).thenReturn(session); ArrayList<Workflow> flowList = new ArrayList<>(); Workflow flow = new Workflow(); flow.setStringVersion("Esto es una prueba"); flow.setWsdls("<wsdl>http://www.ua.es</wsdl>"); flow.setUrls("http://www.ua.es"); ArrayList<WorkflowInput> flowInputs = new ArrayList<>(); WorkflowInput input = new WorkflowInput("pru0Input"); input.setDepth(1); flowInputs.add(input); input = new WorkflowInput("pru1Input"); input.setDepth(0); flowInputs.add(input); flow.setInputs(flowInputs); flowList.add(flow); when(session.getAttribute("workflows")).thenReturn(flowList); when(config.getServletContext()).thenReturn(context); URL url = this.getClass().getResource("/config.properties"); File testFile = new File(url.getFile()); when(context.getRealPath("/")).thenReturn(testFile.getParent() + "/"); Part[] parts = new Part[] { new StringPart("user", "user"), new StringPart("pass", "pass"), new StringPart("workflow0pru0Input", "prueba0"), new StringPart("workflow0pru0Input0", "prueba0.0"), new StringPart("workflow0pru1Input", "prueba1") }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); final ByteArrayInputStream inputContent = new ByteArrayInputStream(requestContent.toByteArray()); when(request.getInputStream()).thenReturn(new ServletInputStream() { @Override public int read() throws IOException { return inputContent.read(); } }); when(request.getContentType()).thenReturn(multipartRequestEntity.getContentType()); WorkflowRunner runer = new WorkflowRunner(); try { runer.init(config); runer.doPost(request, response); } catch (ServletException ex) { fail("Should not raise exception " + ex.toString()); } catch (IOException ex) { fail("Should not raise exception " + ex.toString()); } catch (NullPointerException ex) { //ok no funciona el server de taverna } }
From source file:eu.impact_project.iif.t2.client.WorkflowRunnerTest.java
/** * Test of doPost method, of class WorkflowRunner. *//* ww w . j a v a 2 s .c om*/ @Test public void testDoPostURLFail() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); ServletConfig config = mock(ServletConfig.class); ServletContext context = mock(ServletContext.class); RequestDispatcher dispatcher = mock(RequestDispatcher.class); ServletOutputStream stream = mock(ServletOutputStream.class); HttpSession session = mock(HttpSession.class); when(request.getSession(true)).thenReturn(session); ArrayList<Workflow> flowList = new ArrayList<>(); Workflow flow = new Workflow(); flow.setStringVersion("Esto es una prueba"); flow.setWsdls("<wsdl>http://www.ua.es</wsdl>"); flow.setUrls("http://falsa.es"); ArrayList<WorkflowInput> flowInputs = new ArrayList<>(); WorkflowInput input = new WorkflowInput("pru0Input"); input.setDepth(1); flowInputs.add(input); input = new WorkflowInput("pru1Input"); input.setDepth(0); flowInputs.add(input); flow.setInputs(flowInputs); flowList.add(flow); when(session.getAttribute("workflows")).thenReturn(flowList); when(config.getServletContext()).thenReturn(context); URL url = this.getClass().getResource("/config.properties"); File testFile = new File(url.getFile()); when(context.getRealPath("/")).thenReturn(testFile.getParent() + "/"); Part[] parts = new Part[] { new StringPart("user", "user"), new StringPart("pass", "pass"), new StringPart("workflow0pru0Input", "prueba0"), new StringPart("workflow0pru0Input0", "prueba0.0"), new StringPart("workflow0pru1Input", "prueba1") }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); final ByteArrayInputStream inputContent = new ByteArrayInputStream(requestContent.toByteArray()); when(request.getInputStream()).thenReturn(new ServletInputStream() { @Override public int read() throws IOException { return inputContent.read(); } }); when(request.getContentType()).thenReturn(multipartRequestEntity.getContentType()); WorkflowRunner runer = new WorkflowRunner(); try { runer.init(config); runer.doPost(request, response); } catch (ServletException ex) { fail("Should not raise exception " + ex.toString()); } catch (IOException ex) { fail("Should not raise exception " + ex.toString()); } catch (NullPointerException ex) { //ok no funciona el server de taverna } }
From source file:com.collective.celos.ci.deploy.JScpWorkerTest.java
@Test public void testGetFileObjectByUri() throws Exception { JScpWorker worker = new JScpWorker("uname"); URL res = Thread.currentThread().getContextClassLoader() .getResource("com/collective/celos/ci/testing/config/target.json"); FileObject object = worker.getFileObjectByUri(res.toURI()); IOUtils.contentEquals(object.getContent().getInputStream(), new FileInputStream(res.getFile())); }
From source file:info.magnolia.freemarker.FreemarkerServletContextWrapper.java
/** * Clean url and get the file.//w ww . j a v a 2s. co m * @param url url to cleanup * @return file to url */ private File sanitizeToFile(URL url) { try { String fileUrl = url.getFile(); // needed because somehow the URLClassLoader has encoded URLs, and getFile does not decode them. fileUrl = URLDecoder.decode(fileUrl, "UTF-8"); // needed for Resin - for some reason, its URLs are formed as jar:file:/absolutepath/foo/bar.jar instead of // using the :///abs.. notation fileUrl = StringUtils.removeStart(fileUrl, "file:"); fileUrl = StringUtils.removeEnd(fileUrl, "!/"); return new File(fileUrl); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }