001/*
002 * (C) Copyright 2013-2014 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Delbosc Benoit
018 */
019package org.nuxeo.runtime.metrics;
020
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeMap;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.runtime.model.Descriptor;
029
030/**
031 * @since 11.1
032 */
033@XObject("reporter")
034public class MetricsReporterDescriptor implements Descriptor {
035
036    @XNode("@enabled")
037    protected boolean isEnabled = true;
038
039    @XNode("@name")
040    public String name;
041
042    @XNode("@class")
043    public Class<? extends MetricsReporter> klass;
044
045    @XNode("@pollInterval")
046    protected long pollInterval = 60;
047
048    public long getPollInterval() {
049        return pollInterval;
050    }
051
052    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
053    public Map<String, String> options = new HashMap<>();
054
055    @Override
056    public String getId() {
057        return name;
058    }
059
060    public boolean isEnabled() {
061        return isEnabled;
062    }
063
064    public Map<String, String> getOptions() {
065        return Collections.unmodifiableMap(options);
066    }
067
068    public MetricsReporter newInstance() {
069        if (!MetricsReporter.class.isAssignableFrom(klass)) {
070            throw new IllegalArgumentException(
071                    "Cannot create reporter: " + getId() + ", class must implement MetricsReporter");
072        }
073        try {
074            MetricsReporter ret = klass.getDeclaredConstructor().newInstance();
075            ret.init(getPollInterval(), getOptions());
076            return ret;
077        } catch (ReflectiveOperationException e) {
078            throw new IllegalArgumentException("Cannot create reporter: " + getId(), e);
079        }
080    }
081}