Exemplo n.º 1
0
  /**
   * Creates a {@link InMemoryConfigurator} to run through the process of generation of {@link
   * AdapterDefinition}
   *
   * @param deploymentInfo Location of the input and output location
   */
  @Override
  public void process(AdapterDeploymentInfo deploymentInfo) throws Exception {
    InMemoryAdapterConfigurator inMemoryAdapterConfigurator =
        new InMemoryAdapterConfigurator(
            cConf,
            namespace,
            templateJarLocation,
            adapterName,
            deploymentInfo.getAdapterConfig(),
            deploymentInfo.getTemplateSpec(),
            pluginRepository);

    ConfigResponse configResponse;

    try {
      configResponse = inMemoryAdapterConfigurator.config().get(120, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
      if (e.getCause() instanceof Exception) {
        throw (Exception) e.getCause();
      }
      throw e;
    }
    InputSupplier<? extends Reader> configSupplier = configResponse.get();
    if (configResponse.getExitCode() != 0 || configSupplier == null) {
      throw new IllegalArgumentException("Failed to configure adapter: " + deploymentInfo);
    }
    Reader reader = configSupplier.getInput();
    try {
      emit(GSON.fromJson(reader, AdapterDefinition.class));
    } finally {
      Closeables.closeQuietly(reader);
    }
  }
 @Before
 public void setUp() throws IOException {
   InputSupplier<InputStream> inputStreamSupplier =
       newInputStreamSupplier(getResource("person.avro"));
   schema = new Schema.Parser().parse(inputStreamSupplier.getInput());
   avroFile = File.createTempFile("test", ".av");
 }
Exemplo n.º 3
0
 public String request(String pathStartingWithSlash) {
   InputSupplier<InputStream> inputSupplier = doRequest(pathStartingWithSlash);
   try {
     return IOUtils.toString(inputSupplier.getInput(), "UTF-8");
   } catch (HttpDownloader.HttpException he) {
     throw handleHttpException(he);
   } catch (Exception e) {
     throw new SonarException(String.format("Unable to request: %s", pathStartingWithSlash), e);
   }
 }
Exemplo n.º 4
0
  public static ConfigurationFactory createConfigurationFactory(URI configUri) throws IOException {
    Properties properties = new Properties();
    InputSupplier<InputStream> configPropertiesStream =
        newConfigEntrySupplier(configUri, "etc/config.properties");
    InputStream input = configPropertiesStream.getInput();
    try {
      properties.load(input);
    } finally {
      input.close();
    }

    return new ConfigurationFactory((Map<String, String>) (Object) properties);
  }
Exemplo n.º 5
0
 private static Template compile(String name, InputSupplier<InputStreamReader> supplier) {
   try (InputStreamReader reader = supplier.getInput()) {
     return Mustache.compiler().escapeHTML(false).compile(reader);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Exemplo n.º 6
0
 /** @return the cached BuildFileTree, or a new lazily constructed BuildFileTree. */
 @Override
 public BuildFileTree getInput() throws IOException {
   if (buildFileTree == null) {
     buildFileTree = supplier.getInput();
   }
   return buildFileTree;
 }
 @Override
 public void process(
     String outputName,
     InputSupplier<? extends InputStream> in,
     OutputSupplier<? extends OutputStream> out,
     boolean closeAtFinish)
     throws IOException {
   process(outputName, in.getInput(), out.getOutput(), closeAtFinish);
 }
Exemplo n.º 8
0
 public static JvmOptions decode(InputSupplier<? extends Reader> readerSupplier)
     throws IOException {
   Reader reader = readerSupplier.getInput();
   try {
     return GSON.fromJson(reader, JvmOptions.class);
   } finally {
     reader.close();
   }
 }
Exemplo n.º 9
0
 public static void unpackConfig(
     InputSupplier<? extends InputStream> inputSupplier, File outputDir) throws IOException {
   ZipInputStream in = new ZipInputStream(inputSupplier.getInput());
   try {
     for (ZipEntry zipEntry = in.getNextEntry(); zipEntry != null; zipEntry = in.getNextEntry()) {
       File file = new File(outputDir, zipEntry.getName());
       if (zipEntry.getName().endsWith("/")) {
         // this is a directory
         file.mkdirs();
       } else {
         file.getParentFile().mkdirs();
         ByteStreams.copy(in, Files.newOutputStreamSupplier(file));
         file.setLastModified(zipEntry.getTime());
       }
     }
   } finally {
     in.close();
   }
 }
Exemplo n.º 10
0
 /**
  * Sends content as provided by the given {@link InputSupplier} as small chunks to the given
  * {@link BodyConsumer}.
  */
 private void sendChunks(
     InputSupplier<? extends InputStream> inputSupplier,
     int chunkSize,
     BodyConsumer bodyConsumer,
     HttpResponder responder)
     throws IOException {
   InputStream input = inputSupplier.getInput();
   try {
     byte[] bytes = new byte[chunkSize];
     int len = input.read(bytes);
     while (len >= 0) {
       bodyConsumer.chunk(ChannelBuffers.copiedBuffer(bytes, 0, len), responder);
       len = input.read(bytes);
     }
     bodyConsumer.finished(responder);
   } catch (Exception e) {
     bodyConsumer.handleError(e);
   } finally {
     input.close();
   }
 }
Exemplo n.º 11
0
  /**
   * Reads conll sentences, parses them, and writes the json-serialized results.
   *
   * @param inputSupplier where to read conll sentences from
   * @param outputSupplier where to write the results to
   * @param numThreads the number of threads to use
   * @throws IOException
   * @throws InterruptedException
   */
  public void runParser(
      final InputSupplier<? extends Readable> inputSupplier,
      final OutputSupplier<? extends Writer> outputSupplier,
      final int numThreads)
      throws IOException, InterruptedException {
    // use the producer-worker-consumer pattern to parse all sentences in multiple threads, while
    // keeping
    // output in order.
    final BlockingQueue<Future<Optional<SemaforParseResult>>> results =
        Queues.newLinkedBlockingDeque(5 * numThreads);
    final ExecutorService workerThreadPool = newFixedThreadPool(numThreads);
    // try to shutdown gracefully. don't worry too much if it doesn't work
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    try {
                      workerThreadPool.shutdown();
                      workerThreadPool.awaitTermination(5, TimeUnit.SECONDS);
                    } catch (InterruptedException ignored) {
                    }
                  }
                }));

    final PrintWriter output = new PrintWriter(outputSupplier.getOutput());
    try {
      // Start thread to fetch computed results and write to file
      final Thread consumer =
          new Thread(
              new Runnable() {
                @Override
                public void run() {
                  while (!Thread.currentThread().isInterrupted()) {
                    try {
                      final Optional<SemaforParseResult> oResult = results.take().get();
                      if (!oResult.isPresent()) break; // got poison pill. we're done
                      output.println(oResult.get().toJson());
                      output.flush();
                    } catch (Exception e) {
                      e.printStackTrace();
                      throw new RuntimeException(e);
                    }
                  }
                }
              });
      consumer.start();
      // in main thread, put placeholders on results queue (so results stay in order), then
      // tell a worker thread to fill up the placeholder
      final SentenceCodec.SentenceIterator sentences =
          ConllCodec.readInput(inputSupplier.getInput());
      try {
        int i = 0;
        while (sentences.hasNext()) {
          final Sentence sentence = sentences.next();
          final int sentenceId = i;
          results.put(
              workerThreadPool.submit(
                  new Callable<Optional<SemaforParseResult>>() {
                    @Override
                    public Optional<SemaforParseResult> call() throws Exception {
                      final long start = System.currentTimeMillis();
                      try {
                        final SemaforParseResult result = parseSentence(sentence);
                        final long end = System.currentTimeMillis();
                        System.err.printf(
                            "parsed sentence %d in %d millis.%n", sentenceId, end - start);
                        return Optional.of(result);
                      } catch (Exception e) {
                        e.printStackTrace();
                        throw e;
                      }
                    }
                  }));
          i++;
        }
        // put a poison pill on the queue to signal that we're done
        results.put(
            workerThreadPool.submit(
                new Callable<Optional<SemaforParseResult>>() {
                  @Override
                  public Optional<SemaforParseResult> call() throws Exception {
                    return Optional.absent();
                  }
                }));
        workerThreadPool.shutdown();
      } finally {
        closeQuietly(sentences);
      }
      // wait for consumer to finish
      consumer.join();
    } finally {
      closeQuietly(output);
    }
    System.err.println("Done.");
  }
 @Override
 public Pair<Reader, Version> open() throws IOException {
   return Pair.of((Reader) contentSupplier.getInput(), Version.STABLE_VERSION);
 }