Exemple #1
0
    def test_load_class (self):

        loader = ClassLoader ()

        t0 = time.time ()
        cls = loader.loadClass ("ClassLoaderHelperWorking", path=[os.path.dirname(__file__)])
        t = time.time ()
        
        assert cls.__name__ == "ClassLoaderHelperWorking"

        # test cache (use time to prove that cache is faster)
        t0 = time.time()
        cls = loader.loadClass ("ClassLoaderHelperWorking", path=[os.path.dirname(__file__)])
        t1 = time.time () - t0
        
        assert cls.__name__ == "ClassLoaderHelperWorking"
        assert t1 < t
        
        # test case in-sensitivite when looking for ClasName
        loader._cache = {} # clear cache
        cls = loader.loadClass ("ClAsSloAdErHeLpErWoRkiNg", path=[os.path.dirname(__file__)])
        
        assert_raises (ClassLoaderException, loader.loadClass, "ClassLoaderHelperNotFound")       
        assert_raises (ClassLoaderException, loader.loadClass, "ClassLoaderHelperFoundWithoutClass")
        assert_raises (ClassLoaderException, loader.loadClass, "ClassLoaderHelperFoundNotWorking1")
Exemple #2
0
    def test_load_class (self):

        loader = ClassLoader ()

        t0 = time.time ()
        cls = loader.loadClass ("ClassLoaderHelperWorking", path=[os.path.dirname(__file__)])
        t = time.time ()
        
        assert cls.__name__ == "ClassLoaderHelperWorking"

        # test cache (use time to prove that cache is faster)
        t0 = time.time()
        cls = loader.loadClass ("ClassLoaderHelperWorking", path=[os.path.dirname(__file__)])
        t1 = time.time () - t0
        
        assert cls.__name__ == "ClassLoaderHelperWorking"
        assert t1 < t
        
        # test case in-sensitivite when looking for ClasName
        loader._cache = {} # clear cache
        cls = loader.loadClass ("ClAsSloAdErHeLpErWoRkiNg", path=[os.path.dirname(__file__)])
        
        assert_raises (ClassLoaderException, loader.loadClass, "ClassLoaderHelperNotFound")       
        assert_raises (ClassLoaderException, loader.loadClass, "ClassLoaderHelperFoundWithoutClass")
        assert_raises (ClassLoaderException, loader.loadClass, "ClassLoaderHelperFoundNotWorking1")
        assert_raises (ClassLoaderException, loader.loadClass, "ClassLoaderHelperFoundNotWorking2")
Exemple #3
0
class Manager(RemoteObject):
    """
    This is the main class of Chimera.

    Use this class to get Proxies, add objects to the system, and so on.

    This class handles objects life-cycle as described in ILifecycle.

    @group Add/Remove: add*, remove
    @group Start/Stop: start, stop
    @group Proxy: getProxy
    @group Shutdown: wait, shutdown

    """
    def __init__(self, host=None, port=None, local=False):
        RemoteObject.__init__(self)

        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        if not local:
            try:
                ManagerLocator.locate()
                raise ChimeraException("Chimera is already running"
                                       " on this system. Use chimera-admin"
                                       " to manage it.")
            except ManagerNotFoundException:
                # ok, we are alone.
                pass

        # our daemon server
        self.adapter = ManagerAdapter(self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # finder beacon
        if not local:
            self.beacon = ManagerBeacon(self)
            self.beaconThread = threading.Thread(target=self.beacon.run)
            self.beaconThread.setDaemon(True)
            self.beaconThread.start()
        else:
            self.beacon = None

        # register ourself
        self.resources.add(MANAGER_LOCATION, self,
                           getManagerURI(self.getHostname(), self.getPort()))

        # signals
        signal.signal(signal.SIGTERM, self._sighandler)
        signal.signal(signal.SIGINT, self._sighandler)
        atexit.register(self._sighandler)

    # private
    def __repr__(self):
        if hasattr(self, 'adapter') and self.adapter:
            return "<Manager for %s:%d at %s>" % (
                self.adapter.hostname, self.adapter.port, hex(id(self)))
        else:
            return "<Manager at %s>" % hex(id(self))

    def _sighandler(self, sig=None, frame=None):
        self.shutdown()

    # adapter host/port
    def getHostname(self):
        if self.adapter:
            return self.adapter.hostname
        else:
            return None

    def getPort(self):
        if self.adapter:
            return self.adapter.port
        else:
            return None

    # reflection (console)
    def getResources(self):
        """
        Returns a list with the Location of all the available resources
        """
        return self.resources.keys()

    def getResourcesByClass(self, cls):
        resources = self.getResources()
        toRet = []
        for r in resources:
            if r.cls == cls:
                toRet.append(r)
        return toRet

    # helpers

    def getDaemon(self):
        return self.adapter

    def getProxy(self, location, name='0', host=None, port=None, lazy=False):
        """
        Get a proxy for the object pointed by location. The given location can contain index
        instead of names, e.g. '/Object/0' to get objects when you don't know their names.

        location can also be a class. getProxy will return an instance
        named 'name' at the given host/port (or on the current
        manager, if None given).

        host and port parameters determines which Manager we will
        lookup for location/instance. If None, look at this
        Manager. host/port is only used when location is a
        class, otherwise, host and port are determined by location
        itself.

        lazy parameter determines if Manager will try to locate the
        selected Manager at host/port and ask them for a valid
        object/instance. If False, Manager just return an proxy for
        the selected parameters but can't guarantee that the returned
        Proxy have an active object bounded.

        For objects managed by this own Manager, lazy is always False.

        @param location: Object location or class.
        @type location: Location or class

        @param name: Instance name.
        @type name: str

        @param host: Manager's hostname.
        @type host: str

        @param port: Manager's port.
        @type port: int

        @param lazy: Manager's laziness (check for already bound objects on host/port Manager)
        @type lazy: bool

        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises ObjectNotFoundException: When te request object or the Manager was not found.
        @raises InvalidLocationException: When the requested location s invalid.

        @return: Proxy for selected object.
        @rtype: Proxy
        """

        if not location:
            raise ObjectNotFoundException("Couldn't find an object at the"
                                          " given location %s" % location)

        if not isinstance(location, StringType) and not isinstance(
                location, Location):

            if issubclass(location, ChimeraObject):
                location = Location(cls=location.__name__,
                                    name=name,
                                    host=host or self.getHostname(),
                                    port=port or self.getPort())
            else:
                raise NotValidChimeraObjectException(
                    "Can't get a proxy from non ChimeraObject's descendent object (%s)."
                    % location)

        else:
            location = Location(location,
                                host=host or self.getHostname(),
                                port=port or self.getPort())

        # who manages this location?
        if self._belongsToMe(location):

            ret = self.resources.get(location)
            if not ret:
                raise ObjectNotFoundException("Couldn't found an object at the"
                                              " given location %s" % location)

            return Proxy(uri=ret.uri)

        else:

            if lazy:
                return Proxy(location)
            else:

                # contact other manager
                other = Proxy(location=MANAGER_LOCATION,
                              host=location.host or host,
                              port=location.port or port)

                if not other.ping():
                    raise ObjectNotFoundException(
                        "Can't contact %s manager at %s." %
                        (location, other.URI.address))

                proxy = other.getProxy(location)

                if not proxy:
                    raise ObjectNotFoundException(
                        "Couldn't found an object at the"
                        " given location %s" % location)
                else:
                    return proxy

    def _belongsToMe(self, location):

        meHost = self.getHostname()
        meName = socket.gethostbyname(meHost)
        mePort = self.getPort()

        return (location.host == None or location.host in (meHost, meName)) and \
               (location.port == None or location.port == self.getPort())

    # shutdown management

    def shutdown(self):
        """
        Ask the system to shutdown. Closing all sockets and stopping
        all threads.

        @return: Nothing
        @rtype: None
        """

        # die, but only if we are alive ;)
        if not self.died.isSet():

            log.info("Shuting down manager.")

            # stop objects
            # damm 2.4, on 2.5 try/except/finally works
            try:
                try:

                    elderly_first = sorted(
                        list(self.resources.values()),
                        cmp=lambda x, y: cmp(x.created, y.created),
                        reverse=True)

                    for resource in elderly_first:

                        # except Manager
                        if resource.location == MANAGER_LOCATION: continue

                        # stop object
                        self.stop(resource.location)

                except ChimeraException:
                    pass
            finally:
                # kill our adapter
                self.adapter.shutdown(disconnect=True)
                if self.beacon:
                    self.beacon.shutdown()
                    self.beaconThread.join()

                # die!
                self.died.set()
                log.info("Manager finished.")

    def wait(self):
        """
        Ask the system to wait until anyone calls L{shutdown}.

        If nobody calls L{shutdown}, you can stop the system using
        Ctrl+C.

        @return: Nothing
        @rtype: None
        """

        try:
            while not self.died.isSet():
                time.sleep(1)
        except IOError:
            # On Windows, Ctrl+C on a sleep call raise IOError 'cause
            # of the interrupted syscall
            pass

    # objects lifecycle

    def addLocation(self, location, path=[], start=True):
        """
        Add the class pointed by 'location' to the system configuring it using 'config'.

        Manager will look for the class in 'path' plus sys.path.

        @param path: The class search path.
        @type path: list

        @param start: start the object after initialization.
        @type start: bool

        @raises ChimeraObjectException: Internal error on managed (user) object.
        @raises ClassLoaderException: Class not found.
        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises InvalidLocationException: When the requested location s invalid.              

        @return: retuns a proxy for the object if sucessuful, False otherwise.
        @rtype: Proxy or bool
        """

        if type(location) != Location:
            location = Location(location)

        # get the class
        cls = None

        cls = self.classLoader.loadClass(location.cls, path)

        return self.addClass(cls, location.name, location.config, start)

    def addClass(self, cls, name, config={}, start=True):
        """
        Add the class 'cls' to the system configuring it using 'config'.

        @param cls: The class to add to the system.
        @type cls: ChimeraObject

        @param name: The name of the new class instance.
        @type name: str

        @param config: The configuration dictionary for the object.
        @type config: dict

        @param start: start the object after initialization.
        @type start: bool

        @raises ChimeraObjectException: Internal error on managed (user) object.
        @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location.
        @raises InvalidLocationException: When the requested location s invalid.              

        @return: retuns a proxy for the object if sucessuful, False otherwise.
        @rtype: Proxy or bool
        """

        location = Location(cls=cls.__name__, name=name, config=config)

        # names must not start with a digit
        if location.name[0] in "0123456789":
            raise InvalidLocationException(
                "Invalid instance name: %s (must start with a letter)" %
                location)

        if location in self.resources:
            raise InvalidLocationException(
                "Location %s is already in the system. Only one allowed (Tip. change the name!)."
                % location)

        # check if it's a valid ChimeraObject
        if not issubclass(cls, ChimeraObject):
            raise NotValidChimeraObjectException(
                "Cannot add the class %s. It doesn't descend from ChimeraObject."
                % cls.__name__)

        # run object __init__ and configure using location configuration
        # it runs on the same thread, so be a good boy
        # and don't block manager's thread
        try:
            obj = cls()
        except Exception:
            log.exception("Error in %s __init__." % location)
            raise ChimeraObjectException("Error in %s __init__." % location)

        try:
            for k, v in location.config.items():
                obj[k] = v
        except (OptionConversionException, KeyError), e:
            log.exception("Error configuring %s." % location)
            raise ChimeraObjectException("Error configuring %s. (%s)" %
                                         (location, e))

        # connect
        obj.__setlocation__(location)
        next = len(self.resources.getByClass(location.cls))
        uri = self.adapter.connect(obj,
                                   index=str(
                                       Location(cls=location.cls, name=next)))
        self.resources.add(location, obj, uri)

        if start:
            self.start(location)

        return Proxy(uri=uri)