public String blame(XmlDocument xmlDocument) throws IOException, SAXException, ParserConfigurationException { ImmutableMultimap<Integer, Record> resultingSourceMapping = getResultingSourceMapping(xmlDocument); LineReader lineReader = new LineReader( new StringReader(xmlDocument.prettyPrint())); StringBuilder actualMappings = new StringBuilder(); String line; int count = 1; while ((line = lineReader.readLine()) != null) { actualMappings.append(count).append(line).append("\n"); if (resultingSourceMapping.containsKey(count)) { for (Record record : resultingSourceMapping.get(count)) { actualMappings.append(count).append("-->") .append(record.getActionLocation().toString()) .append("\n"); } } count++; } return actualMappings.toString(); }
private Set<Integer> getInstances(Iterable<Discoverable> discoverables) throws IOException { Set<Integer> instances = Sets.newHashSet(); for (Discoverable discoverable : discoverables) { InetSocketAddress socketAddress = discoverable.getSocketAddress(); try (Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort())) { PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true); LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)); String msg = "Failure"; writer.println(msg); String line = reader.readLine(); Assert.assertTrue(line.endsWith(msg)); instances.add(Integer.parseInt(line.substring(0, line.length() - msg.length()))); } } return instances; }
@Test public void simple() throws IOException { layout.start(); final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); final Logger logger = lc.getLogger(LOGGER_NAME); final String logMsg = layout.doLayout(simpleLoggingEvent(logger, null)); final ObjectMapper om = new ObjectMapper(); final JsonNode jsonNode = om.readTree(logMsg); basicValidation(jsonNode); final LineReader msg = new LineReader(new StringReader(jsonNode.get("full_message").textValue())); assertEquals("message 1", msg.readLine()); }
StashSplitIterator(AmazonS3 s3, String bucket, String key) { InputStream rawIn = new RestartingS3InputStream(s3, bucket, key); try { // File is gzipped // Note: // Because the content may be concatenated gzip files we cannot use the default GZIPInputStream. // GzipCompressorInputStream supports concatenated gzip files. GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(rawIn, true); _in = new BufferedReader(new InputStreamReader(gzipIn, Charsets.UTF_8)); // Create a line reader _reader = new LineReader(_in); } catch (Exception e) { try { Closeables.close(rawIn, true); } catch (IOException ignore) { // Won't happen, already caught and logged } throw Throwables.propagate(e); } }
@Test public void testPackageUploadWithFileSucceeds() throws Exception { Pipe pipe = Pipe.open(); String contents = "This is a test!"; File tmpFile = makeFileWithContents("file.txt", contents); when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))) .thenReturn(ImmutableList.of(StorageObjectOrIOException.create( new FileNotFoundException("some/path")))); when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink()); List<DataflowPackage> targets = defaultPackageUtil.stageClasspathElements( ImmutableList.of(tmpFile.getAbsolutePath()), STAGING_PATH, createOptions); DataflowPackage target = Iterables.getOnlyElement(targets); verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class)); verify(mockGcsUtil).create(any(GcsPath.class), anyString()); verifyNoMoreInteractions(mockGcsUtil); assertThat(target.getName(), RegexMatcher.matches("file-" + HASH_PATTERN + ".txt")); assertThat(target.getLocation(), equalTo(STAGING_PATH + target.getName())); assertThat(new LineReader(Channels.newReader(pipe.source(), "UTF-8")).readLine(), equalTo(contents)); }
public String blame(XmlDocument xmlDocument) throws IOException, SAXException, ParserConfigurationException { ImmutableMultimap<Integer, Record> resultingSourceMapping = getResultingSourceMapping(xmlDocument); LineReader lineReader = new LineReader( new StringReader(xmlDocument.prettyPrint())); StringBuilder actualMappings = new StringBuilder(); String line; int count = 0; while ((line = lineReader.readLine()) != null) { actualMappings.append(count + 1).append(line).append("\n"); if (resultingSourceMapping.containsKey(count)) { for (Record record : resultingSourceMapping.get(count)) { actualMappings.append(count + 1).append("-->") .append(record.getActionLocation().toString()) .append("\n"); } } count++; } return actualMappings.toString(); }
public static List<Alias> getAliasFromFile(String fileName) throws IOException{ List<Alias> fileAliases = new ArrayList<Alias>(); BufferedReader br = new BufferedReader(new FileReader(fileName)); LineReader lineReader = new LineReader(br); String sCurrentLine; String[] values; int countcfs = 0; while ((sCurrentLine = lineReader.readLine()) != null) { values = sCurrentLine.split(";"); if (values.length<3) System.err.println("Erro na linha " + countcfs); String rep = values[0]; String dev1 = values[1]; String dev2 = values[2]; fileAliases.add(new Alias(rep, dev1, dev2)); countcfs++; } return fileAliases; }
private static Alias[] readFile(String fileName) throws IOException{ List<Alias> fileAliases = new ArrayList<Alias>(); BufferedReader br = new BufferedReader(new FileReader(fileName)); LineReader lineReader = new LineReader(br); String sCurrentLine; String[] values; int countcfs = 0; while ((sCurrentLine = lineReader.readLine()) != null) { values = sCurrentLine.split(";"); if (values.length<3) System.err.println("Erro na linha " + countcfs); String rep = values[0]; String dev1 = values[1]; String dev2 = values[2]; fileAliases.add(new Alias(rep, dev1, dev2)); countcfs++; } return fileAliases.toArray(new Alias[0]); }
public static Map<String, List<LineInfo>> getFileInfo(String fileName) throws IOException{ Map<String, List<LineInfo>> fileInfoMap = new HashMap<String, List<LineInfo>>(); BufferedReader br = new BufferedReader(new FileReader(fileName)); LineReader lineReader = new LineReader(br); String sCurrentLine; String[] values; int countcfs = 0; while ((sCurrentLine = lineReader.readLine()) != null) { if (sCurrentLine.startsWith("#")) continue; values = sCurrentLine.split(";"); if (values.length<3) System.err.println("Erro na linha " + countcfs); String rep = values[0]; if (!fileInfoMap.containsKey(rep)) { fileInfoMap.put(rep, new ArrayList<LineInfo>()); } fileInfoMap.get(rep).add(new LineInfo(rep, Arrays.asList(values).subList(1, values.length))); } //lineReader.close(); return fileInfoMap; }
public GuidDatasetUrnStateStoreNameParser(FileSystem fs, Path jobStatestoreRootDir) throws IOException { this.fs = fs; this.sanitizedNameToDatasetURNMap = Maps.synchronizedBiMap(HashBiMap.<String, String>create()); this.versionIdentifier = new Path(jobStatestoreRootDir, StateStoreNameVersion.V1.getDatasetUrnNameMapFile()); if (this.fs.exists(versionIdentifier)) { this.version = StateStoreNameVersion.V1; try (InputStream in = this.fs.open(versionIdentifier)) { LineReader lineReader = new LineReader(new InputStreamReader(in, Charsets.UTF_8)); String shortenName = lineReader.readLine(); while (shortenName != null) { String datasetUrn = lineReader.readLine(); this.sanitizedNameToDatasetURNMap.put(shortenName, datasetUrn); shortenName = lineReader.readLine(); } } } else { this.version = StateStoreNameVersion.V0; } }
private Set<Integer> getInstances(Iterable<Discoverable> discoverables) throws IOException { Set<Integer> instances = Sets.newHashSet(); for (Discoverable discoverable : discoverables) { InetSocketAddress socketAddress = discoverable.getSocketAddress(); Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort()); try { PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true); LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)); String msg = "Failure"; writer.println(msg); String line = reader.readLine(); Assert.assertTrue(line.endsWith(msg)); instances.add(Integer.parseInt(line.substring(0, line.length() - msg.length()))); } finally { socket.close(); } } return instances; }
public Map<String,String> extractMetrics(String payload,List<String> metricKeys,String metricPrefix){ Map<String,String> metricsMap = Maps.newHashMap(); try { LineReader reader = new LineReader(new StringReader(payload)); List<String> headers = parseALine(reader.readLine()); List<Integer> keyOffsets = getMetricKeyOffsets(headers, metricKeys); String line = ""; while((line = reader.readLine()) != null){ List<String> metrics = parseALine(line); extractMetricKeyValue(metricsMap, headers, keyOffsets, metrics,metricPrefix); } } catch (IOException e) { logger.error("Error in extracting metrics " + e); } return metricsMap; }
@NonNull public String blame(@NonNull XmlDocument xmlDocument) throws IOException, SAXException, ParserConfigurationException { ImmutableMultimap<Integer, Record> resultingSourceMapping = getResultingSourceMapping(xmlDocument); LineReader lineReader = new LineReader( new StringReader(xmlDocument.prettyPrint())); StringBuilder actualMappings = new StringBuilder(); String line; int count = 0; while ((line = lineReader.readLine()) != null) { actualMappings.append(count + 1).append(line).append("\n"); if (resultingSourceMapping.containsKey(count)) { for (Record record : resultingSourceMapping.get(count)) { actualMappings.append(count + 1).append("-->") .append(record.getActionLocation().toString()) .append("\n"); } } count++; } return actualMappings.toString(); }
private void init() throws Exception { try { socket = new Socket(targetHost, targetPort); outputStream = socket.getOutputStream(); PrintWriter out = new PrintWriter(outputStream, false); InputStream inputStream = socket.getInputStream(); // send an HTTP request to the web server out.println(String.format("SOURCE %s HTTP/1.0", mounter)); out.println(String.format("Authorization: Basic %s", HttpRequest.Base64.encode(user + ":" + password))); out.println("User-Agent: libshout/2.3.1"); out.println(String.format("Content-Type: %s", mimeType.getContentType())); out.println(String.format("ice-name: %s", iceName)); out.println("ice-public: 0"); if (iceDesc != null) { out.println(String.format("ice-description: %s", iceDesc)); } out.println(); out.flush(); // check if 404 LineReader lineReader = new LineReader(new InputStreamReader(inputStream)); String data = lineReader.readLine(); handleResponse(data); } catch (Exception e) { if (socket != null && !socket.isClosed()) { try { socket.close(); } catch (IOException e1) { // skip } } throw e; } }
@Override public Collection<Seed> initSeeds(VSCrawlerContext vsCrawlerContext) { Properties properties = VSCrawlerContext.vsCrawlerConfigFileWatcher.loadedProperties(); String seedFilePath = PathResolver.resolveAbsolutePath(properties.getProperty(String.format(VSCrawlerConstant.VSCRAWLER_INIT_SEED_FILE, vsCrawlerContext.getCrawlerName()))); if (StringUtils.isBlank(seedFilePath) || !new File(seedFilePath).exists()) { if (StringUtils.isNotBlank(seedFilePath)) { log.warn("can not find file:{}", seedFilePath); } seedFilePath = PathResolver.resolveAbsolutePath(filePath); } if (StringUtils.isEmpty(seedFilePath) || !new File(seedFilePath).exists()) { if (StringUtils.isNotBlank(seedFilePath)) { log.warn("can not find file:{}", seedFilePath); } return Collections.emptyList(); } vsCrawlerContext.getAutoEventRegistry().registerEvent(LoadNextBatchSeedEvent.class); Collection<Seed> seeds = null; try { fileReader = new FileReader(new File(PathResolver.resolveAbsolutePath(seedFilePath))); lineReader = new LineReader(fileReader); seeds = readBatch(); return seeds; } catch (IOException e) { log.error("error when load init seed resource"); return Collections.emptyList(); } finally { closeOrReadNextBatch(seeds, vsCrawlerContext); } }
public CharSourceLineReader(CharSource charSource) { try { reader = charSource.openStream(); } catch (Exception e) { throw new RuntimeException(e); } lineReader = new LineReader(reader); }
public void processResource( String resource, InputStream is, final List<Relocator> relocators ) throws IOException { ServiceStream out = serviceEntries.get( resource ); if ( out == null ) { out = new ServiceStream(); serviceEntries.put( resource, out ); } final ServiceStream fout = out; final String content = IOUtils.toString( is ); StringReader reader = new StringReader( content ); LineReader lineReader = new LineReader( reader ); String line; while ( ( line = lineReader.readLine() ) != null ) { String relContent = line; for ( Relocator relocator : relocators ) { if ( relocator.canRelocateClass( relContent ) ) { relContent = relocator.applyToSourceContent( relContent ); } } fout.append( relContent + "\n" ); } if ( this.relocators == null ) { this.relocators = relocators; } }
@Test public void testEnv() throws Exception { TwillRunner runner = getTwillRunner(); TwillController controller = runner.prepare(new EchoApp()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .withApplicationArguments("echo") .withArguments("echo1", "echo1") .withArguments("echo2", "echo2") .withEnv(ImmutableMap.of("GREETING", "Hello")) .withEnv("echo2", ImmutableMap.of("GREETING", "Hello2")) .start(); // Service echo1 should returns "Hello" as greeting, echo2 should returns "Hello2" Map<String, String> runnableGreetings = ImmutableMap.of("echo1", "Hello", "echo2", "Hello2"); for (Map.Entry<String, String> entry : runnableGreetings.entrySet()) { Discoverable discoverable = getDiscoverable(controller.discoverService(entry.getKey()), 60, TimeUnit.SECONDS); try ( Socket socket = new Socket(discoverable.getSocketAddress().getAddress(), discoverable.getSocketAddress().getPort()) ) { PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true); LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)); writer.println("GREETING"); Assert.assertEquals(entry.getValue(), reader.readLine()); } } controller.terminate().get(); }
@Test public void exception() throws IOException { layout.start(); final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); final Logger logger = lc.getLogger(LOGGER_NAME); final String logMsg; try { throw new IllegalArgumentException("Example Exception"); } catch (final IllegalArgumentException e) { logMsg = layout.doLayout(new LoggingEvent( LOGGER_NAME, logger, Level.DEBUG, "message {}", e, new Object[]{1})); } final ObjectMapper om = new ObjectMapper(); final JsonNode jsonNode = om.readTree(logMsg); basicValidation(jsonNode); final LineReader msg = new LineReader(new StringReader(jsonNode.get("full_message").textValue())); assertEquals("message 1", msg.readLine()); assertEquals("java.lang.IllegalArgumentException: Example Exception", msg.readLine()); final String line = msg.readLine(); assertTrue("Unexpected line: " + line, line.matches( "^\tat de.siegmar.logbackgelf.GelfLayoutTest.exception\\(GelfLayoutTest.java:\\d+\\) " + "~\\[.+/:na]$")); }
@Override public void install(CallingContext ctx, RaptureURI uri, PluginTransportItem item) { SeriesApi api = Kernel.getSeries(); try { LineReader reader = new LineReader(new StringReader(new String(item.getContent(), Charsets.UTF_8))); for (String line = reader.readLine(); line != null; line = reader.readLine()) { String part[] = line.split(",", 2); if (part.length != 2) { throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Malformed Series Descption: " + uri); } String column = part[0]; String val = part[1]; // TODO MEL find a way to bypass the wrapper so we don't reparse // URI and entitle/audit each point if (val.startsWith("'")) { api.addStringToSeries(ctx, uri.toString(), column, val.substring(1)); } else if (val.startsWith("{")) { api.addStructureToSeries(ctx, uri.toString(), column, val); } else if (val.contains(".")) { api.addDoubleToSeries(ctx, uri.toString(), column, Double.parseDouble(val)); } else { api.addLongToSeries(ctx, uri.toString(), column, Long.parseLong(val)); } } } catch (IOException e) { throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error installing series at uri " + uri.toString(), e); } }
@Test public void testReadWithExistingFile() throws Exception { String expected = "my test string"; File existingFile = temporaryFolder.newFile(); Files.write(expected, existingFile, StandardCharsets.UTF_8); String data; try (Reader reader = Channels.newReader( localFileSystem.open(LocalResourceId.fromPath( existingFile.toPath(), false /* isDirectory */)), StandardCharsets.UTF_8.name())) { data = new LineReader(reader).readLine(); } assertEquals(expected, data); }
@NonNull private static List<String> readLines(String input) throws IOException { List<String> result = new ArrayList<String>(); LineReader lineReader = new LineReader((new StringReader(input))); String line = lineReader.readLine(); while(line != null) { result.add(line); line = lineReader.readLine(); } return result; }
default Mappings parse(Readable readable) throws IOException { LineReader lineReader = new LineReader(readable); LineProcessor<Mappings> lineProcessor = createLineProcessor(); String line; while ((line = lineReader.readLine()) != null) { if (!lineProcessor.processLine(line)) { break; } } return lineProcessor.getResult(); }
private void importPoints() { // minor hack to handle LTOP format if (this.mime.equals("application/octet-stream") || this.mime.equals("text/plain") || this.mime.isEmpty()) { // We need to check if the file is a LTOP file or not. // This verification can only be achieved by reading the // first line of the file. try { ContentResolver cr = this.getContentResolver(); InputStreamReader in = new InputStreamReader(cr.openInputStream(this.dataUri)); LineReader lr = new LineReader(in); String firstLine = lr.readLine(); if (firstLine == null) { ViewUtils.showToast(this, this.getString( R.string.error_unsupported_format)); return; } else if ((firstLine.length() >= 4) && firstLine.substring(0, 4).equals("$$PK")) { // fix the MIME type this.mime = "text/ltop"; } else { // small hack for handling PTP files because there is no // proper way to detect them this.mime = "text/ptp"; } } catch (IOException e) { Logger.log(Logger.ErrLabel.IO_ERROR, e.getMessage()); ViewUtils.showToast(this, e.getMessage()); } } this.importFromExternalFile(); }
public static Optional<String> getMasterPassword(FileSystem fs, Path masterPasswordFile) { try (Closer closer = Closer.create()) { if (!fs.exists(masterPasswordFile) || fs.getFileStatus(masterPasswordFile).isDirectory()) { LOG.warn(masterPasswordFile + " does not exist or is not a file. Cannot decrypt any encrypted password."); return Optional.absent(); } InputStream in = closer.register(fs.open(masterPasswordFile)); return Optional.of(new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine()); } catch (IOException e) { throw new RuntimeException("Failed to obtain master password from " + masterPasswordFile, e); } }
@Test public void testLocalFile() throws Exception { String header = Files.readFirstLine(new File(getClass().getClassLoader().getResource("header.txt").toURI()), Charsets.UTF_8); TwillRunner runner = YarnTestUtils.getTwillRunner(); TwillController controller = runner.prepare(new LocalFileApplication()) .addJVMOptions(" -verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails") .withApplicationArguments("local") .withArguments("LocalFileSocketServer", "local2") .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .start(); Iterable<Discoverable> discoverables = controller.discoverService("local"); Assert.assertTrue(YarnTestUtils.waitForSize(discoverables, 1, 60)); InetSocketAddress socketAddress = discoverables.iterator().next().getSocketAddress(); Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort()); try { PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true); LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)); String msg = "Local file test"; writer.println(msg); Assert.assertEquals(header, reader.readLine()); Assert.assertEquals(msg, reader.readLine()); } finally { socket.close(); } controller.stopAndWait(); Assert.assertTrue(YarnTestUtils.waitForSize(discoverables, 0, 60)); TimeUnit.SECONDS.sleep(2); }
@Override public void processResource(String resource, InputStream is, final List<Relocator> relocators) throws IOException { ServiceStream out = serviceEntries.get(resource); if (out == null) { out = new ServiceStream(); serviceEntries.put(resource, out); } final ServiceStream fout = out; final String content = IOUtils.toString(is); StringReader reader = new StringReader(content); LineReader lineReader = new LineReader(reader); String line; while ((line = lineReader.readLine()) != null) { String relContent = line; for (Relocator relocator : relocators) { if (relocator.canRelocateClass(relContent)) { relContent = relocator.applyToSourceContent(relContent); } } fout.append(relContent + "\n"); } if (this.relocators == null) { this.relocators = relocators; } }
/** * 使用LineReader * @throws Exception */ @Test public void example6() throws Exception{ File file = new File("src/main/resources/sample.txt"); LineReader lineReader = new LineReader(new FileReader(file)); for(String line = lineReader.readLine();line!=null;line=lineReader.readLine()){ System.out.println(line); } }
private char[][] readMapFile() throws IOException { File mapFile = new File(GameConstants.MAP_FILENAME); LineReader lineReader = new LineReader(new FileReader(mapFile)); ArrayList<char[]> result = new ArrayList<>(); String line; while ((line = lineReader.readLine()) != null) { if (line.length() == 0) { break; } result.add(line.toCharArray()); } return result.toArray(new char[result.size()][]); }
@Test public void testRunnablesGetAllowedResourcesInEnv() throws InterruptedException, IOException, TimeoutException, ExecutionException { TwillRunner runner = getTwillRunner(); ResourceSpecification resourceSpec = ResourceSpecification.Builder.with() .setVirtualCores(1) .setMemory(2048, ResourceSpecification.SizeUnit.MEGA) .setInstances(1) .build(); TwillController controller = runner.prepare(new EnvironmentEchoServer(), resourceSpec) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .withApplicationArguments("envecho") .withArguments("EnvironmentEchoServer", "echo2") .start(); final CountDownLatch running = new CountDownLatch(1); controller.onRunning(new Runnable() { @Override public void run() { running.countDown(); } }, Threads.SAME_THREAD_EXECUTOR); Assert.assertTrue(running.await(120, TimeUnit.SECONDS)); Iterable<Discoverable> envEchoServices = controller.discoverService("envecho"); Assert.assertTrue(waitForSize(envEchoServices, 1, 120)); // TODO: check virtual cores once yarn adds the ability Map<String, String> expectedValues = Maps.newHashMap(); expectedValues.put(EnvKeys.YARN_CONTAINER_MEMORY_MB, "2048"); expectedValues.put(EnvKeys.TWILL_INSTANCE_COUNT, "1"); // check environment of the runnable. Discoverable discoverable = envEchoServices.iterator().next(); for (Map.Entry<String, String> expected : expectedValues.entrySet()) { try ( Socket socket = new Socket(discoverable.getSocketAddress().getHostName(), discoverable.getSocketAddress().getPort()) ) { PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true); LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)); writer.println(expected.getKey()); Assert.assertEquals(expected.getValue(), reader.readLine()); } } controller.terminate().get(120, TimeUnit.SECONDS); // Sleep a bit before exiting. TimeUnit.SECONDS.sleep(2); }
@Test public void testLocalFile() throws Exception { // Generate a header and a footer files. File headerFile = tmpFolder.newFile("header.txt"); File footerFile = tmpFolder.newFile("footer.txt"); String headerMsg = "Header Message"; String footerMsg = "Footer Message"; Files.write(headerMsg, headerFile, StandardCharsets.UTF_8); Files.write(footerMsg, footerFile, StandardCharsets.UTF_8); TwillRunner runner = getTwillRunner(); TwillController controller = runner.prepare(new LocalFileApplication(headerFile)) .addJVMOptions(" -verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails") .withApplicationArguments("local") .withArguments("LocalFileSocketServer", "local2") .withResources(footerFile.toURI()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .start(); Iterable<Discoverable> discoverables = controller.discoverService("local"); Assert.assertTrue(waitForSize(discoverables, 1, 60)); InetSocketAddress socketAddress = discoverables.iterator().next().getSocketAddress(); try (Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort())) { PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true); LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)); String msg = "Local file test"; writer.println(msg); Assert.assertEquals(headerMsg, reader.readLine()); Assert.assertEquals(msg, reader.readLine()); Assert.assertEquals(footerMsg, reader.readLine()); } controller.terminate().get(120, TimeUnit.SECONDS); Assert.assertTrue(waitForSize(discoverables, 0, 60)); TimeUnit.SECONDS.sleep(2); }
@Override public void run() throws Exception { checkArgument( (assignee == null) == (assigneesFile != null), "Exactly one of either assignee or filename must be specified."); checkArgument( (assigneesFile == null) || (metadata == null), "Metadata cannot be specified along with a filename."); checkArgument( (assignee == null) || (metadataColumns == null), "Metadata columns cannot be specified along with an assignee."); ImmutableSet<String> validTlds = ImmutableSet.copyOf(assertTldsExist(tlds)); LineReader reader = new LineReader( (assigneesFile != null) ? Files.newReader(assigneesFile.toFile(), UTF_8) : new StringReader(assignee)); String line = null; do { ImmutableSet.Builder<LrpTokenEntity> tokensToSaveBuilder = new ImmutableSet.Builder<>(); for (String token : generateTokens(BATCH_SIZE)) { line = reader.readLine(); if (!isNullOrEmpty(line)) { ImmutableList<String> values = ImmutableList.copyOf( Splitter.onPattern(COMMA_EXCEPT_WHEN_QUOTED_REGEX) // Results should not be surrounded in double quotes. .trimResults(CharMatcher.is('\"')) .split(line)); LrpTokenEntity.Builder tokenBuilder = new LrpTokenEntity.Builder() .setAssignee(values.get(0)) .setToken(token) .setValidTlds(validTlds); if (metadata != null) { tokenBuilder.setMetadata(metadata); } else if (metadataColumns != null) { ImmutableMap.Builder<String, String> metadataBuilder = ImmutableMap.builder(); for (ImmutableMap.Entry<String, Integer> entry : metadataColumns.entrySet()) { checkArgument( values.size() > entry.getValue(), "Entry for %s does not have a value for %s (index %s)", values.get(0), entry.getKey(), entry.getValue()); metadataBuilder.put(entry.getKey(), values.get(entry.getValue())); } tokenBuilder.setMetadata(metadataBuilder.build()); } tokensToSaveBuilder.add(tokenBuilder.build()); } } final ImmutableSet<LrpTokenEntity> tokensToSave = tokensToSaveBuilder.build(); // Wrap in a retrier to deal with transient 404 errors (thrown as RemoteApiExceptions). retrier.callWithRetry(() -> saveTokens(tokensToSave), RemoteApiException.class); } while (line != null); }
public static void main(String[] args) throws Exception { if (System.getenv("ASANA_CLIENT_ID") == null || System.getenv("ASANA_CLIENT_SECRET") == null) { throw new Error("Please set the ASANA_CLIENT_ID and ASANA_CLIENT_SECRET environment variables."); } System.out.println("== Example using OAuth Client ID and Client Secret:"); // create an OAuth app with the OAuth credentials: OAuthApp app = new OAuthApp( System.getenv("ASANA_CLIENT_ID"), System.getenv("ASANA_CLIENT_SECRET"), // this special redirect URI will prompt the user to copy/paste the code. // useful for command line scripts and other non-web apps OAuthApp.NATIVE_REDIRECT_URI ); // create an OAuth client with the app Client client = Client.oauth(app); System.out.println("isAuthorized=" + app.isAuthorized()); // get an authorization URL: String url = app.getAuthorizationUrl("FIXME: random state"); System.out.println(url); // in a web app you'd redirect the user to this URL when they take action to // login with Asana or connect their account to Asana Desktop.getDesktop().browse(new URI(url)); // prompt the user to copy and paste the code from the browser window System.out.println("Copy and paste the returned code from the browser and press enter:"); String code = new LineReader(new InputStreamReader(System.in)).readLine(); // exchange the code for a bearer token // normally you'd persist this token somewhere String accessToken = app.fetchToken(code); System.out.println("isAuthorized=" + app.isAuthorized()); System.out.println("token=" + accessToken); // get some information about your own user User user = client.users.me().execute(); System.out.println("me=" + user.name); System.out.println(user.id); // get your photo, if you have one if (user.photo != null) { System.out.println(user.photo.image_128x128); } System.out.println(user.workspaces.iterator().next().name); // demonstrate creating a client using a previously obtained bearer token System.out.println("== Example using OAuth Access Token:"); app = new OAuthApp( System.getenv("ASANA_CLIENT_ID"), System.getenv("ASANA_CLIENT_SECRET"), "urn:ietf:wg:oauth:2.0:oob", accessToken ); client = Client.oauth(app); System.out.println("isAuthorized=" + app.isAuthorized()); System.out.println("me=" + client.users.me().execute().name); }
@Test public void testRunnablesGetAllowedResourcesInEnv() throws InterruptedException, IOException, TimeoutException, ExecutionException { TwillRunner runner = YarnTestUtils.getTwillRunner(); ResourceSpecification resourceSpec = ResourceSpecification.Builder.with() .setVirtualCores(1) .setMemory(2048, ResourceSpecification.SizeUnit.MEGA) .setInstances(1) .build(); TwillController controller = runner.prepare(new EnvironmentEchoServer(), resourceSpec) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .withApplicationArguments("envecho") .withArguments("EnvironmentEchoServer", "echo2") .start(); final CountDownLatch running = new CountDownLatch(1); controller.addListener(new ServiceListenerAdapter() { @Override public void running() { running.countDown(); } }, Threads.SAME_THREAD_EXECUTOR); Assert.assertTrue(running.await(120, TimeUnit.SECONDS)); Iterable<Discoverable> envEchoServices = controller.discoverService("envecho"); Assert.assertTrue(YarnTestUtils.waitForSize(envEchoServices, 1, 120)); // TODO: check virtual cores once yarn adds the ability Map<String, String> expectedValues = Maps.newHashMap(); expectedValues.put(EnvKeys.YARN_CONTAINER_MEMORY_MB, "2048"); expectedValues.put(EnvKeys.TWILL_INSTANCE_COUNT, "1"); // check environment of the runnable. Discoverable discoverable = envEchoServices.iterator().next(); for (Map.Entry<String, String> expected : expectedValues.entrySet()) { Socket socket = new Socket(discoverable.getSocketAddress().getHostName(), discoverable.getSocketAddress().getPort()); try { PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true); LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)); writer.println(expected.getKey()); Assert.assertEquals(expected.getValue(), reader.readLine()); } finally { socket.close(); } } controller.stop().get(120, TimeUnit.SECONDS); // Sleep a bit before exiting. TimeUnit.SECONDS.sleep(2); }