Example usage for com.google.common.io Closeables closeQuietly

List of usage examples for com.google.common.io Closeables closeQuietly

Introduction

In this page you can find the example usage for com.google.common.io Closeables closeQuietly.

Prototype

public static void closeQuietly(@Nullable Reader reader) 

Source Link

Document

Closes the given Reader , logging any IOException that's thrown rather than propagating it.

Usage

From source file:org.sonar.db.MyBatisConfBuilder.java

public void loadMapper(String mapperName) {
    String configFile = configFilePath(mapperName);
    InputStream input = null;//from  w  w  w  . jav a  2 s.  c om
    try {
        input = getClass().getResourceAsStream(configFile);
        checkArgument(input != null, format("Can not find mapper XML file %s", configFile));
        new XMLMapperBuilder(input, conf, mapperName, conf.getSqlFragments()).parse();
        loadAndConfigureLogger(mapperName);
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to load mapper " + mapperName, e);
    } finally {
        Closeables.closeQuietly(input);
    }
}

From source file:org.sonar.ant.SonarTaskUtils.java

/**
 * Returns the version of the Ant Task/*from   w  w  w  .j  av  a 2  s.co  m*/
 *
 * @return the version
 */
public static String getTaskVersion() {
    InputStream in = null;
    try {
        in = SonarTask.class.getResourceAsStream("/org/sonar/ant/version.txt");
        Properties props = new Properties();
        props.load(in);
        return props.getProperty("version");
    } catch (IOException e) {
        throw new BuildException("Could not load the version information for SonarQube Ant Task", e);
    } finally {
        Closeables.closeQuietly(in);
    }
}

From source file:com.github.zhongl.io.FileAppender.java

public File force() throws IOException {
    if (!thisChannel().isOpen())
        return file;
    try {/*from w ww .  j  a v  a 2s.  com*/
        if (batchBuffer.position() > 0)
            write();
        thisChannel().force(false);
        DirectByteBufferCleaner.clean(batchBuffer);
        return file;
    } finally {
        Closeables.closeQuietly(fileOutputStream);
    }
}

From source file:org.jclouds.elastichosts.handlers.ElasticHostsErrorHandler.java

public void handleError(HttpCommand command, HttpResponse response) {
    // it is important to always read fully and close streams
    String message = parseMessage(response);
    Exception exception = message != null ? new HttpResponseException(command, response, message)
            : new HttpResponseException(command, response);
    try {// w  w w.  j a v a2s .c  om
        message = message != null ? message
                : String.format("%s -> %s", command.getRequest().getRequestLine(), response.getStatusLine());
        switch (response.getStatusCode()) {
        case 400:
            exception = new IllegalArgumentException(message, exception);
            break;
        case 401:
            exception = new AuthorizationException(message, exception);
            break;
        case 404:
            if (!command.getRequest().getMethod().equals("DELETE")) {
                exception = new ResourceNotFoundException(message, exception);
            }
            break;
        case 405:
            exception = new IllegalArgumentException(message, exception);
            break;
        case 409:
            exception = new IllegalStateException(message, exception);
            break;
        }
    } finally {
        if (response.getPayload() != null)
            Closeables.closeQuietly(response.getPayload().getInput());
        command.setException(exception);
    }
}

From source file:org.sonar.sslr.channel.CodeBuffer.java

/**
 * Note that this constructor will read everything from reader and will close it.
 *//*from  ww w.  ja  v  a 2 s.com*/
protected CodeBuffer(Reader initialCodeReader, CodeReaderConfiguration configuration) {
    Reader reader = null;

    try {
        lastChar = -1;
        cursor = new Cursor();
        tabWidth = configuration.getTabWidth();

        /* Setup the filters on the reader */
        reader = initialCodeReader;
        for (CodeReaderFilter<?> codeReaderFilter : configuration.getCodeReaderFilters()) {
            reader = new Filter(reader, codeReaderFilter, configuration);
        }

        buffer = CharStreams.toString(reader).toCharArray();
    } catch (IOException e) {
        throw new ChannelException(e.getMessage(), e);
    } finally {
        Closeables.closeQuietly(reader);
    }
}

From source file:com.bazaarvoice.snitch.servlet.VariableServlet.java

@SuppressWarnings("unchecked")
@Override//from  w w  w.j  ava2 s . com
protected void doGet(HttpServletRequest req, HttpServletResponse response)
        throws ServletException, IOException {
    addClientNoCacheHeaders(response);
    response.setContentType("application/json");

    // Organize the variables into a multimap indexed by key
    Multimap<String, Variable> variables = HashMultimap.create();
    for (Variable variable : _snitch.getVariables()) {
        variables.put(variable.getName(), variable);
    }

    JsonWriter writer = new JsonWriter(new BufferedWriter(response.getWriter()));
    writer.setIndent("  "); // Pretty print by default

    try {
        writer.beginObject();
        for (String name : variables.keySet()) {
            Collection<Variable> vars = variables.get(name);

            writer.name(name);
            if (vars.size() > 1) {
                // Only render as an array if we have a name collision
                writer.beginArray();
            }
            for (Variable variable : vars) {
                Formatter formatter = _snitch.getFormatter(variable);
                formatter.format(variable.getValue(), writer);
            }
            if (vars.size() > 1) {
                writer.endArray();
            }
        }
        writer.endObject();
    } finally {
        Closeables.closeQuietly(writer);
    }
}

From source file:org.sonar.plugins.pmd.PmdExecutor.java

public Report execute() {
    TimeProfiler profiler = new TimeProfiler().start("Execute PMD " + PmdVersion.getVersion());

    ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
    URLClassLoader classLoader = createClassloader();
    try {//from  w  ww.java 2 s . c  om
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

        return executePmd(classLoader);
    } finally {
        Closeables.closeQuietly(classLoader);
        Thread.currentThread().setContextClassLoader(initialClassLoader);
        profiler.stop();
    }
}

From source file:com.backelite.sonarqube.objectivec.issues.ObjectiveCProfile.java

@Override
public RulesProfile createProfile(ValidationMessages messages) {

    LOGGER.info("Creating Objective-C Profile");

    Reader config = null;//from  w ww. j  av a 2 s . c om
    final RulesProfile profile = RulesProfile.create("Objective-C", ObjectiveC.KEY);
    profile.setDefaultProfile(true);

    try {
        config = new InputStreamReader(getClass().getResourceAsStream(OCLintProfile.PROFILE_PATH));
        RulesProfile ocLintRulesProfile = ocLintProfileImporter.importProfile(config, messages);
        for (ActiveRule rule : ocLintRulesProfile.getActiveRules()) {
            profile.addActiveRule(rule);
        }

        config = new InputStreamReader(getClass().getResourceAsStream(FauxPasProfile.PROFILE_PATH));
        RulesProfile fauxPasRulesProfile = fauxPasProfileImporter.importProfile(config, messages);
        for (ActiveRule rule : fauxPasRulesProfile.getActiveRules()) {
            profile.addActiveRule(rule);
        }

        return profile;
    } finally {

        Closeables.closeQuietly(config);
    }
}

From source file:org.sonar.plugins.pmd.PmdTemplate.java

public void process(File file, RuleSets rulesets, RuleContext ruleContext) {
    ruleContext.setSourceCodeFilename(file.getAbsolutePath());
    InputStream inputStream = null;
    try {//from  w  w w .ja va2s.  co m
        inputStream = new FileInputStream(file);
        processor.processSourceCode(inputStream, rulesets, ruleContext);
    } catch (Throwable e) {
        LOG.error("Fail to execute PMD. Following file is ignored: " + file, e);
    } finally {
        Closeables.closeQuietly(inputStream);
    }
}

From source file:com.netflix.exhibitor.servlet.ExhibitorServletContextListener.java

@Override
public void contextDestroyed(ServletContextEvent event) {
    if (exhibitor != null) {
        Closeables.closeQuietly(exhibitor);
        exhibitor = null;/*from ww  w  .  jav  a2  s.co m*/
    }

    if (exhibitorCreator != null) {
        for (Closeable closeable : exhibitorCreator.getCloseables()) {
            Closeables.closeQuietly(closeable);
        }
    }
}