Package com.google.common.io

Examples of com.google.common.io.Closer


    @Override
    public Object addingService(ServiceReference reference) {
        Object service = bundleContext.getService(reference);

        if (service instanceof Observer) {
            Closer subscription = Closer.create();
            BackgroundObserver observer = subscription.register(
                    new BackgroundObserver((Observer) service, executor));
            subscription.register(
                    observable.addObserver(observer));
            subscriptions.put(reference, subscription);
            return service;
        } else {
            bundleContext.ungetService(reference);
View Full Code Here


   * Copy and decompress the saved PairList in rda format.
   * @param dataFile the source data format
   * @throws IOException
   */
  private void processRDataFile(File dataFile) throws IOException {
    Closer closer = Closer.create();
    InputStream in = closer.register(DatasetsBuilder.decompress(dataFile));
    SEXP exp;
    try {
      RDataReader reader = new RDataReader(in);
      exp = reader.readFile();
    } catch(Throwable e) {
      throw closer.rethrow(e);
    } finally {
      in.close();
    }
   
    if(!(exp instanceof PairList)) {
View Full Code Here

    }
  }

  public static ImmutableMap<String, String> loadProperties(ByteSource is) throws IOException {
    Properties props = new Properties();
    Closer closer = Closer.create();
    InputStream in = closer.register(is.openStream());
    try {
      props.load(in);
    } finally {
      closer.close();
    }
    return Maps.fromProperties(props);
  }
View Full Code Here

    Manifest manifest = new Manifest();
    // Without version, the manifest is silently ignored. Ugh!
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarFile.getName());

    Closer closer = Closer.create();
    try {
      FileOutputStream fileOut = closer.register(new FileOutputStream(jarFile));
      JarOutputStream jarOut = closer.register(new JarOutputStream(fileOut));
      for (String entry : entries) {
        jarOut.putNextEntry(new ZipEntry(entry));
        Resources.copy(ClassPathTest.class.getResource(entry), jarOut);
        jarOut.closeEntry();
      }
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }
View Full Code Here

      .decayExponent(-0.02);

    lr.close();

    byte[] output;
    Closer closer = Closer.create();

    try {
      ByteArrayOutputStream byteArrayOutputStream = closer.register(new ByteArrayOutputStream());
      DataOutputStream dataOutputStream = closer.register(new DataOutputStream(byteArrayOutputStream));
      PolymorphicWritable.write(dataOutputStream, lr);
      output = byteArrayOutputStream.toByteArray();
    } finally {
      closer.close();
    }

    OnlineLogisticRegression read;

    try {
      ByteArrayInputStream byteArrayInputStream = closer.register(new ByteArrayInputStream(output));
      DataInputStream dataInputStream = closer.register(new DataInputStream(byteArrayInputStream));
      read = closer.register(PolymorphicWritable.read(dataInputStream, OnlineLogisticRegression.class));
    } finally {
      closer.close();
    }

    //lambda
    Assert.assertEquals((1.0e-3), read.getLambda(), 1.0e-7);
View Full Code Here

  }

  @Inject void loadLoggingConfig() {
    File loggingPropertiesFile = new File(caliperDirectory, "logging.properties");
    if (loggingPropertiesFile.isFile()) {
      Closer closer = Closer.create();
      FileInputStream fis = null;
      try {
        fis = closer.register(new FileInputStream(loggingPropertiesFile));
        logManager.readConfiguration(fis);
      } catch (SecurityException e) {
        logConfigurationException(e);
      } catch (IOException e) {
        logConfigurationException(e);
      } finally {
        try {
          closer.close();
        } catch (IOException e) {
          logger.log(SEVERE, "could not close " + loggingPropertiesFile, e);
        }
      }
      logger.info(String.format("Using logging configuration at %s", loggingPropertiesFile));
View Full Code Here

                        final File attemptDir = new File(taskDir, attemptUUID);

                        final ProcessHolder processHolder;
                        final int childPort = portFinder.findUnusedPort();
                        try {
                          final Closer closer = Closer.create();
                          try {
                            if (!attemptDir.mkdirs()) {
                              throw new IOException(String.format("Could not create directories: %s", attemptDir));
                            }

                            final File taskFile = new File(attemptDir, "task.json");
                            final File statusFile = new File(attemptDir, "status.json");
                            final File logFile = new File(attemptDir, "log");

                            // time to adjust process holders
                            synchronized (tasks) {
                              final ForkingTaskRunnerWorkItem taskWorkItem = tasks.get(task.getId());

                              if (taskWorkItem.shutdown) {
                                throw new IllegalStateException("Task has been shut down!");
                              }

                              if (taskWorkItem == null) {
                                log.makeAlert("WTF?! TaskInfo disappeared!").addData("task", task.getId()).emit();
                                throw new ISE("TaskInfo disappeared for task[%s]!", task.getId());
                              }

                              if (taskWorkItem.processHolder != null) {
                                log.makeAlert("WTF?! TaskInfo already has a processHolder")
                                   .addData("task", task.getId())
                                   .emit();
                                throw new ISE("TaskInfo already has processHolder for task[%s]!", task.getId());
                              }

                              final List<String> command = Lists.newArrayList();
                              final String childHost = String.format("%s:%d", node.getHostNoPort(), childPort);
                              final String taskClasspath;
                              if (task.getClasspathPrefix() != null && !task.getClasspathPrefix().isEmpty()) {
                                taskClasspath = Joiner.on(File.pathSeparator).join(
                                    task.getClasspathPrefix(),
                                    config.getClasspath()
                                );
                              } else {
                                taskClasspath = config.getClasspath();
                              }

                              command.add(config.getJavaCommand());
                              command.add("-cp");
                              command.add(taskClasspath);

                              Iterables.addAll(command, whiteSpaceSplitter.split(config.getJavaOpts()));

                              for (String propName : props.stringPropertyNames()) {
                                for (String allowedPrefix : config.getAllowedPrefixes()) {
                                  if (propName.startsWith(allowedPrefix)) {
                                    command.add(
                                        String.format(
                                            "-D%s=%s",
                                            propName,
                                            props.getProperty(propName)
                                        )
                                    );
                                  }
                                }
                              }

                              // Override child JVM specific properties
                              for (String propName : props.stringPropertyNames()) {
                                if (propName.startsWith(CHILD_PROPERTY_PREFIX)) {
                                  command.add(
                                      String.format(
                                          "-D%s=%s",
                                          propName.substring(CHILD_PROPERTY_PREFIX.length()),
                                          props.getProperty(propName)
                                      )
                                  );
                                }
                              }

                              command.add(String.format("-Ddruid.host=%s", childHost));
                              command.add(String.format("-Ddruid.port=%d", childPort));

                              command.add("io.druid.cli.Main");
                              command.add("internal");
                              command.add("peon");
                              command.add(taskFile.toString());
                              command.add(statusFile.toString());
                              String nodeType = task.getNodeType();
                              if (nodeType != null) {
                                command.add("--nodeType");
                                command.add(nodeType);
                              }

                              jsonMapper.writeValue(taskFile, task);

                              log.info("Running command: %s", Joiner.on(" ").join(command));
                              taskWorkItem.processHolder = new ProcessHolder(
                                  new ProcessBuilder(ImmutableList.copyOf(command)).redirectErrorStream(true).start(),
                                  logFile,
                                  childPort
                              );

                              processHolder = taskWorkItem.processHolder;
                              processHolder.registerWithCloser(closer);
                            }

                            log.info("Logging task %s output to: %s", task.getId(), logFile);
                            boolean runFailed = true;

                            try (final OutputStream toLogfile = Files.newOutputStreamSupplier(logFile).getOutput()) {
                              ByteStreams.copy(processHolder.process.getInputStream(), toLogfile);
                              final int statusCode = processHolder.process.waitFor();
                              log.info("Process exited with status[%d] for task: %s", statusCode, task.getId());
                              if (statusCode == 0) {
                                runFailed = false;
                              }
                            }
                            finally {
                              // Upload task logs
                              taskLogPusher.pushTaskLog(task.getId(), logFile);
                            }

                            if (!runFailed) {
                              // Process exited successfully
                              return jsonMapper.readValue(statusFile, TaskStatus.class);
                            } else {
                              // Process exited unsuccessfully
                              return TaskStatus.failure(task.getId());
                            }
                          } catch (Throwable t) {
                            throw closer.rethrow(t);
                          } finally {
                            closer.close();
                          }
                        }
                        catch (Throwable t) {
                          log.info(t, "Exception caught during execution");
                          throw Throwables.propagate(t);
View Full Code Here

      assertThat(api.describeJob(VAULT_NAME, inventoryRetrievalJob).getStatusCode()).isEqualTo(JobStatus.SUCCEEDED);
   }

   @Test(groups = {"live", "livelong", "longtest"}, dependsOnMethods = {"testWaitForSucceed"})
   public void testGetJobOutput() throws IOException {
      Closer closer = Closer.create();
      try {
         InputStream inputStream = closer.register(api.getJobOutput(VAULT_NAME, archiveRetrievalJob).openStream());
         InputStream expectedInputStream = closer.register(buildData(PART_SIZE * 2 * MiB).openStream());
         assertThat(inputStream).hasContentEqualTo(expectedInputStream);
      } finally {
         closer.close();
      }
   }
View Full Code Here

      inputFile = new File(args[0]);
    }

    Gson gson = Serializer.createGSon(DumpStreamsFromEventStream.class.getClassLoader());

    Closer closer = Closer.create();
    try {
      OutputStream sysout = new BufferedOutputStream(new FileOutputStream(new File(inputFile.getAbsolutePath() + ".sysout")));
      closer.register(sysout);

      OutputStream syserr = new BufferedOutputStream(new FileOutputStream(new File(inputFile.getAbsolutePath() + ".syserr")));
      closer.register(syserr);

      InputStream is = new BufferedInputStream(new FileInputStream(inputFile));
      closer.register(is);
      JsonReader input = new JsonReader(new InputStreamReader(is, Charsets.UTF_8));
      input.setLenient(true);

      JsonToken peek;
      while (true) {
        peek = input.peek();
       
        if (peek == JsonToken.END_DOCUMENT) {
          return;
        }
 
        input.beginArray();
        EventType type = EventType.valueOf(input.nextString());
        switch (type) {
          case APPEND_STDERR:
            ((IStreamEvent) gson.fromJson(input, type.eventClass)).copyTo(syserr);
            break;
 
          case APPEND_STDOUT:
            ((IStreamEvent) gson.fromJson(input, type.eventClass)).copyTo(sysout);
            break;
 
          default:
            input.skipValue();
        }
        input.endArray();
      }
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

      public void receiveQuit(QuitEvent e) {
        slave.end = System.currentTimeMillis();
      }     
    });

    Closer closer = Closer.create();
    closer.register(eventStream);
    closer.register(w);
    try {
      OutputStream sysout = closer.register(new BufferedOutputStream(new FileOutputStream(sysoutFile)));
      OutputStream syserr = closer.register(new BufferedOutputStream(new FileOutputStream(syserrFile)));
      RandomAccessFile streamsBuffer = closer.register(new RandomAccessFile(streamsBufferFile, "rw"));

      Execute execute = forkProcess(slave, eventBus, commandline, eventStream, sysout, syserr, streamsBuffer);
      log("Forked JVM J" + slave.id + " finished with exit code: " + execute.getExitValue(), Project.MSG_DEBUG);

      if (execute.isFailure()) {
        final int exitStatus = execute.getExitValue();
        switch (exitStatus) {
          case SlaveMain.ERR_NO_JUNIT:
            throw new BuildException("Forked JVM's classpath must include a junit4 JAR.");
          case SlaveMain.ERR_OLD_JUNIT:
            throw new BuildException("Forked JVM's classpath must use JUnit 4.10 or newer.");
          default:
            Closeables.close(sysout, false);
            Closeables.close(syserr, false);

            StringBuilder message = new StringBuilder();
            if (exitStatus == SlaveMain.ERR_OOM) {
              message.append("Forked JVM ran out of memory.");
            } else {
              message.append("Forked process returned with error code: ").append(exitStatus).append(".");
            }

            if (sysoutFile.length() > 0 || syserrFile.length() > 0) {
              if (exitStatus != SlaveMain.ERR_OOM) {
                message.append(" Very likely a JVM crash. ");
              }

              if (jvmOutputAction.contains(JvmOutputAction.PIPE)) {
                message.append(" Process output piped in logs above.");
              } else if (!jvmOutputAction.contains(JvmOutputAction.IGNORE)) {
                if (sysoutFile.length() > 0) {
                  message.append(" See process stdout at: " + sysoutFile.getAbsolutePath());
                }
                if (syserrFile.length() > 0) {
                  message.append(" See process stderr at: " + syserrFile.getAbsolutePath());
                }
              }
            }
            throw new BuildException(message.toString());
        }
      }
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      try {
        closer.close();
      } finally {
        Files.asByteSource(classNamesDynamic).copyTo(Files.asByteSink(classNamesFile, FileWriteMode.APPEND));
        classNamesDynamic.delete();
        streamsBufferFile.delete();
       
View Full Code Here

TOP

Related Classes of com.google.common.io.Closer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.