001/*
002 * (C) Copyright 2006-2008 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 *     matic
018 */
019package org.nuxeo.ecm.platform.audit.service.management;
020
021import javax.management.ObjectName;
022
023import org.nuxeo.ecm.platform.audit.api.Logs;
024import org.nuxeo.ecm.platform.audit.service.NXAuditEventsService;
025import org.nuxeo.runtime.api.Framework;
026import org.nuxeo.runtime.management.ObjectNameFactory;
027import org.nuxeo.runtime.management.ResourceFactory;
028import org.nuxeo.runtime.management.ResourceFactoryDescriptor;
029import org.nuxeo.runtime.management.ResourcePublisherService;
030
031/**
032 * @author matic
033 */
034public class AuditEventMetricFactory implements ResourceFactory {
035
036    protected Logs auditService;
037
038    protected ResourcePublisherService publisherService;
039
040    public void configure(ResourcePublisherService service, ResourceFactoryDescriptor descriptor) {
041        publisherService = service;
042        auditService = Framework.getService(Logs.class);
043    }
044
045    public static String formatQualifiedName(String name) {
046        return ObjectNameFactory.formatMetricQualifiedName(NXAuditEventsService.NAME, name);
047    }
048
049    public static String formatShortcutName(String name) {
050        return ObjectNameFactory.formatMetricShortName("event-" + name);
051    }
052
053    public static ObjectName getObjectName(String name) {
054        return ObjectNameFactory.getObjectName(formatQualifiedName(name));
055    }
056
057    protected void doRegisterResource(String name) {
058        publisherService.registerResource(formatShortcutName(name), formatQualifiedName(name),
059                AuditEventMetricMBean.class, new AuditEventMetricMBeanAdapter(auditService, name));
060    }
061
062    protected void doUnregisterResource(String name) {
063        publisherService.unregisterResource(name, formatQualifiedName(name));
064    }
065
066    public void registerResources() {
067        for (String name : auditService.getAuditableEventNames()) {
068            doRegisterResource(name);
069        }
070    }
071
072}