Search in sources :

Example 1 with Service

use of org.opennms.netmgt.config.service.Service in project opennms by OpenNMS.

the class Invoker method instantiateClasses.

/**
     * <p>instantiateClasses</p>
     */
public void instantiateClasses() {
    /*
         * Preload the classes and register a new instance with the
         * MBeanServer.
         */
    for (InvokerService invokerService : getServices()) {
        Service service = invokerService.getService();
        try {
            // preload the class
            LOG.debug("loading class {}", service.getClassName());
            Class<?> clazz = Class.forName(service.getClassName());
            // Get a new instance of the class
            LOG.debug("create new instance of {}", service.getClassName());
            Map<String, String> mdc = Logging.getCopyOfContextMap();
            Object bean;
            try {
                bean = clazz.newInstance();
            } finally {
                Logging.setContextMap(mdc);
            }
            // Register the mbean
            LOG.debug("registering mbean instance {}", service.getName());
            ObjectName name = new ObjectName(service.getName());
            invokerService.setMbean(getServer().registerMBean(bean, name));
            // Set attributes
            final List<org.opennms.netmgt.config.service.Attribute> attribs = service.getAttributes();
            if (attribs != null) {
                for (final org.opennms.netmgt.config.service.Attribute attrib : attribs) {
                    LOG.debug("setting attribute {}", attrib.getName());
                    getServer().setAttribute(name, getAttribute(attrib));
                }
            }
        } catch (Throwable t) {
            LOG.error("An error occurred loading the mbean {} of type {}", service.getName(), service.getClassName(), t);
            invokerService.setBadThrowable(t);
        }
    }
}
Also used : Attribute(javax.management.Attribute) Service(org.opennms.netmgt.config.service.Service) ObjectName(javax.management.ObjectName)

Example 2 with Service

use of org.opennms.netmgt.config.service.Service in project opennms by OpenNMS.

the class ServiceConfigMigratorOffline method execute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#execute()
     */
@Override
public void execute() throws OnmsUpgradeException {
    if (skipMe)
        return;
    try {
        ServiceConfiguration currentCfg = JaxbUtils.unmarshal(ServiceConfiguration.class, configFile);
        int index = 0;
        for (Service baseSvc : baseConfig.getServices()) {
            Service localSvc = getService(currentCfg, baseSvc.getName());
            if (localSvc == null) {
                if (baseSvc.isEnabled()) {
                    log("Adding new service %s\n", baseSvc.getName());
                } else {
                    log("Marking service %s as disabled\n", baseSvc.getName());
                }
                currentCfg.getServices().add(index, baseSvc);
                continue;
            }
            if (!baseSvc.isEnabled()) {
                log("Disabling service %s because it is not on the default list of enabled services\n", localSvc.getName());
                localSvc.setEnabled(false);
            }
            if (localSvc.getClassName().equals("org.opennms.netmgt.poller.jmx.RemotePollerBackEnd")) {
                log("Fixing the class path for RemotePollerBackEnd.\n");
                localSvc.setClassName("org.opennms.netmgt.poller.remote.jmx.RemotePollerBackEnd");
            }
            if (localSvc.getName().equals("OpenNMS:Name=Linkd")) {
                log("Disabling Linkd (to promote EnhancedLinkd)\n");
                localSvc.setEnabled(false);
            }
            Attribute a = getLoggingPrefix(localSvc);
            if (a != null) {
                String prefix = a.getValue().getContent().toLowerCase();
                // If the logging prefix isn't already lower case...
                if (!a.getValue().getContent().equals(prefix)) {
                    // then set it to the lower case value
                    log("Fixing logging prefix for service %s\n", localSvc.getName());
                    a.getValue().setContent(prefix);
                }
            }
            index++;
        }
        StringWriter sw = new StringWriter();
        sw.write("<?xml version=\"1.0\"?>\n");
        sw.write("<!-- NOTE!!!!!!!!!!!!!!!!!!!\n");
        sw.write("The order in which these services are specified is important - for example, Eventd\n");
        sw.write("will need to come up last so that none of the event topic subcribers loose any event.\n");
        sw.write("\nWhen splitting services to run on mutiple VMs, the order of the services should be\n");
        sw.write("maintained\n");
        sw.write("-->\n");
        JaxbUtils.marshal(currentCfg, sw);
        FileWriter fw = new FileWriter(configFile);
        fw.write(sw.toString());
        fw.close();
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
    }
}
Also used : ServiceConfiguration(org.opennms.netmgt.config.service.ServiceConfiguration) StringWriter(java.io.StringWriter) Attribute(org.opennms.netmgt.config.service.Attribute) FileWriter(java.io.FileWriter) Service(org.opennms.netmgt.config.service.Service) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 3 with Service

use of org.opennms.netmgt.config.service.Service in project opennms by OpenNMS.

the class EOLServiceConfigMigratorOfflineTest method testUpgradeConfig.

/**
     * Test fixing the configuration file.
     *
     * @throws Exception the exception
     */
@Test
public void testUpgradeConfig() throws Exception {
    File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.SERVICE_CONF_FILE_NAME);
    ServiceConfiguration cfg = JaxbUtils.unmarshal(ServiceConfiguration.class, cfgFile);
    // check the total and active services before doing the upgrade
    assertEquals(m_totalBefore, cfg.getServices().size());
    // perform the upgrade
    final EOLServiceConfigMigratorOffline migrator = new EOLServiceConfigMigratorOffline();
    migrator.execute();
    // confirm the total and active services after the upgrade
    cfg = JaxbUtils.unmarshal(ServiceConfiguration.class, cfgFile);
    final ServiceConfigFactory factory = new ServiceConfigFactory();
    Assert.assertEquals(m_totalAfter, cfg.getServices().size());
    Assert.assertEquals(m_enabledAfter, factory.getServices().length);
    for (final Service svc : cfg.getServices()) {
        final String serviceName = svc.getName();
        if (m_disabled.contains(serviceName)) {
            assertFalse("Service " + serviceName + " should be disabled.", svc.isEnabled());
        }
    }
    for (final Service svc : factory.getServices()) {
        final String serviceName = svc.getName();
        assertFalse("Service " + serviceName + " should not be in the active service list.", m_disabled.contains(svc.getName()));
    }
}
Also used : ServiceConfiguration(org.opennms.netmgt.config.service.ServiceConfiguration) ServiceConfigFactory(org.opennms.netmgt.config.ServiceConfigFactory) Service(org.opennms.netmgt.config.service.Service) File(java.io.File) Test(org.junit.Test)

Example 4 with Service

use of org.opennms.netmgt.config.service.Service in project opennms by OpenNMS.

the class ServiceConfig1701MigratorOfflineTest method testUpgradeConfig.

/**
     * Test fixing the configuration file.
     *
     * @throws Exception the exception
     */
@Test
public void testUpgradeConfig() throws Exception {
    File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.SERVICE_CONF_FILE_NAME);
    ServiceConfiguration cfg = JaxbUtils.unmarshal(ServiceConfiguration.class, cfgFile);
    // check the total and active services before doing the upgrade
    assertEquals(m_totalBefore, cfg.getServices().size());
    // perform the upgrade
    final ServiceConfig1701MigratorOffline migrator = new ServiceConfig1701MigratorOffline();
    migrator.execute();
    // confirm the total and active services after the upgrade
    cfg = JaxbUtils.unmarshal(ServiceConfiguration.class, cfgFile);
    final ServiceConfigFactory factory = new ServiceConfigFactory();
    Assert.assertEquals(m_totalAfter, cfg.getServices().size());
    Assert.assertEquals(m_enabledAfter, factory.getServices().length);
    for (final Service svc : cfg.getServices()) {
        final String serviceName = svc.getName();
        if (m_disabled.contains(serviceName)) {
            assertFalse("Service " + serviceName + " should be disabled.", svc.isEnabled());
        }
    }
    for (final Service svc : factory.getServices()) {
        final String serviceName = svc.getName();
        assertFalse("Service " + serviceName + " should not be in the active service list.", m_disabled.contains(svc.getName()));
    }
}
Also used : ServiceConfiguration(org.opennms.netmgt.config.service.ServiceConfiguration) ServiceConfigFactory(org.opennms.netmgt.config.ServiceConfigFactory) Service(org.opennms.netmgt.config.service.Service) File(java.io.File) Test(org.junit.Test)

Example 5 with Service

use of org.opennms.netmgt.config.service.Service in project opennms by OpenNMS.

the class ServiceConfig1701MigratorOffline method execute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#execute()
     */
@Override
public void execute() throws OnmsUpgradeException {
    try {
        ServiceConfiguration currentCfg = JaxbUtils.unmarshal(ServiceConfiguration.class, configFile);
        log("Current configuration: " + currentCfg.getServices().size() + " services.\n");
        for (int i = currentCfg.getServices().size() - 1; i >= 0; i--) {
            final Service localSvc = (Service) currentCfg.getServices().get(i);
            final String name = localSvc.getName();
            if (oldServices.contains(name)) {
                log("Removing old service %s\n", name);
                currentCfg.getServices().remove(i);
            }
        }
        log("New configuration: " + currentCfg.getServices().size() + " services.\n");
        // now remove 
        final StringWriter sw = new StringWriter();
        sw.write("<?xml version=\"1.0\"?>\n");
        sw.write("<!-- NOTE!!!!!!!!!!!!!!!!!!!\n");
        sw.write("The order in which these services are specified is important - for example, Eventd\n");
        sw.write("will need to come up last so that none of the event topic subcribers loose any event.\n");
        sw.write("\nWhen splitting services to run on mutiple VMs, the order of the services should be\n");
        sw.write("maintained\n");
        sw.write("-->\n");
        JaxbUtils.marshal(currentCfg, sw);
        final FileWriter fw = new FileWriter(configFile);
        fw.write(sw.toString());
        fw.close();
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
    }
}
Also used : ServiceConfiguration(org.opennms.netmgt.config.service.ServiceConfiguration) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Service(org.opennms.netmgt.config.service.Service) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Aggregations

Service (org.opennms.netmgt.config.service.Service)12 ServiceConfiguration (org.opennms.netmgt.config.service.ServiceConfiguration)5 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)3 ArrayList (java.util.ArrayList)3 ObjectName (javax.management.ObjectName)3 ServiceConfigFactory (org.opennms.netmgt.config.ServiceConfigFactory)3 OnmsUpgradeException (org.opennms.upgrade.api.OnmsUpgradeException)3 File (java.io.File)2 MBeanServer (javax.management.MBeanServer)2 Test (org.junit.Test)2 Invoke (org.opennms.netmgt.config.service.Invoke)2 StringReader (java.io.StringReader)1 Attribute (javax.management.Attribute)1 ObjectInstance (javax.management.ObjectInstance)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 GnuParser (org.apache.commons.cli.GnuParser)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1