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