All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.lang.ShutdownHookJava7 Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2011-2021 Objectos Software LTDA.
 *
 * This file is part of the ObjectosComuns project.
 *
 * ObjectosComuns is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * ObjectosComuns is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ObjectosComuns.  If not, see .
 */
package br.com.objectos.lang;

import static br.com.objectos.lang.Lang.checkNotNull;

import br.com.objectos.multirelease.Concrete;
import java.util.ArrayList;
import java.util.List;

@Concrete.Bridge
class ShutdownHookJava7 extends AbstractShutdownHook {

  private List closeables;

  @Concrete.Constructor
  ShutdownHookJava7() {}

  public final void addCloseable(AutoCloseable closeable) {
    checkNotNull(closeable, "closeable == null");

    if (closeables == null) {
      synchronized (this) {
        if (closeables == null) {
          closeables = new ArrayList<>();
        }
      }
    }

    synchronized (closeables) {
      closeables.add(closeable);
    }
  }

  @Override
  final void run0() {
    if (closeables != null) {
      doCloseables();
    }
  }

  private void doCloseables() {
    for (int i = 0; i < closeables.size(); i++) {
      AutoCloseable c;
      c = closeables.get(i);

      try {
        c.close();
      } catch (Exception e) {
        log(e);
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy