![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.tomcat.util.modeler.Registry Maven / Gradle / Ivy
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.tomcat.util.modeler; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.lang.management.ManagementFactory; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import javax.management.DynamicMBean; import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanRegistration; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.modeler.modules.ModelerSource; /* Issues: - exceptions - too many "throws Exception" - double check the interfaces - start removing the use of the experimental methods in tomcat, then remove the methods ( before 1.1 final ) - is the security enough to prevent Registry beeing used to avoid the permission checks in the mbean server ? */ /** * Registry for modeler MBeans. * * This is the main entry point into modeler. It provides methods to create * and manipulate model mbeans and simplify their use. * * Starting with version 1.1, this is no longer a singleton and the static * methods are strongly deprecated. In a container environment we can expect * different applications to use different registries. * * This class is itself an mbean. * * IMPORTANT: public methods not marked with @since x.x are experimental or * internal. Should not be used. * * @author Craig R. McClanahan * @author Costin Manolache */ public class Registry implements RegistryMBean, MBeanRegistration { /** * The Log instance to which we will write our log messages. */ private static final Log log = LogFactory.getLog(Registry.class); // Support for the factory methods /** Will be used to isolate different apps and enhance security. */ private static HashMap
instance. * */ public synchronized MBeanServer getMBeanServer() { long t1=System.currentTimeMillis(); if (server == null) { if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) { server = MBeanServerFactory.findMBeanServer(null).get(0); if( log.isDebugEnabled() ) { log.debug("Using existing MBeanServer " + (System.currentTimeMillis() - t1 )); } } else { server = ManagementFactory.getPlatformMBeanServer(); if( log.isDebugEnabled() ) { log.debug("Creating MBeanServer"+ (System.currentTimeMillis() - t1 )); } } } return (server); } /** Find or load metadata. */ public ManagedBean findManagedBean(Object bean, Class> beanClass, String type) throws Exception { if( bean!=null && beanClass==null ) { beanClass=bean.getClass(); } if( type==null ) { type=beanClass.getName(); } // first look for existing descriptor ManagedBean managed = findManagedBean(type); // Search for a descriptor in the same package if( managed==null ) { // check package and parent packages if( log.isDebugEnabled() ) { log.debug( "Looking for descriptor "); } findDescriptor( beanClass, type ); managed=findManagedBean(type); } if( bean instanceof DynamicMBean ) { if( log.isDebugEnabled() ) { log.debug( "Dynamic mbean support "); } // Dynamic mbean loadDescriptors("MbeansDescriptorsDynamicMBeanSource", bean, type); managed=findManagedBean(type); } // Still not found - use introspection if( managed==null ) { if( log.isDebugEnabled() ) { log.debug( "Introspecting "); } // introspection loadDescriptors("MbeansDescriptorsIntrospectionSource", beanClass, type); managed=findManagedBean(type); if( managed==null ) { log.warn( "No metadata found for " + type ); return null; } managed.setName( type ); addManagedBean(managed); } return managed; } /** EXPERIMENTAL Convert a string to object, based on type. Used by several * components. We could provide some pluggability. It is here to keep * things consistent and avoid duplication in other tasks * * @param type Fully qualified class name of the resulting value * @param value String value to be converted * @return Converted value */ public Object convertValue(String type, String value) { Object objValue=value; if( type==null || "java.lang.String".equals( type )) { // string is default objValue=value; } else if( "javax.management.ObjectName".equals( type ) || "ObjectName".equals( type )) { try { objValue=new ObjectName( value ); } catch (MalformedObjectNameException e) { return null; } } else if( "java.lang.Integer".equals( type ) || "int".equals( type )) { objValue=new Integer( value ); } else if( "java.lang.Long".equals( type ) || "long".equals( type )) { objValue=new Long( value ); } else if( "java.lang.Boolean".equals( type ) || "boolean".equals( type )) { objValue=Boolean.valueOf( value ); } return objValue; } /** Experimental. * * @param sourceType * @param source * @param param * @return List of descriptors * @throws Exception */ public Listload( String sourceType, Object source, String param) throws Exception { if( log.isTraceEnabled()) { log.trace("load " + source ); } String location=null; String type=null; Object inputsource=null; if( source instanceof DynamicMBean ) { sourceType="MbeansDescriptorsDynamicMBeanSource"; inputsource=source; } else if( source instanceof URL ) { URL url=(URL)source; location=url.toString(); type=param; inputsource=url.openStream(); if( sourceType == null ) { sourceType = sourceTypeFromExt(location); } } else if( source instanceof File ) { location=((File)source).getAbsolutePath(); inputsource=new FileInputStream((File)source); type=param; if( sourceType == null ) { sourceType = sourceTypeFromExt(location); } } else if( source instanceof InputStream ) { type=param; inputsource=source; } else if( source instanceof Class> ) { location=((Class>)source).getName(); type=param; inputsource=source; if( sourceType== null ) { sourceType="MbeansDescriptorsIntrospectionSource"; } } if( sourceType==null ) { sourceType="MbeansDescriptorsDigesterSource"; } ModelerSource ds=getModelerSource(sourceType); List mbeans = ds.loadDescriptors(this, location, type, inputsource); return mbeans; } private String sourceTypeFromExt( String s ) { if( s.endsWith( ".ser")) { return "MbeansDescriptorsSerSource"; } else if( s.endsWith(".xml")) { return "MbeansDescriptorsDigesterSource"; } return null; } /** Register a component * XXX make it private * * @param bean * @param oname * @param type * @throws Exception */ public void registerComponent(Object bean, ObjectName oname, String type) throws Exception { if( log.isDebugEnabled() ) { log.debug( "Managed= "+ oname); } if( bean ==null ) { log.error("Null component " + oname ); return; } try { if( type==null ) { type=bean.getClass().getName(); } ManagedBean managed = findManagedBean(bean.getClass(), type); // The real mbean is created and registered DynamicMBean mbean = managed.createMBean(bean); if( getMBeanServer().isRegistered( oname )) { if( log.isDebugEnabled()) { log.debug("Unregistering existing component " + oname ); } getMBeanServer().unregisterMBean( oname ); } getMBeanServer().registerMBean( mbean, oname); } catch( Exception ex) { log.error("Error registering " + oname, ex ); throw ex; } } /** Lookup the component descriptor in the package and * in the parent packages. * * @param packageName */ public void loadDescriptors( String packageName, ClassLoader classLoader ) { String res=packageName.replace( '.', '/'); if( log.isTraceEnabled() ) { log.trace("Finding descriptor " + res ); } if( searchedPaths.get( packageName ) != null ) { return; } String descriptors=res + "/mbeans-descriptors.ser"; URL dURL=classLoader.getResource( descriptors ); if( dURL == null ) { descriptors=res + "/mbeans-descriptors.xml"; dURL=classLoader.getResource( descriptors ); } if( dURL == null ) { return; } log.debug( "Found " + dURL); searchedPaths.put( packageName, dURL ); try { if( descriptors.endsWith(".xml" )) loadDescriptors("MbeansDescriptorsDigesterSource", dURL, null); else loadDescriptors("MbeansDescriptorsSerSource", dURL, null); return; } catch(Exception ex ) { log.error("Error loading " + dURL); } return; } /** * @param sourceType * @param source * @param param * @throws Exception */ private void loadDescriptors(String sourceType, Object source, String param) throws Exception { load(sourceType, source, param); } /** Lookup the component descriptor in the package and * in the parent packages. * * @param beanClass * @param type */ private void findDescriptor(Class> beanClass, String type) { if( type==null ) { type=beanClass.getName(); } ClassLoader classLoader=null; if( beanClass!=null ) { classLoader=beanClass.getClassLoader(); } if( classLoader==null ) { classLoader=Thread.currentThread().getContextClassLoader(); } if( classLoader==null ) { classLoader=this.getClass().getClassLoader(); } String className=type; String pkg=className; while( pkg.indexOf( ".") > 0 ) { int lastComp=pkg.lastIndexOf( "."); if( lastComp <= 0 ) return; pkg=pkg.substring(0, lastComp); if( searchedPaths.get( pkg ) != null ) { return; } loadDescriptors(pkg, classLoader); } return; } private ModelerSource getModelerSource( String type ) throws Exception { if( type==null ) type="MbeansDescriptorsDigesterSource"; if( type.indexOf( ".") < 0 ) { type="org.apache.tomcat.util.modeler.modules." + type; } Class> c = Class.forName(type); ModelerSource ds=(ModelerSource)c.newInstance(); return ds; } // -------------------- Registration -------------------- public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { this.server=server; return name; } public void postRegister(Boolean registrationDone) { } public void preDeregister() throws Exception { } public void postDeregister() { } // -------------------- DEPRECATED METHODS -------------------- // May still be used in tomcat // Never part of an official release public ManagedBean findManagedBean(Class> beanClass, String type) throws Exception { return findManagedBean(null, beanClass, type); } /** * Set the MBeanServer
to be utilized for our * registered management beans. * * @param server The newMBeanServer
instance */ public void setMBeanServer( MBeanServer server ) { this.server=server; } public void resetMetadata() { stop(); } }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy