Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.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 final 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);
}
// Still not found - use introspection
if( managed==null ) {
if( log.isDebugEnabled() ) {
log.debug( "Introspecting ");
}
// introspection
load("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 List load( 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 URL ) {
URL url=(URL)source;
location=url.toString();
type=param;
inputsource=url.openStream();
if (sourceType == null && location.endsWith(".xml")) {
sourceType = "MbeansDescriptorsDigesterSource";
}
} else if( source instanceof File ) {
location=((File)source).getAbsolutePath();
inputsource=new FileInputStream((File)source);
type=param;
if (sourceType == null && location.endsWith(".xml")) {
sourceType = "MbeansDescriptorsDigesterSource";
}
} 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, type, inputsource);
return mbeans;
}
/** 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(null, 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.xml";
URL dURL = classLoader.getResource( descriptors );
if (dURL == null) {
return;
}
log.debug( "Found " + dURL);
searchedPaths.put( packageName, dURL );
try {
load("MbeansDescriptorsDigesterSource", dURL, null);
} catch(Exception ex ) {
log.error("Error loading " + dURL);
}
}
/** 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 --------------------
@Override
public ObjectName preRegister(MBeanServer server,
ObjectName name) throws Exception
{
this.server=server;
return name;
}
@Override
public void postRegister(Boolean registrationDone) {
}
@Override
public void preDeregister() throws Exception {
}
@Override
public void postDeregister() {
}
}