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.impl.services.functions;
017
018import static com.google.common.base.Preconditions.checkNotNull;
019import static org.slf4j.LoggerFactory.getLogger;
020
021import java.util.LinkedHashMap;
022import java.util.List;
023import java.util.Map;
024
025import javax.jcr.Repository;
026
027import org.infinispan.Cache;
028import org.infinispan.manager.DefaultCacheManager;
029import org.modeshape.jcr.GetBinaryStore;
030import org.modeshape.jcr.value.binary.BinaryStore;
031import org.modeshape.jcr.value.binary.infinispan.InfinispanBinaryStore;
032import org.slf4j.Logger;
033
034import com.google.common.base.Function;
035
036/**
037 * Extract the Infinispan cluster configuration and state
038 * from a running Modeshape repository
039 * @author Gregory Jansen
040 * @since Apr 26, 2013
041 */
042public class GetClusterConfiguration implements
043        Function<Repository, Map<String, String>> {
044
045    private static final Logger LOGGER =
046            getLogger(GetClusterConfiguration.class);
047
048    public static final String CLUSTER_NAME = "clusterName";
049    public static final String CACHE_MODE = "clusterCacheMode";
050    public static final String NODE_ADDRESS = "clusterNodeAddress";
051    public static final String PHYSICAL_ADDRESS = "clusterPhysicalAddress";
052    public static final String NODE_VIEW = "clusterNodeView";
053    public static final String CLUSTER_SIZE = "clusterSize";
054    public static final String CLUSTER_MEMBERS = "clusterMembers";
055    public static final int UNKNOWN_NODE_VIEW = -1;
056
057    private GetBinaryStore getBinaryStore = new GetBinaryStore();
058
059    /**
060     * Extract the BinaryStore out of Modeshape
061     * (infinspan, jdbc, file, transient, etc)
062     * @return the binary store configuration as a map
063     */
064    @Override
065    public Map<String, String> apply(final Repository input) {
066        checkNotNull(input, "null cannot have a BinaryStore!");
067
068        final Map<String, String> result =
069            new LinkedHashMap<>();
070        final BinaryStore store = getBinaryStore.apply(input);
071
072        if (!(store instanceof InfinispanBinaryStore)) {
073            return result;
074        }
075
076        final InfinispanBinaryStore ispnStore = (InfinispanBinaryStore) store;
077
078        final List<Cache<?, ?>> caches = ispnStore.getCaches();
079        final DefaultCacheManager cm =
080            (DefaultCacheManager) caches.get(0).getCacheManager();
081
082        if (cm == null) {
083            LOGGER.debug("Could not get cluster configuration information");
084            return result;
085        }
086
087        final int nodeView;
088
089        if (cm.getTransport() != null) {
090            nodeView = cm.getTransport().getViewId() + 1;
091        } else {
092            nodeView = UNKNOWN_NODE_VIEW;
093        }
094
095        result.put(CLUSTER_NAME, cm.getClusterName());
096        result.put(CACHE_MODE, cm.getCache().getCacheConfiguration()
097                                   .clustering().cacheMode().toString());
098        result.put(NODE_ADDRESS, cm.getNodeAddress());
099        result.put(PHYSICAL_ADDRESS, cm.getPhysicalAddresses());
100        result.put(NODE_VIEW, nodeView == UNKNOWN_NODE_VIEW ?
101                                  "Unknown" :
102                                  String.valueOf(nodeView));
103        result.put(CLUSTER_SIZE, String.valueOf(cm.getClusterSize()));
104        result.put(CLUSTER_MEMBERS, cm.getClusterMembers());
105        return result;
106    }
107
108}