001// Copyright 2008, 2009, 2010 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.spring;
016
017import org.apache.tapestry5.TapestryFilter;
018import org.apache.tapestry5.internal.spring.TapestryBeanFactory;
019import org.apache.tapestry5.ioc.Registry;
020import org.springframework.beans.factory.support.DefaultListableBeanFactory;
021import org.springframework.web.context.support.XmlWebApplicationContext;
022
023/**
024 * Extension of Spring's {@link org.springframework.web.context.support.XmlWebApplicationContext} that includes hooks to
025 * resolve some injections into Spring beans as Tapestry services. When using the Tapestry/Spring Integration Library,
026 * this class (or a subclass) must be the configured context class.  If not specified, this  class is the default
027 * context class.
028 */
029public class TapestryApplicationContext extends XmlWebApplicationContext
030{
031    @Override
032    protected DefaultListableBeanFactory createBeanFactory()
033    {
034        Registry registry = (Registry) getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
035
036        if (registry == null)
037            throw new IllegalStateException(
038                    "Expected a Tapestry IoC Registry to be stored in the ServletContext, but the attribute was null.");
039
040        return new TapestryBeanFactory(getInternalParentBeanFactory(), registry);
041    }
042}