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    @Override
041    public void configure(ResourcePublisherService service, ResourceFactoryDescriptor descriptor) {
042        publisherService = service;
043        auditService = Framework.getService(Logs.class);
044    }
045
046    public static String formatQualifiedName(String name) {
047        return ObjectNameFactory.formatMetricQualifiedName(NXAuditEventsService.NAME, name);
048    }
049
050    public static String formatShortcutName(String name) {
051        return ObjectNameFactory.formatMetricShortName("event-" + name);
052    }
053
054    public static ObjectName getObjectName(String name) {
055        return ObjectNameFactory.getObjectName(formatQualifiedName(name));
056    }
057
058    protected void doRegisterResource(String name) {
059        publisherService.registerResource(formatShortcutName(name), formatQualifiedName(name),
060                AuditEventMetricMBean.class, new AuditEventMetricMBeanAdapter(auditService, name));
061    }
062
063    protected void doUnregisterResource(String name) {
064        publisherService.unregisterResource(name, formatQualifiedName(name));
065    }
066
067    @Override
068    public void registerResources() {
069        for (String name : auditService.getAuditableEventNames()) {
070            doRegisterResource(name);
071        }
072    }
073
074}