View Javadoc

1   package org.kuali.rice.krad.data.provider.spring;
2   
3   import org.kuali.rice.core.api.util.ClassLoaderUtils;
4   import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
5   import org.kuali.rice.krad.data.metadata.impl.DataObjectMetadataImpl;
6   import org.kuali.rice.krad.data.provider.impl.MetadataProviderBase;
7   import org.springframework.beans.factory.support.DefaultListableBeanFactory;
8   import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
9   import org.springframework.core.io.DefaultResourceLoader;
10  
11  import java.util.Collection;
12  import java.util.List;
13  import java.util.Map;
14  
15  /**
16   * Metadata provider which can be configured via the standard spring mechanisms. The bean locations are listed as part
17   * of the metadata provider service definition. The beans in this provider are loaded into a separate context from all
18   * other beans in Rice.
19   * 
20   * @author jonathan
21   * 
22   */
23  public class SpringMetadataProviderImpl extends MetadataProviderBase {
24  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
25  			.getLogger(SpringMetadataProviderImpl.class);
26  
27  	protected List<String> resourceLocations;
28  	protected DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
29  	protected DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
30  
31  	public SpringMetadataProviderImpl() {
32  		LOG.debug("Building SpringMetadataProviderImpl");
33  	}
34  
35  	@Override
36  	public synchronized void initializeMetadata(Collection<Class<?>> types) {
37  		// First, extract the data from the spring configuration into a form usable by the Spring XML parser
38  		if (LOG.isDebugEnabled()) {
39  			LOG.debug("Loading Metadata Bean Definitions from Locations:");
40  			for (String loc : resourceLocations) {
41  				LOG.debug(loc);
42  			}
43  		}
44  		// Now, parse the beans and load them into the bean factory
45  		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(beanFactory);
46  		String configFileLocationsArray[] = new String[resourceLocations.size()];
47  		configFileLocationsArray = resourceLocations.toArray(configFileLocationsArray);
48  		xmlReader.loadBeanDefinitions(configFileLocationsArray);
49  
50  		// extract the objects from the bean factory, by pulling all the DataObjectMetadata objects
51  		Map<String, DataObjectMetadata> metadataObjects = beanFactory.getBeansOfType(DataObjectMetadata.class);
52  		if (LOG.isInfoEnabled()) {
53  			LOG.info(metadataObjects.size() + " DataObjectMetadata objects in Spring configuration files");
54  		}
55  		// populate the map
56  		masterMetadataMap.clear();
57  		for (DataObjectMetadata metadata : metadataObjects.values()) {
58  			if (metadata.getType() != null) {
59  				if (metadata instanceof DataObjectMetadataImpl) {
60  					((DataObjectMetadataImpl) metadata).setProvider(this);
61  				}
62  				masterMetadataMap.put(metadata.getType(), metadata);
63  			} else {
64  				LOG.error("Configuration Error.  MetadataObject in the Spring context contained a null DataObjectType reference: "
65  						+ metadata);
66  			}
67  		}
68  	}
69  
70  	public List<String> getResourceLocations() {
71  		return resourceLocations;
72  	}
73  
74  	public void setResourceLocations(List<String> resourceLocations) {
75  		if (LOG.isDebugEnabled()) {
76  			LOG.debug("Resource locations set to: " + resourceLocations);
77  		}
78  		this.resourceLocations = resourceLocations;
79  	}
80  
81  	@Override
82  	public String toString() {
83  		return getClass().getName() + " : " + resourceLocations;
84  	}
85  }