001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker.jmx;
018
019import com.fasterxml.jackson.databind.ObjectMapper;
020import org.apache.activemq.management.TimeStatisticImpl;
021import org.apache.activemq.store.PersistenceAdapter;
022import org.apache.activemq.store.PersistenceAdapterStatistics;
023
024import java.io.IOException;
025import java.util.HashMap;
026import java.util.Map;
027import java.util.concurrent.Callable;
028
029public class PersistenceAdapterView implements PersistenceAdapterViewMBean {
030
031    private final static ObjectMapper mapper = new ObjectMapper();
032    private final String name;
033    private final PersistenceAdapter persistenceAdapter;
034
035    private Callable<String> inflightTransactionViewCallable;
036    private Callable<String> dataViewCallable;
037    private PersistenceAdapterStatistics persistenceAdapterStatistics;
038
039    public PersistenceAdapterView(PersistenceAdapter adapter) {
040        this.name = adapter.toString();
041        this.persistenceAdapter = adapter;
042    }
043
044    @Override
045    public String getName() {
046        return name;
047    }
048
049    @Override
050    public String getTransactions() {
051        return invoke(inflightTransactionViewCallable);
052    }
053
054    @Override
055    public String getData() {
056        return invoke(dataViewCallable);
057    }
058
059    @Override
060    public long getSize() {
061        return persistenceAdapter.size();
062    }
063
064    @Override
065    public String getStatistics() {
066        return serializePersistenceAdapterStatistics();
067    }
068
069    @Override
070    public String resetStatistics() {
071        final String result = serializePersistenceAdapterStatistics();
072
073        if (persistenceAdapterStatistics != null) {
074            persistenceAdapterStatistics.reset();
075        }
076
077        return result;
078    }
079
080    private String invoke(Callable<String> callable) {
081        String result = null;
082        if (callable != null) {
083            try {
084                result = callable.call();
085            } catch (Exception e) {
086                result = e.toString();
087            }
088        }
089        return result;
090    }
091
092    private String serializePersistenceAdapterStatistics() {
093        if (persistenceAdapterStatistics != null) {
094            try {
095                Map<String, Object> result = new HashMap<String, Object>();
096                result.put("slowCleanupTime", getTimeStatisticAsMap(persistenceAdapterStatistics.getSlowCleanupTime()));
097                result.put("slowWriteTime", getTimeStatisticAsMap(persistenceAdapterStatistics.getSlowWriteTime()));
098                result.put("slowReadTime", getTimeStatisticAsMap(persistenceAdapterStatistics.getSlowReadTime()));
099                result.put("writeTime", getTimeStatisticAsMap(persistenceAdapterStatistics.getWriteTime()));
100                result.put("readTime", getTimeStatisticAsMap(persistenceAdapterStatistics.getReadTime()));
101                return mapper.writeValueAsString(result);
102            } catch (IOException e) {
103                return e.toString();
104            }
105        }
106
107        return null;
108    }
109
110    private Map<String, Object> getTimeStatisticAsMap(final TimeStatisticImpl timeStatistic) {
111        Map<String, Object> result = new HashMap<String, Object>();
112
113        result.put("count", timeStatistic.getCount());
114        result.put("maxTime", timeStatistic.getMaxTime());
115        result.put("minTime", timeStatistic.getMinTime());
116        result.put("totalTime", timeStatistic.getTotalTime());
117        result.put("averageTime", timeStatistic.getAverageTime());
118        result.put("averageTimeExMinMax", timeStatistic.getAverageTimeExcludingMinMax());
119        result.put("averagePerSecond", timeStatistic.getAveragePerSecond());
120        result.put("averagePerSecondExMinMax", timeStatistic.getAveragePerSecondExcludingMinMax());
121
122        return result;
123    }
124
125    public void setDataViewCallable(Callable<String> dataViewCallable) {
126        this.dataViewCallable = dataViewCallable;
127    }
128
129    public void setInflightTransactionViewCallable(Callable<String> inflightTransactionViewCallable) {
130        this.inflightTransactionViewCallable = inflightTransactionViewCallable;
131    }
132
133    public void setPersistenceAdapterStatistics(PersistenceAdapterStatistics persistenceAdapterStatistics) {
134        this.persistenceAdapterStatistics = persistenceAdapterStatistics;
135    }
136}