protected void connectSensors()

in software/osgi/src/main/java/org/apache/brooklyn/entity/osgi/karaf/KarafContainerImpl.java [108:164]


    protected void connectSensors() {
        super.connectSensors();

        //FIXME should have a better way of setting config -- firstly, not here!
        //preferred style is to have config auto-applied to attributes, and have default values in their definition, not here
        //use of "properties.{user,password}" is non-standard; is that requried? use default jmxUser, jmxPassword flags?
        sensors().set(JMX_CONTEXT, String.format("karaf-%s", getConfig(KARAF_NAME.getConfigKey())));
        
        ConfigToAttributes.apply(this);

        ObjectName karafAdminObjectName = JmxHelper.createObjectName(String.format(KARAF_ADMIN, getConfig(KARAF_NAME.getConfigKey())));
        
        jmxFeed = JmxFeed.builder()
                .entity(this)
                .helper(jmxHelper)
                .period(500, TimeUnit.MILLISECONDS)
                .pollAttribute(new JmxAttributePollConfig<Map>(KARAF_INSTANCES)
                        .objectName(karafAdminObjectName)
                        .attributeName("Instances")
                        .onSuccess(new Function<Object, Map>() {
                            @Override
                            public Map apply(Object input) {
                                return JmxValueFunctions.tabularDataToMap((TabularData)input);
                            }
                        })
                        .onException(new Function<Exception,Map>() {
                                @Override public Map apply(Exception input) {
                                    // If MBean is unreachable, then mark as service-down
                                    if (Boolean.TRUE.equals(getAttribute(SERVICE_UP))) {
                                        LOG.debug("Entity "+this+" is not reachable on JMX");
                                        sensors().set(SERVICE_UP, false);
                                    }
                                    return null;
                                }}))
                .build();

        
        
        // INSTANCES aggregates data for the other sensors.
        subscriptions().subscribe(this, KARAF_INSTANCES, new SensorEventListener<Map>() {
                @Override public void onEvent(SensorEvent<Map> event) {
                    Map<?,?> map = event.getValue();
                    if (map == null) return;
                    
                    sensors().set(SERVICE_UP, "Started".equals(map.get("State")));
                    sensors().set(KARAF_ROOT, (Boolean) map.get("Is Root"));
                    sensors().set(KARAF_JAVA_OPTS, (String) map.get("JavaOpts"));
                    sensors().set(KARAF_INSTALL_LOCATION, (String) map.get("Location"));
                    sensors().set(KARAF_NAME, (String) map.get("Name"));
                    sensors().set(KARAF_PID, (Integer) map.get("Pid"));
                    sensors().set(KARAF_SSH_PORT, (Integer) map.get("Ssh Port"));
                    sensors().set(KARAF_RMI_REGISTRY_PORT, (Integer) map.get("RMI Registry Port"));
                    sensors().set(KARAF_RMI_SERVER_PORT, (Integer) map.get("RMI Server Port"));
                    sensors().set(KARAF_STATE, (String) map.get("State"));
                }});
        
    }