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.util;
018
019import org.springframework.beans.BeansException;
020import org.springframework.beans.MutablePropertyValues;
021import org.springframework.beans.PropertyValue;
022import org.springframework.beans.factory.config.BeanDefinition;
023import org.springframework.beans.factory.config.BeanDefinitionHolder;
024import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
025import org.springframework.beans.factory.config.ConstructorArgumentValues;
026import org.springframework.beans.factory.config.RuntimeBeanReference;
027
028import java.util.Collection;
029import java.util.Iterator;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * Default do nothing implementation of SpringVisitor.
035 * @author Dain Sundstrom
036 * @version $Id$
037 * @since 2.0
038 */
039public abstract class AbstractSpringVisitor implements SpringVisitor {
040    public void visitBeanFactory(ConfigurableListableBeanFactory beanRegistry, Object data) throws BeansException {
041        String[] beanNames = beanRegistry.getBeanDefinitionNames();
042        for (int i = 0; i < beanNames.length; i++) {
043            String beanName = beanNames[i];
044            visitBeanDefinition(beanName, beanRegistry.getBeanDefinition(beanName), data);
045        }
046    }
047
048    public void visitBeanDefinitionHolder(BeanDefinitionHolder beanDefinitionHolder, Object data) throws BeansException {
049        visitBeanDefinition(beanDefinitionHolder.getBeanName(), beanDefinitionHolder.getBeanDefinition(), data);
050    }
051
052    public void visitBeanDefinition(String beanName, BeanDefinition beanDefinition, Object data) throws BeansException {
053        visitBeanDefinition(beanDefinition, data);
054    }
055
056    public void visitBeanDefinition(BeanDefinition beanDefinition, Object data) throws BeansException {
057        visitConstructorArgumentValues(beanDefinition.getConstructorArgumentValues(), data);
058        visitMutablePropertyValues(beanDefinition.getPropertyValues(), data);
059    }
060
061    public void visitMutablePropertyValues(MutablePropertyValues propertyValues, Object data) throws BeansException {
062        PropertyValue[] values = propertyValues.getPropertyValues();
063        for (int i = 0; i < values.length; i++) {
064            visitPropertyValue(values[i], data);
065        }
066    }
067
068    public void visitConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues, Object data) throws BeansException {
069        Map indexedArgumentValues = constructorArgumentValues.getIndexedArgumentValues();
070        for (Iterator iterator = indexedArgumentValues.values().iterator(); iterator.hasNext();) {
071            visitConstructorArgumentValue((ConstructorArgumentValues.ValueHolder) iterator.next(), data);
072        }
073        List genericArgumentValues = constructorArgumentValues.getGenericArgumentValues();
074        for (Iterator iterator = genericArgumentValues.iterator(); iterator.hasNext();) {
075            visitConstructorArgumentValue((ConstructorArgumentValues.ValueHolder) iterator.next(), data);
076        }
077    }
078
079    public void visitConstructorArgumentValue(ConstructorArgumentValues.ValueHolder valueHolder, Object data) throws BeansException {
080        visitNext(valueHolder.getValue(), data);
081    }
082
083    public void visitPropertyValue(PropertyValue propertyValue, Object data) throws BeansException {
084        visitNext(propertyValue.getValue(), data);
085    }
086
087    public void visitRuntimeBeanReference(RuntimeBeanReference beanReference, Object data) throws BeansException {
088    }
089
090    public void visitCollection(Collection collection, Object data)  throws BeansException {
091        for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
092            visitNext(iterator.next(), data);
093        }
094    }
095
096    public void visitMap(Map map, Object data)  throws BeansException {
097        for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
098            Map.Entry entry = (Map.Entry) iterator.next();
099            visitNext(entry.getKey(), data);
100            visitNext(entry.getValue(), data);
101        }
102    }
103
104    public void visitObject(Object value, Object data)  throws BeansException {
105    }
106
107    protected void visitNext(Object value, Object data) throws BeansException {
108        if (value == null) {
109            return;
110        }
111
112        if (value instanceof ConfigurableListableBeanFactory) {
113            visitBeanFactory((ConfigurableListableBeanFactory) value, data);
114        } else if (value instanceof BeanDefinitionHolder) {
115            visitBeanDefinitionHolder((BeanDefinitionHolder) value, data);
116        } else if (value instanceof BeanDefinition) {
117            visitBeanDefinition((BeanDefinition) value, data);
118        } else if (value instanceof ConstructorArgumentValues) {
119            visitConstructorArgumentValues((ConstructorArgumentValues) value, data);
120        } else if (value instanceof ConstructorArgumentValues.ValueHolder) {
121            visitConstructorArgumentValue((ConstructorArgumentValues.ValueHolder) value, data);
122        } else if (value instanceof MutablePropertyValues) {
123            visitMutablePropertyValues((MutablePropertyValues) value, data);
124        } else if (value instanceof PropertyValue) {
125            visitPropertyValue((PropertyValue) value, data);
126        } else if (value instanceof RuntimeBeanReference) {
127            visitRuntimeBeanReference((RuntimeBeanReference) value, data);
128        } else if (value instanceof Map) {
129            visitMap((Map) value, data);
130        } else if (value instanceof Collection) {
131            visitCollection((Collection) value, data);
132        } else {
133            visitObject(value, data);
134        }
135    }
136
137}