001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.modeshape.services.functions;
017
018import static java.util.Objects.requireNonNull;
019import static org.slf4j.LoggerFactory.getLogger;
020
021import java.util.LinkedHashMap;
022import java.util.Map;
023import java.util.function.Function;
024
025import javax.jcr.Repository;
026
027import org.infinispan.manager.DefaultCacheManager;
028import org.modeshape.jcr.JcrRepository;
029import org.modeshape.jcr.RepositoryConfiguration;
030import org.modeshape.jcr.value.binary.infinispan.InfinispanBinaryStore;
031import org.slf4j.Logger;
032
033/**
034 * Extract the Infinispan cluster configuration and state
035 * from a running Modeshape repository
036 * @author Gregory Jansen
037 * @since Apr 26, 2013
038 */
039public class GetClusterConfiguration implements Function<Repository, Map<String, String>> {
040
041    private static final Logger LOGGER = getLogger(GetClusterConfiguration.class);
042
043    public static final String CLUSTER_NAME = "clusterName";
044    public static final String CACHE_MODE = "clusterCacheMode";
045    public static final String NODE_ADDRESS = "clusterNodeAddress";
046    public static final String PHYSICAL_ADDRESS = "clusterPhysicalAddress";
047    public static final String NODE_VIEW = "clusterNodeView";
048    public static final String CLUSTER_SIZE = "clusterSize";
049    public static final String CLUSTER_MEMBERS = "clusterMembers";
050    public static final int UNKNOWN_NODE_VIEW = -1;
051
052    private final GetCacheManager getCacheManager = new GetCacheManager();
053
054    /**
055     * Extract the BinaryStore out of Modeshape
056     * (infinspan, jdbc, file, transient, etc)
057     * @return the binary store configuration as a map
058     */
059    @Override
060    public Map<String, String> apply(final Repository input) {
061        requireNonNull(input, "null cannot have a BinaryStore!");
062
063        final Map<String, String> result = new LinkedHashMap<>();
064
065        final RepositoryConfiguration config = ((JcrRepository)input).getConfiguration();
066
067        try {
068            if (!(config.getBinaryStorage().getBinaryStore() instanceof InfinispanBinaryStore)) {
069                return result;
070            }
071        } catch (Exception ex) {
072            LOGGER.debug("Could not extract BinaryStore configuration", ex);
073        }
074
075        final DefaultCacheManager cm = (DefaultCacheManager)getCacheManager.apply(input);
076
077        if (cm == null) {
078            LOGGER.debug("Could not access infinispan configuration");
079            return result;
080        }
081
082        final int nodeView;
083
084        if (cm.getTransport() != null) {
085            nodeView = cm.getTransport().getViewId() + 1;
086        } else {
087            nodeView = UNKNOWN_NODE_VIEW;
088        }
089
090        result.put(CLUSTER_NAME, cm.getClusterName());
091        result.put(CACHE_MODE, cm.getCache().getCacheConfiguration().clustering().cacheMode().toString());
092        result.put(NODE_ADDRESS, cm.getNodeAddress());
093        result.put(PHYSICAL_ADDRESS, cm.getPhysicalAddresses());
094        result.put(NODE_VIEW, nodeView == UNKNOWN_NODE_VIEW ? "Unknown" : String.valueOf(nodeView));
095        result.put(CLUSTER_SIZE, String.valueOf(cm.getClusterSize()));
096        result.put(CLUSTER_MEMBERS, cm.getClusterMembers());
097        return result;
098    }
099}