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.spring.context.impl;
018    
019    import java.lang.reflect.Constructor;
020    import java.util.List;
021    
022    import org.apache.xbean.spring.context.SpringApplicationContext;
023    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
024    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
025    
026    public class XBeanHelper {
027    
028        public static XmlBeanDefinitionReader createBeanDefinitionReader(
029                        SpringApplicationContext applicationContext,
030                        BeanDefinitionRegistry registry,
031                        List xmlPreprocessors) {
032            
033            String version = "2.0";
034            
035            try {
036                Class spring20Clazz = Class.forName("org.springframework.core.AttributeAccessorSupport");
037                version = "2.0";
038            } catch(ClassNotFoundException e) {
039                version = "1.2.8";
040            }
041            
042            String className = "org.apache.xbean.spring.context.v" + version.charAt(0) + ".XBeanXmlBeanDefinitionReader";
043            try {
044                Class cl = Class.forName(className);
045                Constructor cstr = cl.getConstructor(new Class[] { SpringApplicationContext.class, BeanDefinitionRegistry.class, List.class });
046                return (XmlBeanDefinitionReader) cstr.newInstance(new Object[] { applicationContext, registry, xmlPreprocessors });
047            } catch (Exception e) {
048                throw (IllegalStateException) new IllegalStateException("Could not find valid implementation for: " + version).initCause(e);
049            }
050        }
051    }