001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     matic
016 */
017package org.nuxeo.ecm.platform.management.adapters;
018
019import org.nuxeo.runtime.api.Framework;
020import org.nuxeo.runtime.management.AbstractResourceFactory;
021import org.nuxeo.runtime.management.ObjectNameFactory;
022import org.nuxeo.runtime.model.ComponentName;
023import org.nuxeo.runtime.model.Extension;
024import org.nuxeo.runtime.model.ExtensionPoint;
025import org.nuxeo.runtime.model.RegistrationInfo;
026
027/**
028 * @author Stephane Lacoin (Nuxeo EP Software Engineer)
029 */
030public class RuntimeInventoryFactory extends AbstractResourceFactory {
031
032    public void registerResources() {
033        service.registerResource("runtime-inventory",
034                ObjectNameFactory.formatQualifiedName("factory", "RuntimeInventory"), RuntimeInventoryMBean.class,
035                new RuntimeInventoryAdapter(this));
036    }
037
038    public void bindTree() {
039        doVisitInventory(new Callback() {
040            public void invokeFor(String name, String qualifiedName, Class<?> info, Object instance) {
041                service.registerResource(null, qualifiedName + ",management=inventory", info, instance);
042            }
043        });
044    }
045
046    public void unbindTree() {
047        doVisitInventory(new Callback() {
048            public void invokeFor(String name, String qualifiedName, Class<?> info, Object instance) {
049                service.unregisterResource(null, qualifiedName + ",management=inventory");
050            }
051        });
052    }
053
054    private interface Callback {
055        void invokeFor(String name, String qualifiedName, Class<?> info, Object instance);
056    }
057
058    protected void doVisitInventory(Callback callback) {
059        for (RegistrationInfo info : Framework.getRuntime().getComponentManager().getRegistrations()) {
060            doVisitInventoryComponent(callback, info);
061        }
062    }
063
064    protected void doVisitInventoryComponent(Callback callback, RegistrationInfo info) {
065        ComponentName componentName = info.getName();
066        String name = componentName.getName();
067        String qualifiedName = ObjectNameFactory.formatQualifiedName(componentName);
068        callback.invokeFor(name, qualifiedName, ComponentInventoryMBean.class, new ComponentInventoryAdapter(info));
069        for (ExtensionPoint extensionPoint : info.getExtensionPoints()) {
070            doVisitInventoryExtensionPoint(callback, name, qualifiedName, extensionPoint);
071        }
072        for (Extension extension : info.getExtensions()) {
073            doVisitInventoryExtension(callback, name, qualifiedName, extension);
074        }
075    }
076
077    protected void doVisitInventoryExtensionPoint(Callback callback, String name, String qualifiedName,
078            ExtensionPoint extensionPoint) {
079        name += "-" + extensionPoint.getName();
080        qualifiedName += ",extensionPoint=" + extensionPoint.getName();
081        callback.invokeFor(name, qualifiedName, ExtensionPointInventoryMBean.class, new ExtensionPointInventoryAdapter(
082                extensionPoint));
083
084    }
085
086    protected void doVisitInventoryExtension(Callback callback, String name, String qualifiedName, Extension extension) {
087        qualifiedName += ",extensionPoint=" + extension.getExtensionPoint();
088        Object[] contributions = extension.getContributions();
089        if (contributions == null) {
090            return;
091        }
092        for (Object contribution : contributions) {
093            doVisitInventoryContribution(callback, name, qualifiedName, contribution);
094        }
095    }
096
097    private static void doVisitInventoryContribution(Callback callback, String name, String qualifiedName,
098            Object contribution) {
099        String hexName = Integer.toHexString(contribution.hashCode());
100        name += "-" + hexName;
101        qualifiedName += ",contribution=" + hexName;
102        callback.invokeFor(name, qualifiedName, contribution.getClass(), contribution);
103    }
104
105}