Search in sources :

Example 1 with BeanFactoryReference

use of org.springframework.beans.factory.access.BeanFactoryReference in project opennms by OpenNMS.

the class BSFNotificationStrategy method declareBeans.

private static void declareBeans(BSFNotificationStrategy obj) throws BSFException {
    // Retrieve the parameters before accessing them
    obj.retrieveParams();
    Integer nodeId;
    try {
        nodeId = Integer.valueOf(obj.m_notifParams.get(NotificationManager.PARAM_NODE));
    } catch (NumberFormatException nfe) {
        nodeId = null;
    }
    OnmsNode node = null;
    OnmsAssetRecord assets = null;
    final List<String> categories = new ArrayList<String>();
    String nodeLabel = null;
    String foreignSource = null;
    String foreignId = null;
    if (nodeId != null) {
        final BeanFactoryReference bf = BeanUtils.getBeanFactory("notifdContext");
        final NodeDao nodeDao = BeanUtils.getBean(bf, "nodeDao", NodeDao.class);
        final TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class);
        try {
            // Redeclare the node id as final
            final int theNodeId = nodeId;
            node = transTemplate.execute(new TransactionCallback<OnmsNode>() {

                @Override
                public OnmsNode doInTransaction(final TransactionStatus status) {
                    final OnmsNode node = nodeDao.get(theNodeId);
                    // Retrieve the categories in the context of the transaction
                    if (node != null) {
                        for (OnmsCategory cat : node.getCategories()) {
                            categories.add(cat.getName());
                        }
                    }
                    return node;
                }
            });
            if (node == null) {
                LOG.error("Could not find a node with id: {}", theNodeId);
            } else {
                nodeLabel = node.getLabel();
                assets = node.getAssetRecord();
                foreignSource = node.getForeignSource();
                foreignId = node.getForeignId();
            }
        } catch (final RuntimeException e) {
            LOG.error("Error while retrieving node with id {}", nodeId, e);
        }
    }
    s_bsfManager.declareBean("bsf_notif_strategy", obj, BSFNotificationStrategy.class);
    s_bsfManager.declareBean("logger", LOG, Logger.class);
    s_bsfManager.declareBean("notif_params", obj.m_notifParams, Map.class);
    s_bsfManager.declareBean("node_label", nodeLabel, String.class);
    s_bsfManager.declareBean("foreign_source", foreignSource, String.class);
    s_bsfManager.declareBean("foreign_id", foreignId, String.class);
    s_bsfManager.declareBean("node_assets", assets, OnmsAssetRecord.class);
    s_bsfManager.declareBean("node_categories", categories, List.class);
    s_bsfManager.declareBean("node", node, OnmsNode.class);
    for (Argument arg : obj.m_arguments) {
        if (NotificationManager.PARAM_TEXT_MSG.equals(arg.getSwitch()))
            s_bsfManager.declareBean("text_message", arg.getValue(), String.class);
        if (NotificationManager.PARAM_NUM_MSG.equals(arg.getSwitch()))
            s_bsfManager.declareBean("numeric_message", arg.getValue(), String.class);
        if (NotificationManager.PARAM_NODE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("node_id", arg.getValue(), String.class);
        if (NotificationManager.PARAM_INTERFACE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("ip_addr", arg.getValue(), String.class);
        if (NotificationManager.PARAM_SERVICE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("svc_name", arg.getValue(), String.class);
        if (NotificationManager.PARAM_SUBJECT.equals(arg.getSwitch()))
            s_bsfManager.declareBean("subject", arg.getValue(), String.class);
        if (NotificationManager.PARAM_EMAIL.equals(arg.getSwitch()))
            s_bsfManager.declareBean("email", arg.getValue(), String.class);
        if (NotificationManager.PARAM_PAGER_EMAIL.equals(arg.getSwitch()))
            s_bsfManager.declareBean("pager_email", arg.getValue(), String.class);
        if (NotificationManager.PARAM_XMPP_ADDRESS.equals(arg.getSwitch()))
            s_bsfManager.declareBean("xmpp_address", arg.getValue(), String.class);
        if (NotificationManager.PARAM_TEXT_PAGER_PIN.equals(arg.getSwitch()))
            s_bsfManager.declareBean("text_pin", arg.getValue(), String.class);
        if (NotificationManager.PARAM_NUM_PAGER_PIN.equals(arg.getSwitch()))
            s_bsfManager.declareBean("numeric_pin", arg.getValue(), String.class);
        if (NotificationManager.PARAM_WORK_PHONE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("work_phone", arg.getValue(), String.class);
        if (NotificationManager.PARAM_HOME_PHONE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("home_phone", arg.getValue(), String.class);
        if (NotificationManager.PARAM_MOBILE_PHONE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("mobile_phone", arg.getValue(), String.class);
        if (NotificationManager.PARAM_TUI_PIN.equals(arg.getSwitch()))
            s_bsfManager.declareBean("phone_pin", arg.getValue(), String.class);
        if (NotificationManager.PARAM_MICROBLOG_USERNAME.equals(arg.getSwitch()))
            s_bsfManager.declareBean("microblog_username", arg.getValue(), String.class);
    }
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Argument(org.opennms.netmgt.model.notifd.Argument) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) ArrayList(java.util.ArrayList) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) NodeDao(org.opennms.netmgt.dao.api.NodeDao) TransactionCallback(org.springframework.transaction.support.TransactionCallback) OnmsCategory(org.opennms.netmgt.model.OnmsCategory)

Example 2 with BeanFactoryReference

use of org.springframework.beans.factory.access.BeanFactoryReference in project opennms by OpenNMS.

the class Scriptd method onInit.

/**
     * Initialize the <em>Scriptd</em> service.
     */
@Override
protected void onInit() {
    // Load the configuration information
    //
    ScriptdConfigFactory aFactory = null;
    try {
        ScriptdConfigFactory.reload();
        aFactory = ScriptdConfigFactory.getInstance();
    } catch (IOException ex) {
        LOG.error("Failed to load scriptd configuration", ex);
        throw new UndeclaredThrowableException(ex);
    }
    // get the node DAO
    BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext");
    NodeDao nodeDao = BeanUtils.getBean(bf, "nodeDao", NodeDao.class);
    m_executor = new Executor(aFactory, nodeDao);
}
Also used : BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) NodeDao(org.opennms.netmgt.dao.api.NodeDao) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ScriptdConfigFactory(org.opennms.netmgt.config.ScriptdConfigFactory) IOException(java.io.IOException)

Example 3 with BeanFactoryReference

use of org.springframework.beans.factory.access.BeanFactoryReference in project opennms by OpenNMS.

the class InsSession method getEventsByCriteria.

private void getEventsByCriteria() {
    LOG.debug("clearing events");
    clearEvents();
    final BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext");
    final EventDao eventDao = BeanUtils.getBean(bf, "eventDao", EventDao.class);
    final TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class);
    try {
        transTemplate.execute(new TransactionCallbackWithoutResult() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                LOG.debug("Entering transaction call back: selection with criteria: {}", criteriaRestriction);
                final OnmsCriteria criteria = new OnmsCriteria(OnmsEvent.class);
                criteria.add(Restrictions.sqlRestriction(criteriaRestriction));
                final List<OnmsEvent> events = eventDao.findMatching(criteria);
                LOG.info("Found {} event(s) with criteria: {}", events.size(), criteriaRestriction);
                for (final OnmsEvent onmsEvent : events) {
                    final Event xmlEvent = getXMLEvent(onmsEvent);
                    if (xmlEvent != null)
                        addEvent(xmlEvent);
                }
            }
        });
    } catch (final RuntimeException e) {
        LOG.error("Error while getting events.", e);
    }
}
Also used : BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) EventDao(org.opennms.netmgt.dao.api.EventDao) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.netmgt.xml.event.Event) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) ArrayList(java.util.ArrayList) List(java.util.List) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 4 with BeanFactoryReference

use of org.springframework.beans.factory.access.BeanFactoryReference in project simba-os by cegeka.

the class SpringAwareLocator method getApplicationContext.

private ApplicationContext getApplicationContext() {
    if (context == null) {
        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(contextConfigLocation);
        BeanFactoryReference reference = locator.useBeanFactory("simbaContext");
        context = (AbstractApplicationContext) reference.getFactory();
    }
    return context;
}
Also used : BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) ContextSingletonBeanFactoryLocator(org.springframework.context.access.ContextSingletonBeanFactoryLocator) BeanFactoryLocator(org.springframework.beans.factory.access.BeanFactoryLocator)

Example 5 with BeanFactoryReference

use of org.springframework.beans.factory.access.BeanFactoryReference in project opennms by OpenNMS.

the class InsAbstractSession method getIfAlias.

/**
     * @param nodeid
     * @param ifindex
     * @return
     */
protected String getIfAlias(final int nodeid, final int ifindex) {
    LOG.debug("getting ifalias for nodeid: {} and ifindex: {}", nodeid, ifindex);
    setCriteria("nodeid = " + nodeid + " AND snmpifindex = " + ifindex);
    BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext");
    final SnmpInterfaceDao snmpInterfaceDao = BeanUtils.getBean(bf, "snmpInterfaceDao", SnmpInterfaceDao.class);
    final TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class);
    final OnmsSnmpInterface iface = transTemplate.execute(new TransactionCallback<OnmsSnmpInterface>() {

        public OnmsSnmpInterface doInTransaction(final TransactionStatus status) {
            return snmpInterfaceDao.findByNodeIdAndIfIndex(nodeid, ifindex);
        }
    });
    if (iface == null) {
        return "-1";
    } else {
        final String ifAlias = iface.getIfAlias();
        LOG.debug("ifalias found: {}", ifAlias);
        return ifAlias;
    }
}
Also used : BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) SnmpInterfaceDao(org.opennms.netmgt.dao.api.SnmpInterfaceDao) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) TransactionStatus(org.springframework.transaction.TransactionStatus)

Aggregations

BeanFactoryReference (org.springframework.beans.factory.access.BeanFactoryReference)6 TransactionStatus (org.springframework.transaction.TransactionStatus)4 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)4 ArrayList (java.util.ArrayList)3 NodeDao (org.opennms.netmgt.dao.api.NodeDao)2 Event (org.opennms.netmgt.xml.event.Event)2 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)2 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ScriptdConfigFactory (org.opennms.netmgt.config.ScriptdConfigFactory)1 AlarmDao (org.opennms.netmgt.dao.api.AlarmDao)1 EventDao (org.opennms.netmgt.dao.api.EventDao)1 SnmpInterfaceDao (org.opennms.netmgt.dao.api.SnmpInterfaceDao)1 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)1 OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)1 OnmsCategory (org.opennms.netmgt.model.OnmsCategory)1 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)1