001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.application.support;
017    
018    import java.util.Map;
019    
020    import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
021    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
022    import org.springframework.context.support.ApplicationObjectSupport;
023    import org.springframework.richclient.application.ViewDescriptor;
024    import org.springframework.richclient.application.ViewDescriptorRegistry;
025    import org.springframework.richclient.util.Assert;
026    
027    
028    /**
029     * A simple {@link ViewDescriptorRegistry} implementation that pulls singleton view definitions out
030     * of a spring application context. This class is intended to be managed by a Spring IoC container.
031     * If being created programatically, be sure to call the 
032     * {@link #setApplicationContext(org.springframework.context.ApplicationContext)} method.
033     * 
034     * 
035     * @author Keith Donald
036     * @author Kevin Stembridge
037     */
038    public class BeanFactoryViewDescriptorRegistry extends ApplicationObjectSupport implements ViewDescriptorRegistry {
039    
040        /**
041         * {@inheritDoc}
042         */
043        public ViewDescriptor[] getViewDescriptors() {
044            Map beans = getApplicationContext().getBeansOfType(ViewDescriptor.class, false, false);
045            return (ViewDescriptor[])beans.values().toArray(new ViewDescriptor[beans.size()]);
046        }
047    
048        /**
049         * Returns the view descriptor with the given identifier, or null if no such bean definition 
050         * with the given name exists in the current application context.
051         * 
052         * @param viewName The bean name of the view descriptor that is to be retrieved from the 
053         * underlying application context. Must not be null.
054         * 
055         * @throws IllegalArgumentException if {@code viewName} is null.
056         * @throws BeanNotOfRequiredTypeException if the bean retrieved from the underlying application
057         * context is not of type {@link ViewDescriptor}.
058         * 
059         */
060        public ViewDescriptor getViewDescriptor(String viewName) {
061            
062            Assert.required(viewName, "viewName");
063            
064            try {
065                return (ViewDescriptor) getApplicationContext().getBean(viewName, ViewDescriptor.class);
066            }
067            catch (NoSuchBeanDefinitionException e) {
068                return null;
069            }
070            
071        }
072    
073    }