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

org.freedesktop.dbus.utils.ReflectionFileDescriptorHelper Maven / Gradle / Ivy

Go to download

Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/). This is the OSGi compliant bundle of all required libraries in one bundle.

The newest version!
package org.freedesktop.dbus.utils;

import org.freedesktop.dbus.spi.message.ISocketProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;

/**
 * Helper to work with {@link FileDescriptor} instances by using reflection
 *
 * @author Sergey Shatunov
 * @since 5.0.0 - 2023-10-07
 */
public final class ReflectionFileDescriptorHelper {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionFileDescriptorHelper.class);
    private static final Optional INSTANCE = createInstance();

    private final Field fdField;
    private final Constructor constructor;

    private ReflectionFileDescriptorHelper() throws ReflectiveOperationException {
        fdField = FileDescriptor.class.getDeclaredField("fd");
        fdField.setAccessible(true);
        constructor = FileDescriptor.class.getDeclaredConstructor(int.class);
        constructor.setAccessible(true);
    }

    /**
     * @see ISocketProvider#getFileDescriptorValue(FileDescriptor)
     */
    public Optional getFileDescriptorValue(FileDescriptor _fd) {
        try {
            return Optional.of(fdField.getInt(_fd));
        } catch (SecurityException | IllegalArgumentException | IllegalAccessException _ex) {
            LOGGER.error("Could not get file descriptor by reflection.", _ex);
            return Optional.empty();
        }
    }

    /**
     * @see ISocketProvider#createFileDescriptor(int)
     */
    public Optional createFileDescriptor(int _fd) {
        try {
            return Optional.of(constructor.newInstance(_fd));
        } catch (SecurityException | InstantiationException | IllegalAccessException
                 | IllegalArgumentException | InvocationTargetException _ex) {
            LOGGER.error("Could not create new FileDescriptor instance by reflection.", _ex);
            return Optional.empty();
        }
    }

    private static Optional createInstance() {
        try {
            return Optional.of(new ReflectionFileDescriptorHelper());
        } catch (ReflectiveOperationException _ex) {
            LOGGER.error("Unable to hook up java.io.FileDescriptor by using reflection.", _ex);
            return Optional.empty();
        }
    }

    /**
     * @return {@link ReflectionFileDescriptorHelper} instance, or {@link Optional#empty()} if it cannot be initialized
     * (mainly due to missing reflection access)
     */
    public static Optional getInstance() {
        return INSTANCE;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy