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 */ 017package org.apache.xbean.spring.context.v2; 018 019import java.lang.reflect.Constructor; 020 021import org.springframework.beans.factory.config.BeanDefinition; 022import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate; 023import org.springframework.beans.factory.xml.NamespaceHandler; 024import org.springframework.beans.factory.xml.XmlReaderContext; 025import org.w3c.dom.Element; 026 027public class XBeanV2Helper { 028 029 public static String version = null; 030 031 public static BeanDefinitionParserDelegate createParser(XmlReaderContext readerContext) { 032 try { 033 String className = "org.apache.xbean.spring.context." + getVersion() + ".XBeanBeanDefinitionParserDelegate"; 034 Class clParser = Class.forName(className); 035 Constructor cns = clParser.getConstructor(new Class[] { XmlReaderContext.class }); 036 return (BeanDefinitionParserDelegate) cns.newInstance(new Object[] { readerContext }); 037 } catch (Throwable e) { 038 throw (IllegalStateException) new IllegalStateException("Unable to create namespace handler for: " + version).initCause(e); 039 } 040 } 041 042 public static NamespaceHandler createNamespaceHandler() { 043 try { 044 String className = "org.apache.xbean.spring.context." + getVersion() + ".XBeanNamespaceHandler"; 045 Class clHandler = Class.forName(className); 046 return (NamespaceHandler) clHandler.newInstance(); 047 } catch (Throwable e) { 048 throw (IllegalStateException) new IllegalStateException("Unable to create namespace handler for: " + version).initCause(e); 049 } 050 } 051 052 private static String getVersion() { 053 if (version == null) { 054 try { 055 try { 056 Class.forName("org.springframework.beans.factory.parsing.BeanComponentDefinition"); 057 version = "v2c"; 058 } catch (ClassNotFoundException e) { 059 Class cl = Class.forName("org.springframework.beans.factory.xml.BeanDefinitionParserDelegate"); 060 try { 061 cl.getMethod("parsePropertyElements", new Class[] { Element.class, BeanDefinition.class }); 062 version = "v2b"; 063 } catch (NoSuchMethodException e2) { 064 version = "v2a"; 065 } 066 } 067 } catch (Throwable e) { 068 throw (IllegalStateException) new IllegalStateException("Could not create namespace handler for: " + version).initCause(e); 069 } 070 } 071 return version; 072 } 073}