001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.server.spring.jmx;
018    
019    import javax.management.MBeanServer;
020    
021    import org.springframework.beans.factory.FactoryBean;
022    import org.springframework.beans.factory.InitializingBean;
023    
024    /**
025     * A FactoryBean that uses the xbean.tiger MBeanServerFactoryBean if it can be loaded or, uses 
026     * the spring MBeanServerFactoryBean otherwise.
027     * 
028     * @version $Revision$
029     */
030    public class MBeanServerFactoryBean implements FactoryBean {
031    
032        public Object getObject() throws Exception {
033            
034            ClassLoader classLoader = MBeanServerFactoryBean.class.getClassLoader();
035            try {
036                
037                // Try to use the xbean tiger version first...
038                FactoryBean fb = (FactoryBean) classLoader.loadClass("org.apache.xbean.tiger.MBeanServerFactoryBean").newInstance();
039                return fb.getObject();
040                
041            } catch (Throwable e) {
042                
043                // Fallback to using the spring factory bean then 
044                FactoryBean fb = (FactoryBean) classLoader.loadClass("org.springframework.jmx.support.MBeanServerFactoryBean").newInstance();
045                ((InitializingBean)fb).afterPropertiesSet();
046                return fb.getObject();
047                
048            }
049        }
050    
051        public Class getObjectType() {
052            return MBeanServer.class;
053        }
054    
055        public boolean isSingleton() {
056            return true;
057        }
058    
059    }