View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.provider.spring;
17  
18  import java.util.Collection;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.rice.core.api.util.ClassLoaderUtils;
23  import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
24  import org.kuali.rice.krad.data.metadata.impl.DataObjectMetadataImpl;
25  import org.kuali.rice.krad.data.provider.impl.MetadataProviderBase;
26  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
27  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
28  import org.springframework.core.io.DefaultResourceLoader;
29  
30  /**
31   * Metadata provider which can be configured via the standard spring mechanisms.
32   *
33   * <p>
34   * The bean locations are listed as part of the metadata provider service definition. The beans in this provider are
35   * loaded into a separate context from all other beans in Rice.
36   * </p>
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   * 
40   */
41  public class SpringMetadataProviderImpl extends MetadataProviderBase {
42  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
43  			.getLogger(SpringMetadataProviderImpl.class);
44  
45      /**
46       * The locations of the bean resources.
47       */
48  	protected List<String> resourceLocations;
49  
50      /**
51       * The default resource loader to use.
52       */
53  	protected DefaultResourceLoader resourceLoader = new DefaultResourceLoader(ClassLoaderUtils.getDefaultClassLoader());
54  
55      /**
56       * The default bean factory to use.
57       */
58  	protected DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
59  
60      /**
61       * Creates a metadata provider which can be configured via the standard spring mechanisms.
62       */
63  	public SpringMetadataProviderImpl() {
64  		LOG.debug("Building SpringMetadataProviderImpl");
65  	}
66  
67      /**
68       * {@inheritDoc}
69       */
70  	@Override
71  	public synchronized void initializeMetadata(Collection<Class<?>> types) {
72  		// First, extract the data from the spring configuration into a form usable by the Spring XML parser
73  		if (LOG.isDebugEnabled()) {
74  			LOG.debug("Loading Metadata Bean Definitions from Locations:");
75  			for (String loc : resourceLocations) {
76  				LOG.debug(loc);
77  			}
78  		}
79  		// Now, parse the beans and load them into the bean factory
80  		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(beanFactory);
81  		String configFileLocationsArray[] = new String[resourceLocations.size()];
82  		configFileLocationsArray = resourceLocations.toArray(configFileLocationsArray);
83  		xmlReader.loadBeanDefinitions(configFileLocationsArray);
84  
85  		// extract the objects from the bean factory, by pulling all the DataObjectMetadata objects
86  		Map<String, DataObjectMetadata> metadataObjects = beanFactory.getBeansOfType(DataObjectMetadata.class);
87  		if (LOG.isInfoEnabled()) {
88  			LOG.info(metadataObjects.size() + " DataObjectMetadata objects in Spring configuration files");
89  		}
90  		// populate the map
91  		masterMetadataMap.clear();
92  		for (DataObjectMetadata metadata : metadataObjects.values()) {
93  			if (metadata.getType() != null) {
94  				if (metadata instanceof DataObjectMetadataImpl) {
95  					((DataObjectMetadataImpl) metadata).setProviderName(this.getClass().getSimpleName());
96  				}
97  				masterMetadataMap.put(metadata.getType(), metadata);
98  			} else {
99  				LOG.error("Configuration Error.  MetadataObject in the Spring context contained a null DataObjectType reference: "
100 						+ metadata);
101 			}
102 		}
103 	}
104 
105     /**
106      * Gets the locations of the bean resources.
107      *
108      * @return the locations of the bean resources.
109      */
110 	public List<String> getResourceLocations() {
111 		return resourceLocations;
112 	}
113 
114     /**
115      * Setter for the resource locations.
116      *
117      * @param resourceLocations the resource locations to set.
118      */
119 	public void setResourceLocations(List<String> resourceLocations) {
120 		if (LOG.isDebugEnabled()) {
121 			LOG.debug("Resource locations set to: " + resourceLocations);
122 		}
123 		this.resourceLocations = resourceLocations;
124 	}
125 
126     /**
127      * {@inheritDoc}
128      */
129 	@Override
130 	public String toString() {
131 		return getClass().getName() + " : " + resourceLocations;
132 	}
133 }