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.management.adapters; 020 021import org.nuxeo.runtime.api.Framework; 022import org.nuxeo.runtime.management.AbstractResourceFactory; 023import org.nuxeo.runtime.management.ObjectNameFactory; 024import org.nuxeo.runtime.model.ComponentName; 025import org.nuxeo.runtime.model.Extension; 026import org.nuxeo.runtime.model.ExtensionPoint; 027import org.nuxeo.runtime.model.RegistrationInfo; 028 029/** 030 * @author Stephane Lacoin (Nuxeo EP Software Engineer) 031 */ 032public class RuntimeInventoryFactory extends AbstractResourceFactory { 033 034 @Override 035 public void registerResources() { 036 service.registerResource("runtime-inventory", 037 ObjectNameFactory.formatQualifiedName("factory", "RuntimeInventory"), RuntimeInventoryMBean.class, 038 new RuntimeInventoryAdapter(this)); 039 } 040 041 public void bindTree() { 042 doVisitInventory(new Callback() { 043 @Override 044 public void invokeFor(String name, String qualifiedName, Class<?> info, Object instance) { 045 service.registerResource(null, qualifiedName + ",management=inventory", info, instance); 046 } 047 }); 048 } 049 050 public void unbindTree() { 051 doVisitInventory(new Callback() { 052 @Override 053 public void invokeFor(String name, String qualifiedName, Class<?> info, Object instance) { 054 service.unregisterResource(null, qualifiedName + ",management=inventory"); 055 } 056 }); 057 } 058 059 private interface Callback { 060 void invokeFor(String name, String qualifiedName, Class<?> info, Object instance); 061 } 062 063 protected void doVisitInventory(Callback callback) { 064 for (RegistrationInfo info : Framework.getRuntime().getComponentManager().getRegistrations()) { 065 doVisitInventoryComponent(callback, info); 066 } 067 } 068 069 protected void doVisitInventoryComponent(Callback callback, RegistrationInfo info) { 070 ComponentName componentName = info.getName(); 071 String name = componentName.getName(); 072 String qualifiedName = ObjectNameFactory.formatQualifiedName(componentName); 073 callback.invokeFor(name, qualifiedName, ComponentInventoryMBean.class, new ComponentInventoryAdapter(info)); 074 for (ExtensionPoint extensionPoint : info.getExtensionPoints()) { 075 doVisitInventoryExtensionPoint(callback, name, qualifiedName, extensionPoint); 076 } 077 for (Extension extension : info.getExtensions()) { 078 doVisitInventoryExtension(callback, name, qualifiedName, extension); 079 } 080 } 081 082 protected void doVisitInventoryExtensionPoint(Callback callback, String name, String qualifiedName, 083 ExtensionPoint extensionPoint) { 084 name += "-" + extensionPoint.getName(); 085 qualifiedName += ",extensionPoint=" + extensionPoint.getName(); 086 callback.invokeFor(name, qualifiedName, ExtensionPointInventoryMBean.class, new ExtensionPointInventoryAdapter( 087 extensionPoint)); 088 089 } 090 091 protected void doVisitInventoryExtension(Callback callback, String name, String qualifiedName, Extension extension) { 092 qualifiedName += ",extensionPoint=" + extension.getExtensionPoint(); 093 Object[] contributions = extension.getContributions(); 094 if (contributions == null) { 095 return; 096 } 097 for (Object contribution : contributions) { 098 doVisitInventoryContribution(callback, name, qualifiedName, contribution); 099 } 100 } 101 102 private static void doVisitInventoryContribution(Callback callback, String name, String qualifiedName, 103 Object contribution) { 104 String hexName = Integer.toHexString(contribution.hashCode()); 105 name += "-" + hexName; 106 qualifiedName += ",contribution=" + hexName; 107 callback.invokeFor(name, qualifiedName, contribution.getClass(), contribution); 108 } 109 110}