class ContentModelListener(ConnectionListener): ''' classdocs ''' def __init__(self, content_models, host='localhost', port=61613, user='', passcode='', fedora_url=''): ''' Constructor ''' self.conn = Connection([(host, port)], user, passcode) self.conn.set_listener('', self) self.conn.start() logging.info('Connecting to STOMP server %(host)s on port %(port)s.' % {'host': host, 'port': port}) self.transaction_id = None logging.info("Connecting to Fedora server at %(url)s" % {'url': fedora_url}) self.fc = fcrepo.connection.Connection(fedora_url, username = user, password = passcode) self.client = FedoraClient(self.fc) self.fedora_url = fedora_url self.username = user self.password = passcode # Create plugin manager self.manager = PluginManager(categories_filter = {"FedoraMicroService": FedoraMicroService}) plugin_path = os.path.dirname(__file__) self.manager.setPluginPlaces([plugin_path + "/plugins"]) logging.debug("Plugin path: " + plugin_path + "/plugins") # Load plugins. self.manager.locatePlugins() self.manager.loadPlugins() self.contentModels = {} for plugin in self.manager.getPluginsOfCategory("FedoraMicroService"): # plugin.plugin_object is an instance of the plubin logging.info("Loading plugin: %(name)s for content model %(cmodel)s." % {'name': plugin.plugin_object.name, 'cmodel': plugin.plugin_object.content_model}) plugin.plugin_object.config = config if type(plugin.plugin_object.content_model) == types.StringType: content_models = [plugin.plugin_object.content_model] else: content_models = plugin.plugin_object.content_model for content_model in content_models: if content_model in self.contentModels: self.contentModels[content_model].append(plugin.plugin_object) else: self.contentModels[content_model] = [plugin.plugin_object] def __print_async(self, frame_type, headers, body): """ Utility function for printing messages. """ #logging.debug("\r \r", end='') logging.debug(frame_type) for header_key in headers.keys(): logging.debug('%s: %s' % (header_key, headers[header_key])) logging.debug(body) def on_connecting(self, host_and_port): """ \see ConnectionListener::on_connecting """ self.conn.connect(wait=True) def on_disconnected(self): """ \see ConnectionListener::on_disconnected """ logging.error("lost connection reconnect in %d sec..." % reconnect_wait) signal.alarm(reconnect_wait) def on_message(self, headers, body): """ \see ConnectionListener::on_message """ try: global TOPIC_PREFIX self.__print_async('MESSAGE', headers, body) pid = headers['pid'] dsid = headers['dsid'] obj = self.client.getObject(pid) content_model = headers['destination'][len(TOPIC_PREFIX):] if content_model in self.contentModels: logging.info('Running rules for %(pid)s from %(cmodel)s.' % {'pid': obj.pid, 'cmodel': content_model} ) for plugin in self.contentModels[content_model]: plugin.runRules(obj, dsid, body) except FedoraConnectionException: logging.warning('Object %s was not found.' % (pid)) except: logging.error("an exception occurred: " + str(sys.exc_info()[0])) def on_error(self, headers, body): """ \see ConnectionListener::on_error """ self.__print_async("ERROR", headers, body) def on_connected(self, headers, body): """ \see ConnectionListener::on_connected """ self.__print_async("CONNECTED", headers, body) def ack(self, args): """ Required Parameters: message-id - the id of the message being acknowledged Description: Acknowledge consumption of a message from a subscription using client acknowledgement. When a client has issued a subscribe with an 'ack' flag set to client received from that destination will not be considered to have been consumed (by the server) until the message has been acknowledged. """ if not self.transaction_id: self.conn.ack(headers = { 'message-id' : args[1]}) else: self.conn.ack(headers = { 'message-id' : args[1]}, transaction=self.transaction_id) def abort(self, args): """ Description: Roll back a transaction in progress. """ if self.transaction_id: self.conn.abort(transaction=self.transaction_id) self.transaction_id = None def begin(self, args): """ Description Start a transaction. Transactions in this case apply to sending and acknowledging any messages sent or acknowledged during a transaction will be handled atomically based on teh transaction. """ if not self.transaction_id: self.transaction_id = self.conn.begin() def commit(self, args): """ Description: Commit a transaction in progress. """ if self.transaction_id: self.conn.commit(transaction=self.transaction_id) self.transaction_id = None def disconnect(self, args): """ Description: Gracefully disconnect from the server. """ try: self.conn.disconnect() except NotConnectedException: pass def send(self, destination, correlation_id, message): """ Required Parametes: destination - where to send the message message - the content to send Description: Sends a message to a destination in the message system. """ self.conn.send(destination=destination, message=message, headers={'correlation-id': correlation_id}) def subscribe(self, destination, ack='auto'): """ Required Parameters: destination - the name to subscribe to Optional Parameters: ack - how to handle acknowledgements for a message, either automatically (auto) or manually (client) Description Register to listen to a given destination. Like send, the subscribe command requires a destination header indicating which destination to subscribe to. The ack parameter is optional, and defaults to auto. """ self.conn.subscribe(destination=destination, ack=ack) def unsubscribe(self, destination): """ Required Parameters: destination - the name to unsubscribe from Description: Remove an existing subscription - so that the client no longer receives messages from that destination. """ self.conn.unsubscribe(destination) def connect(self): self.conn.start() self.fc = fcrepo.connection.Connection(self.fedora_url, username = self.username, password = self.password) self.client = FedoraClient(self.fc)
class StompFedora(ConnectionListener): """ A custom interface to the stomp.py client. See \link stomp::internal::connect::Connection \endlink for more information on establishing a connection to a stomp server. """ def __init__(self, host='localhost', port=61613, user='', passcode='', fedora_url=''): self.conn = Connection([(host, port)], user, passcode) self.conn.set_listener('', self) self.conn.start() self.transaction_id = None self.fc = fcrepo.connection.Connection(fedora_url, username = user, password = passcode) self.client = FedoraClient(self.fc) self.fedora_url = fedora_url self.user = user self.password = passcode def __print_async(self, frame_type, headers, body): """ Utility function for printing messages. """ logging.debug(frame_type) for header_key in headers.keys(): logging.debug('%s: %s' % (header_key, headers[header_key])) logging.debug(body) def __get_content_models(self, pid): """ Get a list of content models that apply to the object. """ obj = self.client.getObject(pid) if 'RELS-EXT' in obj: ds = obj['RELS-EXT'] return obj, [elem['value'].split('/')[1] for elem in ds[NS.fedoramodel.hasModel]] else: return obj, [] def on_connecting(self, host_and_port): """ \see ConnectionListener::on_connecting """ self.conn.connect(wait=True) def on_disconnected(self): """ \see ConnectionListener::on_disconnected """ logging.error("lost connection reconnect in %d sec..." % reconnect_wait) signal.alarm(reconnect_wait) def on_message(self, headers, body): """ \see ConnectionListener::on_message """ self.__print_async("MESSSAGE", headers, body) method = headers['methodName'] pid = headers['pid'] newheaders = { 'methodname':headers['methodName'], 'pid':headers['pid']} if method in ['addDatastream', 'modifyDatastreamByValue', 'modifyDatastreamByReference', 'modifyObject']: f = feedparser.parse(body) tags = f['entries'][0]['tags'] dsids = [tag['term'] for tag in tags if tag['scheme'] == 'fedora-types:dsID'] dsid = '' if dsids: dsid = dsids[0] newheaders['dsid'] = dsid obj, content_models = self.__get_content_models(pid) # and 'OBJ' in dsIDs: for content_model in content_models: logging.info("/topic/fedora.contentmodel.%s %s" % (content_model, dsid)) self.send("/topic/fedora.contentmodel.%s" % content_model, newheaders, body) elif method in ['ingest']: obj, content_models = self.__get_content_models(pid) for dsid in obj: for content_model in content_models: newheaders['dsid'] = dsid logging.info("/topic/fedora.contentmodel.%s %s" % (content_model, dsid)) self.send("/topic/fedora.contentmodel.%s" % content_model, newheaders, body) def on_error(self, headers, body): """ \see ConnectionListener::on_error """ self.__print_async("ERROR", headers, body) def on_connected(self, headers, body): """ \see ConnectionListener::on_connected """ self.__print_async("CONNECTED", headers, body) def ack(self, args): """ Required Parameters: message-id - the id of the message being acknowledged Description: Acknowledge consumption of a message from a subscription using client acknowledgement. When a client has issued a subscribe with an 'ack' flag set to client received from that destination will not be considered to have been consumed (by the server) until the message has been acknowledged. """ if not self.transaction_id: self.conn.ack(headers = { 'message-id' : args[1]}) else: self.conn.ack(headers = { 'message-id' : args[1]}, transaction=self.transaction_id) def abort(self, args): """ Description: Roll back a transaction in progress. """ if self.transaction_id: self.conn.abort(transaction=self.transaction_id) self.transaction_id = None def begin(self, args): """ Description Start a transaction. Transactions in this case apply to sending and acknowledging any messages sent or acknowledged during a transaction will be handled atomically based on teh transaction. """ if not self.transaction_id: self.transaction_id = self.conn.begin() def commit(self, args): """ Description: Commit a transaction in progress. """ if self.transaction_id: self.conn.commit(transaction=self.transaction_id) self.transaction_id = None def disconnect(self, args=None): """ Description: Gracefully disconnect from the server. """ try: self.conn.disconnect() except NotConnectedException: pass def send(self, destination, headers, message): """ Required Parametes: destination - where to send the message message - the content to send Description: Sends a message to a destination in the message system. """ self.conn.send(destination=destination, message=message, headers=headers) def subscribe(self, destination, ack='auto'): """ Required Parameters: destination - the name to subscribe to Optional Parameters: ack - how to handle acknowledgements for a message, either automatically (auto) or manually (client) Description Register to listen to a given destination. Like send, the subscribe command requires a destination header indicating which destination to subscribe to. The ack parameter is optional, and defaults to auto. """ self.conn.subscribe(destination=destination, ack=ack) def connect(self): self.conn.start() self.fc = fcrepo.connection.Connection(self.fedora_url, username = self.user, password = self.password) self.client = FedoraClient(self.fc) def unsubscribe(self, destination): """ Required Parameters: destination - the name to unsubscribe from Description: Remove an existing subscription - so that the client no longer receives messages from that destination. """ self.conn.unsubscribe(destination)
class ContentModelListener(ConnectionListener): ''' classdocs ''' def __init__(self, content_models, host='localhost', port=61613, user='', passcode='', fedora_url=''): ''' Constructor ''' self.conn = Connection([(host, port)], user, passcode) self.conn.set_listener('', self) self.conn.start() logging.info('Connecting to STOMP server %(host)s on port %(port)s.' % { 'host': host, 'port': port }) self.transaction_id = None logging.info("Connecting to Fedora server at %(url)s" % {'url': fedora_url}) self.fc = fcrepo.connection.Connection(fedora_url, username=user, password=passcode) self.client = FedoraClient(self.fc) # Create plugin manager self.manager = PluginManager( categories_filter={"FedoraMicroService": FedoraMicroService}) self.manager.setPluginPlaces(["plugins"]) # Load plugins. self.manager.locatePlugins() self.manager.loadPlugins() self.contentModels = {} for plugin in self.manager.getPluginsOfCategory("FedoraMicroService"): # plugin.plugin_object is an instance of the plubin logging.info( "Loading plugin: %(name)s for content model %(cmodel)s." % { 'name': plugin.plugin_object.name, 'cmodel': plugin.plugin_object.content_model }) plugin.plugin_object.config = config if plugin.plugin_object.content_model in self.contentModels: self.contentModels[plugin.plugin_object.content_model].append( plugin.plugin_object) else: self.contentModels[plugin.plugin_object.content_model] = [ plugin.plugin_object ] def __print_async(self, frame_type, headers, body): """ Utility function for printing messages. """ #logging.debug("\r \r", end='') logging.debug(frame_type) for header_key in headers.keys(): logging.debug('%s: %s' % (header_key, headers[header_key])) logging.debug(body) def on_connecting(self, host_and_port): """ \see ConnectionListener::on_connecting """ self.conn.connect(wait=True) def on_disconnected(self): """ \see ConnectionListener::on_disconnected """ def on_message(self, headers, body): """ \see ConnectionListener::on_message """ global TOPIC_PREFIX self.__print_async('MESSAGE', headers, body) f = feedparser.parse(body) tags = f['entries'][0]['tags'] pid = [ tag['term'] for tag in tags if tag['scheme'] == 'fedora-types:pid' ][0] dsID = [ tag['term'] for tag in tags if tag['scheme'] == 'fedora-types:dsID' ][0] obj = self.client.getObject(pid) content_model = headers['destination'][len(TOPIC_PREFIX):] if content_model in self.contentModels: logging.info('Running rules for %(pid)s from %(cmodel)s.' % { 'pid': obj.pid, 'cmodel': content_model }) for plugin in self.contentModels[content_model]: plugin.runRules(obj, dsID) return def on_error(self, headers, body): """ \see ConnectionListener::on_error """ self.__print_async("ERROR", headers, body) def on_connected(self, headers, body): """ \see ConnectionListener::on_connected """ self.__print_async("CONNECTED", headers, body) def ack(self, args): """ Required Parameters: message-id - the id of the message being acknowledged Description: Acknowledge consumption of a message from a subscription using client acknowledgement. When a client has issued a subscribe with an 'ack' flag set to client received from that destination will not be considered to have been consumed (by the server) until the message has been acknowledged. """ if not self.transaction_id: self.conn.ack(headers={'message-id': args[1]}) else: self.conn.ack(headers={'message-id': args[1]}, transaction=self.transaction_id) def abort(self, args): """ Description: Roll back a transaction in progress. """ if self.transaction_id: self.conn.abort(transaction=self.transaction_id) self.transaction_id = None def begin(self, args): """ Description Start a transaction. Transactions in this case apply to sending and acknowledging any messages sent or acknowledged during a transaction will be handled atomically based on teh transaction. """ if not self.transaction_id: self.transaction_id = self.conn.begin() def commit(self, args): """ Description: Commit a transaction in progress. """ if self.transaction_id: self.conn.commit(transaction=self.transaction_id) self.transaction_id = None def disconnect(self, args): """ Description: Gracefully disconnect from the server. """ try: self.conn.disconnect() except NotConnectedException: pass def send(self, destination, correlation_id, message): """ Required Parametes: destination - where to send the message message - the content to send Description: Sends a message to a destination in the message system. """ self.conn.send(destination=destination, message=message, headers={'correlation-id': correlation_id}) def subscribe(self, destination, ack='auto'): """ Required Parameters: destination - the name to subscribe to Optional Parameters: ack - how to handle acknowledgements for a message, either automatically (auto) or manually (client) Description Register to listen to a given destination. Like send, the subscribe command requires a destination header indicating which destination to subscribe to. The ack parameter is optional, and defaults to auto. """ self.conn.subscribe(destination=destination, ack=ack) def unsubscribe(self, destination): """ Required Parameters: destination - the name to unsubscribe from Description: Remove an existing subscription - so that the client no longer receives messages from that destination. """ self.conn.unsubscribe(destination)
class StompFedora(ConnectionListener): """ A custom interface to the stomp.py client. See \link stomp::internal::connect::Connection \endlink for more information on establishing a connection to a stomp server. """ def __init__(self, host="localhost", port=61613, user="", passcode="", fedora_url=""): self.conn = Connection([(host, port)], user, passcode) self.conn.set_listener("", self) self.conn.start() self.transaction_id = None self.fc = fcrepo.connection.Connection(fedora_url, username=user, password=passcode) self.client = FedoraClient(self.fc) def __print_async(self, frame_type, headers, body): """ Utility function for printing messages. """ logging.debug(frame_type) for header_key in headers.keys(): logging.debug("%s: %s" % (header_key, headers[header_key])) logging.debug(body) def __get_content_models(self, pid): """ Get a list of content models that apply to the object. """ obj = self.client.getObject(pid) if "RELS-EXT" in obj: ds = obj["RELS-EXT"] return [elem["value"].split("/")[1] for elem in ds[NS.fedoramodel.hasModel]] else: return [] def on_connecting(self, host_and_port): """ \see ConnectionListener::on_connecting """ self.conn.connect(wait=True) def on_disconnected(self): """ \see ConnectionListener::on_disconnected """ sysout("lost connection") def on_message(self, headers, body): """ \see ConnectionListener::on_message """ self.__print_async("MESSSAGE", headers, body) f = feedparser.parse(body) method = headers["methodName"] if method in ["addDatastream", "modifyDatastreamByValue", "modifyDatastreamByReference"]: tags = f["entries"][0]["tags"] pid = [tag["term"] for tag in tags if tag["scheme"] == "fedora-types:pid"][0] dsIDs = [tag["term"] for tag in tags if tag["scheme"] == "fedora-types:dsID"] content_models = self.__get_content_models(pid) # and 'OBJ' in dsIDs: for content_model in content_models: print "/topic/fedora.contentmodel.%s" % content_model self.send("/topic/fedora.contentmodel.%s" % content_model, "", body) def on_error(self, headers, body): """ \see ConnectionListener::on_error """ self.__print_async("ERROR", headers, body) def on_connected(self, headers, body): """ \see ConnectionListener::on_connected """ self.__print_async("CONNECTED", headers, body) def ack(self, args): """ Required Parameters: message-id - the id of the message being acknowledged Description: Acknowledge consumption of a message from a subscription using client acknowledgement. When a client has issued a subscribe with an 'ack' flag set to client received from that destination will not be considered to have been consumed (by the server) until the message has been acknowledged. """ if not self.transaction_id: self.conn.ack(headers={"message-id": args[1]}) else: self.conn.ack(headers={"message-id": args[1]}, transaction=self.transaction_id) def abort(self, args): """ Description: Roll back a transaction in progress. """ if self.transaction_id: self.conn.abort(transaction=self.transaction_id) self.transaction_id = None def begin(self, args): """ Description Start a transaction. Transactions in this case apply to sending and acknowledging any messages sent or acknowledged during a transaction will be handled atomically based on teh transaction. """ if not self.transaction_id: self.transaction_id = self.conn.begin() def commit(self, args): """ Description: Commit a transaction in progress. """ if self.transaction_id: self.conn.commit(transaction=self.transaction_id) self.transaction_id = None def disconnect(self, args): """ Description: Gracefully disconnect from the server. """ try: self.conn.disconnect() except NotConnectedException: pass def send(self, destination, correlation_id, message): """ Required Parametes: destination - where to send the message message - the content to send Description: Sends a message to a destination in the message system. """ self.conn.send(destination=destination, message=message, headers={"correlation-id": correlation_id}) def subscribe(self, destination, ack="auto"): """ Required Parameters: destination - the name to subscribe to Optional Parameters: ack - how to handle acknowledgements for a message, either automatically (auto) or manually (client) Description Register to listen to a given destination. Like send, the subscribe command requires a destination header indicating which destination to subscribe to. The ack parameter is optional, and defaults to auto. """ self.conn.subscribe(destination=destination, ack=ack) def unsubscribe(self, destination): """ Required Parameters: destination - the name to unsubscribe from Description: Remove an existing subscription - so that the client no longer receives messages from that destination. """ self.conn.unsubscribe(destination)
class StompFedora(ConnectionListener): """ A custom interface to the stomp.py client. See \link stomp::internal::connect::Connection \endlink for more information on establishing a connection to a stomp server. """ def __init__(self, host='localhost', port=61613, user='', passcode='', fedora_url=''): self.conn = Connection([(host, port)], user, passcode) self.conn.set_listener('', self) self.conn.start() self.transaction_id = None self.fc = fcrepo.connection.Connection(fedora_url, username=user, password=passcode) self.client = FedoraClient(self.fc) self.fedora_url = fedora_url self.user = user self.password = passcode def __print_async(self, frame_type, headers, body): """ Utility function for printing messages. """ logging.debug(frame_type) for header_key in headers.keys(): logging.debug('%s: %s' % (header_key, headers[header_key])) logging.debug(body) def __get_content_models(self, pid): """ Get a list of content models that apply to the object. """ obj = self.client.getObject(pid) if 'RELS-EXT' in obj: ds = obj['RELS-EXT'] return obj, [ elem['value'].split('/')[1] for elem in ds[NS.fedoramodel.hasModel] ] else: return obj, [] def on_connecting(self, host_and_port): """ \see ConnectionListener::on_connecting """ self.conn.connect(wait=True) def on_disconnected(self): """ \see ConnectionListener::on_disconnected """ logging.error("lost connection reconnect in %d sec..." % reconnect_wait) signal.alarm(reconnect_wait) def on_message(self, headers, body): """ \see ConnectionListener::on_message """ self.__print_async("MESSSAGE", headers, body) method = headers['methodName'] pid = headers['pid'] newheaders = { 'methodname': headers['methodName'], 'pid': headers['pid'] } if method in [ 'addDatastream', 'modifyDatastreamByValue', 'modifyDatastreamByReference', 'modifyObject' ]: f = feedparser.parse(body) tags = f['entries'][0]['tags'] dsids = [ tag['term'] for tag in tags if tag['scheme'] == 'fedora-types:dsID' ] dsid = '' if dsids: dsid = dsids[0] newheaders['dsid'] = dsid obj, content_models = self.__get_content_models( pid) # and 'OBJ' in dsIDs: for content_model in content_models: logging.info("/topic/fedora.contentmodel.%s %s" % (content_model, dsid)) self.send("/topic/fedora.contentmodel.%s" % content_model, newheaders, body) elif method in ['ingest']: obj, content_models = self.__get_content_models(pid) for dsid in obj: for content_model in content_models: newheaders['dsid'] = dsid logging.info("/topic/fedora.contentmodel.%s %s" % (content_model, dsid)) self.send("/topic/fedora.contentmodel.%s" % content_model, newheaders, body) def on_error(self, headers, body): """ \see ConnectionListener::on_error """ self.__print_async("ERROR", headers, body) def on_connected(self, headers, body): """ \see ConnectionListener::on_connected """ self.__print_async("CONNECTED", headers, body) def ack(self, args): """ Required Parameters: message-id - the id of the message being acknowledged Description: Acknowledge consumption of a message from a subscription using client acknowledgement. When a client has issued a subscribe with an 'ack' flag set to client received from that destination will not be considered to have been consumed (by the server) until the message has been acknowledged. """ if not self.transaction_id: self.conn.ack(headers={'message-id': args[1]}) else: self.conn.ack(headers={'message-id': args[1]}, transaction=self.transaction_id) def abort(self, args): """ Description: Roll back a transaction in progress. """ if self.transaction_id: self.conn.abort(transaction=self.transaction_id) self.transaction_id = None def begin(self, args): """ Description Start a transaction. Transactions in this case apply to sending and acknowledging any messages sent or acknowledged during a transaction will be handled atomically based on teh transaction. """ if not self.transaction_id: self.transaction_id = self.conn.begin() def commit(self, args): """ Description: Commit a transaction in progress. """ if self.transaction_id: self.conn.commit(transaction=self.transaction_id) self.transaction_id = None def disconnect(self, args=None): """ Description: Gracefully disconnect from the server. """ try: self.conn.disconnect() except NotConnectedException: pass def send(self, destination, headers, message): """ Required Parametes: destination - where to send the message message - the content to send Description: Sends a message to a destination in the message system. """ self.conn.send(destination=destination, message=message, headers=headers) def subscribe(self, destination, ack='auto'): """ Required Parameters: destination - the name to subscribe to Optional Parameters: ack - how to handle acknowledgements for a message, either automatically (auto) or manually (client) Description Register to listen to a given destination. Like send, the subscribe command requires a destination header indicating which destination to subscribe to. The ack parameter is optional, and defaults to auto. """ self.conn.subscribe(destination=destination, ack=ack) def connect(self): self.conn.start() self.fc = fcrepo.connection.Connection(self.fedora_url, username=self.user, password=self.password) self.client = FedoraClient(self.fc) def unsubscribe(self, destination): """ Required Parameters: destination - the name to unsubscribe from Description: Remove an existing subscription - so that the client no longer receives messages from that destination. """ self.conn.unsubscribe(destination)