<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.vfs2.impl;

import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;

import org.apache.commons.logging.Log;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSelector;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.provider.FileReplicator;
import org.apache.commons.vfs2.provider.VfsComponent;
import org.apache.commons.vfs2.provider.VfsComponentContext;

/**
 * A file replicator that wraps another file replicator, performing the replication as a privileged action.
 */
public class PrivilegedFileReplicator implements FileReplicator, VfsComponent {

    private final FileReplicator replicator;
    private final VfsComponent replicatorComponent;

    public PrivilegedFileReplicator(final FileReplicator replicator) {
        this.replicator = replicator;
        if (replicator instanceof VfsComponent) {
            replicatorComponent = (VfsComponent) replicator;
        } else {
            replicatorComponent = null;
        }
    }

    /**
     * Sets the Logger to use for the component.
     *
     * @param logger The logger.
     */
    @Override
    public void setLogger(final Log logger) {
        if (replicatorComponent != null) {
            replicatorComponent.setLogger(logger);
        }
    }

    /**
     * Sets the context for the replicator.
     *
     * @param context The component context.
     */
    @Override
    public void setContext(final VfsComponentContext context) {
        if (replicatorComponent != null) {
            replicatorComponent.setContext(context);
        }
    }

    /**
     * Initializes the component.
     *
     * @throws FileSystemException if an error occurs.
     */
    @Override
    public void init() throws FileSystemException {
        if (replicatorComponent != null) {
            try {
                AccessController.doPrivileged(new InitAction());
            } catch (final PrivilegedActionException e) {
                throw new FileSystemException("vfs.impl/init-replicator.error", e);
            }
        }
    }

    /**
     * Closes the replicator.
     */
    @Override
    public void close() {
        if (replicatorComponent != null) {
            AccessController.doPrivileged(new CloseAction());
        }
    }

    /**
     * Creates a local copy of the file, and all its descendants.
     *
     * @param srcFile The source FileObject.
     * @param selector The file selector.
     * @return The replicated file.
     * @throws FileSystemException if an error occurs.
     */
    @Override
    public File replicateFile(final FileObject srcFile, final FileSelector selector) throws FileSystemException {
        try {
            final ReplicateAction action = new ReplicateAction(srcFile, selector);
            return AccessController.doPrivileged(action);
        } catch (final PrivilegedActionException e) {
            throw new FileSystemException("vfs.impl/replicate-file.error", e, srcFile.getName());
        }
    }

    /**
     * An action that initialises the wrapped replicator.
     */
    private class InitAction implements PrivilegedExceptionAction<Object> {
        /**
         * Performs the action.
         */
        @Override
        public Object run() throws Exception {
            replicatorComponent.init();
            return null;
        }
    }<commit_msg>Normalize to US English spelling.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.vfs2.impl;

import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;

import org.apache.commons.logging.Log;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSelector;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.provider.FileReplicator;
import org.apache.commons.vfs2.provider.VfsComponent;
import org.apache.commons.vfs2.provider.VfsComponentContext;

/**
 * A file replicator that wraps another file replicator, performing the replication as a privileged action.
 */
public class PrivilegedFileReplicator implements FileReplicator, VfsComponent {

    private final FileReplicator replicator;
    private final VfsComponent replicatorComponent;

    public PrivilegedFileReplicator(final FileReplicator replicator) {
        this.replicator = replicator;
        if (replicator instanceof VfsComponent) {
            replicatorComponent = (VfsComponent) replicator;
        } else {
            replicatorComponent = null;
        }
    }

    /**
     * Sets the Logger to use for the component.
     *
     * @param logger The logger.
     */
    @Override
    public void setLogger(final Log logger) {
        if (replicatorComponent != null) {
            replicatorComponent.setLogger(logger);
        }
    }

    /**
     * Sets the context for the replicator.
     *
     * @param context The component context.
     */
    @Override
    public void setContext(final VfsComponentContext context) {
        if (replicatorComponent != null) {
            replicatorComponent.setContext(context);
        }
    }

    /**
     * Initializes the component.
     *
     * @throws FileSystemException if an error occurs.
     */
    @Override
    public void init() throws FileSystemException {
        if (replicatorComponent != null) {
            try {
                AccessController.doPrivileged(new InitAction());
            } catch (final PrivilegedActionException e) {
                throw new FileSystemException("vfs.impl/init-replicator.error", e);
            }
        }
    }

    /**
     * Closes the replicator.
     */
    @Override
    public void close() {
        if (replicatorComponent != null) {
            AccessController.doPrivileged(new CloseAction());
        }
    }

    /**
     * Creates a local copy of the file, and all its descendants.
     *
     * @param srcFile The source FileObject.
     * @param selector The file selector.
     * @return The replicated file.
     * @throws FileSystemException if an error occurs.
     */
    @Override
    public File replicateFile(final FileObject srcFile, final FileSelector selector) throws FileSystemException {
        try {
            final ReplicateAction action = new ReplicateAction(srcFile, selector);
            return AccessController.doPrivileged(action);
        } catch (final PrivilegedActionException e) {
            throw new FileSystemException("vfs.impl/replicate-file.error", e, srcFile.getName());
        }
    }

    /**
     * An action that initializes the wrapped replicator.
     */
    private class InitAction implements PrivilegedExceptionAction<Object> {
        /**
         * Performs the action.
         */
        @Override
        public Object run() throws Exception {
            replicatorComponent.init();
            return null;
        }
    }
{'fraction_non_alphanumeric': 0.07875373649114739, 'fraction_numerical': 0.0025293170843872156, 'mean_word_length': 2.708013640238704, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7491585', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2226, 'n_words': 708}