All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.oak.spi.whiteboard;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Collections.emptyMap;

import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.apache.jackrabbit.oak.commons.jmx.JmxUtil;
import org.apache.jackrabbit.oak.spi.commit.Observer;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;

public class WhiteboardUtils {

    /**
     * JMX Domain name under which Oak related JMX MBeans are registered
     */
    public static final String JMX_OAK_DOMAIN = "org.apache.jackrabbit.oak";

    public static Registration scheduleWithFixedDelay(
            Whiteboard whiteboard, Runnable runnable, long delayInSeconds) {
        return scheduleWithFixedDelay(whiteboard, runnable, delayInSeconds, false);
    }

    public static Registration scheduleWithFixedDelay(
            Whiteboard whiteboard, Runnable runnable, long delayInSeconds, boolean runOnSingleClusterNode) {
        ImmutableMap.Builder builder = ImmutableMap.builder()
                .put("scheduler.period", delayInSeconds)
                .put("scheduler.concurrent", false);
        if (runOnSingleClusterNode) {
            //Make use of feature while running in Sling SLING-2979
            builder.put("scheduler.runOn", "SINGLE");
        }
        return whiteboard.register(
                Runnable.class, runnable, builder.build());
    }

    public static  Registration registerMBean(
            Whiteboard whiteboard,
            Class iface, T bean, String type, String name) {
        return registerMBean(whiteboard, iface, bean, type, name, Collections.emptyMap());
    }

    public static  Registration registerMBean(
            Whiteboard whiteboard,
            Class iface, T bean, String type, String name, Map attrs) {
        try {

            Hashtable table = new Hashtable(attrs);
            table.put("type", JmxUtil.quoteValueIfRequired(type));
            table.put("name", JmxUtil.quoteValueIfRequired(name));
            return whiteboard.register(iface, bean, ImmutableMap.of(
                    "jmx.objectname",
                    new ObjectName(JMX_OAK_DOMAIN, table)));
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static Registration registerObserver(
            @Nonnull Whiteboard whiteboard,
            @Nonnull Observer observer) {
        return checkNotNull(whiteboard)
                .register(Observer.class, checkNotNull(observer), emptyMap());
    }

    /**
     * Returns the currently available services from the whiteboard of the tracked type.
     *
     * Note that the underlying tracker is closed automatically.
     *
     * @param wb the whiteboard
     * @param type the service type
     * @return a list of services
     */
    @Nonnull
    public static  List getServices(@Nonnull Whiteboard wb, @Nonnull Class type) {
        return getServices(wb, type, null);
    }

    /**
     * Returns the one of the currently available services from the whiteboard of the tracked type.
     *
     * Note that the underlying tracker is closed automatically.
     *
     * @return one service or {@code null}
     */
    @CheckForNull
    public static  T getService(@Nonnull Whiteboard wb, @Nonnull Class type) {
        return getService(wb, type, null);
    }

    /**
     * Returns the currently available services from the whiteboard of the tracked type. If {@code predicate} is
     * not {@code null} the returned list is limited to the ones that match the predicate.
     *
     * Note that the underlying tracker is stopped automatically after the services are returned.
     *
     * @param wb the whiteboard
     * @param type the service type
     * @param predicate filtering predicate or {@code null}
     * @return a list of services
     */
    @Nonnull
    public static  List getServices(@Nonnull Whiteboard wb, @Nonnull Class type, @Nullable Predicate predicate) {
        Tracker tracker = wb.track(type);
        try {
            if (predicate == null) {
                return tracker.getServices();
            } else {
                return ImmutableList.copyOf(Iterables.filter(tracker.getServices(), predicate));
            }
        } finally {
            tracker.stop();
        }
    }

    /**
     * Returns the one of the currently available services from the whiteboard of the tracked type. If {@code predicate} is
     * not {@code null} only a service that match the predicate is returned.
     *
     * Note that the underlying tracker is closed automatically.
     *
     * @param wb the whiteboard
     * @param type the service type
     * @param predicate filtering predicate or {@code null}
     * @return one service or {@code null}
     */
    @CheckForNull
    public static  T getService(@Nonnull Whiteboard wb, @Nonnull Class type, @Nullable Predicate predicate) {
        Tracker tracker = wb.track(type);
        try {
            for (T service : tracker.getServices()) {
                if (predicate == null || predicate.apply(service)) {
                    return service;
                }
            }
            return null;
        } finally {
            tracker.stop();
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy