Java 类org.apache.logging.log4j.core.util.FileUtils 实例源码

项目:gatk    文件:IOUtilsUnitTest.java   
@Test
public void testDeleteDirOnExit() throws IOException {
    //This just tests that the code runs without crashing.
    //It runs at jvm shutdown so there isn't a good way to test it properly.
    //If you see a directory in the hellbender main folder called

    final File dir = new File(GATKBaseTest.publicTestDir + "I_SHOULD_HAVE_BEEN_DELETED");
    IOUtils.deleteRecursivelyOnExit(dir);

    FileUtils.mkdir(dir, true);
    final File subdir = new File(dir, "subdir");
    FileUtils.mkdir(subdir, true);
    File someFile = new File(dir, "someFile");
    someFile.createNewFile();
    File anotherFile = new File(subdir, "anotherFile");
    anotherFile.createNewFile();
}
项目:logging-log4j2    文件:StatusConfiguration.java   
private PrintStream parseStreamName(final String name) throws URISyntaxException, FileNotFoundException {
    if (name == null || name.equalsIgnoreCase("out")) {
        return DEFAULT_STREAM;
    }
    if (name.equalsIgnoreCase("err")) {
        return System.err;
    }
    final URI destUri = NetUtils.toURI(name);
    final File output = FileUtils.fileFromUri(destUri);
    if (output == null) {
        // don't want any NPEs, no sir
        return DEFAULT_STREAM;
    }
    final FileOutputStream fos = new FileOutputStream(output);
    return new PrintStream(fos, true);
}
项目:logging-log4j2    文件:ConfigurationFactory.java   
/**
 * Loads the configuration from the location represented by the String.
 * @param config The configuration location.
 * @param loader The default ClassLoader to use.
 * @return The InputSource to use to read the configuration.
 */
protected ConfigurationSource getInputFromString(final String config, final ClassLoader loader) {
    try {
        final URL url = new URL(config);
        return new ConfigurationSource(url.openStream(), FileUtils.fileFromUri(url.toURI()));
    } catch (final Exception ex) {
        final ConfigurationSource source = ConfigurationSource.fromResource(config, loader);
        if (source == null) {
            try {
                final File file = new File(config);
                return new ConfigurationSource(new FileInputStream(file), file);
            } catch (final FileNotFoundException fnfe) {
                // Ignore the exception
                LOGGER.catching(Level.DEBUG, fnfe);
            }
        }
        return source;
    }
}
项目:logging-log4j2    文件:FileManager.java   
/**
 * Creates a FileManager.
 * @param name The name of the File.
 * @param data The FactoryData
 * @return The FileManager for the File.
 */
@Override
public FileManager createManager(final String name, final FactoryData data) {
    final File file = new File(name);
    try {
        FileUtils.makeParentDirs(file);
        final boolean writeHeader = !data.append || !file.exists();
        final int actualSize = data.bufferedIo ? data.bufferSize : Constants.ENCODER_BYTE_BUFFER_SIZE;
        final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[actualSize]);
        final FileOutputStream fos = data.createOnDemand ? null : new FileOutputStream(file, data.append);
        final FileManager fm = new FileManager(data.getLoggerContext(), name, fos, data.append, data.locking,
                data.createOnDemand, data.advertiseURI, data.layout,
                data.filePermissions, data.fileOwner, data.fileGroup, writeHeader, byteBuffer);
        if (fos != null && fm.attributeViewEnabled) {
            fm.defineAttributeView(file.toPath());
        }
        return fm;
    } catch (final IOException ex) {
        LOGGER.error("FileManager (" + name + ") " + ex, ex);
    }
    return null;
}
项目:logging-log4j2    文件:RollingRandomAccessFileManager.java   
@Override
protected synchronized void writeToDestination(final byte[] bytes, final int offset, final int length) {
    try {
        if (randomAccessFile == null) {
            String fileName = getFileName();
            File file = new File(fileName);
            FileUtils.makeParentDirs(file);
            createFileAfterRollover(fileName);
        }
        randomAccessFile.write(bytes, offset, length);
        size += length;
    } catch (final IOException ex) {
        final String msg = "Error writing to RandomAccessFile " + getName();
        throw new AppenderLoggingException(msg, ex);
    }
}
项目:logging-log4j2    文件:PosixViewAttributeAction.java   
@Override
public PosixViewAttributeAction build() {
    if (Strings.isEmpty(basePath)) {
        LOGGER.error("Posix file attribute view action not valid because base path is empty.");
        return null;
    }

    if (filePermissions == null && Strings.isEmpty(filePermissionsString)
                && Strings.isEmpty(fileOwner) && Strings.isEmpty(fileGroup)) {
        LOGGER.error("Posix file attribute view not valid because nor permissions, user or group defined.");
        return null;
    }

    if (!FileUtils.isFilePosixAttributeViewSupported()) {
        LOGGER.warn("Posix file attribute view defined but it is not supported by this files system.");
        return null;
    }

    return new PosixViewAttributeAction(basePath, followLinks, maxDepth, pathConditions,
            subst != null ? subst : configuration.getStrSubstitutor(),
            filePermissions != null ? filePermissions :
                        filePermissionsString != null ? PosixFilePermissions.fromString(filePermissionsString) : null,
            fileOwner,
            fileGroup);
}
项目:logging-log4j2    文件:PosixViewAttributeAction.java   
@Override
protected FileVisitor<Path> createFileVisitor(final Path basePath,
        final List<PathCondition> conditions) {
    return new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            for (final PathCondition pathFilter : conditions) {
                final Path relative = basePath.relativize(file);
                if (!pathFilter.accept(basePath, relative, attrs)) {
                    LOGGER.trace("Not defining posix attribute base={}, relative={}", basePath, relative);
                    return FileVisitResult.CONTINUE;
                }
            }
            FileUtils.defineFilePosixAttributeView(file, filePermissions, fileOwner, fileGroup);
            return FileVisitResult.CONTINUE;
        }
    };
}
项目:logging-log4j2    文件:MemoryMappedFileManager.java   
/**
 * Create a MemoryMappedFileManager.
 *
 * @param name The name of the File.
 * @param data The FactoryData
 * @return The MemoryMappedFileManager for the File.
 */
@SuppressWarnings("resource")
@Override
public MemoryMappedFileManager createManager(final String name, final FactoryData data) {
    final File file = new File(name);
    if (!data.append) {
        file.delete();
    }

    final boolean writeHeader = !data.append || !file.exists();
    final OutputStream os = NullOutputStream.getInstance();
    RandomAccessFile raf = null;
    try {
        FileUtils.makeParentDirs(file);
        raf = new RandomAccessFile(name, "rw");
        final long position = (data.append) ? raf.length() : 0;
        raf.setLength(position + data.regionLength);
        return new MemoryMappedFileManager(raf, name, os, data.immediateFlush, position, data.regionLength,
                data.advertiseURI, data.layout, writeHeader);
    } catch (final Exception ex) {
        LOGGER.error("MemoryMappedFileManager (" + name + ") " + ex, ex);
        Closer.closeSilently(raf);
    }
    return null;
}
项目:logging-log4j2    文件:RandomAccessFileManager.java   
/**
 * Create a RandomAccessFileManager.
 *
 * @param name The name of the File.
 * @param data The FactoryData
 * @return The RandomAccessFileManager for the File.
 */
@Override
public RandomAccessFileManager createManager(final String name, final FactoryData data) {
    final File file = new File(name);
    if (!data.append) {
        file.delete();
    }

    final boolean writeHeader = !data.append || !file.exists();
    final OutputStream os = NullOutputStream.getInstance();
    RandomAccessFile raf;
    try {
        FileUtils.makeParentDirs(file);
        raf = new RandomAccessFile(name, "rw");
        if (data.append) {
            raf.seek(raf.length());
        } else {
            raf.setLength(0);
        }
        return new RandomAccessFileManager(data.getLoggerContext(), raf, name,
                os, data.bufferSize, data.advertiseURI, data.layout, writeHeader);
    } catch (final Exception ex) {
        LOGGER.error("RandomAccessFileManager (" + name + ") " + ex, ex);
    }
    return null;
}
项目:logging-log4j2    文件:ConfigurationSource.java   
/**
 * Retrieves the configuration via the ClassLoader.
 * @param resource The resource to load.
 * @param loader The default ClassLoader to use.
 * @return The ConfigurationSource for the configuration.
 */
public static ConfigurationSource fromResource(final String resource, final ClassLoader loader) {
    final URL url = Loader.getResource(resource, loader);
    if (url == null) {
        return null;
    }
    InputStream is = null;
    try {
        is = url.openStream();
    } catch (final IOException ioe) {
        ConfigurationFactory.LOGGER.catching(Level.DEBUG, ioe);
        return null;
    }
    if (is == null) {
        return null;
    }

    if (FileUtils.isFile(url)) {
        try {
            return new ConfigurationSource(is, FileUtils.fileFromUri(url.toURI()));
        } catch (final URISyntaxException ex) {
            // Just ignore the exception.
            ConfigurationFactory.LOGGER.catching(Level.DEBUG, ex);
        }
    }
    return new ConfigurationSource(is, url);
}
项目:logging-log4j2    文件:FileManager.java   
protected void defineAttributeView(final Path path) {
    if (attributeViewEnabled) {
        try {
            // FileOutputStream may not create new file on all jvm
            path.toFile().createNewFile();

            FileUtils.defineFilePosixAttributeView(path, filePermissions, fileOwner, fileGroup);
        } catch (final Exception e) {
            LOGGER.error("Could not define attribute view on path \"{}\" got {}", path, e.getMessage(), e);
        }
    }
}
项目:logging-log4j2    文件:AsyncRootReloadTest.java   
@Test
public void testLog4j2_807() throws InterruptedException, URISyntaxException {
    final URL url = AsyncRootReloadTest.class.getResource("/" + ISSUE_CONFIG);
    final File configFile = FileUtils.fileFromUri(url.toURI());

    final Logger logger = LogManager.getLogger(AsyncRootReloadTest.class);
    logger.info("Log4j configured, will be reconfigured in aprox. 5 sec");

    configFile.setLastModified(System.currentTimeMillis());

    for (int i = 0; i < 10; i++) {
        Thread.sleep(1000);
        logger.info("Log4j waiting for reconfiguration");
    }
}
项目:logging-log4j2    文件:ScriptFile.java   
@PluginFactory
public static ScriptFile createScript(
        // @formatter:off
        @PluginAttribute("name") String name,
        @PluginAttribute("language") String language,
        @PluginAttribute("path") final String filePathOrUri,
        @PluginAttribute("isWatched") final Boolean isWatched,
        @PluginAttribute("charset") final Charset charset) {
        // @formatter:on
    if (filePathOrUri == null) {
        LOGGER.error("No script path provided for ScriptFile");
        return null;
    }
    if (name == null) {
        name = filePathOrUri;
    }
    final URI uri = NetUtils.toURI(filePathOrUri);
    final File file = FileUtils.fileFromUri(uri);
    if (language == null && file != null) {
        final String fileExtension = FileUtils.getFileExtension(file);
        if (fileExtension != null) {
            final ExtensionLanguageMapping mapping = ExtensionLanguageMapping.getByExtension(fileExtension);
            if (mapping != null) {
                language = mapping.getLanguage();
            }
        }
    }
    if (language == null) {
        LOGGER.info("No script language supplied, defaulting to {}", DEFAULT_LANGUAGE);
        language = DEFAULT_LANGUAGE;
    }

    final Charset actualCharset = charset == null ? Charset.defaultCharset() : charset;
    String scriptText;
    try (final Reader reader = new InputStreamReader(
            file != null ? new FileInputStream(file) : uri.toURL().openStream(), actualCharset)) {
        scriptText = IOUtils.toString(reader);
    } catch (final IOException e) {
        LOGGER.error("{}: language={}, path={}, actualCharset={}", e.getClass().getSimpleName(),
                language, filePathOrUri, actualCharset);
        return null;
    }
    final Path path = file != null ? Paths.get(file.toURI()) : Paths.get(uri);
    if (path == null) {
        LOGGER.error("Unable to convert {} to a Path", uri.toString());
        return null;
    }
    return new ScriptFile(name, path, language, isWatched == null ? Boolean.FALSE : isWatched, scriptText);
}
项目:logging-log4j2    文件:RollingFileManager.java   
/**
 * Creates a RollingFileManager.
 * @param name The name of the entity to manage.
 * @param data The data required to create the entity.
 * @return a RollingFileManager.
 */
@Override
public RollingFileManager createManager(final String name, final FactoryData data) {
    long size = 0;
    boolean writeHeader = !data.append;
    File file = null;
    if (data.fileName != null) {
        file = new File(data.fileName);
        // LOG4J2-1140: check writeHeader before creating the file
        writeHeader = !data.append || !file.exists();

        try {
            FileUtils.makeParentDirs(file);
            final boolean created = data.createOnDemand ? false : file.createNewFile();
            LOGGER.trace("New file '{}' created = {}", name, created);
        } catch (final IOException ioe) {
            LOGGER.error("Unable to create file " + name, ioe);
            return null;
        }
        size = data.append ? file.length() : 0;
    }

    try {
        final int actualSize = data.bufferedIO ? data.bufferSize : Constants.ENCODER_BYTE_BUFFER_SIZE;
        final ByteBuffer buffer = ByteBuffer.wrap(new byte[actualSize]);
        final OutputStream os = data.createOnDemand  || data.fileName == null ? null :
                new FileOutputStream(data.fileName, data.append);
        final long time = data.createOnDemand || file == null ?
                System.currentTimeMillis() : file.lastModified(); // LOG4J2-531 create file first so time has valid value

        final RollingFileManager rm = new RollingFileManager(data.getLoggerContext(), data.fileName, data.pattern, os,
            data.append, data.createOnDemand, size, time, data.policy, data.strategy, data.advertiseURI,
            data.layout, data.filePermissions, data.fileOwner, data.fileGroup, writeHeader, buffer);
        if (os != null && rm.isAttributeViewEnabled()) {
            rm.defineAttributeView(file.toPath());
        }

        return rm;
    } catch (final IOException ex) {
        LOGGER.error("RollingFileManager (" + name + ") " + ex, ex);
    }
    return null;
}
项目:logging-log4j2    文件:FileAppenderPermissionsXmlConfigTest.java   
@BeforeClass
public static void beforeClass() {
    Assume.assumeTrue(FileUtils.isFilePosixAttributeViewSupported());
}
项目:logging-log4j2    文件:RollingAppenderSizeCompressPermissionsTest.java   
@BeforeClass
public static void beforeClass() {
    Assume.assumeTrue(FileUtils.isFilePosixAttributeViewSupported());
}
项目:logging-log4j2    文件:FileAppenderPermissionsTest.java   
@BeforeClass
public static void beforeClass() {
    System.setProperty("log4j2.debug", "true");
    Assume.assumeTrue(FileUtils.isFilePosixAttributeViewSupported());
}