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.PageDescriptor;
024    import org.springframework.richclient.application.PageDescriptorRegistry;
025    import org.springframework.richclient.util.Assert;
026    
027    
028    /**
029     * A simple {@link PageDescriptorRegistry} implementation that pulls singleton page 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     * @author Rogan Dawes
038     */
039    public class BeanFactoryPageDescriptorRegistry extends ApplicationObjectSupport implements PageDescriptorRegistry {
040    
041        /**
042         * {@inheritDoc}
043         */
044        public PageDescriptor[] getPageDescriptors() {
045            Map beans = getApplicationContext().getBeansOfType(PageDescriptor.class, false, false);
046            return (PageDescriptor[])beans.values().toArray(new PageDescriptor[beans.size()]);
047        }
048    
049        /**
050         * Returns the page descriptor with the given identifier, or null if no such bean definition
051         * with the given name exists in the current application context.
052         *
053         * @param pageName The bean name of the page descriptor that is to be retrieved from the
054         * underlying application context. Must not be null.
055         *
056         * @throws IllegalArgumentException if {@code pageName} is null.
057         * @throws BeanNotOfRequiredTypeException if the bean retrieved from the underlying application
058         * context is not of type {@link PageDescriptor}.
059         *
060         */
061        public PageDescriptor getPageDescriptor(String pageName) {
062    
063            Assert.required(pageName, "pageName");
064    
065            try {
066                return (PageDescriptor) getApplicationContext().getBean(pageName, PageDescriptor.class);
067            }
068            catch (NoSuchBeanDefinitionException e) {
069                return null;
070            }
071    
072        }
073    
074    }