package org.rhq.metrics.restServlet;

import java.util.Collections;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.rhq.metrics.core.MetricsService;

/**
 * @author John Sanda
 */
@ApplicationScoped
public class MetricsServiceProducer {

    @Inject
    private javax.servlet.ServletContext context;

    private MetricsService metricsService;

    @Produces
    public MetricsService getMetricsService() {
        if (metricsService == null) {
            try {
                String className = context.getInitParameter("rhq-metrics.backend");
                Class clazz = Class.forName(className);
                metricsService = (MetricsService) clazz.newInstance();
                // TODO passs servlet params
                metricsService.startUp(Collections.<String, String>emptyMap());

                ServiceKeeper.getInstance().service = metricsService;

                return metricsService;
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                throw new RuntimeException("Cannot create MetricsService class", e);
            }
        }
        return metricsService;
    }

}

@@ -258,16 +258,44 @@
 nda%0A */%0A
+@SuppressWarnings(%22unused%22)%0A
 @Applica
@@ -305,16 +305,16 @@
 nScoped%0A
-
 public c
@@ -603,16 +603,591 @@
 ssName =
+ null;%0A                String backend = System.getProperty(%22rhq-metrics.backend%22);%0A                if (backend!=null) %7B%0A                    switch (backend) %7B%0A                    case %22mem%22:%0A                        className=%22org.rhq.metrics.impl.memory.MemoryMetricsService%22;%0A                        break;%0A                    case %22cass%22:%0A                        className=%22org.rhq.metrics.impl.cassandra.MetricsServiceCassandra%22;%0A                        break;%0A                    %7D%0A                %7D%0A%0A                if (className==null) %7B%0A                    className =
  context
@@ -1220,32 +1220,50 @@
 rics.backend%22);%0A
+                %7D%0A
                 

Allow to specify the backend as vm property rhq-metrics.backend. Choices are 'mem' and 'cass'.