-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hypfvieh:master' into master
- Loading branch information
Showing
21 changed files
with
464 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 67 additions & 33 deletions
100
dbus-java-core/src/main/java/org/freedesktop/dbus/FileDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,94 @@ | ||
package org.freedesktop.dbus; | ||
|
||
import org.freedesktop.dbus.exceptions.MarshallingException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.freedesktop.dbus.spi.message.ISocketProvider; | ||
import org.freedesktop.dbus.utils.ReflectionFileDescriptorHelper; | ||
|
||
import java.lang.reflect.*; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Represents a FileDescriptor to be passed over the bus. Can be created from | ||
* either an integer(gotten through some JNI/JNA/JNR call) or from a | ||
* java.io.FileDescriptor. | ||
* | ||
* Represents a FileDescriptor to be passed over the bus. <br> | ||
* Can be created from either an integer (gotten through some JNI/JNA/JNR call) or from a | ||
* {@link java.io.FileDescriptor}. | ||
*/ | ||
public class FileDescriptor { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
public final class FileDescriptor { | ||
|
||
private final int fd; | ||
|
||
public FileDescriptor(int _fd) { | ||
fd = _fd; | ||
} | ||
|
||
public FileDescriptor(java.io.FileDescriptor _data) throws MarshallingException { | ||
fd = getFileDescriptor(_data); | ||
} | ||
/** | ||
* Converts this DBus {@link FileDescriptor} to a {@link java.io.FileDescriptor}.<br> | ||
* Tries to use the provided ISocketProvider if present first. <br> | ||
* If not present or conversion failed, tries to convert using reflection. | ||
* | ||
* @param _provider provider or null | ||
* | ||
* @return java file descriptor | ||
* @throws MarshallingException when converting fails | ||
*/ | ||
public java.io.FileDescriptor toJavaFileDescriptor(ISocketProvider _provider) throws MarshallingException { | ||
if (_provider != null) { | ||
Optional<java.io.FileDescriptor> result = _provider.createFileDescriptor(fd); | ||
if (result.isPresent()) { | ||
return result.get(); | ||
} | ||
} | ||
|
||
public java.io.FileDescriptor toJavaFileDescriptor() throws MarshallingException { | ||
return createFileDescriptorByReflection(fd); | ||
return ReflectionFileDescriptorHelper.getInstance() | ||
.flatMap(helper -> helper.createFileDescriptor(fd)) | ||
.orElseThrow(() -> new MarshallingException("Could not create new FileDescriptor instance")); | ||
} | ||
|
||
public int getIntFileDescriptor() { | ||
return fd; | ||
} | ||
|
||
private int getFileDescriptor(java.io.FileDescriptor _data) throws MarshallingException { | ||
Field declaredField; | ||
try { | ||
declaredField = _data.getClass().getDeclaredField("fd"); | ||
declaredField.setAccessible(true); | ||
return declaredField.getInt(_data); | ||
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException _ex) { | ||
logger.error("Could not get filedescriptor by reflection.", _ex); | ||
throw new MarshallingException("Could not get member 'fd' of FileDescriptor by reflection!", _ex); | ||
@Override | ||
public boolean equals(Object _o) { | ||
if (this == _o) { | ||
return true; | ||
} | ||
if (_o == null || getClass() != _o.getClass()) { | ||
return false; | ||
} | ||
FileDescriptor that = (FileDescriptor) _o; | ||
return fd == that.fd; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return fd; | ||
} | ||
|
||
private java.io.FileDescriptor createFileDescriptorByReflection(long _demarshallint) throws MarshallingException { | ||
try { | ||
Constructor<java.io.FileDescriptor> constructor = java.io.FileDescriptor.class.getDeclaredConstructor(int.class); | ||
constructor.setAccessible(true); | ||
return constructor.newInstance((int) _demarshallint); | ||
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | ||
| IllegalArgumentException | InvocationTargetException _ex) { | ||
logger.error("Could not create new FileDescriptor instance by reflection.", _ex); | ||
throw new MarshallingException("Could not create new FileDescriptor instance by reflection", _ex); | ||
@Override | ||
public String toString() { | ||
return FileDescriptor.class.getSimpleName() + "[fd=" + fd + "]"; | ||
} | ||
|
||
/** | ||
* Utility method to create a DBus {@link FileDescriptor} from a {@link java.io.FileDescriptor}.<br> | ||
* Tries to use the provided ISocketProvider if present first.<br> | ||
* If not present or conversion failed, tries to convert using reflection. | ||
* | ||
* @param _data file descriptor | ||
* @param _provider socket provider or null | ||
* | ||
* @return DBus FileDescriptor | ||
* @throws MarshallingException when conversion fails | ||
*/ | ||
public static FileDescriptor fromJavaFileDescriptor(java.io.FileDescriptor _data, ISocketProvider _provider) throws MarshallingException { | ||
if (_provider != null) { | ||
Optional<Integer> result = _provider.getFileDescriptorValue(_data); | ||
if (result.isPresent()) { | ||
return new FileDescriptor(result.get()); | ||
} | ||
} | ||
|
||
return new FileDescriptor(ReflectionFileDescriptorHelper.getInstance() | ||
.flatMap(helper -> helper.getFileDescriptorValue(_data)) | ||
.orElseThrow(() -> new MarshallingException("Could not get FileDescriptor value"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.