下面列出了怎么用com.google.common.io.LineReader的API类实例代码及写法,或者点击链接到github查看源代码。
protected TypedCheckResult parse(Response response) throws IOException {
LineReader reader = new LineReader(new StringReader(response.getResponseBody(Constants.UTF_8.name())));
Map<Meta, VersionProfile> result = new HashMap<Meta, VersionProfile>();
String line;
try {
while ((line = reader.readLine()) != null) {
append(result, line);
}
} catch (IOException e) {
//ignore
}
if (Constants.PULL.equals(response.getHeader(Constants.UPDATE_TYPE))) {
return new TypedCheckResult(result, TypedCheckResult.Type.PULL);
} else {
return new TypedCheckResult(result, TypedCheckResult.Type.UPDATE);
}
}
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();
}
@Override
public MessageCheckpoint fromBytes(final byte[] data) {
try {
final LineReader reader = new LineReader(new StringReader(new String(data, Charsets.UTF_8)));
final int version = Integer.parseInt(reader.readLine());
switch (version) {
case VERSION_V1:
throw new RuntimeException("v1 checkpoint not support");
case VERSION_V2:
return parseV2(reader);
default:
throw new RuntimeException("unknown snapshot content version " + version);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private MessageCheckpoint parseV2(final LineReader reader) throws IOException {
final long offset = Long.parseLong(reader.readLine());
final Map<String, Long> sequences = new HashMap<>();
while (true) {
final String line = reader.readLine();
if (Strings.isNullOrEmpty(line)) {
break;
}
final List<String> parts = SLASH_SPLITTER.splitToList(line);
final String subject = parts.get(0);
final long maxSequence = Long.parseLong(parts.get(1));
sequences.put(subject, maxSequence);
}
return new MessageCheckpoint(offset, sequences);
}
@Override
public ActionCheckpoint fromBytes(final byte[] data) {
try {
final LineReader reader = new LineReader(new StringReader(new String(data, Charsets.UTF_8)));
final int version = Integer.parseInt(reader.readLine());
switch (version) {
case VERSION_V1:
throw new RuntimeException("v1 checkpoint not support");
case VERSION_V2:
throw new RuntimeException("v2 checkpoint not support");
case VERSION_V3:
return parseV3(reader);
default:
throw new RuntimeException("unknown snapshot content version " + version);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
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();
}
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 {
encoder.start();
final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger logger = lc.getLogger(LOGGER_NAME);
final String logMsg = encodeToStr(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);
}
}
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;
}
private boolean addUrlDirToZipRecursively(ZipOutputStream zout, String root, String item, InputStream itemFound, Predicate<? super String> filter) throws IOException {
LineReader lr = new LineReader(new InputStreamReader(itemFound));
boolean readSubdirFile = false;
while (true) {
String line = lr.readLine();
if (line==null) {
// at end of file return true if we were able to recurse, else false
return readSubdirFile;
}
boolean isFile = addUrlItemRecursively(zout, root, item+"/"+line, filter);
if (isFile) {
readSubdirFile = true;
} else {
if (!readSubdirFile) {
// not a folder
return false;
} else {
// previous entry suggested it was a folder, but this one didn't work! -- was a false positive
// but zip will be in inconsistent state, so throw
throw new IllegalStateException("Failed to read entry "+line+" in "+item+" but previous entry implied it was a directory");
}
}
}
}
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 ProcessStreamReader(
ExecutorService executorService,
InputStream stream,
@Nullable Consumer<String> logConsumer) {
this.stream = stream;
future =
executorService.submit(
() -> {
final List<String> lines = Lists.newArrayList();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
LineReader lineReader = new LineReader(reader);
String line;
while ((line = lineReader.readLine()) != null) {
if (logConsumer != null) {
logConsumer.accept(line);
}
lines.add(line);
}
} catch (IOException e) {
exception.set(e);
}
return lines;
});
}
@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();
}
public void read(Reader reader) throws IOException {
ObjectMapper om = new ObjectMapper();
LineReader lineReader = new LineReader(reader);
String line;
while ((line = lineReader.readLine()) != null) {
JsonLine jsonLine = om.readValue(line, JsonLine.class);
data.put(
jsonLine.linkedResource,
new BindResult(jsonLine.linkedResource, jsonLine.messageName, jsonLine.serviceName));
}
}
private ActionCheckpoint parseV3(LineReader reader) throws IOException {
final long offset = Long.parseLong(reader.readLine());
final Table<String, String, ConsumerGroupProgress> progresses = HashBasedTable.create();
while (true) {
final String subjectLine = reader.readLine();
if (Strings.isNullOrEmpty(subjectLine)) {
break;
}
final List<String> subjectParts = SLASH_SPLITTER.splitToList(subjectLine);
final String subject = subjectParts.get(0);
final int groupCount = Integer.parseInt(subjectParts.get(1));
for (int i = 0; i < groupCount; i++) {
final String groupLine = reader.readLine();
final List<String> groupParts = SLASH_SPLITTER.splitToList(groupLine);
final String group = groupParts.get(0);
final boolean broadcast = short2Boolean(Short.parseShort(groupParts.get(1)));
final long maxPulledMessageSequence = Long.parseLong(groupParts.get(2));
final int consumerCount = Integer.parseInt(groupParts.get(3));
final ConsumerGroupProgress progress = new ConsumerGroupProgress(subject, group, broadcast, maxPulledMessageSequence, new HashMap<>(consumerCount));
progresses.put(subject, group, progress);
final Map<String, ConsumerProgress> consumers = progress.getConsumers();
for (int j = 0; j < consumerCount; j++) {
final String consumerLine = reader.readLine();
final List<String> consumerParts = SLASH_SPLITTER.splitToList(consumerLine);
final String consumerId = consumerParts.get(0);
final long pull = Long.parseLong(consumerParts.get(1));
final long ack = Long.parseLong(consumerParts.get(2));
consumers.put(consumerId, new ConsumerProgress(subject, group, consumerId, pull, ack));
}
}
}
return new ActionCheckpoint(offset, progresses);
}
@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);
}
}
@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 {
encoder.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 = encodeToStr(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(line.matches("^\tat de.siegmar.logbackgelf.GelfEncoderTest.exception"
+ "\\(GelfEncoderTest.java:\\d+\\)$"), "Unexpected line: " + line);
}
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);
}
}
/**
* 使用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);
}
}
/**
* Test of getFile method, of class MultimediaObject.
*/
@Test
public void testAnalyzeNewLine1() {
System.out.println("analyzeNewLine 1");
File file = new File("src/test/resources/testoutput1.txt");
ConversionOutputAnalyzer oa1= new ConversionOutputAnalyzer(0, null);
try
{
FileInputStream fis = new FileInputStream(file);
InputStreamReader streamReader = new InputStreamReader(fis, "UTF-8");
LineReader reader = new LineReader(streamReader);
String sLine = null;
while ((sLine = reader.readLine()) != null)
{
oa1.analyzeNewLine(sLine);
}
String result= oa1.getLastWarning();
String expResult= null;
assertEquals(expResult, result);
}
catch (IOException ioError)
{
System.out.println("IO error "+ioError.getMessage());
ioError.printStackTrace();
throw new AssertionError("IO error "+ioError.getMessage());
}
catch (EncoderException enError)
{
System.out.println("Encoder error "+enError.getMessage());
enError.printStackTrace();
throw new AssertionError("Encoder error "+enError.getMessage());
}
}
/**
* Test of getFile method, of class MultimediaObject.
*/
@Test
public void testAnalyzeNewLine1() {
System.out.println("analyzeNewLine 1");
File file = new File(getResourceSourcePath(), "testoutput1.txt");
ConversionOutputAnalyzer oa1= new ConversionOutputAnalyzer(0, null);
try
{
FileInputStream fis = new FileInputStream(file);
InputStreamReader streamReader = new InputStreamReader(fis, "UTF-8");
LineReader reader = new LineReader(streamReader);
String sLine = null;
while ((sLine = reader.readLine()) != null)
{
oa1.analyzeNewLine(sLine);
}
String result= oa1.getLastWarning();
String expResult= null;
assertEquals(expResult, result);
}
catch (IOException ioError)
{
System.out.println("IO error "+ioError.getMessage());
ioError.printStackTrace();
throw new AssertionError("IO error "+ioError.getMessage());
}
catch (EncoderException enError)
{
System.out.println("Encoder error "+enError.getMessage());
enError.printStackTrace();
throw new AssertionError("Encoder error "+enError.getMessage());
}
}
@Test
public void testEchoServer() throws Exception {
TwillRunner runner = getTwillRunner();
TwillController controller = runner.prepare(new EchoServer(),
ResourceSpecification.Builder.with()
.setVirtualCores(1)
.setMemory(1, ResourceSpecification.SizeUnit.GIGA)
.setInstances(2)
.build())
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationArguments("echo")
.withArguments("EchoServer", "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> echoServices = controller.discoverService("echo");
Assert.assertTrue(waitForSize(echoServices, 2, 120));
for (Discoverable discoverable : echoServices) {
String msg = "Hello: " + discoverable.getSocketAddress();
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(msg);
Assert.assertEquals(msg, reader.readLine());
}
}
// Increase number of instances
controller.changeInstances("EchoServer", 3).get(60, TimeUnit.SECONDS);
Assert.assertTrue(waitForSize(echoServices, 3, 120));
echoServices = controller.discoverService("echo2");
// Decrease number of instances
controller.changeInstances("EchoServer", 1).get(60, TimeUnit.SECONDS);
Assert.assertTrue(waitForSize(echoServices, 1, 120));
// Increase number of instances again
controller.changeInstances("EchoServer", 2).get(60, TimeUnit.SECONDS);
Assert.assertTrue(waitForSize(echoServices, 2, 120));
// Test restart on instances for runnable
Map<Integer, String> instanceIdToContainerId = Maps.newHashMap();
ResourceReport report = waitForAfterRestartResourceReport(controller, "EchoServer", 15L,
TimeUnit.MINUTES, 2, null);
Assert.assertTrue(report != null);
Collection<TwillRunResources> runResources = report.getRunnableResources("EchoServer");
for (TwillRunResources twillRunResources : runResources) {
instanceIdToContainerId.put(twillRunResources.getInstanceId(), twillRunResources.getContainerId());
}
controller.restartAllInstances("EchoServer").get(60, TimeUnit.SECONDS);
Assert.assertTrue(waitForSize(echoServices, 2, 120));
report = waitForAfterRestartResourceReport(controller, "EchoServer", 15L, TimeUnit.MINUTES, 2,
instanceIdToContainerId);
Assert.assertTrue(report != null);
// Make sure still only one app is running
Iterable<TwillRunner.LiveInfo> apps = runner.lookupLive();
Assert.assertTrue(waitForSize(apps, 1, 120));
// Creates a new runner service to check it can regain control over running app.
TwillRunnerService runnerService = TWILL_TESTER.createTwillRunnerService();
runnerService.start();
try {
Iterable <TwillController> controllers = runnerService.lookup("EchoServer");
Assert.assertTrue(waitForSize(controllers, 1, 120));
for (TwillController c : controllers) {
LOG.info("Stopping application: " + c.getRunId());
c.terminate().get(30, TimeUnit.SECONDS);
}
Assert.assertTrue(waitForSize(apps, 0, 120));
} finally {
runnerService.stop();
}
// Sleep a bit before exiting.
TimeUnit.SECONDS.sleep(2);
}
@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);
}
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.gid);
// 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);
}
private List<TextEncryptor> getEncryptors(CachedInstanceKey cacheKey) {
List<TextEncryptor> encryptors = new ArrayList<>();
int numOfEncryptionKeys = cacheKey.numOfEncryptionKeys;
String suffix = "";
int i = 1;
if (cacheKey.masterPasswordFile == null || numOfEncryptionKeys < 1) {
return encryptors;
}
Exception exception = null;
do {
Path currentMasterPasswordFile = new Path(cacheKey.masterPasswordFile + suffix);
try (Closer closer = Closer.create()) {
if (!fs.exists(currentMasterPasswordFile) ||
fs.getFileStatus(currentMasterPasswordFile).isDirectory()) {
continue;
}
InputStream in = closer.register(fs.open(currentMasterPasswordFile));
String masterPassword = new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine();
TextEncryptor encryptor = useStrongEncryptor ? new StrongTextEncryptor() : new BasicTextEncryptor();
// setPassword() needs to be called via reflection since the TextEncryptor interface doesn't have this method.
encryptor.getClass().getMethod("setPassword", String.class).invoke(encryptor, masterPassword);
encryptors.add(encryptor);
suffix = "." + String.valueOf(i);
} catch (FileNotFoundException fnf) {
// It is ok for password files not being present
LOG.warn("Master password file " + currentMasterPasswordFile + " not found.");
} catch (IOException ioe) {
exception = ioe;
LOG.warn("Master password could not be read from file " + currentMasterPasswordFile);
} catch (Exception e) {
LOG.warn("Encryptor could not be instantiated.");
}
} while (i++ < numOfEncryptionKeys);
// Throw exception if could not read any existing password file
if (encryptors.size() < 1 && exception != null) {
throw new RuntimeException("Master Password could not be read from any master password file.", exception);
}
return encryptors;
}