- android/ 167个模块 (app/content/os/view/widget/graphics/media/hardware/net/telephony/provider等) - java/ 35个模块 (lang/io/util/net/nio/security/crypto/sql/beans/rmi/awt等) - javax/ 4个模块 (net/crypto/security/sql) - org/ 5个模块 (json/w3c_dom/xml_sax/xml_pull/apache_http) - dalvik/ 1个模块 (system) - ~3053个类/接口,方法签名完全对齐Android SDK - 桌面跨平台运行 (Windows/macOS/Linux) - 修复所有导入错误和Python关键字冲突
776 lines
18 KiB
Python
776 lines
18 KiB
Python
"""
|
|
Java java.rmi standard library APIs implemented in Python.
|
|
java.rmi package - Remote, Registry, Activation, etc.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from .._core import Stub, AndroidOnlyError
|
|
from .lang import RuntimeException, Exception as _JavaException
|
|
import typing as _typing
|
|
|
|
__all__ = [
|
|
"Remote", "RemoteException", "RMISecurityManager", "RMISecurityException",
|
|
"MarshalledObject", "Naming", "NamingException",
|
|
"NotBoundException", "AlreadyBoundException",
|
|
"ConnectException", "ConnectIOException", "ServerException",
|
|
"ServerError", "ServerRuntimeException", "StubNotFoundException",
|
|
"UnexpectedException", "UnmarshalException", "UnknownHostException",
|
|
"NoSuchObjectException", "AccessException", "MarshalException",
|
|
"ExportException", "SocketSecurityException",
|
|
"Registry", "RegistryHandler",
|
|
"ActivationException", "UnknownObjectException",
|
|
"Activatable", "ActivationDesc", "ActivationGroup",
|
|
"ActivationGroupDesc", "ActivationGroupID", "ActivationID",
|
|
"ActivationMonitor", "ActivationSystem", "Activator",
|
|
"RMIClassLoader", "RMISocketFactory", "RMIFailureHandler",
|
|
"LogStream", "RemoteCall", "RemoteRef", "ServerRef", "Operation",
|
|
"RemoteObject", "RemoteServer", "UnicastRemoteObject", "Unreferenced",
|
|
]
|
|
|
|
|
|
# ============================================================
|
|
# Remote interface
|
|
# ============================================================
|
|
|
|
class Remote(Stub):
|
|
"""java.rmi.Remote"""
|
|
pass
|
|
|
|
|
|
# ============================================================
|
|
# Exception hierarchy
|
|
# ============================================================
|
|
|
|
class RemoteException(_JavaException):
|
|
"""java.rmi.RemoteException"""
|
|
def __init__(self, message=None, cause=None):
|
|
msg = message or ""
|
|
super().__init__(msg)
|
|
self._detail = message
|
|
self._cause = cause
|
|
|
|
def getMessage(self):
|
|
return self._detail or ""
|
|
|
|
def getCause(self):
|
|
return self._cause
|
|
|
|
|
|
class NamingException(RemoteException):
|
|
"""java.rmi.NamingException"""
|
|
pass
|
|
|
|
|
|
class NotBoundException(RemoteException):
|
|
"""java.rmi.NotBoundException"""
|
|
pass
|
|
|
|
|
|
class AlreadyBoundException(RemoteException):
|
|
"""java.rmi.AlreadyBoundException"""
|
|
pass
|
|
|
|
|
|
class ConnectException(RemoteException):
|
|
"""java.rmi.ConnectException"""
|
|
pass
|
|
|
|
|
|
class ConnectIOException(RemoteException):
|
|
"""java.rmi.ConnectIOException"""
|
|
pass
|
|
|
|
|
|
class ServerException(RemoteException):
|
|
"""java.rmi.ServerException"""
|
|
pass
|
|
|
|
|
|
class ServerError(RemoteException):
|
|
"""java.rmi.ServerError"""
|
|
pass
|
|
|
|
|
|
class ServerRuntimeException(RemoteException):
|
|
"""java.rmi.ServerRuntimeException"""
|
|
pass
|
|
|
|
|
|
class StubNotFoundException(RemoteException):
|
|
"""java.rmi.StubNotFoundException"""
|
|
pass
|
|
|
|
|
|
class UnexpectedException(RemoteException):
|
|
"""java.rmi.UnexpectedException"""
|
|
pass
|
|
|
|
|
|
class UnmarshalException(RemoteException):
|
|
"""java.rmi.UnmarshalException"""
|
|
pass
|
|
|
|
|
|
class UnknownHostException(RemoteException):
|
|
"""java.rmi.UnknownHostException"""
|
|
pass
|
|
|
|
|
|
class NoSuchObjectException(RemoteException):
|
|
"""java.rmi.NoSuchObjectException"""
|
|
pass
|
|
|
|
|
|
class AccessException(RemoteException):
|
|
"""java.rmi.AccessException"""
|
|
pass
|
|
|
|
|
|
class MarshalException(RemoteException):
|
|
"""java.rmi.MarshalException"""
|
|
pass
|
|
|
|
|
|
class ExportException(RemoteException):
|
|
"""java.rmi.ExportException"""
|
|
pass
|
|
|
|
|
|
class SocketSecurityException(ExportException):
|
|
"""java.rmi.SocketSecurityException"""
|
|
pass
|
|
|
|
|
|
# java.lang.SecurityException (桌面模拟)
|
|
class SecurityException(RuntimeException):
|
|
def __init__(self, message=""):
|
|
super().__init__(message)
|
|
|
|
|
|
class RMISecurityException(SecurityException):
|
|
"""java.rmi.RMISecurityException"""
|
|
pass
|
|
|
|
|
|
# ============================================================
|
|
# RMISecurityManager
|
|
# ============================================================
|
|
|
|
class RMISecurityManager(Stub):
|
|
"""java.rmi.RMISecurityManager"""
|
|
def checkConnect(self, host, port):
|
|
pass
|
|
|
|
def checkAccept(self, host, port):
|
|
pass
|
|
|
|
def checkListen(self, port):
|
|
pass
|
|
|
|
|
|
# ============================================================
|
|
# MarshalledObject
|
|
# ============================================================
|
|
|
|
class MarshalledObject(Stub):
|
|
"""java.rmi.MarshalledObject"""
|
|
def __init__(self, obj):
|
|
super().__init__()
|
|
self._obj = obj
|
|
import pickle
|
|
self._bytes = pickle.dumps(obj)
|
|
|
|
def get(self):
|
|
import pickle
|
|
try:
|
|
return pickle.loads(self._bytes)
|
|
except Exception:
|
|
return self._obj
|
|
|
|
def hashCode(self):
|
|
return hash(self._bytes)
|
|
|
|
def equals(self, obj):
|
|
if not isinstance(obj, MarshalledObject):
|
|
return False
|
|
return self._bytes == obj._bytes
|
|
|
|
|
|
# ============================================================
|
|
# Naming
|
|
# ============================================================
|
|
|
|
class Naming(Stub):
|
|
"""java.rmi.Naming"""
|
|
_registry = {}
|
|
|
|
@staticmethod
|
|
def lookup(name):
|
|
if name in Naming._registry:
|
|
return Naming._registry[name]
|
|
raise NotBoundException(name)
|
|
|
|
@staticmethod
|
|
def bind(name, obj):
|
|
if name in Naming._registry:
|
|
raise AlreadyBoundException(name)
|
|
Naming._registry[name] = obj
|
|
|
|
@staticmethod
|
|
def unbind(name):
|
|
if name not in Naming._registry:
|
|
raise NotBoundException(name)
|
|
del Naming._registry[name]
|
|
|
|
@staticmethod
|
|
def rebind(name, obj):
|
|
Naming._registry[name] = obj
|
|
|
|
@staticmethod
|
|
def list(name):
|
|
return list(Naming._registry.keys())
|
|
|
|
|
|
# ============================================================
|
|
# Registry
|
|
# ============================================================
|
|
|
|
class Registry(Stub):
|
|
"""java.rmi.registry.Registry"""
|
|
def lookup(self, name):
|
|
raise NotBoundException(name)
|
|
|
|
def bind(self, name, obj):
|
|
raise AlreadyBoundException(name)
|
|
|
|
def unbind(self, name):
|
|
raise NotBoundException(name)
|
|
|
|
def rebind(self, name, obj):
|
|
pass
|
|
|
|
def list(self):
|
|
return []
|
|
|
|
REGISTRY_PORT = 1099
|
|
|
|
|
|
class RegistryHandler(Stub):
|
|
"""java.rmi.registry.RegistryHandler"""
|
|
def registryStub(self, host, port):
|
|
return Registry()
|
|
|
|
def registryImpl(self, port):
|
|
return Registry()
|
|
|
|
|
|
# ============================================================
|
|
# Activation Exceptions
|
|
# ============================================================
|
|
|
|
class ActivationException(RemoteException):
|
|
"""java.rmi.activation.ActivationException"""
|
|
pass
|
|
|
|
|
|
class UnknownObjectException(ActivationException):
|
|
"""java.rmi.activation.UnknownObjectException"""
|
|
pass
|
|
|
|
|
|
# ============================================================
|
|
# ActivationID
|
|
# ============================================================
|
|
|
|
class ActivationID(Stub):
|
|
"""java.rmi.activation.ActivationID"""
|
|
def __init__(self, activator=None):
|
|
super().__init__()
|
|
self._activator = activator
|
|
|
|
def activate(self, force):
|
|
if self._activator:
|
|
return self._activator.activate(self, force)
|
|
return None
|
|
|
|
def hashCode(self):
|
|
return hash(id(self))
|
|
|
|
def equals(self, obj):
|
|
return self is obj
|
|
|
|
|
|
# ============================================================
|
|
# ActivationDesc
|
|
# ============================================================
|
|
|
|
class ActivationDesc(Stub):
|
|
"""java.rmi.activation.ActivationDesc"""
|
|
def __init__(self, class_name=None, location=None, marshalled_data=None,
|
|
group_id=None, restart_mode=False):
|
|
super().__init__()
|
|
self._class_name = class_name
|
|
self._location = location
|
|
self._marshalled_data = marshalled_data
|
|
self._group_id = group_id
|
|
self._restart_mode = restart_mode
|
|
|
|
def getClassName(self):
|
|
return self._class_name
|
|
|
|
def getLocation(self):
|
|
return self._location
|
|
|
|
def getData(self):
|
|
return self._marshalled_data
|
|
|
|
def getGroupID(self):
|
|
return self._group_id
|
|
|
|
def getRestartMode(self):
|
|
return self._restart_mode
|
|
|
|
|
|
# ============================================================
|
|
# Activatable
|
|
# ============================================================
|
|
|
|
class Activatable(Remote):
|
|
"""java.rmi.activation.Activatable"""
|
|
def __init__(self, activation_desc, port, obj_id=None, server_port=0,
|
|
csf=None, ssf=None):
|
|
super().__init__()
|
|
self._activation_desc = activation_desc
|
|
self._port = port
|
|
self._group_id = ActivationGroupID()
|
|
self._id = ActivationID()
|
|
|
|
@staticmethod
|
|
def register(desc):
|
|
return ActivationID()
|
|
|
|
@staticmethod
|
|
def inactive(id_):
|
|
return True
|
|
|
|
@staticmethod
|
|
def unregister(id_):
|
|
pass
|
|
|
|
@staticmethod
|
|
def exportObject(obj, desc, port):
|
|
return Remote()
|
|
|
|
def getID(self):
|
|
return self._id
|
|
|
|
|
|
# ============================================================
|
|
# ActivationGroup / ActivationGroupDesc / ActivationGroupID
|
|
# ============================================================
|
|
|
|
class ActivationGroupID(Stub):
|
|
"""java.rmi.activation.ActivationGroupID"""
|
|
def __init__(self, activation_system=None):
|
|
super().__init__()
|
|
self._activation_system = activation_system
|
|
|
|
def getSystem(self):
|
|
return self._activation_system
|
|
|
|
def hashCode(self):
|
|
return hash(id(self))
|
|
|
|
|
|
class ActivationGroupDesc(Stub):
|
|
"""java.rmi.activation.ActivationGroupDesc"""
|
|
class CommandEnvironment(Stub):
|
|
def __init__(self, cmd=None, props=None):
|
|
super().__init__()
|
|
self._command = cmd
|
|
self._properties = props or []
|
|
|
|
def getCommandPath(self):
|
|
return self._command
|
|
|
|
def getCommandProperties(self):
|
|
return self._properties
|
|
|
|
def __init__(self, props=None, env=None, class_name=None,
|
|
location=None, info=None):
|
|
super().__init__()
|
|
self._props = props or {}
|
|
self._env = env or ActivationGroupDesc.CommandEnvironment()
|
|
self._class_name = class_name
|
|
self._location = location
|
|
self._info = info
|
|
|
|
def getPropertyOverrides(self):
|
|
return self._props
|
|
|
|
def getCommandEnvironment(self):
|
|
return self._env
|
|
|
|
def getClassName(self):
|
|
return self._class_name
|
|
|
|
def getLocation(self):
|
|
return self._location
|
|
|
|
def getData(self):
|
|
return self._info
|
|
|
|
|
|
class ActivationGroup(Activatable):
|
|
"""java.rmi.activation.ActivationGroup"""
|
|
def __init__(self, group_id, desc, incarnation):
|
|
super().__init__(None, 0)
|
|
self._group_id = group_id
|
|
self._desc = desc
|
|
self._incarnation = incarnation
|
|
|
|
@staticmethod
|
|
def createGroup(id_, desc, incarnation):
|
|
return ActivationGroup(id_, desc, incarnation)
|
|
|
|
@staticmethod
|
|
def currentGroupID():
|
|
return ActivationGroupID()
|
|
|
|
@staticmethod
|
|
def setGroup(group):
|
|
pass
|
|
|
|
@staticmethod
|
|
def activeGroup(id_, group, incarnation):
|
|
return True
|
|
|
|
@staticmethod
|
|
def inactiveGroup(group_id):
|
|
pass
|
|
|
|
def newInstance(self, desc):
|
|
return None
|
|
|
|
def activeObject(self, id_, obj):
|
|
pass
|
|
|
|
def inactiveObject(self, id_):
|
|
pass
|
|
|
|
|
|
# ============================================================
|
|
# ActivationMonitor / ActivationSystem / Activator
|
|
# ============================================================
|
|
|
|
class ActivationMonitor(Stub):
|
|
"""java.rmi.activation.ActivationMonitor"""
|
|
def inactiveObject(self, id_):
|
|
pass
|
|
|
|
def activeObject(self, id_, obj):
|
|
pass
|
|
|
|
def inactiveGroup(self, group_id, incarnation):
|
|
pass
|
|
|
|
|
|
class ActivationSystem(Stub):
|
|
"""java.rmi.activation.ActivationSystem"""
|
|
def registerActivationDesc(self, desc):
|
|
return ActivationID()
|
|
|
|
def unregisterActivationDesc(self, id_):
|
|
pass
|
|
|
|
def shutdown(self):
|
|
pass
|
|
|
|
def setActivationMonitor(self, monitor):
|
|
pass
|
|
|
|
def getActivationMonitor(self):
|
|
return ActivationMonitor()
|
|
|
|
def activeGroup(self, id_, group, incarnation):
|
|
return ActivationMonitor()
|
|
|
|
def registerGroup(self, desc):
|
|
return ActivationGroupID()
|
|
|
|
def unregisterGroup(self, group_id):
|
|
pass
|
|
|
|
SYSTEM_PORT = 1098
|
|
|
|
|
|
class Activator(Stub):
|
|
"""java.rmi.activation.Activator"""
|
|
def activate(self, id_, force):
|
|
return MarshalledObject(None)
|
|
|
|
|
|
# ============================================================
|
|
# RMIClassLoader
|
|
# ============================================================
|
|
|
|
class RMIClassLoader(Stub):
|
|
"""java.rmi.server.RMIClassLoader"""
|
|
@staticmethod
|
|
def loadClass(codebase, name, default_loader=None):
|
|
import importlib
|
|
try:
|
|
return importlib.import_module(name)
|
|
except ImportError:
|
|
return None
|
|
|
|
@staticmethod
|
|
def getClassAnnotation(cls):
|
|
return ""
|
|
|
|
@staticmethod
|
|
def getClassLoader(codebase):
|
|
return None
|
|
|
|
@staticmethod
|
|
def getDefaultProviderInstance():
|
|
return None
|
|
|
|
@staticmethod
|
|
def loadProxyClass(codebase, interfaces, default_loader):
|
|
return None
|
|
|
|
|
|
# ============================================================
|
|
# RMISocketFactory / RMIFailureHandler
|
|
# ============================================================
|
|
|
|
class RMISocketFactory(Stub):
|
|
"""java.rmi.server.RMISocketFactory"""
|
|
_factory = None
|
|
_handler = None
|
|
|
|
@staticmethod
|
|
def getDefaultSocketFactory():
|
|
if RMISocketFactory._factory is None:
|
|
RMISocketFactory._factory = RMISocketFactory()
|
|
return RMISocketFactory._factory
|
|
|
|
@staticmethod
|
|
def setSocketFactory(factory):
|
|
RMISocketFactory._factory = factory
|
|
|
|
@staticmethod
|
|
def getFailureHandler():
|
|
return RMISocketFactory._handler
|
|
|
|
@staticmethod
|
|
def setFailureHandler(handler):
|
|
RMISocketFactory._handler = handler
|
|
|
|
def createSocket(self, host, port):
|
|
import socket
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.connect((host, port))
|
|
return s
|
|
|
|
def getDefaultPort(self):
|
|
return 0
|
|
|
|
|
|
class RMIFailureHandler(Stub):
|
|
"""java.rmi.server.RMIFailureHandler"""
|
|
def failure(self, ex):
|
|
return False
|
|
|
|
|
|
# ============================================================
|
|
# LogStream
|
|
# ============================================================
|
|
|
|
class LogStream(Stub):
|
|
"""java.rmi.server.LogStream"""
|
|
def __init__(self, name):
|
|
super().__init__()
|
|
self._name = name
|
|
self._stream = None
|
|
|
|
@staticmethod
|
|
def log(name):
|
|
return LogStream(name)
|
|
|
|
@staticmethod
|
|
def getDefaultStream():
|
|
return None
|
|
|
|
@staticmethod
|
|
def setDefaultStream(stream):
|
|
pass
|
|
|
|
def getOutputStream(self):
|
|
return self._stream
|
|
|
|
def setOutputStream(self, stream):
|
|
self._stream = stream
|
|
|
|
def toString(self):
|
|
return self._name
|
|
|
|
SILENT = 0
|
|
BRIEF = 10
|
|
VERBOSE = 20
|
|
|
|
|
|
# ============================================================
|
|
# RemoteCall / RemoteRef / ServerRef / Operation
|
|
# ============================================================
|
|
|
|
class RemoteCall(Stub):
|
|
"""java.rmi.server.RemoteCall"""
|
|
def getInputStream(self):
|
|
return None
|
|
|
|
def getOutputStream(self):
|
|
return None
|
|
|
|
def getResultStream(self, success):
|
|
return None
|
|
|
|
def executeCall(self):
|
|
pass
|
|
|
|
def done(self):
|
|
pass
|
|
|
|
def releaseInputStream(self):
|
|
pass
|
|
|
|
def releaseOutputStream(self):
|
|
pass
|
|
|
|
|
|
class RemoteRef(Stub):
|
|
"""java.rmi.server.RemoteRef"""
|
|
def invoke(self, call):
|
|
return None
|
|
|
|
def remoteEquals(self, obj):
|
|
return False
|
|
|
|
def remoteHashCode(self):
|
|
return 0
|
|
|
|
def remoteToString(self):
|
|
return ""
|
|
|
|
def getRefClass(self, out):
|
|
return ""
|
|
|
|
def newCall(self, obj, operations, opnum, hash_):
|
|
return RemoteCall()
|
|
|
|
def invoke(self, call):
|
|
pass
|
|
|
|
def done(self, call):
|
|
pass
|
|
|
|
packagePrefix = "sun.rmi.server"
|
|
|
|
|
|
class ServerRef(RemoteRef):
|
|
"""java.rmi.server.ServerRef"""
|
|
def exportObject(self, obj, data):
|
|
return RemoteStub()
|
|
|
|
def getClientHost(self):
|
|
return "localhost"
|
|
|
|
|
|
class RemoteStub(Stub):
|
|
"""java.rmi.server.RemoteStub"""
|
|
def __init__(self, ref=None):
|
|
super().__init__()
|
|
self._ref = ref
|
|
|
|
|
|
class Operation(Stub):
|
|
"""java.rmi.server.Operation"""
|
|
def __init__(self, name):
|
|
super().__init__()
|
|
self._name = name
|
|
|
|
def getOperation(self):
|
|
return self._name
|
|
|
|
def toString(self):
|
|
return self._name
|
|
|
|
|
|
# ============================================================
|
|
# RemoteObject / RemoteServer / UnicastRemoteObject
|
|
# ============================================================
|
|
|
|
class RemoteObject(Stub):
|
|
"""java.rmi.server.RemoteObject"""
|
|
def __init__(self, ref=None):
|
|
super().__init__()
|
|
self._ref = ref
|
|
|
|
def getRef(self):
|
|
return self._ref
|
|
|
|
def toString(self):
|
|
return "RemoteObject"
|
|
|
|
def hashCode(self):
|
|
return super().hashCode()
|
|
|
|
def equals(self, obj):
|
|
return super().equals(obj)
|
|
|
|
|
|
class RemoteServer(RemoteObject):
|
|
"""java.rmi.server.RemoteServer"""
|
|
def __init__(self, ref=None):
|
|
super().__init__(ref)
|
|
|
|
@staticmethod
|
|
def getClientHost():
|
|
return "localhost"
|
|
|
|
@staticmethod
|
|
def setLog(output_stream):
|
|
pass
|
|
|
|
@staticmethod
|
|
def getLog():
|
|
return None
|
|
|
|
|
|
class UnicastRemoteObject(RemoteServer):
|
|
"""java.rmi.server.UnicastRemoteObject"""
|
|
def __init__(self, port=0, csf=None, ssf=None):
|
|
super().__init__()
|
|
self._port = port
|
|
self._csf = csf
|
|
self._ssf = ssf
|
|
self._exported = False
|
|
|
|
@staticmethod
|
|
def exportObject(obj, port=0, csf=None, ssf=None):
|
|
return RemoteStub()
|
|
|
|
@staticmethod
|
|
def unexportObject(obj, force):
|
|
return True
|
|
|
|
def clone(self):
|
|
return UnicastRemoteObject(self._port)
|
|
|
|
|
|
class Unreferenced(Stub):
|
|
"""java.rmi.server.Unreferenced"""
|
|
def unreferenced(self):
|
|
pass |